diff --git a/support/platform/test/fuzztest/BUILD.gn b/support/platform/test/fuzztest/BUILD.gn index 941b0fc5..5d43bca4 100644 --- a/support/platform/test/fuzztest/BUILD.gn +++ b/support/platform/test/fuzztest/BUILD.gn @@ -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", ] } diff --git a/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.cpp b/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.cpp index 5e7fb780..68766d3e 100644 --- a/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.cpp +++ b/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.cpp @@ -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 -#include #include -#include -#include +#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 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 *)¶ms, sizeof(params), data, sizeof(params)) != EOK) { return false; } - - int32_t number = randNum(); - switch (static_cast(number)) { - case ApiNumber::NUM_ZERO: - GpioWrite(gpioTestNum, convertor.params.descVal); + number = randNum(MIN, MAX); + switch (static_cast(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); diff --git a/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.h b/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.h index 0bbd10c0..15fa5960 100644 --- a/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.h +++ b/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.h @@ -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 diff --git a/support/platform/test/fuzztest/gpio_fuzzer/project.xml b/support/platform/test/fuzztest/gpio_fuzzer/project.xml index aae2579b..91ed0fe5 100644 --- a/support/platform/test/fuzztest/gpio_fuzzer/project.xml +++ b/support/platform/test/fuzztest/gpio_fuzzer/project.xml @@ -1,12 +1,10 @@ - diff --git a/support/platform/test/fuzztest/pwm_fuzzer/project.xml b/support/platform/test/fuzztest/pwm_fuzzer/project.xml index bc1da67d..39080f37 100644 --- a/support/platform/test/fuzztest/pwm_fuzzer/project.xml +++ b/support/platform/test/fuzztest/pwm_fuzzer/project.xml @@ -1,11 +1,10 @@ - diff --git a/support/platform/test/fuzztest/pwm_fuzzer/pwm_fuzzer.cpp b/support/platform/test/fuzztest/pwm_fuzzer/pwm_fuzzer.cpp index 6229d40e..bd6536a4 100644 --- a/support/platform/test/fuzztest/pwm_fuzzer/pwm_fuzzer.cpp +++ b/support/platform/test/fuzztest/pwm_fuzzer/pwm_fuzzer.cpp @@ -7,23 +7,18 @@ */ #include "pwm_fuzzer.h" -#include -#include #include -#include -#include -#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 *)¶ms, sizeof(params), data, sizeof(params)) != EOK) { return false; } number = randNum(MIN, MAX); - handle = PwmOpen(PWM_TEST_NUM); - switch (static_cast(number)) { - case ApiNumber::NUM_ZERO: - PwmSetPeriod(handle, convertor.params.descPer); + handle = PwmOpen(PWM_FUZZ_NUM); + switch (static_cast(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); diff --git a/support/platform/test/fuzztest/pwm_fuzzer/pwm_fuzzer.h b/support/platform/test/fuzztest/pwm_fuzzer/pwm_fuzzer.h index ff968f58..36268621 100644 --- a/support/platform/test/fuzztest/pwm_fuzzer/pwm_fuzzer.h +++ b/support/platform/test/fuzztest/pwm_fuzzer/pwm_fuzzer.h @@ -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 diff --git a/support/platform/test/fuzztest/rtc_fuzzer/BUILD.gn b/support/platform/test/fuzztest/rtc_fuzzer/BUILD.gn new file mode 100644 index 00000000..3977dedb --- /dev/null +++ b/support/platform/test/fuzztest/rtc_fuzzer/BUILD.gn @@ -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" ] +} diff --git a/support/platform/test/fuzztest/rtc_fuzzer/corpus/init b/support/platform/test/fuzztest/rtc_fuzzer/corpus/init new file mode 100644 index 00000000..217a7309 --- /dev/null +++ b/support/platform/test/fuzztest/rtc_fuzzer/corpus/init @@ -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 diff --git a/support/platform/test/fuzztest/rtc_fuzzer/project.xml b/support/platform/test/fuzztest/rtc_fuzzer/project.xml new file mode 100644 index 00000000..869f389a --- /dev/null +++ b/support/platform/test/fuzztest/rtc_fuzzer/project.xml @@ -0,0 +1,18 @@ + + + + + + 32 + + 120 + + 2048 + + diff --git a/support/platform/test/fuzztest/rtc_fuzzer/rtc_fuzzer.cpp b/support/platform/test/fuzztest/rtc_fuzzer/rtc_fuzzer.cpp new file mode 100644 index 00000000..269af1fb --- /dev/null +++ b/support/platform/test/fuzztest/rtc_fuzzer/rtc_fuzzer.cpp @@ -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 +#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 *)¶ms, sizeof(params), data, sizeof(params)) != EOK) { + return false; + } + + number = randNum(MIN, MAX); + handle = RtcOpen(); + switch (static_cast(number)) { + case ApiTestCmd::RTC_FUZZ_WRITETIME: + RtcWriteTime(handle, ¶ms.paraTime); + break; + case ApiTestCmd::RTC_FUZZ_WRITEALARM: + RtcWriteAlarm(handle, (enum RtcAlarmIndex)params.paraAlarmIndex, + ¶ms.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; +} diff --git a/support/platform/test/fuzztest/rtc_fuzzer/rtc_fuzzer.h b/support/platform/test/fuzztest/rtc_fuzzer/rtc_fuzzer.h new file mode 100644 index 00000000..05f92b9f --- /dev/null +++ b/support/platform/test/fuzztest/rtc_fuzzer/rtc_fuzzer.h @@ -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 diff --git a/support/platform/test/fuzztest/uart_fuzzer/BUILD.gn b/support/platform/test/fuzztest/uart_fuzzer/BUILD.gn new file mode 100644 index 00000000..756e3813 --- /dev/null +++ b/support/platform/test/fuzztest/uart_fuzzer/BUILD.gn @@ -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" ] +} diff --git a/support/platform/test/fuzztest/uart_fuzzer/corpus/init b/support/platform/test/fuzztest/uart_fuzzer/corpus/init new file mode 100644 index 00000000..217a7309 --- /dev/null +++ b/support/platform/test/fuzztest/uart_fuzzer/corpus/init @@ -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 diff --git a/support/platform/test/fuzztest/uart_fuzzer/project.xml b/support/platform/test/fuzztest/uart_fuzzer/project.xml new file mode 100644 index 00000000..39080f37 --- /dev/null +++ b/support/platform/test/fuzztest/uart_fuzzer/project.xml @@ -0,0 +1,18 @@ + + + + + + 15 + + 120 + + 2048 + + diff --git a/support/platform/test/fuzztest/uart_fuzzer/uart_fuzzer.cpp b/support/platform/test/fuzztest/uart_fuzzer/uart_fuzzer.cpp new file mode 100644 index 00000000..4cefaf6f --- /dev/null +++ b/support/platform/test/fuzztest/uart_fuzzer/uart_fuzzer.cpp @@ -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 +#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 *)¶ms, sizeof(params), data, sizeof(params)) != EOK) { + return false; + } + + number = randNum(MIN, MAX); + handle = UartOpen(UART_FUZZ_PORT); + switch (static_cast(number)) { + case ApiTestCmd::UART_FUZZ_SET_BAUD: + UartSetBaud(handle, params.desBaudRate); + break; + case ApiTestCmd::UART_FUZZ_SET_ATTRIBUTE: + UartSetAttribute(handle, ¶ms.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; +} diff --git a/support/platform/test/fuzztest/uart_fuzzer/uart_fuzzer.h b/support/platform/test/fuzztest/uart_fuzzer/uart_fuzzer.h new file mode 100644 index 00000000..49c7e5f7 --- /dev/null +++ b/support/platform/test/fuzztest/uart_fuzzer/uart_fuzzer.h @@ -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