From 85737714d011e90f4c39d40d056553d44183d596 Mon Sep 17 00:00:00 2001 From: zhaoyrr Date: Mon, 8 Sep 2025 10:22:13 +0800 Subject: [PATCH] fix fuzz Signed-off-by: zhaoyrr --- .../connectionobserverclient_fuzzer.cpp | 42 +++---------------- ...nnectionobserverclientimplfifth_fuzzer.cpp | 40 ++++++++++++++++-- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/test/fuzztest/connectionobserverclient_fuzzer/connectionobserverclient_fuzzer.cpp b/test/fuzztest/connectionobserverclient_fuzzer/connectionobserverclient_fuzzer.cpp index fc3004b99f4..43faf42bede 100755 --- a/test/fuzztest/connectionobserverclient_fuzzer/connectionobserverclient_fuzzer.cpp +++ b/test/fuzztest/connectionobserverclient_fuzzer/connectionobserverclient_fuzzer.cpp @@ -77,22 +77,15 @@ public: }; } -uint32_t GetU32Data(const char* ptr) -{ - // convert fuzz input data to an integer - return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; -} - bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) { - (void)data; - std::shared_ptr observer = std::make_shared(); + std:: observer; // fuzz for connectionObserverClient - auto connectionObserverClient = std::make_shared(); - connectionObserverClient->UnregisterObserver(observer); + FuzzedDataProvider fdp(data, size); + ConnectionObserverClient::GetInstance().UnregisterObserver(observer); sptr remoteObj; auto serviceProxyAdapter = std::make_shared(remoteObj); - sptr cobserver = new MyAbilityConnectionObserver(); + sptr cobserver = std::make_shared(); serviceProxyAdapter->UnregisterObserver(cobserver); serviceProxyAdapter->GetProxyObject(); return true; @@ -103,32 +96,7 @@ bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ - if (data == nullptr) { - return 0; - } - - /* Validate the length of size */ - if (size < OHOS::U32_AT_SIZE) { - return 0; - } - - char* ch = static_cast(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; + OHOS::DoSomethingInterestingWithMyAPI(data, size); return 0; } diff --git a/test/fuzztest/connectionobserverclientimplfifth_fuzzer/connectionobserverclientimplfifth_fuzzer.cpp b/test/fuzztest/connectionobserverclientimplfifth_fuzzer/connectionobserverclientimplfifth_fuzzer.cpp index bd85bee0565..7da535fe01e 100644 --- a/test/fuzztest/connectionobserverclientimplfifth_fuzzer/connectionobserverclientimplfifth_fuzzer.cpp +++ b/test/fuzztest/connectionobserverclientimplfifth_fuzzer/connectionobserverclientimplfifth_fuzzer.cpp @@ -38,12 +38,43 @@ using namespace OHOS::AbilityRuntime; namespace OHOS { namespace { constexpr size_t STRING_MAX_LENGTH = 128; +class MyConnectionObserver : public ConnectionObserver { +public: + MyConnectionObserver() = default; + virtual ~MyConnectionObserver() = default; + void OnExtensionConnected(const ConnectionData& data) override + {} + void OnExtensionDisconnected(const ConnectionData& data) override + {} + void OnDlpAbilityOpened(const DlpStateData& data) override + {} + void OnDlpAbilityClosed(const DlpStateData& data) override + {} + void OnServiceDied() override + {} +}; + +class ConnectionObserverClientSingleton { +public: + static std::shared_ptr GetInstance() { + if (!instance) { + instance = std::make_shared(); + } + return instance; + } + +private: + ConnectionObserverClientSingleton() = default; + ~ConnectionObserverClientSingleton() = default; + static std::shared_ptr instance; +}; +std::shared_ptr ConnectionObserverClientSingleton::instance = nullptr; } bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) { - auto connectionObserverClientImpl = std::make_shared(); - std::shared_ptr observer; + auto connectionObserverClientImpl = ConnectionObserverClientSingleton::GetInstance(); + std::shared_ptr observer = std::make_shared(); wptr remote; FuzzedDataProvider fdp(data, size); connectionObserverClientImpl->RemoveObserversLocked(observer); @@ -54,8 +85,11 @@ bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) connectionObserverClientImpl->ResetStatus(); connectionObserverClientImpl->NotifyServiceDiedToObservers(); connectionObserverClientImpl->GetObservers(); - auto deathRecipient = new (std::nothrow) AbilityRuntime::ConnectionObserverClientImpl::ServiceDeathRecipient( + auto deathRecipient = std::make_shared( connectionObserverClientImpl); + if (!deathRecipient) { + return false; + } deathRecipient->OnRemoteDied(remote); return true; } -- Gitee