diff --git a/support/platform/test/fuzztest/BUILD.gn b/support/platform/test/fuzztest/BUILD.gn new file mode 100644 index 00000000..ee91bbdb --- /dev/null +++ b/support/platform/test/fuzztest/BUILD.gn @@ -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" ] +} diff --git a/support/platform/test/fuzztest/gpio_fuzzer/BUILD.gn b/support/platform/test/fuzztest/gpio_fuzzer/BUILD.gn new file mode 100644 index 00000000..fb9cb394 --- /dev/null +++ b/support/platform/test/fuzztest/gpio_fuzzer/BUILD.gn @@ -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" ] +} diff --git a/support/platform/test/fuzztest/gpio_fuzzer/corpus/init b/support/platform/test/fuzztest/gpio_fuzzer/corpus/init new file mode 100644 index 00000000..c2cc6e66 --- /dev/null +++ b/support/platform/test/fuzztest/gpio_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/gpio_fuzzer/gpio_fuzzer.cpp b/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.cpp new file mode 100644 index 00000000..5e7fb780 --- /dev/null +++ b/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.cpp @@ -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 +#include +#include +#include +#include +#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 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(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; +} diff --git a/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.h b/support/platform/test/fuzztest/gpio_fuzzer/gpio_fuzzer.h new file mode 100644 index 00000000..0bbd10c0 --- /dev/null +++ b/support/platform/test/fuzztest/gpio_fuzzer/gpio_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 GPIO_FUZZER +#define GPIO_FUZZER + +#define FUZZ_PROJECT_NAME "gpio_fuzzer" + +enum class ApiNumber { + NUM_ZERO = 0, + NUM_ONE, + NUM_TWO, +}; + +#endif diff --git a/support/platform/test/fuzztest/gpio_fuzzer/project.xml b/support/platform/test/fuzztest/gpio_fuzzer/project.xml new file mode 100644 index 00000000..aae2579b --- /dev/null +++ b/support/platform/test/fuzztest/gpio_fuzzer/project.xml @@ -0,0 +1,20 @@ + + + + + + 10 + + 120 + + 2048 + +