diff --git a/test/fuzztest/addnotificationslots_fuzzer/BUILD.gn b/test/fuzztest/addnotificationslots_fuzzer/BUILD.gn index 9122535cb16e0b131542a32bac4d09944f0038bc..e4d87870c7f48012f71dceecddbfcbc79eff34ff 100644 --- a/test/fuzztest/addnotificationslots_fuzzer/BUILD.gn +++ b/test/fuzztest/addnotificationslots_fuzzer/BUILD.gn @@ -23,7 +23,7 @@ ohos_fuzztest("AddNotificationSlotsFuzzTest") { fuzz_config_file = "${component_path}/test/fuzztest/addnotificationslots_fuzzer" - include_dirs = [] + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] cflags = [ "-g", "-O0", @@ -32,7 +32,10 @@ ohos_fuzztest("AddNotificationSlotsFuzzTest") { ] sources = [ "addnotificationslots_fuzzer.cpp" ] - deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] external_deps = [ "ability_base:want", diff --git a/test/fuzztest/addnotificationslots_fuzzer/addnotificationslots_fuzzer.cpp b/test/fuzztest/addnotificationslots_fuzzer/addnotificationslots_fuzzer.cpp index 652be8a9ce2af2f77a1990fd3d56f24df8eb5213..bdbd1ccc6b6183ff705080e0d5ef1bbe9ca05586 100644 --- a/test/fuzztest/addnotificationslots_fuzzer/addnotificationslots_fuzzer.cpp +++ b/test/fuzztest/addnotificationslots_fuzzer/addnotificationslots_fuzzer.cpp @@ -23,15 +23,15 @@ constexpr uint8_t SLOT_VISIBLENESS_TYPE_NUM = 4; constexpr uint8_t SLOT_TYPE_NUM = 5; namespace OHOS { - bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { - std::string stringData = reinterpret_cast(data); + std::string stringData(data); Notification::NotificationSlot slot; slot.SetDescription(stringData); slot.SetEnableLight(*data % ENABLE); slot.SetEnableVibration(*data % ENABLE); - slot.SetLedLightColor(U32_AT(data)); + slot.SetLedLightColor(GetU32Data(data)); uint8_t level = *data % SLOT_LEVEL_NUM; Notification::NotificationSlot::NotificationLevel notificatoinLevel = @@ -57,6 +57,11 @@ namespace OHOS { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - OHOS::DoSomethingInterestingWithMyAPI(data, size); + char *ch = ParseData(data, size); + if (ch != nullptr && size >= GetU32Size()) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } return 0; } diff --git a/test/fuzztest/addnotificationslots_fuzzer/addnotificationslots_fuzzer.h b/test/fuzztest/addnotificationslots_fuzzer/addnotificationslots_fuzzer.h index 5b3992afcb68da4bed2b68b1087ea9e4da1405f8..32b2436d3563358ec3271a2f6941f880f4e28b3d 100644 --- a/test/fuzztest/addnotificationslots_fuzzer/addnotificationslots_fuzzer.h +++ b/test/fuzztest/addnotificationslots_fuzzer/addnotificationslots_fuzzer.h @@ -16,14 +16,8 @@ #ifndef TEST_FUZZTEST_ADDNOTIFICATIONSLOTS_FUZZER_ADDNOTIFICATIONSLOTS_FUZZER_H #define TEST_FUZZTEST_ADDNOTIFICATIONSLOTS_FUZZER_ADDNOTIFICATIONSLOTS_FUZZER_H -#define FUZZ_PROJECT_NAME "addnotificationslots_fuzzer" - -#include +#include "fuzz_common_base.h" -uint32_t U32_AT(const uint8_t *ptr) -{ - // convert fuzz input data to an integer - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; -} +#define FUZZ_PROJECT_NAME "addnotificationslots_fuzzer" #endif // TEST_FUZZTEST_ADDNOTIFICATIONSLOTS_FUZZER_ADDNOTIFICATIONSLOTS_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/addslotbytype_fuzzer/BUILD.gn b/test/fuzztest/addslotbytype_fuzzer/BUILD.gn index fda93e8549229378ae73e46c3e101169ca032339..87afcae0572a4f9fdc8c2fd2326be9af426a59fe 100644 --- a/test/fuzztest/addslotbytype_fuzzer/BUILD.gn +++ b/test/fuzztest/addslotbytype_fuzzer/BUILD.gn @@ -22,7 +22,7 @@ ohos_fuzztest("AddSlotByTypeFuzzTest") { module_out_path = module_output_path fuzz_config_file = "${component_path}/test/fuzztest/addslotbytype_fuzzer" - include_dirs = [] + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] cflags = [ "-g", "-O0", @@ -31,7 +31,10 @@ ohos_fuzztest("AddSlotByTypeFuzzTest") { ] sources = [ "addslotbytype_fuzzer.cpp" ] - deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] external_deps = [ "ability_base:want", diff --git a/test/fuzztest/addslotbytype_fuzzer/addslotbytype_fuzzer.cpp b/test/fuzztest/addslotbytype_fuzzer/addslotbytype_fuzzer.cpp index 545293a851903c1b094508f533a85cd90f55f481..22c95e0b39dd458f2723f816d60db24b34f3c03c 100644 --- a/test/fuzztest/addslotbytype_fuzzer/addslotbytype_fuzzer.cpp +++ b/test/fuzztest/addslotbytype_fuzzer/addslotbytype_fuzzer.cpp @@ -20,7 +20,7 @@ constexpr uint8_t SLOT_TYPE_NUM = 5; namespace OHOS { - bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { uint8_t type = *data % SLOT_TYPE_NUM; Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type); @@ -32,6 +32,11 @@ namespace OHOS { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - OHOS::DoSomethingInterestingWithMyAPI(data, size); + char *ch = ParseData(data, size); + if (ch != nullptr) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } return 0; } diff --git a/test/fuzztest/addslotbytype_fuzzer/addslotbytype_fuzzer.h b/test/fuzztest/addslotbytype_fuzzer/addslotbytype_fuzzer.h index a53388f4245e771bfe9f46455d624a52133ce736..3eef93eb64034a749f92f69e877232367065751d 100644 --- a/test/fuzztest/addslotbytype_fuzzer/addslotbytype_fuzzer.h +++ b/test/fuzztest/addslotbytype_fuzzer/addslotbytype_fuzzer.h @@ -16,6 +16,8 @@ #ifndef TEST_FUZZTEST_ADDSLOTBYTYPE_FUZZER_ADDSLOTBYTYPE_FUZZER_H #define TEST_FUZZTEST_ADDSLOTBYTYPE_FUZZER_ADDSLOTBYTYPE_FUZZER_H +#include "fuzz_common_base.h" + #define FUZZ_PROJECT_NAME "AddSlotByType_fuzzer" #endif // TEST_FUZZTEST_ADDSLOTBYTYPE_FUZZER_ADDSLOTBYTYPE_FUZZER_H diff --git a/test/fuzztest/cancelnotification_fuzzer/BUILD.gn b/test/fuzztest/cancelnotification_fuzzer/BUILD.gn index 93ff2aca5772d468411a348db38d5bbe04be044c..1e9c69bc78eb1cd330e06643b21905ce3ef5a7ce 100644 --- a/test/fuzztest/cancelnotification_fuzzer/BUILD.gn +++ b/test/fuzztest/cancelnotification_fuzzer/BUILD.gn @@ -22,7 +22,7 @@ ohos_fuzztest("CancelNotificationFuzzTest") { module_out_path = module_output_path fuzz_config_file = "${component_path}/test/fuzztest/cancelnotification_fuzzer" - include_dirs = [] + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] cflags = [ "-g", "-O0", @@ -31,7 +31,10 @@ ohos_fuzztest("CancelNotificationFuzzTest") { ] sources = [ "cancelnotification_fuzzer.cpp" ] - deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] external_deps = [ "ability_base:want", diff --git a/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp b/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp index b5d76b9da5cdfab63f92b36d1be67f780954b5b9..389adbab80dadc4693752ff1796c9a5f2bb84616 100644 --- a/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp +++ b/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.cpp @@ -18,10 +18,10 @@ #include "notification_helper.h" namespace OHOS { - bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { - std::string label = reinterpret_cast(data); - int32_t notificationId = static_cast(U32_AT(data)); + std::string label(data); + int32_t notificationId = static_cast(GetU32Data(data)); return Notification::NotificationHelper::CancelNotification(label, notificationId) == ERR_OK; } } @@ -30,6 +30,11 @@ namespace OHOS { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - OHOS::DoSomethingInterestingWithMyAPI(data, size); + char *ch = ParseData(data, size); + if (ch != nullptr && size >= GetU32Size()) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } return 0; } diff --git a/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.h b/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.h index 51bb27c9da63d87f29f127d91528758c1682c78b..5d9628559613d974655ef3137cba533f16842c65 100644 --- a/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.h +++ b/test/fuzztest/cancelnotification_fuzzer/cancelnotification_fuzzer.h @@ -16,13 +16,10 @@ #ifndef TEST_FUZZTEST_CANCELNOTIFICATION_FUZZER_CANCELNOTIFICATION_FUZZER_H #define TEST_FUZZTEST_CANCELNOTIFICATION_FUZZER_CANCELNOTIFICATION_FUZZER_H +#include "fuzz_common_base.h" + #define FUZZ_PROJECT_NAME "cancelnotification_fuzzer" #include -uint32_t U32_AT(const uint8_t *ptr) -{ - // convert fuzz input data to an integer - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; -} #endif // TEST_FUZZTEST_CANCELNOTIFICATION_FUZZER_CANCELNOTIFICATION_FUZZER_H diff --git a/test/fuzztest/fuzz_common_base/BUILD.gn b/test/fuzztest/fuzz_common_base/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..837ab34025868475aefa281836e41f1f96755564 --- /dev/null +++ b/test/fuzztest/fuzz_common_base/BUILD.gn @@ -0,0 +1,23 @@ +# 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. + +static_library("fuzz_common_base") { + sources = [ "fuzz_common_base.cpp" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + public_deps = [ "//commonlibrary/c_utils/base:utils" ] +} diff --git a/test/fuzztest/fuzz_common_base/fuzz_common_base.cpp b/test/fuzztest/fuzz_common_base/fuzz_common_base.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8192760375f7e2524baf1cf44a1134df903ff0e --- /dev/null +++ b/test/fuzztest/fuzz_common_base/fuzz_common_base.cpp @@ -0,0 +1,57 @@ +/* + * 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 "fuzz_common_base.h" + +extern "C" { +static constexpr uint32_t U32_AT_SIZE = 4; +static constexpr uint32_t MAX_MEMORY_SIZE = 4 * 1024 * 1024; + +uint32_t GetU32Size() +{ + return U32_AT_SIZE; +} + +uint32_t GetU32Data(const char* ptr) +{ + // convert fuzz input data to an integer + return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; +} + +char* ParseData(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return nullptr; + } + + if (size > MAX_MEMORY_SIZE) { + return nullptr; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + return nullptr; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + free(ch); + ch = nullptr; + return nullptr; + } + + return ch; +} +} \ No newline at end of file diff --git a/test/fuzztest/fuzz_common_base/fuzz_common_base.h b/test/fuzztest/fuzz_common_base/fuzz_common_base.h new file mode 100644 index 0000000000000000000000000000000000000000..f1dcc75e186c9d0c3be496ef3c397783c2db501b --- /dev/null +++ b/test/fuzztest/fuzz_common_base/fuzz_common_base.h @@ -0,0 +1,30 @@ +/* + * 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. + */ + +#ifndef FUZZ_COMMON_BASE_H +#define FUZZ_COMMON_BASE_H + +#include +#include "securec.h" + +extern "C" { +uint32_t GetU32Size(); + +uint32_t GetU32Data(const char* ptr); + +char* ParseData(const uint8_t* data, size_t size); +} + +#endif // FUZZ_COMMON_BASE_H diff --git a/test/fuzztest/getnotificationslot_fuzzer/BUILD.gn b/test/fuzztest/getnotificationslot_fuzzer/BUILD.gn index 7b6605c2d349b0b4d0db56693f91966e1c5a7576..e4c124e11b21f9b86aad3946a50c1ba8359d3b3e 100644 --- a/test/fuzztest/getnotificationslot_fuzzer/BUILD.gn +++ b/test/fuzztest/getnotificationslot_fuzzer/BUILD.gn @@ -23,7 +23,7 @@ ohos_fuzztest("GetNotificationSlotFuzzTest") { fuzz_config_file = "${component_path}/test/fuzztest/getnotificationslot_fuzzer" - include_dirs = [] + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] cflags = [ "-g", "-O0", @@ -32,7 +32,10 @@ ohos_fuzztest("GetNotificationSlotFuzzTest") { ] sources = [ "getnotificationslot_fuzzer.cpp" ] - deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] external_deps = [ "ability_base:want", diff --git a/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.cpp b/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.cpp index efa8cf4679240edf4b768b60282978381518d8b8..e79265e3785f8beb6fe2a64d83843c15d2a8eb7b 100644 --- a/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.cpp +++ b/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.cpp @@ -20,7 +20,7 @@ constexpr uint8_t SLOT_TYPE_NUM = 5; namespace OHOS { - bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { uint8_t type = *data % SLOT_TYPE_NUM; Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type); @@ -33,6 +33,11 @@ namespace OHOS { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - OHOS::DoSomethingInterestingWithMyAPI(data, size); + char *ch = ParseData(data, size); + if (ch != nullptr) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } return 0; } diff --git a/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.h b/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.h index d704a6a7a3aa2712bc1afd23151d502028bc591e..b14614dd4abe8e3150147647949dcfc70e8328d8 100644 --- a/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.h +++ b/test/fuzztest/getnotificationslot_fuzzer/getnotificationslot_fuzzer.h @@ -16,14 +16,8 @@ #ifndef TEST_FUZZTEST_GETNOTIFICATIONSLOT_FUZZER_GETNOTIFICATIONSLOT_FUZZER_H #define TEST_FUZZTEST_GETNOTIFICATIONSLOT_FUZZER_GETNOTIFICATIONSLOT_FUZZER_H -#define FUZZ_PROJECT_NAME "getnotificationslot_fuzzer" - -#include +#include "fuzz_common_base.h" -uint32_t U32_AT(const uint8_t *ptr) -{ - // convert fuzz input data to an integer - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; -} +#define FUZZ_PROJECT_NAME "getnotificationslot_fuzzer" #endif // TEST_FUZZTEST_GETNOTIFICATIONSLOT_FUZZER_GETNOTIFICATIONSLOT_FUZZER_H diff --git a/test/fuzztest/getnotificationslotnumasbundle_fuzzer/BUILD.gn b/test/fuzztest/getnotificationslotnumasbundle_fuzzer/BUILD.gn index 5a1a638d36099a5e5c841dc6ce81466b32389fc3..931dc534821b9c9a473fbb4ad0e8cd4ee7089db8 100644 --- a/test/fuzztest/getnotificationslotnumasbundle_fuzzer/BUILD.gn +++ b/test/fuzztest/getnotificationslotnumasbundle_fuzzer/BUILD.gn @@ -23,7 +23,7 @@ ohos_fuzztest("GetNotificationSlotNumAsBundleFuzzTest") { fuzz_config_file = "${component_path}/test/fuzztest/getnotificationslotnumasbundle_fuzzer" - include_dirs = [] + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] cflags = [ "-g", "-O0", @@ -32,7 +32,10 @@ ohos_fuzztest("GetNotificationSlotNumAsBundleFuzzTest") { ] sources = [ "getnotificationslotnumasbundle_fuzzer.cpp" ] - deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] external_deps = [ "ability_base:want", diff --git a/test/fuzztest/getnotificationslotnumasbundle_fuzzer/getnotificationslotnumasbundle_fuzzer.cpp b/test/fuzztest/getnotificationslotnumasbundle_fuzzer/getnotificationslotnumasbundle_fuzzer.cpp index 14da1968cc0435f92aa3d3c703e2ac71c9fc7e75..e777038f8759892f39ea4688eae39e0c8a108094 100644 --- a/test/fuzztest/getnotificationslotnumasbundle_fuzzer/getnotificationslotnumasbundle_fuzzer.cpp +++ b/test/fuzztest/getnotificationslotnumasbundle_fuzzer/getnotificationslotnumasbundle_fuzzer.cpp @@ -18,12 +18,12 @@ #include "notification_helper.h" namespace OHOS { - bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { Notification::NotificationBundleOption bundleOption; - std::string bundleName = reinterpret_cast(data); + std::string bundleName (data); bundleOption.SetBundleName(bundleName); - int32_t uid = static_cast(U32_AT(data)); + int32_t uid = static_cast(GetU32Data(data)); bundleOption.SetUid(uid); uint64_t num; return Notification::NotificationHelper::GetNotificationSlotNumAsBundle(bundleOption, num) == ERR_OK; @@ -34,6 +34,11 @@ namespace OHOS { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - OHOS::DoSomethingInterestingWithMyAPI(data, size); + char *ch = ParseData(data, size); + if (ch != nullptr && size >= GetU32Size()) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } return 0; } diff --git a/test/fuzztest/getnotificationslotnumasbundle_fuzzer/getnotificationslotnumasbundle_fuzzer.h b/test/fuzztest/getnotificationslotnumasbundle_fuzzer/getnotificationslotnumasbundle_fuzzer.h index 7f361f4f35435f5a0ad1488f16f94e5e73c174d4..9632713f143527a9caab845e80bc89f57e128729 100644 --- a/test/fuzztest/getnotificationslotnumasbundle_fuzzer/getnotificationslotnumasbundle_fuzzer.h +++ b/test/fuzztest/getnotificationslotnumasbundle_fuzzer/getnotificationslotnumasbundle_fuzzer.h @@ -16,14 +16,8 @@ #ifndef TEST_FUZZTEST_GETNOTIFICATIONSLOTNUMASBUNDLE_FUZZER_GETNOTIFICATIONSLOTNUMASBUNDLE_FUZZER_H #define TEST_FUZZTEST_GETNOTIFICATIONSLOTNUMASBUNDLE_FUZZER_GETNOTIFICATIONSLOTNUMASBUNDLE_FUZZER_H -#include +#include "fuzz_common_base.h" #define FUZZ_PROJECT_NAME "getnotificationslotnumasbundle_fuzzer" -uint32_t U32_AT(const uint8_t *ptr) -{ - // convert fuzz input data to an integer - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; -} - #endif // TEST_FUZZTEST_GETNOTIFICATIONSLOTNUMASBUNDLE_FUZZER_GETNOTIFICATIONSLOTNUMASBUNDLE_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/publishnotification_fuzzer/BUILD.gn b/test/fuzztest/publishnotification_fuzzer/BUILD.gn index 18e28369a20df65f2784ee63a983c5cd759f1fa2..a202ab689d4d55ad77eb2bed34d73ed9af8d9915 100644 --- a/test/fuzztest/publishnotification_fuzzer/BUILD.gn +++ b/test/fuzztest/publishnotification_fuzzer/BUILD.gn @@ -23,7 +23,7 @@ ohos_fuzztest("PublishNotificationFuzzTest") { fuzz_config_file = "${component_path}/test/fuzztest/publishnotification_fuzzer" - include_dirs = [] + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] cflags = [ "-g", "-O0", @@ -32,7 +32,10 @@ ohos_fuzztest("PublishNotificationFuzzTest") { ] sources = [ "publishnotification_fuzzer.cpp" ] - deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] external_deps = [ "ability_base:want", diff --git a/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.cpp b/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.cpp index e77c99167020793edcbd74cdd007cb45a55ee853..cdcd93d233d273b6501031432c963f4692eb891b 100644 --- a/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.cpp +++ b/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.cpp @@ -23,20 +23,20 @@ namespace OHOS { constexpr uint8_t SLOT_TYPE_NUM = 5; constexpr uint8_t FLAG_STATUS = 3; } - bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { - std::string stringData = reinterpret_cast(data); + std::string stringData(data); Notification::NotificationRequest request; request.SetAlertOneTime(*data % ENABLE); - int32_t style = static_cast(U32_AT(data)); + int32_t style = static_cast(GetU32Data(data)); Notification::NotificationRequest::BadgeStyle badgeStyle = Notification::NotificationRequest::BadgeStyle(style); request.SetBadgeIconStyle(badgeStyle); request.SetBadgeNumber(style); request.SetClassification(stringData); - uint32_t color = U32_AT(data); + uint32_t color = GetU32Data(data); request.SetColor(color); request.SetColorEnabled(*data % ENABLE); @@ -83,6 +83,11 @@ namespace OHOS { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - OHOS::DoSomethingInterestingWithMyAPI(data, size); + char *ch = ParseData(data, size); + if (ch != nullptr && size >= GetU32Size()) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } return 0; } diff --git a/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.h b/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.h index cf6356f20f33aa7e4d950aa3049aeb2cc616c0f9..753bfc61ed90cd2ced3e13033f831e0ffe086ed8 100644 --- a/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.h +++ b/test/fuzztest/publishnotification_fuzzer/publishnotification_fuzzer.h @@ -16,14 +16,8 @@ #ifndef TEST_FUZZTEST_PUBLISHNOTIFICATION_FUZZER_PUBLISHNOTIFICATION_FUZZER_H #define TEST_FUZZTEST_PUBLISHNOTIFICATION_FUZZER_PUBLISHNOTIFICATION_FUZZER_H -#define FUZZ_PROJECT_NAME "publishnotification_fuzzer" - -#include +#include "fuzz_common_base.h" -uint32_t U32_AT(const uint8_t *ptr) -{ - // convert fuzz input data to an integer - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; -} +#define FUZZ_PROJECT_NAME "publishnotification_fuzzer" #endif // TEST_FUZZTEST_PUBLISHNOTIFICATION_FUZZER_PUBLISHNOTIFICATION_FUZZER_H diff --git a/test/fuzztest/removenotification_fuzzer/BUILD.gn b/test/fuzztest/removenotification_fuzzer/BUILD.gn index fcb4f7dff4954ea020f1f54d3f0e578890d0ea24..cc8a80556b95f25dcab74a1007b5ab890362afd9 100644 --- a/test/fuzztest/removenotification_fuzzer/BUILD.gn +++ b/test/fuzztest/removenotification_fuzzer/BUILD.gn @@ -22,7 +22,7 @@ ohos_fuzztest("RemoveNotificationFuzzTest") { module_out_path = module_output_path fuzz_config_file = "${component_path}/test/fuzztest/removenotification_fuzzer" - include_dirs = [] + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] cflags = [ "-g", "-O0", @@ -31,7 +31,10 @@ ohos_fuzztest("RemoveNotificationFuzzTest") { ] sources = [ "removenotification_fuzzer.cpp" ] - deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] external_deps = [ "ability_base:want", diff --git a/test/fuzztest/removenotification_fuzzer/removenotification_fuzzer.cpp b/test/fuzztest/removenotification_fuzzer/removenotification_fuzzer.cpp index e0ffe410bbe1fcbbaf80df4e3d106464c428be04..1fb67911af9e5a47753be00f9a6f4618926cf208 100644 --- a/test/fuzztest/removenotification_fuzzer/removenotification_fuzzer.cpp +++ b/test/fuzztest/removenotification_fuzzer/removenotification_fuzzer.cpp @@ -18,9 +18,9 @@ #include "notification_helper.h" namespace OHOS { - bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { - std::string key = reinterpret_cast(data); + std::string key(data); return Notification::NotificationHelper::RemoveNotification(key); } } @@ -29,7 +29,12 @@ namespace OHOS { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - OHOS::DoSomethingInterestingWithMyAPI(data, size); + char *ch = ParseData(data, size); + if (ch != nullptr) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } return 0; } diff --git a/test/fuzztest/removenotification_fuzzer/removenotification_fuzzer.h b/test/fuzztest/removenotification_fuzzer/removenotification_fuzzer.h index 9ce1fca6dfc578b9ce27ee7855f3b5253f19886c..6c24ded57017faafee1a29f7c7308a6082c5226c 100644 --- a/test/fuzztest/removenotification_fuzzer/removenotification_fuzzer.h +++ b/test/fuzztest/removenotification_fuzzer/removenotification_fuzzer.h @@ -16,6 +16,8 @@ #ifndef TEST_FUZZTEST_REMOVENOTIFICATION_FUZZER_REMOVENOTIFICATION_FUZZER_H #define TEST_FUZZTEST_REMOVENOTIFICATION_FUZZER_REMOVENOTIFICATION_FUZZER_H +#include "fuzz_common_base.h" + #define FUZZ_PROJECT_NAME "removenotification_fuzzer" #endif // TEST_FUZZTEST_REMOVENOTIFICATION_FUZZER_REMOVENOTIFICATION_FUZZER_H diff --git a/test/fuzztest/removenotificationslot_fuzzer/BUILD.gn b/test/fuzztest/removenotificationslot_fuzzer/BUILD.gn index ca8f1b94ea166015a2b18d5e28fce9c265676b14..1ad0cdedf4e56441964d575cffb611b4dee6ff4d 100644 --- a/test/fuzztest/removenotificationslot_fuzzer/BUILD.gn +++ b/test/fuzztest/removenotificationslot_fuzzer/BUILD.gn @@ -23,7 +23,7 @@ ohos_fuzztest("RemoveNotificationSlotFuzzTest") { fuzz_config_file = "${component_path}/test/fuzztest/removenotificationslot_fuzzer" - include_dirs = [] + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] cflags = [ "-g", "-O0", @@ -32,7 +32,10 @@ ohos_fuzztest("RemoveNotificationSlotFuzzTest") { ] sources = [ "removenotificationslot_fuzzer.cpp" ] - deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] external_deps = [ "ability_base:want", diff --git a/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.cpp b/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.cpp index e10f807c3df789f482f39b2635321edc4d85cccc..3eb7e4f027680184105474496c77cc4453b20e63 100644 --- a/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.cpp +++ b/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.cpp @@ -20,7 +20,7 @@ constexpr uint8_t SLOT_TYPE_NUM = 5; namespace OHOS { - bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { uint8_t type = *data % SLOT_TYPE_NUM; Notification::NotificationConstant::SlotType slotType = Notification::NotificationConstant::SlotType(type); @@ -32,6 +32,11 @@ namespace OHOS { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - OHOS::DoSomethingInterestingWithMyAPI(data, size); + char *ch = ParseData(data, size); + if (ch != nullptr) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } return 0; } diff --git a/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.h b/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.h index 4649800f301e175be29980251df58c164ede57c2..508f0612c003b079ee310fcb69f7fc894d55de70 100644 --- a/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.h +++ b/test/fuzztest/removenotificationslot_fuzzer/removenotificationslot_fuzzer.h @@ -16,6 +16,8 @@ #ifndef TEST_FUZZTEST_REMOVENOTIFICATIONSLOT_FUZZER_REMOVENOTIFICATIONSLOT_FUZZER_H #define TEST_FUZZTEST_REMOVENOTIFICATIONSLOT_FUZZER_REMOVENOTIFICATIONSLOT_FUZZER_H +#include "fuzz_common_base.h" + #define FUZZ_PROJECT_NAME "removenotificationslot_fuzzer" #endif // TEST_FUZZTEST_REMOVENOTIFICATIONSLOT_FUZZER_REMOVENOTIFICATIONSLOT_FUZZER_H diff --git a/test/fuzztest/setnotificationbadgenum_fuzzer/BUILD.gn b/test/fuzztest/setnotificationbadgenum_fuzzer/BUILD.gn index 7445550696fc9fe28fcba937aa2d48b02573c67e..49162f6eb3fdf3bebc0ed2b751efd06abb8ce143 100644 --- a/test/fuzztest/setnotificationbadgenum_fuzzer/BUILD.gn +++ b/test/fuzztest/setnotificationbadgenum_fuzzer/BUILD.gn @@ -23,7 +23,7 @@ ohos_fuzztest("SetNotificationBadgeNumFuzzTest") { fuzz_config_file = "${component_path}/test/fuzztest/setnotificationbadgenum_fuzzer" - include_dirs = [] + include_dirs = [ "${component_path}/test/fuzztest/fuzz_common_base" ] cflags = [ "-g", "-O0", @@ -32,12 +32,14 @@ ohos_fuzztest("SetNotificationBadgeNumFuzzTest") { ] sources = [ "setnotificationbadgenum_fuzzer.cpp" ] - deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + deps = [ + "${component_path}/test/fuzztest/fuzz_common_base:fuzz_common_base", + "${frameworks_module_ans_path}:ans_innerkits", + ] external_deps = [ "ability_base:want", "ability_base:zuri", - "c_utils:utils", "hiviewdfx_hilog_native:libhilog", "multimedia_image_standard:image_native", "relational_store:native_rdb", diff --git a/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp b/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp index b89068681e6e83ca3edccc0a63f93f1aee29477a..0c9400963c05fff63dabc50b90566ead5681e4fe 100644 --- a/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp +++ b/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp @@ -16,11 +16,10 @@ #include "setnotificationbadgenum_fuzzer.h" #include "notification_helper.h" - namespace OHOS { - bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { - return Notification::NotificationHelper::SetNotificationBadgeNum(U32_AT(data)) == ERR_OK; + return Notification::NotificationHelper::SetNotificationBadgeNum(GetU32Data(data)) == ERR_OK; } } @@ -28,6 +27,11 @@ namespace OHOS { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - OHOS::DoSomethingInterestingWithMyAPI(data, size); + char *ch = ParseData(data, size); + if (ch != nullptr && size >= GetU32Size()) { + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + } return 0; } diff --git a/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.h b/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.h index 1d2517607943541672dd1031a7a9d561146b0126..77dd37aa03c764cc1bc6d910136de525c4ebf90b 100644 --- a/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.h +++ b/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.h @@ -16,14 +16,8 @@ #ifndef TEST_FUZZTEST_SETNOTIFICATIONBADGENUM_FUZZER_SETNOTIFICATIONBADGENUM_FUZZER_H #define TEST_FUZZTEST_SETNOTIFICATIONBADGENUM_FUZZER_SETNOTIFICATIONBADGENUM_FUZZER_H -#include +#include "fuzz_common_base.h" #define FUZZ_PROJECT_NAME "setnotificationbadgenum_fuzzer" -uint32_t U32_AT(const uint8_t *ptr) -{ - // convert fuzz input data to an integer - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; -} - #endif // TEST_FUZZTEST_SETNOTIFICATIONBADGENUM_FUZZER_SETNOTIFICATIONBADGENUM_FUZZER_H \ No newline at end of file