diff --git a/test/fuzztest/display_fuzzer/BUILD.gn b/test/fuzztest/display_fuzzer/BUILD.gn index f2497e58c29d11733ddccfe8dd52b69c4c7eb154..797fa50777d242f8b53f5009375e70f27cc15351 100644 --- a/test/fuzztest/display_fuzzer/BUILD.gn +++ b/test/fuzztest/display_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-2023 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 @@ -23,10 +23,15 @@ ohos_fuzztest("DisplayFuzzTest") { include_dirs = [ "./", - "${displaymgr_utils_path}:utils_config", + "${displaymgr_root_path}/service/native/include", + "${displaymgr_root_path}/service/zidl/include", + "${displaymgr_inner_api}/native/include", ] - configs = [ "${displaymgr_utils_path}:coverage_flags" ] + configs = [ + "${displaymgr_utils_path}:utils_config", + "${displaymgr_utils_path}:coverage_flags", + ] cflags = [ "-g", @@ -34,13 +39,38 @@ ohos_fuzztest("DisplayFuzzTest") { "-Wno-unused-variable", "-fno-omit-frame-pointer", ] - sources = [ "./display_fuzzer.cpp" ] - deps = [ "${displaymgr_inner_api}:displaymgr" ] + sources = [ + "${displaymgr_root_path}/service/native/src/display_auto_brightness.cpp", + "${displaymgr_root_path}/service/native/src/display_event_handler.cpp", + "${displaymgr_root_path}/service/native/src/display_param_helper.cpp", + "${displaymgr_root_path}/service/native/src/display_power_mgr_service.cpp", + "${displaymgr_root_path}/service/native/src/display_system_ability.cpp", + "${displaymgr_root_path}/service/native/src/gradual_animator.cpp", + "${displaymgr_root_path}/service/native/src/screen_action.cpp", + "${displaymgr_root_path}/service/native/src/screen_controller.cpp", + "${displaymgr_root_path}/service/zidl/src/display_power_callback_proxy.cpp", + "${displaymgr_root_path}/service/zidl/src/display_power_mgr_stub.cpp", + "./display_fuzzer.cpp", + ] - external_deps = [ + external_deps = [ "power_manager:power_permission" ] + external_deps += [ + "ability_runtime:ability_manager", "c_utils:utils", + "data_share:datashare_consumer", + "display_manager:displaymgr", + "eventhandler:libeventhandler", + "hicollie_native:libhicollie", + "hisysevent_native:libhisysevent", + "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "multimedia_image_framework:image_native", + "power_manager:power_setting", + "power_manager:power_sysparam", "power_manager:powermgr_client", + "safwk:system_ability_fwk", + "sensor:sensor_interface_native", + "window_manager:libdm", ] } diff --git a/test/fuzztest/display_fuzzer/display_fuzzer.cpp b/test/fuzztest/display_fuzzer/display_fuzzer.cpp index 31ddc9c7e11041ef235769c85d9102709f025605..b622c2448129f02e2e031f42cb850fb445a461d9 100644 --- a/test/fuzztest/display_fuzzer/display_fuzzer.cpp +++ b/test/fuzztest/display_fuzzer/display_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -15,267 +15,192 @@ #include "display_fuzzer.h" -#include #include #include #include -#include #include -#include "securec.h" +#include +#include + #include "display_power_mgr_client.h" +#define private public +#define protected public +#include "display_power_mgr_service.h" +#undef private +#undef protected +#include "message_parcel.h" +#include "new" +#include "securec.h" +using namespace OHOS::PowerMgr; using namespace OHOS::DisplayPowerMgr; using namespace std; +using namespace OHOS; namespace { auto& g_displayMgrClient = OHOS::DisplayPowerMgr::DisplayPowerMgrClient::GetInstance(); -constexpr size_t DATANUM = 4; -constexpr int32_t SIZE = 1; -constexpr int32_t INDEX_0 = 0; -} +const int32_t REWIND_READ_DATA = 0; +shared_ptr g_fuzzService = nullptr; +} // namespace -static void SetDisplayState(const uint8_t* data) +static uint32_t GetUint32(const uint8_t* data, size_t size) { - int32_t type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; + uint32_t value = 0; + if (size < sizeof(value)) { + return value; } - g_displayMgrClient.SetDisplayState(static_cast(type[0])); + if (memcpy_s(&value, sizeof(value), data, sizeof(value)) != EOK) { + return value; + } + return value; } -static void GetDisplayState(const uint8_t* data) +static void SetDisplayState(const uint8_t* data, size_t size) { - int32_t type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } - - g_displayMgrClient.GetDisplayState(); + uint32_t type = GetUint32(data, size); + g_displayMgrClient.SetDisplayState(static_cast(type), StateChangeReason(type)); } -static void GetDisplayIds(const uint8_t* data) +static void GetDisplayState(const uint8_t* data, size_t size) { - int32_t type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } + uint32_t value = GetUint32(data, size); + g_displayMgrClient.GetDisplayState(value); +} +static void GetDisplayIds([[maybe_unused]] const uint8_t* data, [[maybe_unused]] size_t size) +{ g_displayMgrClient.GetDisplayIds(); } -static void GetMainDisplayId(const uint8_t* data) +static void GetMainDisplayId([[maybe_unused]] const uint8_t* data, [[maybe_unused]] size_t size) { - int32_t type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } - g_displayMgrClient.GetMainDisplayId(); } -static void SetBrightness(const uint8_t* data) +static void SetBrightness(const uint8_t* data, size_t size) { - int32_t type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } - - g_displayMgrClient.SetBrightness(type[0]); + uint32_t value = GetUint32(data, size); + g_displayMgrClient.SetBrightness(value, value); } static void AdjustBrightness(const uint8_t* data, size_t size) { - int32_t type[1]; - int32_t duration[1]; - size_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } - if (size <= (idSize + DATANUM) || (memcpy_s(duration, sizeof(duration), (data + DATANUM), idSize) != EOK)) { - duration[INDEX_0] = type[INDEX_0]; - } - - g_displayMgrClient.AdjustBrightness(type[0], duration[0]); + uint32_t value = GetUint32(data, size); + g_displayMgrClient.AdjustBrightness(value, value, value); } -static void AutoAdjustBrightness(const uint8_t* data) +static void AutoAdjustBrightness([[maybe_unused]] const uint8_t* data, [[maybe_unused]] size_t size) { - int32_t type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } - - g_displayMgrClient.AutoAdjustBrightness(type[0]); + g_displayMgrClient.AutoAdjustBrightness(true); + g_displayMgrClient.AutoAdjustBrightness(false); } -static void RegisterCallback(const uint8_t* data) +static void RegisterCallback(const uint8_t* data, size_t size) { - static OHOS::sptr callback; - int32_t idSize = 4; - if ((memcpy_s(callback, sizeof(callback), data, idSize)) != EOK) { + OHOS::sptr callback; + if (size < sizeof(callback)) { + g_displayMgrClient.RegisterCallback(nullptr); + return; + } + if ((memcpy_s(callback, sizeof(callback), data, sizeof(callback))) != EOK) { + g_displayMgrClient.RegisterCallback(nullptr); return; } - g_displayMgrClient.RegisterCallback(callback); } -static void OverrideBrightness(const uint8_t* data) +static void OverrideBrightness(const uint8_t* data, size_t size) { - uint32_t type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } - g_displayMgrClient.OverrideBrightness(type[0]); + uint32_t value = GetUint32(data, size); + g_displayMgrClient.OverrideBrightness(value, value); } -static void RestoreBrightness(const uint8_t* data) +static void RestoreBrightness(const uint8_t* data, size_t size) { - uint32_t type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } - g_displayMgrClient.RestoreBrightness(type[0]); + uint32_t value = GetUint32(data, size); + g_displayMgrClient.RestoreBrightness(value); } -static void BoostBrightness(const uint8_t* data) +static void BoostBrightness(const uint8_t* data, size_t size) { - uint32_t type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } - g_displayMgrClient.BoostBrightness(type[0]); + uint32_t value = GetUint32(data, size); + g_displayMgrClient.BoostBrightness(value, value); } -static void CancelBoostBrightness(const uint8_t* data) +static void CancelBoostBrightness(const uint8_t* data, size_t size) { - uint32_t type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } - g_displayMgrClient.CancelBoostBrightness(type[0]); + uint32_t value = GetUint32(data, size); + g_displayMgrClient.CancelBoostBrightness(value); } -static void DiscountBrightness(const uint8_t* data) +static void DiscountBrightness(const uint8_t* data, size_t size) { - double type[1]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } + uint32_t value = GetUint32(data, size); + g_displayMgrClient.DiscountBrightness(value, value); +} - g_displayMgrClient.DiscountBrightness(type[0]); +static void GetBrightness(const uint8_t* data, size_t size) +{ + uint32_t value = GetUint32(data, size); + g_displayMgrClient.GetBrightness(value); } -static void GetBrightness(const uint8_t* data) +static void GetDeviceBrightness(const uint8_t* data, size_t size) { - uint32_t type[SIZE]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { - return; - } - g_displayMgrClient.GetBrightness(type[INDEX_0]); + uint32_t value = GetUint32(data, size); + g_displayMgrClient.GetDeviceBrightness(value); } -static void GetDeviceBrightness(const uint8_t* data) +static void DisplayServiceStub(const uint8_t* data, size_t size) { - uint32_t type[SIZE]; - int32_t idSize = 4; - if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) { + uint32_t code; + if (size < sizeof(code)) { + return; + } + if (memcpy_s(&code, sizeof(code), data, sizeof(code)) != EOK) { return; } - g_displayMgrClient.GetDeviceBrightness(type[INDEX_0]); -} + + MessageParcel datas; + datas.WriteInterfaceToken(DisplayPowerMgrService::GetDescriptor()); + datas.WriteBuffer(data, size); + datas.RewindRead(REWIND_READ_DATA); + MessageParcel reply; + MessageOption option; + if (g_fuzzService == nullptr) { + g_fuzzService = make_shared(); + g_fuzzService->Init(); + } + g_fuzzService->OnRemoteRequest(code, datas, reply, option); +} + +static std::vector> fuzzFunc = { + &SetDisplayState, + &GetDisplayState, + &GetDisplayIds, + &GetMainDisplayId, + &SetBrightness, + &AdjustBrightness, + &AutoAdjustBrightness, + &RegisterCallback, + &OverrideBrightness, + &RestoreBrightness, + &BoostBrightness, + &CancelBoostBrightness, + &DiscountBrightness, + &GetBrightness, + &GetDeviceBrightness +}; namespace OHOS { bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) { - int32_t idSize = 4; - int32_t cond[1]; - if (static_cast(size) > idSize) { - if ((memcpy_s(cond, sizeof(cond), data, idSize)) != EOK) { - return false; - } - std::random_device rd; - std::default_random_engine engine(rd()); - std::uniform_int_distribution randomNum(static_cast(ApiNumber::NUM_ZERO), - static_cast(ApiNumber::NUM_END) - 1); - int32_t number = randomNum(engine); - - switch (static_cast(number)) { - case ApiNumber::NUM_ZERO: - SetDisplayState(data); - break; - case ApiNumber::NUM_ONE: - GetDisplayState(data); - break; - case ApiNumber::NUM_TWO: - GetDisplayIds(data); - break; - case ApiNumber::NUM_THREE: - GetMainDisplayId(data); - break; - case ApiNumber::NUM_FOUR: - SetBrightness(data); - break; - case ApiNumber::NUM_FIVE: - AdjustBrightness(data, size); - break; - case ApiNumber::NUM_SIX: - AutoAdjustBrightness(data); - break; - case ApiNumber::NUM_SEVEN: - RegisterCallback(data); - break; - case ApiNumber::NUM_EIGHT: - OverrideBrightness(data); - break; - case ApiNumber::NUM_NINE: - RestoreBrightness(data); - break; - case ApiNumber::NUM_TEN: - BoostBrightness(data); - break; - case ApiNumber::NUM_ELEVEN: - CancelBoostBrightness(data); - break; - case ApiNumber::NUM_TWELVE: - g_displayMgrClient.GetDefaultBrightness(); - break; - case ApiNumber::NUM_THIRTEEN: - g_displayMgrClient.GetMaxBrightness(); - break; - case ApiNumber::NUM_FOURTEEN: - g_displayMgrClient.GetMinBrightness(); - break; - case ApiNumber::NUM_FIFTEEN: - DiscountBrightness(data); - break; - case ApiNumber::NUM_SIXTEEN: - GetBrightness(data); - break; - case ApiNumber::NUM_SEVENTEEN: - GetDeviceBrightness(data); - break; - case ApiNumber::NUM_EIGHTEEN: - g_displayMgrClient.IsAutoAdjustBrightness(); - break; - case ApiNumber::NUM_NINETEEN: - g_displayMgrClient.GetError(); - break; - default: - break; - } - } + std::random_device rd; + std::default_random_engine engine(rd()); + std::uniform_int_distribution randomNum(0, fuzzFunc.size() - 1); + int32_t number = randomNum(engine); + fuzzFunc[number](data, size); + DisplayServiceStub(data, size); return true; } } diff --git a/test/fuzztest/display_fuzzer/display_fuzzer.h b/test/fuzztest/display_fuzzer/display_fuzzer.h index 409d0ab89cb3d269ab25f71dc5eeb7f042cb25bc..df70d59518f1c462977d77fca2937e023f1abf6b 100644 --- a/test/fuzztest/display_fuzzer/display_fuzzer.h +++ b/test/fuzztest/display_fuzzer/display_fuzzer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -18,29 +18,4 @@ #define FUZZ_PROJECT_NAME "display_fuzzer" -enum class ApiNumber { - NUM_ZERO = 0, - NUM_ONE, - NUM_TWO, - NUM_THREE, - NUM_FOUR, - NUM_FIVE, - NUM_SIX, - NUM_SEVEN, - NUM_EIGHT, - NUM_NINE, - NUM_TEN, - NUM_ELEVEN, - NUM_TWELVE, - NUM_THIRTEEN, - NUM_FOURTEEN, - NUM_FIFTEEN, - NUM_SIXTEEN, - NUM_SEVENTEEN, - NUM_EIGHTEEN, - NUM_NINETEEN, - NUM_END -}; - #endif -