diff --git a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn index c5982924dd458019c17cc208f007a180a598b97f..a5e634d9c320a5069cb6fe496323b07e3c917683 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/fuzztest/BUILD.gn @@ -27,6 +27,7 @@ group("fuzztest") { "dhtransport_fuzzer:fuzztest", "distributedfwkservices_fuzzer:fuzztest", "enabledcompsdump_fuzzer:fuzztest", + "hdfoperate_fuzzer:fuzztest", "metainfomgr_fuzzer:fuzztest", "publisher_fuzzer:fuzztest", "publisheritem_fuzzer:fuzztest", diff --git a/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c4b7fec7bf9bab48ffa966352044e03c8e4665c5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/BUILD.gn @@ -0,0 +1,69 @@ +# Copyright (c) 2025 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/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("HdfOperateFuzzTest") { + module_out_path = fuzz_test_output_path + fuzz_config_file = + "${services_path}/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer" + + include_dirs = [ + "include", + "${utils_path}/include", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/hdfoperate", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${common_path}/utils/include", + "${common_path}/log/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "hdfoperate_fuzzer.cpp" ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + ] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "samgr:samgr_proxy", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"HdfOperateFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":HdfOperateFuzzTest" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 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/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/hdfoperate_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/hdfoperate_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a37efd9c960269e8a1fe64df3d4c87e8d80d6f79 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/hdfoperate_fuzzer.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2025 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 "hdfoperate_fuzzer.h" + +#include +#include + +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "hdf_operate.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + constexpr uint32_t RAND_DHTYPE_COUNT = 7; + const DHType dhTypeFuzz[] = { + DHType::UNKNOWN, + DHType::CAMERA, + DHType::AUDIO + }; +} + +class MockIRemoteObject : public IRemoteObject { +public: + MockIRemoteObject() : IRemoteObject {u"MockIRemoteObject"} + { + } + + ~MockIRemoteObject() + { + } + + int32_t GetObjectRefCount() + { + return 0; + } + + int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) + { + return 0; + } + + bool IsProxyObject() const + { + return true; + } + + bool CheckObjectLegality() const + { + return true; + } + + bool AddDeathRecipient(const sptr &recipient) + { + return true; + } + + bool RemoveDeathRecipient(const sptr &recipient) + { + return true; + } + + sptr AsInterface() + { + return nullptr; + } + + int Dump(int fd, const std::vector &args) + { + return 0; + } +}; + +void HdfOperateFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size < RAND_DHTYPE_COUNT)) { + return; + } + + DHType dhTypes[RAND_DHTYPE_COUNT]; + const DHType *dhTypePtr = dhTypes; + IDistributedHardwareSource *sourcePtr = nullptr; + sptr remote = sptr(new MockIRemoteObject()); + + for (uint32_t i = 0; i < RAND_DHTYPE_COUNT; i++) { + dhTypes[i] = dhTypeFuzz[data[i] % sizeof(dhTypeFuzz)]; + } + + HdfOperateManager::GetInstance().LoadDistributedHDF(*dhTypePtr++); + HdfOperateManager::GetInstance().UnLoadDistributedHDF(*dhTypePtr++); + HdfOperateManager::GetInstance().AddDeathRecipient(*dhTypePtr++, remote); + HdfOperateManager::GetInstance().RemoveDeathRecipient(*dhTypePtr++, remote); + HdfOperateManager::GetInstance().ResetRefCount(*dhTypePtr++); + HdfOperateManager::GetInstance().RigidGetSourcePtr(*dhTypePtr++, sourcePtr); + HdfOperateManager::GetInstance().RigidReleaseSourcePtr(*dhTypePtr++); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::HdfOperateFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/hdfoperate_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/hdfoperate_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..f40c70a44aa385f5890919c6cbce3c716c72e2eb --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/hdfoperate_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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_HDFOPERATE_FUZZER_H +#define TEST_HDFOPERATE_FUZZER_H + +#define FUZZ_PROJECT_NAME "hdfoperate_fuzzer" + +#endif diff --git a/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..66e1dcac475475fb101b6f8670ec699e6e9696aa --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/hdfoperate_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +