fix: add fuzz test case for rtc and uart module.

Signed-off-by: jiaziyang <jiaziyang1@huawei.com>
This commit is contained in:
jiaziyang
2022-05-17 17:20:45 +08:00
parent f27efa08fe
commit e02a330d07
17 changed files with 350 additions and 86 deletions
+2
View File
@@ -11,6 +11,8 @@ group("hdf_platform_fuzztest") {
"gpio_fuzzer:fuzztest",
"i2c_fuzzer:fuzztest",
"pwm_fuzzer:fuzztest",
"rtc_fuzzer:fuzztest",
"uart_fuzzer:fuzztest",
"watchdog_fuzzer:fuzztest",
]
}
@@ -1,27 +1,17 @@
/*
* 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.
* 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.
*/
#include "gpio_fuzzer.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
#include <cstddef>
#include "random.h"
#include "securec.h"
#include "gpio_if.h"
#include "hdf_base.h"
#include "securec.h"
using namespace std;
@@ -29,7 +19,6 @@ namespace {
constexpr int32_t MIN = 0;
constexpr int32_t MAX = 2;
const uint16_t gpioTestNum = 3;
const int32_t MAX_LEN = 10;
}
struct AllParameters {
@@ -38,11 +27,6 @@ struct AllParameters {
uint16_t descMode;
};
union ConvertToParam {
uint8_t data[MAX_LEN];
struct AllParameters params;
};
static int32_t GpioTestIrqHandler(uint16_t gpio, void *data)
{
(void)gpio;
@@ -50,37 +34,29 @@ static int32_t GpioTestIrqHandler(uint16_t 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)
bool GpioFuzzTest(const uint8_t *data, size_t size)
{
union ConvertToParam convertor;
int32_t number;
struct AllParameters params;
if (data == nullptr) {
return false;
}
if (memcpy_s (convertor.data, MAX_LEN, data, MAX_LEN) != EOK) {
if (memcpy_s((void *)&params, sizeof(params), data, sizeof(params)) != EOK) {
return false;
}
int32_t number = randNum();
switch (static_cast<ApiNumber>(number)) {
case ApiNumber::NUM_ZERO:
GpioWrite(gpioTestNum, convertor.params.descVal);
number = randNum(MIN, MAX);
switch (static_cast<ApiTestCmd>(number)) {
case ApiTestCmd::GPIO_FUZZ_WRITE:
GpioWrite(gpioTestNum, params.descVal);
break;
case ApiNumber::NUM_ONE:
GpioSetDir(gpioTestNum, convertor.params.descDir);
case ApiTestCmd::GPIO_FUZZ_SET_DIR:
GpioSetDir(gpioTestNum, params.descDir);
break;
case ApiNumber::NUM_TWO:
GpioSetIrq(gpioTestNum, convertor.params.descMode, GpioTestIrqHandler, &data);
case ApiTestCmd::GPIO_FUZZ_SET_IRQ:
GpioSetIrq(gpioTestNum, params.descMode, GpioTestIrqHandler, &data);
break;
default:
break;
@@ -90,7 +66,7 @@ namespace OHOS {
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
OHOS::GpioFuzzTest(data, size);
@@ -11,10 +11,10 @@
#define FUZZ_PROJECT_NAME "gpio_fuzzer"
enum class ApiNumber {
NUM_ZERO = 0,
NUM_ONE,
NUM_TWO,
enum class ApiTestCmd {
GPIO_FUZZ_WRITE = 0,
GPIO_FUZZ_SET_DIR,
GPIO_FUZZ_SET_IRQ,
};
#endif
@@ -1,12 +1,10 @@
<?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.
*/
<!--
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>
@@ -1,11 +1,10 @@
<?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>
@@ -7,23 +7,18 @@
*/
#include "pwm_fuzzer.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
#include <cstddef>
#include "hdf_base.h"
#include "pwm_if.h"
#include "random.h"
#include "securec.h"
#include "hdf_base.h"
#include "pwm_if.h"
using namespace std;
namespace {
constexpr int32_t MIN = 0;
constexpr int32_t MAX = 2;
const int32_t PWM_TEST_NUM = 1;
const int32_t MAX_LEN = 15;
const int32_t PWM_FUZZ_NUM = 1;
}
struct AllParameters {
@@ -32,47 +27,43 @@ struct AllParameters {
uint8_t descPolar;
};
union ConvertToParam {
uint8_t data[MAX_LEN];
struct AllParameters params;
};
namespace OHOS {
bool PwmFuzzTest(const uint8_t* data, size_t size)
bool PwmFuzzTest(const uint8_t *data, size_t size)
{
int32_t number;
DevHandle handle = nullptr;
union ConvertToParam convertor;
struct AllParameters params;
if (data == nullptr) {
return false;
}
if (memcpy_s (convertor.data, MAX_LEN, data, MAX_LEN) != EOK) {
if (memcpy_s((void *)&params, sizeof(params), data, sizeof(params)) != EOK) {
return false;
}
number = randNum(MIN, MAX);
handle = PwmOpen(PWM_TEST_NUM);
switch (static_cast<ApiNumber>(number)) {
case ApiNumber::NUM_ZERO:
PwmSetPeriod(handle, convertor.params.descPer);
handle = PwmOpen(PWM_FUZZ_NUM);
switch (static_cast<ApiTestCmd>(number)) {
case ApiTestCmd::PWM_FUZZ_SET_PERIOD:
PwmSetPeriod(handle, params.descPer);
break;
case ApiNumber::NUM_ONE:
PwmSetDuty(handle, convertor.params.descDuty);
case ApiTestCmd::PWM_FUZZ_SET_DUTY:
PwmSetDuty(handle, params.descDuty);
break;
case ApiNumber::NUM_TWO:
PwmSetPolarity(handle, convertor.params.descPolar);
case ApiTestCmd::PWM_FUZZ_SET_POLARITY:
PwmSetPolarity(handle, params.descPolar);
break;
default:
break;
}
PwmClose(handle);
return true;
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
OHOS::PwmFuzzTest(data, size);
@@ -11,10 +11,10 @@
#define FUZZ_PROJECT_NAME "pwm_fuzzer"
enum class ApiNumber {
NUM_ZERO = 0,
NUM_ONE,
NUM_TWO,
enum class ApiTestCmd {
PWM_FUZZ_SET_PERIOD = 0,
PWM_FUZZ_SET_DUTY,
PWM_FUZZ_SET_POLARITY,
};
#endif
@@ -0,0 +1,27 @@
# 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("//drivers/framework/support/platform/test/fuzztest/fuzz.gni")
ohos_fuzztest("RtcFuzzTest") {
module_out_path = module_output_path
fuzz_config_file =
"//drivers/framework/support/platform/test/fuzztest/rtc_fuzzer"
include_dirs +=
[ "//drivers/framework/support/platform/test/fuzztest/rtc_fuzzer" ]
sources = [ "rtc_fuzzer.cpp" ]
external_deps = platform_fuzzexternal_deps
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":RtcFuzzTest" ]
}
@@ -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,18 @@
<?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>32</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>
@@ -0,0 +1,81 @@
/*
* 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.
*/
#include "rtc_fuzzer.h"
#include <iostream>
#include "random.h"
#include "securec.h"
#include "hdf_base.h"
#include "rtc_if.h"
using namespace std;
namespace {
constexpr int32_t MIN = 0;
constexpr int32_t MAX = 4;
const uint8_t RTC_USER_INDEX = 8;
}
struct AllParameters {
uint32_t desFreq;
uint8_t desValue;
uint8_t desEnable;
struct RtcTime paraTime;
uint32_t paraAlarmIndex;
};
namespace OHOS {
bool RtcFuzzTest(const uint8_t *data, size_t size)
{
int32_t number;
DevHandle handle = nullptr;
struct AllParameters params;
if (data == nullptr) {
return false;
}
if (memcpy_s((void *)&params, sizeof(params), data, sizeof(params)) != EOK) {
return false;
}
number = randNum(MIN, MAX);
handle = RtcOpen();
switch (static_cast<ApiTestCmd>(number)) {
case ApiTestCmd::RTC_FUZZ_WRITETIME:
RtcWriteTime(handle, &params.paraTime);
break;
case ApiTestCmd::RTC_FUZZ_WRITEALARM:
RtcWriteAlarm(handle, (enum RtcAlarmIndex)params.paraAlarmIndex,
&params.paraTime);
break;
case ApiTestCmd::RTC_FUZZ_ALARMINTERRUPTENABLE:
RtcAlarmInterruptEnable(handle, (enum RtcAlarmIndex)params.paraAlarmIndex,
params.desEnable);
break;
case ApiTestCmd::RTC_FUZZ_SETFREQ:
RtcSetFreq(handle, params.desFreq);
break;
case ApiTestCmd::RTC_FUZZ_WRITEREG:
RtcWriteReg(handle, RTC_USER_INDEX, params.desValue);
break;
default:
break;
}
RtcClose(handle);
return true;
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
OHOS::RtcFuzzTest(data, size);
return 0;
}
@@ -0,0 +1,22 @@
/*
* 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 RTC_FUZZER
#define RTC_FUZZER
#define FUZZ_PROJECT_NAME "rtc_fuzzer"
enum class ApiTestCmd {
RTC_FUZZ_WRITETIME = 0,
RTC_FUZZ_WRITEALARM,
RTC_FUZZ_ALARMINTERRUPTENABLE,
RTC_FUZZ_SETFREQ,
RTC_FUZZ_WRITEREG,
};
#endif
@@ -0,0 +1,27 @@
# 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("//drivers/framework/support/platform/test/fuzztest/fuzz.gni")
ohos_fuzztest("UartFuzzTest") {
module_out_path = module_output_path
fuzz_config_file =
"//drivers/framework/support/platform/test/fuzztest/uart_fuzzer"
include_dirs +=
[ "//drivers/framework/support/platform/test/fuzztest/uart_fuzzer" ]
sources = [ "uart_fuzzer.cpp" ]
external_deps = platform_fuzzexternal_deps
}
group("fuzztest") {
testonly = true
deps = []
deps += [ ":UartFuzzTest" ]
}
@@ -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,18 @@
<?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>15</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>
@@ -0,0 +1,71 @@
/*
* 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.
*/
#include "uart_fuzzer.h"
#include <iostream>
#include "random.h"
#include "securec.h"
#include "hdf_base.h"
#include "uart_if.h"
using namespace std;
namespace {
constexpr int32_t MIN = 0;
constexpr int32_t MAX = 2;
const int32_t UART_FUZZ_PORT = 1;
}
struct AllParameters {
uint32_t desBaudRate;
struct UartAttribute paraAttribute;
uint32_t paraMode;
};
namespace OHOS {
bool UartFuzzTest(const uint8_t *data, size_t size)
{
int32_t number;
DevHandle handle = nullptr;
struct AllParameters params;
if (data == nullptr) {
return false;
}
if (memcpy_s((void *)&params, sizeof(params), data, sizeof(params)) != EOK) {
return false;
}
number = randNum(MIN, MAX);
handle = UartOpen(UART_FUZZ_PORT);
switch (static_cast<ApiTestCmd>(number)) {
case ApiTestCmd::UART_FUZZ_SET_BAUD:
UartSetBaud(handle, params.desBaudRate);
break;
case ApiTestCmd::UART_FUZZ_SET_ATTRIBUTE:
UartSetAttribute(handle, &params.paraAttribute);
break;
case ApiTestCmd::UART_FUZZ_SET_TRANSMODE:
UartSetTransMode(handle, (enum UartTransMode)params.paraMode);
break;
default:
break;
}
UartClose(handle);
return true;
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
OHOS::UartFuzzTest(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 UART_FUZZER
#define UART_FUZZER
#define FUZZ_PROJECT_NAME "uart_fuzzer"
enum class ApiTestCmd {
UART_FUZZ_SET_BAUD = 0,
UART_FUZZ_SET_ATTRIBUTE,
UART_FUZZ_SET_TRANSMODE,
};
#endif