1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "connectionobserverclient_fuzzer.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include <cstddef> 19eace7efcSopenharmony_ci#include <cstdint> 20eace7efcSopenharmony_ci 21eace7efcSopenharmony_ci#define private public 22eace7efcSopenharmony_ci#define protected public 23eace7efcSopenharmony_ci#include "connection_observer_client.h" 24eace7efcSopenharmony_ci#include "service_proxy_adapter.h" 25eace7efcSopenharmony_ci#undef protected 26eace7efcSopenharmony_ci#undef private 27eace7efcSopenharmony_ci 28eace7efcSopenharmony_ci#include "ability_record.h" 29eace7efcSopenharmony_ci#include "continuous_task_callback_info.h" 30eace7efcSopenharmony_ci 31eace7efcSopenharmony_ciusing namespace OHOS::AAFwk; 32eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk; 33eace7efcSopenharmony_ciusing namespace OHOS::AbilityRuntime; 34eace7efcSopenharmony_ci 35eace7efcSopenharmony_cinamespace OHOS { 36eace7efcSopenharmony_cinamespace { 37eace7efcSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024; 38eace7efcSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4; 39eace7efcSopenharmony_ci 40eace7efcSopenharmony_ciclass MyConnectionObserver : public ConnectionObserver { 41eace7efcSopenharmony_cipublic: 42eace7efcSopenharmony_ci MyConnectionObserver() = default; 43eace7efcSopenharmony_ci virtual ~MyConnectionObserver() = default; 44eace7efcSopenharmony_ci void OnExtensionConnected(const ConnectionData& data) override 45eace7efcSopenharmony_ci {} 46eace7efcSopenharmony_ci void OnExtensionDisconnected(const ConnectionData& data) override 47eace7efcSopenharmony_ci {} 48eace7efcSopenharmony_ci void OnDlpAbilityOpened(const DlpStateData& data) override 49eace7efcSopenharmony_ci {} 50eace7efcSopenharmony_ci void OnDlpAbilityClosed(const DlpStateData& data) override 51eace7efcSopenharmony_ci {} 52eace7efcSopenharmony_ci void OnServiceDied() override 53eace7efcSopenharmony_ci {} 54eace7efcSopenharmony_ci}; 55eace7efcSopenharmony_ci 56eace7efcSopenharmony_ciclass MyAbilityConnectionObserver : public IConnectionObserver { 57eace7efcSopenharmony_cipublic: 58eace7efcSopenharmony_ci MyAbilityConnectionObserver() = default; 59eace7efcSopenharmony_ci virtual ~MyAbilityConnectionObserver() = default; 60eace7efcSopenharmony_ci void OnExtensionConnected(const ConnectionData& data) override 61eace7efcSopenharmony_ci {} 62eace7efcSopenharmony_ci void OnExtensionDisconnected(const ConnectionData& data) override 63eace7efcSopenharmony_ci {} 64eace7efcSopenharmony_ci#ifdef WITH_DLP 65eace7efcSopenharmony_ci void OnDlpAbilityOpened(const DlpStateData& data) override 66eace7efcSopenharmony_ci {} 67eace7efcSopenharmony_ci void OnDlpAbilityClosed(const DlpStateData& data) override 68eace7efcSopenharmony_ci {} 69eace7efcSopenharmony_ci#endif // WITH_DLP 70eace7efcSopenharmony_ci sptr<IRemoteObject> AsObject() override 71eace7efcSopenharmony_ci { 72eace7efcSopenharmony_ci return {}; 73eace7efcSopenharmony_ci } 74eace7efcSopenharmony_ci}; 75eace7efcSopenharmony_ci} 76eace7efcSopenharmony_ci 77eace7efcSopenharmony_ciuint32_t GetU32Data(const char* ptr) 78eace7efcSopenharmony_ci{ 79eace7efcSopenharmony_ci // convert fuzz input data to an integer 80eace7efcSopenharmony_ci return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; 81eace7efcSopenharmony_ci} 82eace7efcSopenharmony_ci 83eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPI(const char* data, size_t size) 84eace7efcSopenharmony_ci{ 85eace7efcSopenharmony_ci std::shared_ptr<ConnectionObserver> observer = std::make_shared<MyConnectionObserver>(); 86eace7efcSopenharmony_ci // fuzz for connectionObserverClient 87eace7efcSopenharmony_ci auto connectionObserverClient = std::make_shared<ConnectionObserverClient>(); 88eace7efcSopenharmony_ci connectionObserverClient->UnregisterObserver(observer); 89eace7efcSopenharmony_ci sptr<IRemoteObject> remoteObj; 90eace7efcSopenharmony_ci auto serviceProxyAdapter = std::make_shared<ServiceProxyAdapter>(remoteObj); 91eace7efcSopenharmony_ci sptr<AbilityRuntime::IConnectionObserver> cobserver = new MyAbilityConnectionObserver(); 92eace7efcSopenharmony_ci serviceProxyAdapter->UnregisterObserver(cobserver); 93eace7efcSopenharmony_ci serviceProxyAdapter->GetProxyObject(); 94eace7efcSopenharmony_ci return true; 95eace7efcSopenharmony_ci} 96eace7efcSopenharmony_ci} 97eace7efcSopenharmony_ci 98eace7efcSopenharmony_ci/* Fuzzer entry point */ 99eace7efcSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 100eace7efcSopenharmony_ci{ 101eace7efcSopenharmony_ci /* Run your code on data */ 102eace7efcSopenharmony_ci if (data == nullptr) { 103eace7efcSopenharmony_ci return 0; 104eace7efcSopenharmony_ci } 105eace7efcSopenharmony_ci 106eace7efcSopenharmony_ci /* Validate the length of size */ 107eace7efcSopenharmony_ci if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) { 108eace7efcSopenharmony_ci return 0; 109eace7efcSopenharmony_ci } 110eace7efcSopenharmony_ci 111eace7efcSopenharmony_ci char* ch = (char*)malloc(size + 1); 112eace7efcSopenharmony_ci if (ch == nullptr) { 113eace7efcSopenharmony_ci std::cout << "malloc failed." << std::endl; 114eace7efcSopenharmony_ci return 0; 115eace7efcSopenharmony_ci } 116eace7efcSopenharmony_ci 117eace7efcSopenharmony_ci (void)memset_s(ch, size + 1, 0x00, size + 1); 118eace7efcSopenharmony_ci if (memcpy_s(ch, size, data, size) != EOK) { 119eace7efcSopenharmony_ci std::cout << "copy failed." << std::endl; 120eace7efcSopenharmony_ci free(ch); 121eace7efcSopenharmony_ci ch = nullptr; 122eace7efcSopenharmony_ci return 0; 123eace7efcSopenharmony_ci } 124eace7efcSopenharmony_ci 125eace7efcSopenharmony_ci OHOS::DoSomethingInterestingWithMyAPI(ch, size); 126eace7efcSopenharmony_ci free(ch); 127eace7efcSopenharmony_ci ch = nullptr; 128eace7efcSopenharmony_ci return 0; 129eace7efcSopenharmony_ci} 130eace7efcSopenharmony_ci 131