!941 feat:add gpio driver fuzz test

Merge pull request !941 from 马海/master
This commit is contained in:
openharmony_ci
2022-05-11 03:08:25 +00:00
committed by Gitee
6 changed files with 200 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
# Copyright (c) 2022 Huawei Device Co., Ltd.
#
# HDF is dual licensed: you can use it either under the terms of
# the GPL, or the BSD license, at your option.
# See the LICENSE file in the root of this repository for complete details.
group("hdf_platform_fuzztest") {
testonly = true
deps = []
deps += [ "gpio_fuzzer:fuzztest" ]
}
@@ -0,0 +1,44 @@
# Copyright (c) 2022 Huawei Device Co., Ltd.
#
# HDF is dual licensed: you can use it either under the terms of
# the GPL, or the BSD license, at your option.
# See the LICENSE file in the root of this repository for complete details.
import("//build/config/features.gni")
import("//build/ohos.gni")
import("//build/test.gni")
module_output_path = "hdf/platform"
ohos_fuzztest("GpioFuzzTest") {
module_out_path = module_output_path
fuzz_config_file =
"//drivers/framework/support/platform/test/fuzztest/gpio_fuzzer"
include_dirs = [
"//drivers/framework/support/platform/test/fuzztest/gpio_fuzzer",
"//drivers/framework/include/platform",
]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "gpio_fuzzer.cpp" ]
deps = [ "//drivers/adapter/uhdf2/platform:libhdf_platform" ]
external_deps = [
"hiviewdfx_hilog_native:libhilog",
"utils_base:utils",
]
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":GpioFuzzTest" ]
}
@@ -0,0 +1,7 @@
# Copyright (c) 2022 Huawei Device Co., Ltd.
#
# HDF is dual licensed: you can use it either under the terms of
# the GPL, or the BSD license, at your option.
# See the LICENSE file in the root of this repository for complete details.
FUZZ
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gpio_fuzzer.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
#include <cstddef>
#include "gpio_if.h"
#include "hdf_base.h"
#include "securec.h"
using namespace std;
namespace {
constexpr int32_t MIN = 0;
constexpr int32_t MAX = 2;
const uint16_t gpioTestNum = 3;
const int32_t MAX_LEN = 10;
}
struct AllParameters {
uint16_t descVal;
uint16_t descDir;
uint16_t descMode;
};
union ConvertToParam {
uint8_t data[MAX_LEN];
struct AllParameters params;
};
static int32_t GpioTestIrqHandler(uint16_t gpio, void *data)
{
(void)gpio;
(void)data;
return 0;
}
static int32_t randNum()
{
std::random_device rd;
std::default_random_engine engine(rd());
std::uniform_int_distribution<int32_t> randomNum(MIN, MAX);
return randomNum(engine);
}
namespace OHOS {
bool GpioFuzzTest(const uint8_t* data, size_t size)
{
union ConvertToParam convertor;
if (data == nullptr) {
return false;
}
if (memcpy_s (convertor.data, MAX_LEN, data, MAX_LEN) != EOK) {
return false;
}
int32_t number = randNum();
switch (static_cast<ApiNumber>(number)) {
case ApiNumber::NUM_ZERO:
GpioWrite(gpioTestNum, convertor.params.descVal);
break;
case ApiNumber::NUM_ONE:
GpioSetDir(gpioTestNum, convertor.params.descDir);
break;
case ApiNumber::NUM_TWO:
GpioSetIrq(gpioTestNum, convertor.params.descMode, GpioTestIrqHandler, &data);
break;
default:
break;
}
return true;
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
/* Run your code on data */
OHOS::GpioFuzzTest(data, size);
return 0;
}
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
#ifndef GPIO_FUZZER
#define GPIO_FUZZER
#define FUZZ_PROJECT_NAME "gpio_fuzzer"
enum class ApiNumber {
NUM_ZERO = 0,
NUM_ONE,
NUM_TWO,
};
#endif
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
*
* HDF is dual licensed: you can use it either under the terms of
* the GPL, or the BSD license, at your option.
* See the LICENSE file in the root of this repository for complete details.
*/
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>10</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>120</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>