From 041e26f50892a939cb352904eb915dcdc72e5232 Mon Sep 17 00:00:00 2001 From: li-tiangang4 Date: Sat, 9 Aug 2025 14:45:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9fuzz=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-tiangang4 --- .../getdhdescriptorscallbackstub_fuzzer.cpp | 12 ++++----- .../hardwarestatuslistenerstub_fuzzer.cpp | 27 ++++++++----------- .../capabilityinfomanager_fuzzer.cpp | 25 +++++++---------- .../componentenable_fuzzer.h | 16 +++++------ 4 files changed, 34 insertions(+), 46 deletions(-) diff --git a/interfaces/inner_kits/test/fuzztest/getdhdescriptorscallbackstub_fuzzer/getdhdescriptorscallbackstub_fuzzer.cpp b/interfaces/inner_kits/test/fuzztest/getdhdescriptorscallbackstub_fuzzer/getdhdescriptorscallbackstub_fuzzer.cpp index b1d849f0..8c638dad 100644 --- a/interfaces/inner_kits/test/fuzztest/getdhdescriptorscallbackstub_fuzzer/getdhdescriptorscallbackstub_fuzzer.cpp +++ b/interfaces/inner_kits/test/fuzztest/getdhdescriptorscallbackstub_fuzzer/getdhdescriptorscallbackstub_fuzzer.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -51,20 +52,17 @@ void OnRemoteRequestFuzzTest(const uint8_t *data, size_t size) if ((data == nullptr) || (size < sizeof(uint32_t) + sizeof(uint32_t) + 1)) { return; } + FuzzedDataProvider fdp(data, size); + uint32_t code = fdp.ConsumeIntegral(); MessageParcel parcel; parcel.WriteInterfaceToken(FuzzGetDhDescriptorsCallbackStub().GetDescriptor()); - uint32_t dhType = *(reinterpret_cast(data)); + uint32_t dhType = fdp.ConsumeIntegral(); parcel.WriteUint32(dhType); - std::string id(reinterpret_cast(data + 4), size - 4); + std::string id = fdp.ConsumeRandomLengthString(); parcel.WriteString(id); MessageParcel reply; MessageOption option; - uint32_t code = 0; - if (size >= sizeof(uint32_t)) { - code = *(reinterpret_cast(data)); - } - FuzzGetDhDescriptorsCallbackStub stub; stub.OnRemoteRequest(code, parcel, reply, option); } diff --git a/interfaces/inner_kits/test/fuzztest/hardwarestatuslistenerstub_fuzzer/hardwarestatuslistenerstub_fuzzer.cpp b/interfaces/inner_kits/test/fuzztest/hardwarestatuslistenerstub_fuzzer/hardwarestatuslistenerstub_fuzzer.cpp index ee90ad53..f2ccd5be 100644 --- a/interfaces/inner_kits/test/fuzztest/hardwarestatuslistenerstub_fuzzer/hardwarestatuslistenerstub_fuzzer.cpp +++ b/interfaces/inner_kits/test/fuzztest/hardwarestatuslistenerstub_fuzzer/hardwarestatuslistenerstub_fuzzer.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -33,46 +34,40 @@ namespace OHOS { namespace DistributedHardware { void HDSinkStatusOnRemoteRequestFuzzTest(const uint8_t *data, size_t size) { - if ((data == nullptr) || (size < sizeof(uint32_t))) { + if ((data == nullptr) || (size < sizeof(uint32_t) + sizeof(uint32_t) + 1)) { return; } + FuzzedDataProvider fdp(data, size); + uint32_t code = fdp.ConsumeIntegral(); MessageParcel parcel; parcel.WriteInterfaceToken(FuzzHDSinkStatusListenerStub().GetDescriptor()); - uint32_t dhType = *(reinterpret_cast(data)); + uint32_t dhType = fdp.ConsumeIntegral(); parcel.WriteUint32(dhType); - std::string id(reinterpret_cast(data + 4), size - 4); + std::string id = fdp.ConsumeRandomLengthString(); parcel.WriteString(id); MessageParcel reply; MessageOption option; - uint32_t code = 0; - if (size >= sizeof(uint32_t)) { - code = *(reinterpret_cast(data)); - } - FuzzHDSinkStatusListenerStub stub; stub.OnRemoteRequest(code, parcel, reply, option); } void HDSourceStatusOnRemoteRequestFuzzTest(const uint8_t *data, size_t size) { - if ((data == nullptr) || (size < sizeof(uint32_t))) { + if ((data == nullptr) || (size < sizeof(uint32_t) + sizeof(uint32_t) + 1)) { return; } - + FuzzedDataProvider fdp(data, size); + uint32_t code = fdp.ConsumeIntegral(); MessageParcel parcel; parcel.WriteInterfaceToken(FuzzHDSinkStatusListenerStub().GetDescriptor()); - uint32_t dhType = *(reinterpret_cast(data)); + uint32_t dhType = fdp.ConsumeIntegral(); parcel.WriteUint32(dhType); - std::string id(reinterpret_cast(data + 4), size - 4); + std::string id = fdp.ConsumeRandomLengthString(); parcel.WriteString(id); MessageParcel reply; MessageOption option; - uint32_t code = 0; - if (size >= sizeof(uint32_t)) { - code = *(reinterpret_cast(data)); - } FuzzHDSourceStatusListenerStub stub; stub.OnRemoteRequest(code, parcel, reply, option); } diff --git a/services/distributedhardwarefwkservice/test/fuzztest/capabilityinfomanager_fuzzer/capabilityinfomanager_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/capabilityinfomanager_fuzzer/capabilityinfomanager_fuzzer.cpp index eaf84fe0..3ebaa779 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/capabilityinfomanager_fuzzer/capabilityinfomanager_fuzzer.cpp +++ b/services/distributedhardwarefwkservice/test/fuzztest/capabilityinfomanager_fuzzer/capabilityinfomanager_fuzzer.cpp @@ -15,6 +15,8 @@ #include "capabilityinfomanager_fuzzer.h" +#include + #include "constants.h" #include "capability_info.h" #include "capability_utils.h" @@ -305,9 +307,15 @@ void DumpCapabilityInfosFuzzTest(const uint8_t* data, size_t size) if ((data == nullptr) || (size == 0)) { return; } - + FuzzedDataProvider fdp(data, size); std::vector capInfos; - + std::string dhId = fdp.ConsumeRandomLengthString(); + std::string deviceId = fdp.ConsumeRandomLengthString(); + std::string deviceName = fdp.ConsumeRandomLengthString(); + std::string dhAttrs = fdp.ConsumeRandomLengthString(); + std::string dhSubtype = fdp.ConsumeRandomLengthString(); + CapabilityInfo capInfo(dhId, deviceId, deviceName, TEST_DEV_TYPE_PAD, DHType::CAMERA, dhAttrs, dhSubtype); + capInfos.push_back(capInfo); CapabilityInfoManager::GetInstance()->DumpCapabilityInfos(capInfos); } @@ -382,18 +390,6 @@ void AsyncGetDistributedHardwareFuzzTest(const uint8_t* data, size_t size) CapabilityInfoManager::GetInstance()->AsyncGetDistributedHardware(networkId, enableStep, callback); } -void DoAsyncGetDistributedHardwareFuzzTest(const uint8_t* data, size_t size) -{ - if ((data == nullptr) || (size == 0)) { - return; - } - - std::string networkId(reinterpret_cast(data), size); - EnableStep enableStep = static_cast(data[0] % 4); - sptr callback(new TestGetDistributedHardwareCallback()); - CapabilityInfoManager::GetInstance()->DoAsyncGetDistributedHardware(networkId, enableStep, callback); -} - void GetEntriesByKeysFuzzTest(const uint8_t* data, size_t size) { if ((data == nullptr) || (size == 0)) { @@ -427,7 +423,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) OHOS::DistributedHardware::OnChangeFuzzTest(data, size); OHOS::DistributedHardware::GetDataByDHTypeFuzzTest(data, size); OHOS::DistributedHardware::AsyncGetDistributedHardwareFuzzTest(data, size); - OHOS::DistributedHardware::DoAsyncGetDistributedHardwareFuzzTest(data, size); OHOS::DistributedHardware::GetEntriesByKeysFuzzTest(data, size); return 0; } diff --git a/services/distributedhardwarefwkservice/test/fuzztest/componentenable_fuzzer/componentenable_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/componentenable_fuzzer/componentenable_fuzzer.h index 3ec7cc3c..9772f0eb 100644 --- a/services/distributedhardwarefwkservice/test/fuzztest/componentenable_fuzzer/componentenable_fuzzer.h +++ b/services/distributedhardwarefwkservice/test/fuzztest/componentenable_fuzzer/componentenable_fuzzer.h @@ -29,17 +29,17 @@ public: FuzzDistributedHardwareSource() = default; virtual ~FuzzDistributedHardwareSource() = default; - int32_t InitSource(const std::string&) override; + int32_t InitSource(const std::string& networkId) override; int32_t ReleaseSource() override; - int32_t RegisterDistributedHardware(const std::string&, const std::string&, - const EnableParam&, std::shared_ptr) override; - int32_t UnregisterDistributedHardware(const std::string&, const std::string&, + int32_t RegisterDistributedHardware(const std::string& networkId, const std::string& dhId, const EnableParam& param, + std::shared_ptr callback) override; + int32_t UnregisterDistributedHardware(const std::string& networkId, const std::string& dhId, std::shared_ptr callback) override; - int32_t ConfigDistributedHardware(const std::string&, const std::string&, const std::string&, - const std::string&) override; - void RegisterDistributedHardwareStateListener(std::shared_ptr) override; + int32_t ConfigDistributedHardware(const std::string& networkId, const std::string& dhId, const std::string& key, + const std::string& value) override; + void RegisterDistributedHardwareStateListener(std::shared_ptr listener) override; void UnregisterDistributedHardwareStateListener() override{} - void RegisterDataSyncTriggerListener(std::shared_ptr) override; + void RegisterDataSyncTriggerListener(std::shared_ptr listener) override; void UnregisterDataSyncTriggerListener() override{} }; } // namespace DistributedHardware -- Gitee