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