• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "connectionobserverclientimpl_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "connection_observer_client_impl.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 #include "connection_observer.h"
31 
32 using namespace OHOS::AAFwk;
33 using namespace OHOS::AppExecFwk;
34 using namespace OHOS::AbilityRuntime;
35 
36 namespace OHOS {
37 namespace {
38 constexpr size_t FOO_MAX_LEN = 1024;
39 constexpr size_t U32_AT_SIZE = 4;
40 
41 class MyConnectionObserver : public ConnectionObserver {
42 public:
43     MyConnectionObserver() = default;
44     virtual ~MyConnectionObserver() = default;
OnExtensionConnected(const ConnectionData & data)45     void OnExtensionConnected(const ConnectionData &data) override
46     {}
OnExtensionDisconnected(const ConnectionData & data)47     void OnExtensionDisconnected(const ConnectionData &data) override
48     {}
OnDlpAbilityOpened(const DlpStateData & data)49     void OnDlpAbilityOpened(const DlpStateData &data) override
50     {}
OnDlpAbilityClosed(const DlpStateData & data)51     void OnDlpAbilityClosed(const DlpStateData &data) override
52     {}
OnServiceDied()53     void OnServiceDied() override
54     {}
55 };
56 }
57 
GetU32Data(const char * ptr)58 uint32_t GetU32Data(const char* ptr)
59 {
60     // convert fuzz input data to an integer
61     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
62 }
63 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)64 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
65 {
66     std::shared_ptr<ConnectionObserver> observer = std::make_shared<MyConnectionObserver>();
67     // fuzz for connectionObserverClientImpl
68     auto connectionObserverClientImpl = std::make_shared<ConnectionObserverClientImpl>();
69     connectionObserverClientImpl->UnregisterObserver(observer);
70     AbilityRuntime::ConnectionData connectionData;
71     connectionObserverClientImpl->HandleExtensionConnected(connectionData);
72     connectionObserverClientImpl->HandleExtensionDisconnected(connectionData);
73     AbilityRuntime::DlpStateData dlpStateData;
74     connectionObserverClientImpl->HandleDlpAbilityOpened(dlpStateData);
75     connectionObserverClientImpl->HandleDlpAbilityClosed(dlpStateData);
76     sptr<IRemoteObject> remoteObj;
77     auto serviceProxyAdapter = std::make_shared<ServiceProxyAdapter>(remoteObj);
78     connectionObserverClientImpl->UnregisterFromServiceLocked(serviceProxyAdapter);
79     connectionObserverClientImpl->RemoveObserversLocked(observer);
80     wptr<IRemoteObject> remote;
81     connectionObserverClientImpl->HandleRemoteDied(remote);
82     connectionObserverClientImpl->ResetProxy(remote);
83     connectionObserverClientImpl->ResetStatus();
84     connectionObserverClientImpl->NotifyServiceDiedToObservers();
85     connectionObserverClientImpl->GetObservers();
86     return true;
87 }
88 }
89 
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93     /* Run your code on data */
94     if (data == nullptr) {
95         return 0;
96     }
97 
98     /* Validate the length of size */
99     if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
100         return 0;
101     }
102 
103     char* ch = (char *)malloc(size + 1);
104     if (ch == nullptr) {
105         std::cout << "malloc failed." << std::endl;
106         return 0;
107     }
108 
109     (void)memset_s(ch, size + 1, 0x00, size + 1);
110     if (memcpy_s(ch, size, data, size) != EOK) {
111         std::cout << "copy failed." << std::endl;
112         free(ch);
113         ch = nullptr;
114         return 0;
115     }
116 
117     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
118     free(ch);
119     ch = nullptr;
120     return 0;
121 }
122 
123