From b2effe3776bcf4d8dbe04251f6070b554e224fbf Mon Sep 17 00:00:00 2001 From: xuzheheng Date: Thu, 27 Oct 2022 19:36:52 +0800 Subject: [PATCH] xuzheheng1@huawei.com Signed-off-by: xuzheheng Change-Id: I537974d96737fda368efbf9804c05ba235b93f3e --- test/fuzztest/BUILD.gn | 7 ++ .../abilityattachtimeout_fuzzer/BUILD.gn | 72 +++++++++++++ .../abilityattachtimeout_fuzzer.cpp | 100 +++++++++++++++++ .../abilityattachtimeout_fuzzer.h | 21 ++++ .../abilityattachtimeout_fuzzer/corpus/init | 16 +++ .../abilityattachtimeout_fuzzer/project.xml | 25 +++++ .../clearupapplicationdata_fuzzer.cpp | 11 ++ .../fuzztest/getconfiguration_fuzzer/BUILD.gn | 72 +++++++++++++ .../getconfiguration_fuzzer/corpus/init | 16 +++ .../getconfiguration_fuzzer.cpp | 87 +++++++++++++++ .../getconfiguration_fuzzer.h | 21 ++++ .../getconfiguration_fuzzer/project.xml | 25 +++++ .../BUILD.gn | 70 ++++++++++++ .../corpus/init | 16 +++ .../getprocessrunninginfosbyuserid_fuzzer.cpp | 84 +++++++++++++++ .../getprocessrunninginfosbyuserid_fuzzer.h | 21 ++++ .../project.xml | 25 +++++ .../BUILD.gn | 71 ++++++++++++ .../corpus/init | 16 +++ .../getrunningprocessinfobytoken_fuzzer.cpp | 101 ++++++++++++++++++ .../getrunningprocessinfobytoken_fuzzer.h | 21 ++++ .../project.xml | 25 +++++ .../killapplicationself_fuzzer/BUILD.gn | 71 ++++++++++++ .../killapplicationself_fuzzer/corpus/init | 16 +++ .../killapplicationself_fuzzer.cpp | 84 +++++++++++++++ .../killapplicationself_fuzzer.h | 21 ++++ .../killapplicationself_fuzzer/project.xml | 25 +++++ .../notifymemorylevel_fuzzer/BUILD.gn | 71 ++++++++++++ .../notifymemorylevel_fuzzer/corpus/init | 16 +++ .../notifymemorylevel_fuzzer.cpp | 83 ++++++++++++++ .../notifymemorylevel_fuzzer.h | 21 ++++ .../notifymemorylevel_fuzzer/project.xml | 25 +++++ .../fuzztest/prepareterminate_fuzzer/BUILD.gn | 72 +++++++++++++ .../prepareterminate_fuzzer/corpus/init | 16 +++ .../prepareterminate_fuzzer.cpp | 100 +++++++++++++++++ .../prepareterminate_fuzzer.h | 21 ++++ .../prepareterminate_fuzzer/project.xml | 25 +++++ 37 files changed, 1590 insertions(+) create mode 100755 test/fuzztest/abilityattachtimeout_fuzzer/BUILD.gn create mode 100755 test/fuzztest/abilityattachtimeout_fuzzer/abilityattachtimeout_fuzzer.cpp create mode 100755 test/fuzztest/abilityattachtimeout_fuzzer/abilityattachtimeout_fuzzer.h create mode 100755 test/fuzztest/abilityattachtimeout_fuzzer/corpus/init create mode 100755 test/fuzztest/abilityattachtimeout_fuzzer/project.xml create mode 100755 test/fuzztest/getconfiguration_fuzzer/BUILD.gn create mode 100755 test/fuzztest/getconfiguration_fuzzer/corpus/init create mode 100755 test/fuzztest/getconfiguration_fuzzer/getconfiguration_fuzzer.cpp create mode 100755 test/fuzztest/getconfiguration_fuzzer/getconfiguration_fuzzer.h create mode 100755 test/fuzztest/getconfiguration_fuzzer/project.xml create mode 100755 test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/BUILD.gn create mode 100755 test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/corpus/init create mode 100755 test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/getprocessrunninginfosbyuserid_fuzzer.cpp create mode 100755 test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/getprocessrunninginfosbyuserid_fuzzer.h create mode 100755 test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/project.xml create mode 100755 test/fuzztest/getrunningprocessinfobytoken_fuzzer/BUILD.gn create mode 100755 test/fuzztest/getrunningprocessinfobytoken_fuzzer/corpus/init create mode 100755 test/fuzztest/getrunningprocessinfobytoken_fuzzer/getrunningprocessinfobytoken_fuzzer.cpp create mode 100755 test/fuzztest/getrunningprocessinfobytoken_fuzzer/getrunningprocessinfobytoken_fuzzer.h create mode 100755 test/fuzztest/getrunningprocessinfobytoken_fuzzer/project.xml create mode 100755 test/fuzztest/killapplicationself_fuzzer/BUILD.gn create mode 100755 test/fuzztest/killapplicationself_fuzzer/corpus/init create mode 100755 test/fuzztest/killapplicationself_fuzzer/killapplicationself_fuzzer.cpp create mode 100755 test/fuzztest/killapplicationself_fuzzer/killapplicationself_fuzzer.h create mode 100755 test/fuzztest/killapplicationself_fuzzer/project.xml create mode 100755 test/fuzztest/notifymemorylevel_fuzzer/BUILD.gn create mode 100755 test/fuzztest/notifymemorylevel_fuzzer/corpus/init create mode 100755 test/fuzztest/notifymemorylevel_fuzzer/notifymemorylevel_fuzzer.cpp create mode 100755 test/fuzztest/notifymemorylevel_fuzzer/notifymemorylevel_fuzzer.h create mode 100755 test/fuzztest/notifymemorylevel_fuzzer/project.xml create mode 100755 test/fuzztest/prepareterminate_fuzzer/BUILD.gn create mode 100755 test/fuzztest/prepareterminate_fuzzer/corpus/init create mode 100755 test/fuzztest/prepareterminate_fuzzer/prepareterminate_fuzzer.cpp create mode 100755 test/fuzztest/prepareterminate_fuzzer/prepareterminate_fuzzer.h create mode 100755 test/fuzztest/prepareterminate_fuzzer/project.xml diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index a42ecc643c..95f25b679c 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -17,6 +17,7 @@ group("fuzztest") { testonly = true deps = [ + "abilityattachtimeout_fuzzer:fuzztest", "abilityconnectionstub_fuzzer:fuzztest", "abilitymanager_fuzzer:fuzztest", "abilityschedulerstub_fuzzer:fuzztest", @@ -45,15 +46,19 @@ group("fuzztest") { "forcetimeoutfortest_fuzzer:fuzztest", "getabilityrunninginfos_fuzzer:fuzztest", "getappmemorysize_fuzzer:fuzztest", + "getconfiguration_fuzzer:fuzztest", "getextensionrunninginfos_fuzzer:fuzztest", "getmissionidbytoken_fuzzer:fuzztest", "getmissioninfos_fuzzer:fuzztest", "getmissionsnapshot_fuzzer:fuzztest", "getprocessrunninginfos_fuzzer:fuzztest", + "getprocessrunninginfosbyuserid_fuzzer:fuzztest", + "getrunningprocessinfobytoken_fuzzer:fuzztest", "gettopability_fuzzer:fuzztest", "getwantsender_fuzzer:fuzztest", "isramconstraineddevice_fuzzer:fuzztest", "killapplication_fuzzer:fuzztest", + "killapplicationself_fuzzer:fuzztest", "killprocessbyabilitytoken_fuzzer:fuzztest", "killprocessesbybundlename_fuzzer:fuzztest", "killprocessesbyuserid_fuzzer:fuzztest", @@ -65,6 +70,8 @@ group("fuzztest") { "movemissiontofront_fuzzer:fuzztest", "notifycompletecontinuation_fuzzer:fuzztest", "notifycontinuationresult_fuzzer:fuzztest", + "notifymemorylevel_fuzzer:fuzztest", + "prepareterminate_fuzzer:fuzztest", "registerabilitylifecyclecallback_fuzzer:fuzztest", "registerapplicationstateobserver_fuzzer:fuzztest", "registerenvironmentcallback_fuzzer:fuzztest", diff --git a/test/fuzztest/abilityattachtimeout_fuzzer/BUILD.gn b/test/fuzztest/abilityattachtimeout_fuzzer/BUILD.gn new file mode 100755 index 0000000000..d3f9e105a0 --- /dev/null +++ b/test/fuzztest/abilityattachtimeout_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") +module_output_path = "ability_runtime/app_manager" + +##############################fuzztest########################################## +ohos_fuzztest("AbilityAttachTimeOutFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = + "${ability_runtime_test_path}/fuzztest/abilityattachtimeout_fuzzer" + include_dirs = [ "${ability_runtime_innerkits_path}/ability_manager/include" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "abilityattachtimeout_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_services_path}/abilitymgr:abilityms", + ] + + external_deps = [ + "ability_base:configuration", + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (ability_runtime_graphics) { + external_deps += [ "input:libmmi-client" ] + } +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":AbilityAttachTimeOutFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/abilityattachtimeout_fuzzer/abilityattachtimeout_fuzzer.cpp b/test/fuzztest/abilityattachtimeout_fuzzer/abilityattachtimeout_fuzzer.cpp new file mode 100755 index 0000000000..826b50bcec --- /dev/null +++ b/test/fuzztest/abilityattachtimeout_fuzzer/abilityattachtimeout_fuzzer.cpp @@ -0,0 +1,100 @@ +/* + * 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 "abilityattachtimeout_fuzzer.h" + +#include +#include + +#include "ability_record.h" +#include "app_mgr_client.h" +#include "configuration.h" +#include "parcel.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; + +namespace OHOS { + constexpr size_t FOO_MAX_LEN = 1024; + + sptr GetFuzzAbilityToken() + { + sptr token = nullptr; + AbilityRequest abilityRequest; + abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; + abilityRequest.abilityInfo.name = "MainAbility"; + abilityRequest.abilityInfo.type = AbilityType::DATA; + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + if (abilityRecord) { + token = abilityRecord->GetToken(); + } + return token; + } + + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + AppMgrClient* appMgrClient = new AppMgrClient(); + if (!appMgrClient) { + return false; + } + + sptr token = GetFuzzAbilityToken(); + if (!token) { + std::cout << "Get ability token failed." << std::endl; + return false; + } + + appMgrClient->AbilityAttachTimeOut(token); + + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + std::cout << "invalid data" << std::endl; + return 0; + } + + /* Validate the length of size */ + if (size == 0 || size > OHOS::FOO_MAX_LEN) { + std::cout << "invalid size" << std::endl; + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + std::cout << "malloc failed." << std::endl; + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + std::cout << "copy failed." << std::endl; + free(ch); + ch = nullptr; + return 0; + } + + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} + diff --git a/test/fuzztest/abilityattachtimeout_fuzzer/abilityattachtimeout_fuzzer.h b/test/fuzztest/abilityattachtimeout_fuzzer/abilityattachtimeout_fuzzer.h new file mode 100755 index 0000000000..b5e10d78e6 --- /dev/null +++ b/test/fuzztest/abilityattachtimeout_fuzzer/abilityattachtimeout_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FUZZTEST_OHOS_ABILITY_RUNTIME_ABILITYATTACHTIMEOUT_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_ABILITYATTACHTIMEOUT_FUZZER_H + +#define FUZZ_PROJECT_NAME "abilityattachtimeout_fuzzer" + +#endif // FUZZTEST_OHOS_ABILITY_RUNTIME_ABILITYATTACHTIMEOUT_FUZZER_H diff --git a/test/fuzztest/abilityattachtimeout_fuzzer/corpus/init b/test/fuzztest/abilityattachtimeout_fuzzer/corpus/init new file mode 100755 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/abilityattachtimeout_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/abilityattachtimeout_fuzzer/project.xml b/test/fuzztest/abilityattachtimeout_fuzzer/project.xml new file mode 100755 index 0000000000..6e8ad2cfde --- /dev/null +++ b/test/fuzztest/abilityattachtimeout_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/clearupapplicationdata_fuzzer/clearupapplicationdata_fuzzer.cpp b/test/fuzztest/clearupapplicationdata_fuzzer/clearupapplicationdata_fuzzer.cpp index 968fdca35e..8a30cb0ca3 100644 --- a/test/fuzztest/clearupapplicationdata_fuzzer/clearupapplicationdata_fuzzer.cpp +++ b/test/fuzztest/clearupapplicationdata_fuzzer/clearupapplicationdata_fuzzer.cpp @@ -19,6 +19,7 @@ #include #include "ability_manager_client.h" +#include "app_mgr_client.h" #include "securec.h" using namespace OHOS::AAFwk; @@ -38,6 +39,16 @@ bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) return false; } + // fuzz for AppMgrClient + auto appMgrClient = new AppMgrClient(); + if (!appMgrClient) { + return false; + } + + if (appMgrClient->ClearUpApplicationData(bundleName) != 0) { + return false; + } + return true; } } diff --git a/test/fuzztest/getconfiguration_fuzzer/BUILD.gn b/test/fuzztest/getconfiguration_fuzzer/BUILD.gn new file mode 100755 index 0000000000..ad9036d75a --- /dev/null +++ b/test/fuzztest/getconfiguration_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") +module_output_path = "ability_runtime/app_manager" + +##############################fuzztest########################################## +ohos_fuzztest("GetConfigurationFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = + "${ability_runtime_test_path}/fuzztest/getconfiguration_fuzzer" + include_dirs = [ "${ability_runtime_innerkits_path}/ability_manager/include" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "getconfiguration_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_services_path}/abilitymgr:abilityms", + ] + + external_deps = [ + "ability_base:configuration", + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (ability_runtime_graphics) { + external_deps += [ "input:libmmi-client" ] + } +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":GetConfigurationFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/getconfiguration_fuzzer/corpus/init b/test/fuzztest/getconfiguration_fuzzer/corpus/init new file mode 100755 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/getconfiguration_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/getconfiguration_fuzzer/getconfiguration_fuzzer.cpp b/test/fuzztest/getconfiguration_fuzzer/getconfiguration_fuzzer.cpp new file mode 100755 index 0000000000..04f25f9b6d --- /dev/null +++ b/test/fuzztest/getconfiguration_fuzzer/getconfiguration_fuzzer.cpp @@ -0,0 +1,87 @@ +/* + * 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 "getconfiguration_fuzzer.h" + +#include +#include + +#include "ability_record.h" +#include "app_mgr_client.h" +#include "configuration.h" +#include "parcel.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; + +namespace OHOS { + constexpr size_t FOO_MAX_LEN = 1024; + + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + AppMgrClient* appMgrClient = new AppMgrClient(); + if (!appMgrClient) { + return false; + } + + Configuration config; + if (!config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, reinterpret_cast(data))) { + return false; + } + + if (appMgrClient->GetConfiguration(config) != 0) { + return false; + } + + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + std::cout << "invalid data" << std::endl; + return 0; + } + + /* Validate the length of size */ + if (size == 0 || size > OHOS::FOO_MAX_LEN) { + std::cout << "invalid size" << std::endl; + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + std::cout << "malloc failed." << std::endl; + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + std::cout << "copy failed." << std::endl; + free(ch); + ch = nullptr; + return 0; + } + + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} + diff --git a/test/fuzztest/getconfiguration_fuzzer/getconfiguration_fuzzer.h b/test/fuzztest/getconfiguration_fuzzer/getconfiguration_fuzzer.h new file mode 100755 index 0000000000..86037b5b33 --- /dev/null +++ b/test/fuzztest/getconfiguration_fuzzer/getconfiguration_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FUZZTEST_OHOS_ABILITY_RUNTIME_GETCONFIGURATION_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_GETCONFIGURATION_FUZZER_H + +#define FUZZ_PROJECT_NAME "getconfiguration_fuzzer" + +#endif // FUZZTEST_OHOS_ABILITY_RUNTIME_GETCONFIGURATION_FUZZER_H diff --git a/test/fuzztest/getconfiguration_fuzzer/project.xml b/test/fuzztest/getconfiguration_fuzzer/project.xml new file mode 100755 index 0000000000..6e8ad2cfde --- /dev/null +++ b/test/fuzztest/getconfiguration_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/BUILD.gn b/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/BUILD.gn new file mode 100755 index 0000000000..9a8e01b4a1 --- /dev/null +++ b/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/BUILD.gn @@ -0,0 +1,70 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") +module_output_path = "ability_runtime/app_manager" + +##############################fuzztest########################################## +ohos_fuzztest("GetProcessRunningInfosByUserIdFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "${ability_runtime_test_path}/fuzztest/getprocessrunninginfosbyuserid_fuzzer" + include_dirs = [ "${ability_runtime_innerkits_path}/ability_manager/include" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "getprocessrunninginfosbyuserid_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_services_path}/abilitymgr:abilityms", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (ability_runtime_graphics) { + external_deps += [ "input:libmmi-client" ] + } +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":GetProcessRunningInfosByUserIdFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/corpus/init b/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/corpus/init new file mode 100755 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/getprocessrunninginfosbyuserid_fuzzer.cpp b/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/getprocessrunninginfosbyuserid_fuzzer.cpp new file mode 100755 index 0000000000..aab59f2b87 --- /dev/null +++ b/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/getprocessrunninginfosbyuserid_fuzzer.cpp @@ -0,0 +1,84 @@ +/* + * 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 "getprocessrunninginfosbyuserid_fuzzer.h" + +#include +#include + +#include "ability_record.h" +#include "app_mgr_client.h" +#include "parcel.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; + +namespace OHOS { + constexpr size_t FOO_MAX_LEN = 1024; + + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + AppMgrClient* appMgrClient = new AppMgrClient(); + if (!appMgrClient) { + return false; + } + + std::vector infos; + int32_t userId = 100; + + if (appMgrClient->GetProcessRunningInfosByUserId(infos, userId) != 0) { + return false; + } + + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + std::cout << "invalid data" << std::endl; + return 0; + } + + /* Validate the length of size */ + if (size == 0 || size > OHOS::FOO_MAX_LEN) { + std::cout << "invalid size" << std::endl; + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + std::cout << "malloc failed." << std::endl; + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + std::cout << "copy failed." << std::endl; + free(ch); + ch = nullptr; + return 0; + } + + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} + diff --git a/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/getprocessrunninginfosbyuserid_fuzzer.h b/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/getprocessrunninginfosbyuserid_fuzzer.h new file mode 100755 index 0000000000..5f4d89e558 --- /dev/null +++ b/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/getprocessrunninginfosbyuserid_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FUZZTEST_OHOS_ABILITY_RUNTIME_GETPROCESSRUNNINGINFOSBYUSERID_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_GETPROCESSRUNNINGINFOSBYUSERID_FUZZER_H + +#define FUZZ_PROJECT_NAME "getprocessrunninginfosbyuserid_fuzzer" + +#endif // FUZZTEST_OHOS_ABILITY_RUNTIME_GETPROCESSRUNNINGINFOSBYUSERID_FUZZER_H diff --git a/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/project.xml b/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/project.xml new file mode 100755 index 0000000000..6e8ad2cfde --- /dev/null +++ b/test/fuzztest/getprocessrunninginfosbyuserid_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/getrunningprocessinfobytoken_fuzzer/BUILD.gn b/test/fuzztest/getrunningprocessinfobytoken_fuzzer/BUILD.gn new file mode 100755 index 0000000000..1fe00d2eb1 --- /dev/null +++ b/test/fuzztest/getrunningprocessinfobytoken_fuzzer/BUILD.gn @@ -0,0 +1,71 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") +module_output_path = "ability_runtime/app_manager" + +##############################fuzztest########################################## +ohos_fuzztest("GetRunningProcessInfoByTokenFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "${ability_runtime_test_path}/fuzztest/getrunningprocessinfobytoken_fuzzer" + include_dirs = [ "${ability_runtime_innerkits_path}/ability_manager/include" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "getrunningprocessinfobytoken_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_services_path}/abilitymgr:abilityms", + ] + + external_deps = [ + "ability_base:configuration", + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (ability_runtime_graphics) { + external_deps += [ "input:libmmi-client" ] + } +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":GetRunningProcessInfoByTokenFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/getrunningprocessinfobytoken_fuzzer/corpus/init b/test/fuzztest/getrunningprocessinfobytoken_fuzzer/corpus/init new file mode 100755 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/getrunningprocessinfobytoken_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/getrunningprocessinfobytoken_fuzzer/getrunningprocessinfobytoken_fuzzer.cpp b/test/fuzztest/getrunningprocessinfobytoken_fuzzer/getrunningprocessinfobytoken_fuzzer.cpp new file mode 100755 index 0000000000..e49e5124b0 --- /dev/null +++ b/test/fuzztest/getrunningprocessinfobytoken_fuzzer/getrunningprocessinfobytoken_fuzzer.cpp @@ -0,0 +1,101 @@ +/* + * 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 "getrunningprocessinfobytoken_fuzzer.h" + +#include +#include + +#include "ability_record.h" +#include "app_mgr_client.h" +#include "configuration.h" +#include "parcel.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; + +namespace OHOS { + constexpr size_t FOO_MAX_LEN = 1024; + + sptr GetFuzzAbilityToken() + { + sptr token = nullptr; + AbilityRequest abilityRequest; + abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; + abilityRequest.abilityInfo.name = "MainAbility"; + abilityRequest.abilityInfo.type = AbilityType::DATA; + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + if (abilityRecord) { + token = abilityRecord->GetToken(); + } + return token; + } + + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + AppMgrClient* appMgrClient = new AppMgrClient(); + if (!appMgrClient) { + return false; + } + + sptr token = GetFuzzAbilityToken(); + if (!token) { + std::cout << "Get ability token failed." << std::endl; + return false; + } + RunningProcessInfo info; + + appMgrClient->GetRunningProcessInfoByToken(token, info); + + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + std::cout << "invalid data" << std::endl; + return 0; + } + + /* Validate the length of size */ + if (size == 0 || size > OHOS::FOO_MAX_LEN) { + std::cout << "invalid size" << std::endl; + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + std::cout << "malloc failed." << std::endl; + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + std::cout << "copy failed." << std::endl; + free(ch); + ch = nullptr; + return 0; + } + + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} + diff --git a/test/fuzztest/getrunningprocessinfobytoken_fuzzer/getrunningprocessinfobytoken_fuzzer.h b/test/fuzztest/getrunningprocessinfobytoken_fuzzer/getrunningprocessinfobytoken_fuzzer.h new file mode 100755 index 0000000000..623ce3d000 --- /dev/null +++ b/test/fuzztest/getrunningprocessinfobytoken_fuzzer/getrunningprocessinfobytoken_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FUZZTEST_OHOS_ABILITY_RUNTIME_GETRUNNINGPROCESSINFOBYTOKEN_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_GETRUNNINGPROCESSINFOBYTOKEN_FUZZER_H + +#define FUZZ_PROJECT_NAME "getrunningprocessinfobytoken_fuzzer" + +#endif // FUZZTEST_OHOS_ABILITY_RUNTIME_GETRUNNINGPROCESSINFOBYTOKEN_FUZZER_H diff --git a/test/fuzztest/getrunningprocessinfobytoken_fuzzer/project.xml b/test/fuzztest/getrunningprocessinfobytoken_fuzzer/project.xml new file mode 100755 index 0000000000..6e8ad2cfde --- /dev/null +++ b/test/fuzztest/getrunningprocessinfobytoken_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/killapplicationself_fuzzer/BUILD.gn b/test/fuzztest/killapplicationself_fuzzer/BUILD.gn new file mode 100755 index 0000000000..8ecf4453b8 --- /dev/null +++ b/test/fuzztest/killapplicationself_fuzzer/BUILD.gn @@ -0,0 +1,71 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") +module_output_path = "ability_runtime/app_manager" + +##############################fuzztest########################################## +ohos_fuzztest("KillApplicationSelfFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = + "${ability_runtime_test_path}/fuzztest/killapplicationself_fuzzer" + include_dirs = [ "${ability_runtime_innerkits_path}/ability_manager/include" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "killapplicationself_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_services_path}/abilitymgr:abilityms", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (ability_runtime_graphics) { + external_deps += [ "input:libmmi-client" ] + } +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":KillApplicationSelfFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/killapplicationself_fuzzer/corpus/init b/test/fuzztest/killapplicationself_fuzzer/corpus/init new file mode 100755 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/killapplicationself_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/killapplicationself_fuzzer/killapplicationself_fuzzer.cpp b/test/fuzztest/killapplicationself_fuzzer/killapplicationself_fuzzer.cpp new file mode 100755 index 0000000000..50163ea479 --- /dev/null +++ b/test/fuzztest/killapplicationself_fuzzer/killapplicationself_fuzzer.cpp @@ -0,0 +1,84 @@ +/* + * 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 "killapplicationself_fuzzer.h" + +#include +#include + +#include "ability_record.h" +#include "app_mgr_client.h" +#include "parcel.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; + +namespace OHOS { + constexpr size_t FOO_MAX_LEN = 1024; + + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + AppMgrClient* appMgrClient = new AppMgrClient(); + if (!appMgrClient) { + return false; + } + + if (appMgrClient->KillApplicationSelf() != AppMgrResultCode::RESULT_OK) { + return false; + } + + delete appMgrClient; + appMgrClient = nullptr; + + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + std::cout << "invalid data" << std::endl; + return 0; + } + + /* Validate the length of size */ + if (size == 0 || size > OHOS::FOO_MAX_LEN) { + std::cout << "invalid size" << std::endl; + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + std::cout << "malloc failed." << std::endl; + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + std::cout << "copy failed." << std::endl; + free(ch); + ch = nullptr; + return 0; + } + + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} + diff --git a/test/fuzztest/killapplicationself_fuzzer/killapplicationself_fuzzer.h b/test/fuzztest/killapplicationself_fuzzer/killapplicationself_fuzzer.h new file mode 100755 index 0000000000..4c430d0158 --- /dev/null +++ b/test/fuzztest/killapplicationself_fuzzer/killapplicationself_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FUZZTEST_OHOS_ABILITY_RUNTIME_KILLAPPLICATIONSELF_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_KILLAPPLICATIONSELF_FUZZER_H + +#define FUZZ_PROJECT_NAME "killapplicationself_fuzzer" + +#endif // FUZZTEST_OHOS_ABILITY_RUNTIME_KILLAPPLICATIONSELF_FUZZER_H diff --git a/test/fuzztest/killapplicationself_fuzzer/project.xml b/test/fuzztest/killapplicationself_fuzzer/project.xml new file mode 100755 index 0000000000..6e8ad2cfde --- /dev/null +++ b/test/fuzztest/killapplicationself_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/notifymemorylevel_fuzzer/BUILD.gn b/test/fuzztest/notifymemorylevel_fuzzer/BUILD.gn new file mode 100755 index 0000000000..ad0445b5cd --- /dev/null +++ b/test/fuzztest/notifymemorylevel_fuzzer/BUILD.gn @@ -0,0 +1,71 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") +module_output_path = "ability_runtime/app_manager" + +##############################fuzztest########################################## +ohos_fuzztest("NotifyMemoryLevelFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = + "${ability_runtime_test_path}/fuzztest/notifymemorylevel_fuzzer" + include_dirs = [ "${ability_runtime_innerkits_path}/ability_manager/include" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "notifymemorylevel_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_services_path}/abilitymgr:abilityms", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (ability_runtime_graphics) { + external_deps += [ "input:libmmi-client" ] + } +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":NotifyMemoryLevelFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/notifymemorylevel_fuzzer/corpus/init b/test/fuzztest/notifymemorylevel_fuzzer/corpus/init new file mode 100755 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/notifymemorylevel_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/notifymemorylevel_fuzzer/notifymemorylevel_fuzzer.cpp b/test/fuzztest/notifymemorylevel_fuzzer/notifymemorylevel_fuzzer.cpp new file mode 100755 index 0000000000..f724ad17c1 --- /dev/null +++ b/test/fuzztest/notifymemorylevel_fuzzer/notifymemorylevel_fuzzer.cpp @@ -0,0 +1,83 @@ +/* + * 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 "notifymemorylevel_fuzzer.h" + +#include +#include + +#include "ability_record.h" +#include "app_mgr_client.h" +#include "parcel.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; + +namespace OHOS { + constexpr size_t FOO_MAX_LEN = 1024; + + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + AppMgrClient* appMgrClient = new AppMgrClient(); + if (!appMgrClient) { + return false; + } + + MemoryLevel level = MEMORY_LEVEL_MODERATE; + + if (appMgrClient->NotifyMemoryLevel(level) != 0) { + return false; + } + + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + std::cout << "invalid data" << std::endl; + return 0; + } + + /* Validate the length of size */ + if (size == 0 || size > OHOS::FOO_MAX_LEN) { + std::cout << "invalid size" << std::endl; + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + std::cout << "malloc failed." << std::endl; + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + std::cout << "copy failed." << std::endl; + free(ch); + ch = nullptr; + return 0; + } + + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} + diff --git a/test/fuzztest/notifymemorylevel_fuzzer/notifymemorylevel_fuzzer.h b/test/fuzztest/notifymemorylevel_fuzzer/notifymemorylevel_fuzzer.h new file mode 100755 index 0000000000..f4432f776b --- /dev/null +++ b/test/fuzztest/notifymemorylevel_fuzzer/notifymemorylevel_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FUZZTEST_OHOS_ABILITY_RUNTIME_NOTIFYMEMORYLEVEL_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_NOTIFYMEMORYLEVEL_FUZZER_H + +#define FUZZ_PROJECT_NAME "notifymemorylevel_fuzzer" + +#endif // FUZZTEST_OHOS_ABILITY_RUNTIME_NOTIFYMEMORYLEVEL_FUZZER_H diff --git a/test/fuzztest/notifymemorylevel_fuzzer/project.xml b/test/fuzztest/notifymemorylevel_fuzzer/project.xml new file mode 100755 index 0000000000..6e8ad2cfde --- /dev/null +++ b/test/fuzztest/notifymemorylevel_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/prepareterminate_fuzzer/BUILD.gn b/test/fuzztest/prepareterminate_fuzzer/BUILD.gn new file mode 100755 index 0000000000..7683197af9 --- /dev/null +++ b/test/fuzztest/prepareterminate_fuzzer/BUILD.gn @@ -0,0 +1,72 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") +module_output_path = "ability_runtime/app_manager" + +##############################fuzztest########################################## +ohos_fuzztest("PrepareTerminateFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = + "${ability_runtime_test_path}/fuzztest/prepareterminate_fuzzer" + include_dirs = [ "${ability_runtime_innerkits_path}/ability_manager/include" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "prepareterminate_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_services_path}/abilitymgr:abilityms", + ] + + external_deps = [ + "ability_base:configuration", + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (ability_runtime_graphics) { + external_deps += [ "input:libmmi-client" ] + } +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":PrepareTerminateFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/prepareterminate_fuzzer/corpus/init b/test/fuzztest/prepareterminate_fuzzer/corpus/init new file mode 100755 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/prepareterminate_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/prepareterminate_fuzzer/prepareterminate_fuzzer.cpp b/test/fuzztest/prepareterminate_fuzzer/prepareterminate_fuzzer.cpp new file mode 100755 index 0000000000..e396dcbb33 --- /dev/null +++ b/test/fuzztest/prepareterminate_fuzzer/prepareterminate_fuzzer.cpp @@ -0,0 +1,100 @@ +/* + * 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 "prepareterminate_fuzzer.h" + +#include +#include + +#include "ability_record.h" +#include "app_mgr_client.h" +#include "configuration.h" +#include "parcel.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; + +namespace OHOS { + constexpr size_t FOO_MAX_LEN = 1024; + + sptr GetFuzzAbilityToken() + { + sptr token = nullptr; + AbilityRequest abilityRequest; + abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; + abilityRequest.abilityInfo.name = "MainAbility"; + abilityRequest.abilityInfo.type = AbilityType::DATA; + std::shared_ptr abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); + if (abilityRecord) { + token = abilityRecord->GetToken(); + } + return token; + } + + bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) + { + AppMgrClient* appMgrClient = new AppMgrClient(); + if (!appMgrClient) { + return false; + } + + sptr token = GetFuzzAbilityToken(); + if (!token) { + std::cout << "Get ability token failed." << std::endl; + return false; + } + + appMgrClient->PrepareTerminate(token); + + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + std::cout << "invalid data" << std::endl; + return 0; + } + + /* Validate the length of size */ + if (size == 0 || size > OHOS::FOO_MAX_LEN) { + std::cout << "invalid size" << std::endl; + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + std::cout << "malloc failed." << std::endl; + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + std::cout << "copy failed." << std::endl; + free(ch); + ch = nullptr; + return 0; + } + + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} + diff --git a/test/fuzztest/prepareterminate_fuzzer/prepareterminate_fuzzer.h b/test/fuzztest/prepareterminate_fuzzer/prepareterminate_fuzzer.h new file mode 100755 index 0000000000..ba54abc52e --- /dev/null +++ b/test/fuzztest/prepareterminate_fuzzer/prepareterminate_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FUZZTEST_OHOS_ABILITY_RUNTIME_PREPARETERMINATE_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_PREPARETERMINATE_FUZZER_H + +#define FUZZ_PROJECT_NAME "prepareterminate_fuzzer" + +#endif // FUZZTEST_OHOS_ABILITY_RUNTIME_PREPARETERMINATE_FUZZER_H diff --git a/test/fuzztest/prepareterminate_fuzzer/project.xml b/test/fuzztest/prepareterminate_fuzzer/project.xml new file mode 100755 index 0000000000..6e8ad2cfde --- /dev/null +++ b/test/fuzztest/prepareterminate_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +