1 /*
2 * Copyright (c) 2022 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 FOO_MAX_LEN = 1024;
38 constexpr size_t U32_AT_SIZE = 4;
39
40 class MyConnectionObserver : public ConnectionObserver {
41 public:
42 MyConnectionObserver() = default;
43 virtual ~MyConnectionObserver() = default;
OnExtensionConnected(const ConnectionData & data)44 void OnExtensionConnected(const ConnectionData &data) override
45 {}
OnExtensionDisconnected(const ConnectionData & data)46 void OnExtensionDisconnected(const ConnectionData &data) override
47 {}
OnDlpAbilityOpened(const DlpStateData & data)48 void OnDlpAbilityOpened(const DlpStateData &data) override
49 {}
OnDlpAbilityClosed(const DlpStateData & data)50 void OnDlpAbilityClosed(const DlpStateData &data) override
51 {}
OnServiceDied()52 void OnServiceDied() override
53 {}
54 };
55
56 class MyAbilityConnectionObserver : public IConnectionObserver {
57 public:
58 MyAbilityConnectionObserver() = default;
59 virtual ~MyAbilityConnectionObserver() = default;
OnExtensionConnected(const ConnectionData & data)60 void OnExtensionConnected(const ConnectionData &data) override
61 {}
OnExtensionDisconnected(const ConnectionData & data)62 void OnExtensionDisconnected(const ConnectionData &data) override
63 {}
OnDlpAbilityOpened(const DlpStateData & data)64 void OnDlpAbilityOpened(const DlpStateData &data) override
65 {}
OnDlpAbilityClosed(const DlpStateData & data)66 void OnDlpAbilityClosed(const DlpStateData &data) override
67 {}
AsObject()68 sptr<IRemoteObject> AsObject() override
69 {
70 return {};
71 }
72 };
73 }
74
GetU32Data(const char * ptr)75 uint32_t GetU32Data(const char* ptr)
76 {
77 // convert fuzz input data to an integer
78 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
79 }
80
DoSomethingInterestingWithMyAPI(const char * data,size_t size)81 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
82 {
83 std::shared_ptr<ConnectionObserver> observer = std::make_shared<MyConnectionObserver>();
84 // fuzz for connectionObserverClient
85 auto connectionObserverClient = std::make_shared<ConnectionObserverClient>();
86 connectionObserverClient->UnregisterObserver(observer);
87 sptr<IRemoteObject> remoteObj;
88 auto serviceProxyAdapter = std::make_shared<ServiceProxyAdapter>(remoteObj);
89 sptr<AbilityRuntime::IConnectionObserver> cobserver = new MyAbilityConnectionObserver();
90 serviceProxyAdapter->UnregisterObserver(cobserver);
91 serviceProxyAdapter->GetProxyObject();
92 return true;
93 }
94 }
95
96 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
98 {
99 /* Run your code on data */
100 if (data == nullptr) {
101 return 0;
102 }
103
104 /* Validate the length of size */
105 if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
106 return 0;
107 }
108
109 char* ch = (char *)malloc(size + 1);
110 if (ch == nullptr) {
111 std::cout << "malloc failed." << std::endl;
112 return 0;
113 }
114
115 (void)memset_s(ch, size + 1, 0x00, size + 1);
116 if (memcpy_s(ch, size, data, size) != EOK) {
117 std::cout << "copy failed." << std::endl;
118 free(ch);
119 ch = nullptr;
120 return 0;
121 }
122
123 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
124 free(ch);
125 ch = nullptr;
126 return 0;
127 }
128
129