diff --git a/BUILD.gn b/BUILD.gn index d9ee11f5e6e57589f91dc77829ed93c4e81025e4..8eb3fe2a4f848fb9be3f655b1ade2db3f3ff4e26 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -33,5 +33,8 @@ group("dbms_target") { group("test_target") { testonly = true - deps = [ "services/dbms/test:unittest" ] + deps = [ + "services/dbms/test:unittest", + "test/fuzztest:fuzztest", + ] } diff --git a/interfaces/inner_api/include/distributed_bms_interface.h b/interfaces/inner_api/include/distributed_bms_interface.h index 0b477cd6b2743db34dbcc6fba7411de68b127d31..b93397fc9e6985ebd1f4e15116c571b470ece28d 100644 --- a/interfaces/inner_api/include/distributed_bms_interface.h +++ b/interfaces/inner_api/include/distributed_bms_interface.h @@ -19,6 +19,7 @@ #include #include +#include "appexecfwk_errors.h" #include "distributed_bundle_info.h" #include "element_name.h" #include "iremote_broker.h" @@ -37,7 +38,10 @@ public: * @return Returns result code when get remote ability info. */ virtual int32_t GetRemoteAbilityInfo( - const OHOS::AppExecFwk::ElementName &elementName, RemoteAbilityInfo &remoteAbilityInfo) = 0; + const OHOS::AppExecFwk::ElementName &elementName, RemoteAbilityInfo &remoteAbilityInfo) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } /** * @brief get remote ability info @@ -47,7 +51,10 @@ public: * @return Returns result code when get remote ability info. */ virtual int32_t GetRemoteAbilityInfo(const OHOS::AppExecFwk::ElementName &elementName, - const std::string &localeInfo, RemoteAbilityInfo &remoteAbilityInfo) = 0; + const std::string &localeInfo, RemoteAbilityInfo &remoteAbilityInfo) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } /** * @brief get remote ability infos @@ -56,7 +63,10 @@ public: * @return Returns result code when get remote ability infos. */ virtual int32_t GetRemoteAbilityInfos( - const std::vector &elementNames, std::vector &remoteAbilityInfos) = 0; + const std::vector &elementNames, std::vector &remoteAbilityInfos) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } /** * @brief get remote ability infos @@ -66,7 +76,10 @@ public: * @return Returns result code when get remote ability infos. */ virtual int32_t GetRemoteAbilityInfos(const std::vector &elementNames, const std::string &localeInfo, - std::vector &remoteAbilityInfos) = 0; + std::vector &remoteAbilityInfos) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } /** * @brief get ability info @@ -75,7 +88,10 @@ public: * @return Returns result code when get ability info. */ virtual int32_t GetAbilityInfo( - const OHOS::AppExecFwk::ElementName &elementName, RemoteAbilityInfo &remoteAbilityInfo) = 0; + const OHOS::AppExecFwk::ElementName &elementName, RemoteAbilityInfo &remoteAbilityInfo) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } /** * @brief get ability info @@ -85,7 +101,10 @@ public: * @return Returns result code when get ability info. */ virtual int32_t GetAbilityInfo(const OHOS::AppExecFwk::ElementName &elementName, const std::string &localeInfo, - RemoteAbilityInfo &remoteAbilityInfo) = 0; + RemoteAbilityInfo &remoteAbilityInfo) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } /** * @brief get ability infos @@ -94,7 +113,10 @@ public: * @return Returns result code when get ability infos. */ virtual int32_t GetAbilityInfos( - const std::vector &elementNames, std::vector &remoteAbilityInfos) = 0; + const std::vector &elementNames, std::vector &remoteAbilityInfos) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } /** * @brief get ability infos @@ -104,10 +126,16 @@ public: * @return Returns result code when get ability infos. */ virtual int32_t GetAbilityInfos(const std::vector &elementNames, const std::string &localeInfo, - std::vector &remoteAbilityInfos) = 0; + std::vector &remoteAbilityInfos) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } virtual bool GetDistributedBundleInfo(const std::string &networkId, const std::string &bundleName, - DistributedBundleInfo &distributedBundleInfo) = 0; + DistributedBundleInfo &distributedBundleInfo) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } /** * @brief get distributedBundleName based on a given accessTokenId and networkId. @@ -117,7 +145,10 @@ public: * @return Returns ERR_OK on success, others on failure when get distributed bundle name. */ virtual int32_t GetDistributedBundleName(const std::string &networkId, uint32_t accessTokenId, - std::string &bundleName) = 0; + std::string &bundleName) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } enum class Message { GET_REMOTE_ABILITY_INFO = 0, diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..775d363691a4d149f5a3c9aeb67a07b7ac695ff8 --- /dev/null +++ b/test/fuzztest/BUILD.gn @@ -0,0 +1,24 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# 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. + +import("../../dbms.gni") + +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + # deps file + "distributedbmshost_fuzzer:DistributedBmsHostFuzzTest", + ] +} diff --git a/test/fuzztest/distributedbmshost_fuzzer/BUILD.gn b/test/fuzztest/distributedbmshost_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d923fdcd02b12e2bc92502da6b695205b50003ec --- /dev/null +++ b/test/fuzztest/distributedbmshost_fuzzer/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# 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("../../../dbms.gni") +module_output_path = "bundle_framework/fuzz" + +##############################fuzztest########################################## +ohos_fuzztest("DistributedBmsHostFuzzTest") { + include_dirs = [ "${dbms_services_path}/include" ] + module_out_path = module_output_path + fuzz_config_file = "../../fuzztest/distributedbmshost_fuzzer" + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${dbms_services_path}/src/distributed_bms_host.cpp" ] + sources += [ "distributedbmshost_fuzzer.cpp" ] + + deps = [ + "${common_path}:libappexecfwk_common", + "${dbms_inner_api_path}:dbms_fwk", + ] + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} diff --git a/test/fuzztest/distributedbmshost_fuzzer/corpus/init b/test/fuzztest/distributedbmshost_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/test/fuzztest/distributedbmshost_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# 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/distributedbmshost_fuzzer/distributedbmshost_fuzzer.cpp b/test/fuzztest/distributedbmshost_fuzzer/distributedbmshost_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b13a44face7689c41e11d4ac6a0136bf9102d0e --- /dev/null +++ b/test/fuzztest/distributedbmshost_fuzzer/distributedbmshost_fuzzer.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * 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 "distributedbmshost_fuzzer.h" + +#include +#include + +#include "distributed_bms_host.h" +#include "securec.h" + +using namespace OHOS::AppExecFwk; +namespace OHOS { +constexpr size_t FOO_MAX_LEN = 1024; +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t MESSAGE_SIZE = 10; + +uint32_t GetU32Data(const char* ptr) +{ + return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); +} +bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) +{ + uint32_t code = (GetU32Data(data) % MESSAGE_SIZE); + MessageParcel datas; + std::u16string descriptor = DistributedBmsHost::GetDescriptor(); + datas.WriteInterfaceToken(descriptor); + datas.WriteBuffer(data, size); + datas.RewindRead(0); + MessageParcel reply; + MessageOption option; + DistributedBmsHost distributedBmsHost; + distributedBmsHost.OnRemoteRequest(code, datas, reply, option); + return true; +} +} + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr) { + return 0; + } + + if (size < OHOS::U32_AT_SIZE) { + return 0; + } + + /* Validate the length of size */ + if (size == 0 || size > OHOS::FOO_MAX_LEN) { + return 0; + } + + char* ch = (char *)malloc(size + 1); + if (ch == nullptr) { + return 0; + } + + (void)memset_s(ch, size + 1, 0x00, size + 1); + if (memcpy_s(ch, size, data, size) != EOK) { + free(ch); + ch = nullptr; + return 0; + } + OHOS::DoSomethingInterestingWithMyAPI(ch, size); + free(ch); + ch = nullptr; + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/distributedbmshost_fuzzer/distributedbmshost_fuzzer.h b/test/fuzztest/distributedbmshost_fuzzer/distributedbmshost_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..34ef1d0cb3c69cd65f84d338613dc7eef732f0dc --- /dev/null +++ b/test/fuzztest/distributedbmshost_fuzzer/distributedbmshost_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * 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 TEST_FUZZTEST_DISTRIBUTEDBMSHOST_FUZZER_H +#define TEST_FUZZTEST_DISTRIBUTEDBMSHOST_FUZZER_H + +#define FUZZ_PROJECT_NAME "distributedbmshost_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/distributedbmshost_fuzzer/project.xml b/test/fuzztest/distributedbmshost_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..4fdbc407f205680885fa42663163b5c987f123a6 --- /dev/null +++ b/test/fuzztest/distributedbmshost_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +