diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index e045587c8f..90bbf528af 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -32,6 +32,9 @@ group("fuzztest") { "getwantsender_fuzzer:fuzztest", "killprocesswithaccount_fuzzer:fuzztest", "missionlistenerstub_fuzzer:fuzztest", + "registerabilitylifecyclecallback_fuzzer:fuzztest", + "registerapplicationstateobserver_fuzzer:fuzztest", + "registerenvironmentcallback_fuzzer:fuzztest", "remotemissionlistenerstub_fuzzer:fuzztest", "setmissionicon_fuzzer:fuzztest", "setmissionlabel_fuzzer:fuzztest", @@ -42,6 +45,9 @@ group("fuzztest") { "stopserviceability_fuzzer:fuzztest", "stopserviceextensionability_fuzzer:fuzztest", "terminateability_fuzzer:fuzztest", + "unregisterabilitylifecyclecallback_fuzzer:fuzztest", + "unregisterapplicationstateobserver_fuzzer:fuzztest", + "unregisterenvironmentcallback_fuzzer:fuzztest", "updateconfiguration_fuzzer:fuzztest", "uripermissionmanager_fuzzer:fuzztest", "wantagenthelperstring_fuzzer:fuzztest", diff --git a/test/fuzztest/connectability_fuzzer/BUILD.gn b/test/fuzztest/connectability_fuzzer/BUILD.gn index 6080d8b67e..29a428162f 100755 --- a/test/fuzztest/connectability_fuzzer/BUILD.gn +++ b/test/fuzztest/connectability_fuzzer/BUILD.gn @@ -16,7 +16,7 @@ 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/abilitymgr" +module_output_path = "ability_runtime/ability_context_native" ##############################fuzztest########################################## ohos_fuzztest("ConnectAbilityFuzzTest") { diff --git a/test/fuzztest/disconnectability_fuzzer/BUILD.gn b/test/fuzztest/disconnectability_fuzzer/BUILD.gn index cc1bf1d98c..d6d85e14a2 100755 --- a/test/fuzztest/disconnectability_fuzzer/BUILD.gn +++ b/test/fuzztest/disconnectability_fuzzer/BUILD.gn @@ -16,7 +16,7 @@ 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/abilitymgr" +module_output_path = "ability_runtime/ability_context_native" ##############################fuzztest########################################## ohos_fuzztest("DisconnectAbilityFuzzTest") { diff --git a/test/fuzztest/killprocesswithaccount_fuzzer/BUILD.gn b/test/fuzztest/killprocesswithaccount_fuzzer/BUILD.gn index f1ccd4d06f..9d7244d393 100755 --- a/test/fuzztest/killprocesswithaccount_fuzzer/BUILD.gn +++ b/test/fuzztest/killprocesswithaccount_fuzzer/BUILD.gn @@ -16,7 +16,7 @@ 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/abilitymgr" +module_output_path = "ability_runtime/libappms" ##############################fuzztest########################################## ohos_fuzztest("KillProcessWithAccountFuzzTest") { diff --git a/test/fuzztest/registerabilitylifecyclecallback_fuzzer/BUILD.gn b/test/fuzztest/registerabilitylifecyclecallback_fuzzer/BUILD.gn new file mode 100644 index 0000000000..17c3556020 --- /dev/null +++ b/test/fuzztest/registerabilitylifecyclecallback_fuzzer/BUILD.gn @@ -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. + +#####################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_context" + +##############################fuzztest########################################## +ohos_fuzztest("RegisterAbilityLifecycleCallbackFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "${ability_runtime_test_path}/fuzztest/registerabilitylifecyclecallback_fuzzer" + + include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "registerabilitylifecyclecallback_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ "${ability_runtime_native_path}/appkit:app_context" ] + + external_deps = [ + "ability_base:want", + "c_utils:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":RegisterAbilityLifecycleCallbackFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/registerabilitylifecyclecallback_fuzzer/corpus/init b/test/fuzztest/registerabilitylifecyclecallback_fuzzer/corpus/init new file mode 100644 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/registerabilitylifecyclecallback_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/registerabilitylifecyclecallback_fuzzer/project.xml b/test/fuzztest/registerabilitylifecyclecallback_fuzzer/project.xml new file mode 100644 index 0000000000..85e7ef2c1c --- /dev/null +++ b/test/fuzztest/registerabilitylifecyclecallback_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/registerabilitylifecyclecallback_fuzzer/registerabilitylifecyclecallback_fuzzer.cpp b/test/fuzztest/registerabilitylifecyclecallback_fuzzer/registerabilitylifecyclecallback_fuzzer.cpp new file mode 100644 index 0000000000..af8bd717d5 --- /dev/null +++ b/test/fuzztest/registerabilitylifecyclecallback_fuzzer/registerabilitylifecyclecallback_fuzzer.cpp @@ -0,0 +1,96 @@ +/* + * 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 "registerabilitylifecyclecallback_fuzzer.h" + +#include +#include + +#include "ability_lifecycle_callback.h" +#include "application_context.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; +using namespace OHOS::AbilityRuntime; + +namespace OHOS { +constexpr size_t FOO_MAX_LEN = 1024; +class AbilityLifecycleCallbackFuzz : public AbilityLifecycleCallback { +public: + explicit AbilityLifecycleCallbackFuzz() {}; + virtual ~AbilityLifecycleCallbackFuzz() {}; + void OnAbilityCreate(const std::shared_ptr &ability) override {}; + void OnWindowStageCreate(const std::shared_ptr &ability, + const std::shared_ptr &windowStage) override {}; + void OnWindowStageDestroy(const std::shared_ptr &ability, + const std::shared_ptr &windowStage) override {}; + void OnWindowStageActive(const std::shared_ptr &ability, + const std::shared_ptr &windowStage) override {}; + void OnWindowStageInactive(const std::shared_ptr &ability, + const std::shared_ptr &windowStage) override {}; + void OnAbilityDestroy(const std::shared_ptr &ability) override {}; + void OnAbilityForeground(const std::shared_ptr &ability) override {}; + void OnAbilityBackground(const std::shared_ptr &ability) override {}; + void OnAbilityContinue(const std::shared_ptr &ability) override {}; +}; +bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) +{ + ApplicationContext* context = new ApplicationContext(); + if (!context) { + return false; + } + + std::shared_ptr callback; + context->RegisterAbilityLifecycleCallback(callback); + + 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/registerabilitylifecyclecallback_fuzzer/registerabilitylifecyclecallback_fuzzer.h b/test/fuzztest/registerabilitylifecyclecallback_fuzzer/registerabilitylifecyclecallback_fuzzer.h new file mode 100644 index 0000000000..79fc20d1d9 --- /dev/null +++ b/test/fuzztest/registerabilitylifecyclecallback_fuzzer/registerabilitylifecyclecallback_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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_REGISTERABILITYLIFECYCLECALLBACK_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_REGISTERABILITYLIFECYCLECALLBACK_FUZZER_H + +#define FUZZ_PROJECT_NAME "registerabilitylifecyclecallback_fuzzer" + +#endif diff --git a/test/fuzztest/registerapplicationstateobserver_fuzzer/BUILD.gn b/test/fuzztest/registerapplicationstateobserver_fuzzer/BUILD.gn new file mode 100644 index 0000000000..6b7f9fa0c4 --- /dev/null +++ b/test/fuzztest/registerapplicationstateobserver_fuzzer/BUILD.gn @@ -0,0 +1,75 @@ +# 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/libappms" + +##############################fuzztest########################################## +ohos_fuzztest("RegisterApplicationStateObserverFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "${ability_runtime_test_path}/fuzztest/registerapplicationstateobserver_fuzzer" + include_dirs = [ "${ability_runtime_innerkits_path}/ability_manager/include" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "registerapplicationstateobserver_fuzzer.cpp" ] + + configs = [ "${ability_runtime_services_path}/appmgr:appmgr_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/uri_permission:uri_permission_mgr", + "${ability_runtime_services_path}/appmgr:libappms", + ] + + external_deps = [ + "ability_base:base", + "ability_runtime:ability_deps_wrapper", + "ability_runtime:app_manager", + "appspawn:appspawn_socket_client", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "eventhandler:libeventhandler", + "init:libbeget_proxy", + "init:libbegetutil", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (ability_runtime_graphics) { + deps += [] + external_deps += [ + "i18n:intl_util", + "window_manager:libwm", + ] + } +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":RegisterApplicationStateObserverFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/registerapplicationstateobserver_fuzzer/corpus/init b/test/fuzztest/registerapplicationstateobserver_fuzzer/corpus/init new file mode 100644 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/registerapplicationstateobserver_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/registerapplicationstateobserver_fuzzer/project.xml b/test/fuzztest/registerapplicationstateobserver_fuzzer/project.xml new file mode 100644 index 0000000000..6e8ad2cfde --- /dev/null +++ b/test/fuzztest/registerapplicationstateobserver_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/registerapplicationstateobserver_fuzzer/registerapplicationstateobserver_fuzzer.cpp b/test/fuzztest/registerapplicationstateobserver_fuzzer/registerapplicationstateobserver_fuzzer.cpp new file mode 100644 index 0000000000..230df2a83a --- /dev/null +++ b/test/fuzztest/registerapplicationstateobserver_fuzzer/registerapplicationstateobserver_fuzzer.cpp @@ -0,0 +1,86 @@ +/* + * 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 "registerapplicationstateobserver_fuzzer.h" + +#include +#include + +#define private public +#include "app_mgr_service.h" +#undef private +#include "iapplication_state_observer.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) +{ + std::shared_ptr appMgrService = std::make_shared(); + if (!appMgrService) { + return false; + } + + sptr observer; + std::vector bundleNameList; + std::string bundleName(data); + bundleNameList.emplace_back(bundleName); + + if (appMgrService->RegisterApplicationStateObserver(observer, bundleNameList) != 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/registerapplicationstateobserver_fuzzer/registerapplicationstateobserver_fuzzer.h b/test/fuzztest/registerapplicationstateobserver_fuzzer/registerapplicationstateobserver_fuzzer.h new file mode 100644 index 0000000000..1ad3158ce1 --- /dev/null +++ b/test/fuzztest/registerapplicationstateobserver_fuzzer/registerapplicationstateobserver_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_REGISTERAPPLICATIONSTATEOBSERVER_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_REGISTERAPPLICATIONSTATEOBSERVER_FUZZER_H + +#define FUZZ_PROJECT_NAME "registerapplicationstateobserver_fuzzer" + +#endif diff --git a/test/fuzztest/registerenvironmentcallback_fuzzer/BUILD.gn b/test/fuzztest/registerenvironmentcallback_fuzzer/BUILD.gn new file mode 100644 index 0000000000..71a14e0cdd --- /dev/null +++ b/test/fuzztest/registerenvironmentcallback_fuzzer/BUILD.gn @@ -0,0 +1,58 @@ +# 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_context" + +##############################fuzztest########################################## +ohos_fuzztest("RegisterEnvironmentCallbackFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = + "${ability_runtime_test_path}/fuzztest/registerenvironmentcallback_fuzzer" + + include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "registerenvironmentcallback_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ "${ability_runtime_native_path}/appkit:app_context" ] + + external_deps = [ + "ability_base:want", + "c_utils:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":RegisterEnvironmentCallbackFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/registerenvironmentcallback_fuzzer/corpus/init b/test/fuzztest/registerenvironmentcallback_fuzzer/corpus/init new file mode 100644 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/registerenvironmentcallback_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/registerenvironmentcallback_fuzzer/project.xml b/test/fuzztest/registerenvironmentcallback_fuzzer/project.xml new file mode 100644 index 0000000000..85e7ef2c1c --- /dev/null +++ b/test/fuzztest/registerenvironmentcallback_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/registerenvironmentcallback_fuzzer/registerenvironmentcallback_fuzzer.cpp b/test/fuzztest/registerenvironmentcallback_fuzzer/registerenvironmentcallback_fuzzer.cpp new file mode 100644 index 0000000000..5feadc0256 --- /dev/null +++ b/test/fuzztest/registerenvironmentcallback_fuzzer/registerenvironmentcallback_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 "registerenvironmentcallback_fuzzer.h" + +#include +#include + +#include "environment_callback.h" +#include "application_context.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; +using namespace OHOS::AbilityRuntime; + +namespace OHOS { +constexpr size_t FOO_MAX_LEN = 1024; +class EnvironmentCallbackFuzz : public EnvironmentCallback { +public: + explicit EnvironmentCallbackFuzz() {}; + void OnConfigurationUpdated(const AppExecFwk::Configuration &config) override {}; + void OnMemoryLevel(const int level) override {}; +}; +bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) +{ + ApplicationContext* context = new ApplicationContext(); + if (!context) { + return false; + } + + std::shared_ptr callback; + context->RegisterEnvironmentCallback(callback); + + 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/registerenvironmentcallback_fuzzer/registerenvironmentcallback_fuzzer.h b/test/fuzztest/registerenvironmentcallback_fuzzer/registerenvironmentcallback_fuzzer.h new file mode 100644 index 0000000000..affd990aaf --- /dev/null +++ b/test/fuzztest/registerenvironmentcallback_fuzzer/registerenvironmentcallback_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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_REGISTERENVIRONMENTCALLBACK_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_REGISTERENVIRONMENTCALLBACK_FUZZER_H + +#define FUZZ_PROJECT_NAME "registerenvironmentcallback_fuzzer" + +#endif diff --git a/test/fuzztest/startserviceextensionability_fuzzer/BUILD.gn b/test/fuzztest/startserviceextensionability_fuzzer/BUILD.gn index 26a62f100d..5d3f57f41a 100755 --- a/test/fuzztest/startserviceextensionability_fuzzer/BUILD.gn +++ b/test/fuzztest/startserviceextensionability_fuzzer/BUILD.gn @@ -16,7 +16,7 @@ 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/abilitymgr" +module_output_path = "ability_runtime/ability_context_native" ##############################fuzztest########################################## ohos_fuzztest("StartServiceExtensionAbilityFuzzTest") { diff --git a/test/fuzztest/stopserviceextensionability_fuzzer/BUILD.gn b/test/fuzztest/stopserviceextensionability_fuzzer/BUILD.gn index 036c589155..60d21d7d95 100755 --- a/test/fuzztest/stopserviceextensionability_fuzzer/BUILD.gn +++ b/test/fuzztest/stopserviceextensionability_fuzzer/BUILD.gn @@ -16,7 +16,7 @@ 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/abilitymgr" +module_output_path = "ability_runtime/ability_context_native" ##############################fuzztest########################################## ohos_fuzztest("StopServiceExtensionAbilityFuzzTest") { diff --git a/test/fuzztest/terminateability_fuzzer/BUILD.gn b/test/fuzztest/terminateability_fuzzer/BUILD.gn index d078af14a2..c25d456062 100755 --- a/test/fuzztest/terminateability_fuzzer/BUILD.gn +++ b/test/fuzztest/terminateability_fuzzer/BUILD.gn @@ -16,7 +16,7 @@ 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/abilitymgr" +module_output_path = "ability_runtime/ability_context_native" ##############################fuzztest########################################## ohos_fuzztest("TerminateAbilityFuzzTest") { diff --git a/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/BUILD.gn b/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/BUILD.gn new file mode 100644 index 0000000000..3b224853ae --- /dev/null +++ b/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/BUILD.gn @@ -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. + +#####################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_context" + +##############################fuzztest########################################## +ohos_fuzztest("UnregisterAbilityLifecycleCallbackFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "${ability_runtime_test_path}/fuzztest/unregisterabilitylifecyclecallback_fuzzer" + + include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "unregisterabilitylifecyclecallback_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ "${ability_runtime_native_path}/appkit:app_context" ] + + external_deps = [ + "ability_base:want", + "c_utils:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":UnregisterAbilityLifecycleCallbackFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/corpus/init b/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/corpus/init new file mode 100644 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/unregisterabilitylifecyclecallback_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/unregisterabilitylifecyclecallback_fuzzer/project.xml b/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/project.xml new file mode 100644 index 0000000000..85e7ef2c1c --- /dev/null +++ b/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/unregisterabilitylifecyclecallback_fuzzer.cpp b/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/unregisterabilitylifecyclecallback_fuzzer.cpp new file mode 100644 index 0000000000..5e60942e50 --- /dev/null +++ b/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/unregisterabilitylifecyclecallback_fuzzer.cpp @@ -0,0 +1,96 @@ +/* + * 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 "unregisterabilitylifecyclecallback_fuzzer.h" + +#include +#include + +#include "ability_lifecycle_callback.h" +#include "application_context.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; +using namespace OHOS::AbilityRuntime; + +namespace OHOS { +constexpr size_t FOO_MAX_LEN = 1024; +class AbilityLifecycleCallbackFuzz : public AbilityLifecycleCallback { +public: + explicit AbilityLifecycleCallbackFuzz() {}; + virtual ~AbilityLifecycleCallbackFuzz() {}; + void OnAbilityCreate(const std::shared_ptr &ability) override {}; + void OnWindowStageCreate(const std::shared_ptr &ability, + const std::shared_ptr &windowStage) override {}; + void OnWindowStageDestroy(const std::shared_ptr &ability, + const std::shared_ptr &windowStage) override {}; + void OnWindowStageActive(const std::shared_ptr &ability, + const std::shared_ptr &windowStage) override {}; + void OnWindowStageInactive(const std::shared_ptr &ability, + const std::shared_ptr &windowStage) override {}; + void OnAbilityDestroy(const std::shared_ptr &ability) override {}; + void OnAbilityForeground(const std::shared_ptr &ability) override {}; + void OnAbilityBackground(const std::shared_ptr &ability) override {}; + void OnAbilityContinue(const std::shared_ptr &ability) override {}; +}; +bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) +{ + ApplicationContext* context = new ApplicationContext(); + if (!context) { + return false; + } + + std::shared_ptr callback; + context->UnregisterAbilityLifecycleCallback(callback); + + 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/unregisterabilitylifecyclecallback_fuzzer/unregisterabilitylifecyclecallback_fuzzer.h b/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/unregisterabilitylifecyclecallback_fuzzer.h new file mode 100644 index 0000000000..96af2d8320 --- /dev/null +++ b/test/fuzztest/unregisterabilitylifecyclecallback_fuzzer/unregisterabilitylifecyclecallback_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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_UNREGISTERABILITYLIFECYCLECALLBACK_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_UNREGISTERABILITYLIFECYCLECALLBACK_FUZZER_H + +#define FUZZ_PROJECT_NAME "unregisterabilitylifecyclecallback_fuzzer" + +#endif diff --git a/test/fuzztest/unregisterapplicationstateobserver_fuzzer/BUILD.gn b/test/fuzztest/unregisterapplicationstateobserver_fuzzer/BUILD.gn new file mode 100644 index 0000000000..b126b307ce --- /dev/null +++ b/test/fuzztest/unregisterapplicationstateobserver_fuzzer/BUILD.gn @@ -0,0 +1,75 @@ +# 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/libappms" + +##############################fuzztest########################################## +ohos_fuzztest("UnregisterApplicationStateObserverFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "${ability_runtime_test_path}/fuzztest/unregisterapplicationstateobserver_fuzzer" + include_dirs = [ "${ability_runtime_innerkits_path}/ability_manager/include" ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "unregisterapplicationstateobserver_fuzzer.cpp" ] + + configs = [ "${ability_runtime_services_path}/appmgr:appmgr_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/uri_permission:uri_permission_mgr", + "${ability_runtime_services_path}/appmgr:libappms", + ] + + external_deps = [ + "ability_base:base", + "ability_runtime:ability_deps_wrapper", + "ability_runtime:app_manager", + "appspawn:appspawn_socket_client", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "eventhandler:libeventhandler", + "init:libbeget_proxy", + "init:libbegetutil", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + if (ability_runtime_graphics) { + deps += [] + external_deps += [ + "i18n:intl_util", + "window_manager:libwm", + ] + } +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":UnregisterApplicationStateObserverFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/unregisterapplicationstateobserver_fuzzer/corpus/init b/test/fuzztest/unregisterapplicationstateobserver_fuzzer/corpus/init new file mode 100644 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/unregisterapplicationstateobserver_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/unregisterapplicationstateobserver_fuzzer/project.xml b/test/fuzztest/unregisterapplicationstateobserver_fuzzer/project.xml new file mode 100644 index 0000000000..6e8ad2cfde --- /dev/null +++ b/test/fuzztest/unregisterapplicationstateobserver_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/unregisterapplicationstateobserver_fuzzer/unregisterapplicationstateobserver_fuzzer.cpp b/test/fuzztest/unregisterapplicationstateobserver_fuzzer/unregisterapplicationstateobserver_fuzzer.cpp new file mode 100644 index 0000000000..e864b98cf3 --- /dev/null +++ b/test/fuzztest/unregisterapplicationstateobserver_fuzzer/unregisterapplicationstateobserver_fuzzer.cpp @@ -0,0 +1,82 @@ +/* + * 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 "unregisterapplicationstateobserver_fuzzer.h" + +#include +#include + +#define private public +#include "app_mgr_service.h" +#undef private +#include "iapplication_state_observer.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) +{ + std::shared_ptr appMgrService = std::make_shared(); + sptr observer; + if (!appMgrService) { + return false; + } + + if (appMgrService->UnregisterApplicationStateObserver(observer) != 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/unregisterapplicationstateobserver_fuzzer/unregisterapplicationstateobserver_fuzzer.h b/test/fuzztest/unregisterapplicationstateobserver_fuzzer/unregisterapplicationstateobserver_fuzzer.h new file mode 100644 index 0000000000..ac07ee5701 --- /dev/null +++ b/test/fuzztest/unregisterapplicationstateobserver_fuzzer/unregisterapplicationstateobserver_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_UNREGISTERAPPLICATIONSTATEOBSERVER_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_UNREGISTERAPPLICATIONSTATEOBSERVER_FUZZER_H + +#define FUZZ_PROJECT_NAME "unregisterapplicationstateobserver_fuzzer" + +#endif diff --git a/test/fuzztest/unregisterenvironmentcallback_fuzzer/BUILD.gn b/test/fuzztest/unregisterenvironmentcallback_fuzzer/BUILD.gn new file mode 100644 index 0000000000..c9384737fc --- /dev/null +++ b/test/fuzztest/unregisterenvironmentcallback_fuzzer/BUILD.gn @@ -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. + +#####################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_context" + +##############################fuzztest########################################## +ohos_fuzztest("UnregisterEnvironmentCallbackFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "${ability_runtime_test_path}/fuzztest/unregisterenvironmentcallback_fuzzer" + + include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context" ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ "unregisterenvironmentcallback_fuzzer.cpp" ] + + configs = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager_public_config" ] + + deps = [ "${ability_runtime_native_path}/appkit:app_context" ] + + external_deps = [ + "ability_base:want", + "c_utils:utils", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":UnregisterEnvironmentCallbackFuzzTest", + ] +} +############################################################################### diff --git a/test/fuzztest/unregisterenvironmentcallback_fuzzer/corpus/init b/test/fuzztest/unregisterenvironmentcallback_fuzzer/corpus/init new file mode 100644 index 0000000000..8eb5a7d6eb --- /dev/null +++ b/test/fuzztest/unregisterenvironmentcallback_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/unregisterenvironmentcallback_fuzzer/project.xml b/test/fuzztest/unregisterenvironmentcallback_fuzzer/project.xml new file mode 100644 index 0000000000..85e7ef2c1c --- /dev/null +++ b/test/fuzztest/unregisterenvironmentcallback_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/unregisterenvironmentcallback_fuzzer/unregisterenvironmentcallback_fuzzer.cpp b/test/fuzztest/unregisterenvironmentcallback_fuzzer/unregisterenvironmentcallback_fuzzer.cpp new file mode 100644 index 0000000000..e1604a2a63 --- /dev/null +++ b/test/fuzztest/unregisterenvironmentcallback_fuzzer/unregisterenvironmentcallback_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 "unregisterenvironmentcallback_fuzzer.h" + +#include +#include + +#include "environment_callback.h" +#include "application_context.h" +#include "securec.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; +using namespace OHOS::AbilityRuntime; + +namespace OHOS { +constexpr size_t FOO_MAX_LEN = 1024; +class EnvironmentCallbackFuzz : public EnvironmentCallback { +public: + explicit EnvironmentCallbackFuzz() {}; + void OnConfigurationUpdated(const AppExecFwk::Configuration &config) override {}; + void OnMemoryLevel(const int level) override {}; +}; +bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) +{ + ApplicationContext* context = new ApplicationContext(); + if (!context) { + return false; + } + + std::shared_ptr callback; + context->UnregisterEnvironmentCallback(callback); + + 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/unregisterenvironmentcallback_fuzzer/unregisterenvironmentcallback_fuzzer.h b/test/fuzztest/unregisterenvironmentcallback_fuzzer/unregisterenvironmentcallback_fuzzer.h new file mode 100644 index 0000000000..b7494067e1 --- /dev/null +++ b/test/fuzztest/unregisterenvironmentcallback_fuzzer/unregisterenvironmentcallback_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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_UNREGISTERENVIRONMENTCALLBACK_FUZZER_H +#define FUZZTEST_OHOS_ABILITY_RUNTIME_UNREGISTERENVIRONMENTCALLBACK_FUZZER_H + +#define FUZZ_PROJECT_NAME "unregisterenvironmentcallback_fuzzer" + +#endif