• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "ipcskeleton_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 #include "iremote_object.h"
22 #include "ipc_skeleton.h"
23 
24 namespace OHOS {
SetMaxWorkThreadNumTest(const uint8_t * data,size_t size)25     void SetMaxWorkThreadNumTest(const uint8_t* data, size_t size)
26     {
27         if (data == nullptr || size < sizeof(uint32_t)) {
28             return;
29         }
30 
31         int maxThreadNum = *(reinterpret_cast<const int32_t*>(data));
32         IPCSkeleton::SetMaxWorkThreadNum(maxThreadNum);
33     }
34 
StopWorkThreadTest(const uint8_t * data,size_t size)35     void StopWorkThreadTest(const uint8_t* data, size_t size)
36     {
37         if (data == nullptr || size < sizeof(uint32_t)) {
38             return;
39         }
40 
41         IPCSkeleton::StopWorkThread();
42     }
43 
GetCallingSidTest(const uint8_t * data,size_t size)44     void GetCallingSidTest(const uint8_t* data, size_t size)
45     {
46         if (data == nullptr || size < sizeof(uint32_t)) {
47             return;
48         }
49 
50         IPCSkeleton::GetCallingSid();
51     }
52 
GetCallingPidTest(const uint8_t * data,size_t size)53     void GetCallingPidTest(const uint8_t* data, size_t size)
54     {
55         if (data == nullptr || size < sizeof(uint32_t)) {
56             return;
57         }
58 
59         IPCSkeleton::GetCallingPid();
60     }
61 
GetCallingRealPidTest(const uint8_t * data,size_t size)62     void GetCallingRealPidTest(const uint8_t* data, size_t size)
63     {
64         if (data == nullptr || size < sizeof(uint32_t)) {
65             return;
66         }
67 
68         IPCSkeleton::GetCallingRealPid();
69     }
70 
GetCallingUidTest(const uint8_t * data,size_t size)71     void GetCallingUidTest(const uint8_t* data, size_t size)
72     {
73         if (data == nullptr || size < sizeof(uint32_t)) {
74             return;
75         }
76 
77         IPCSkeleton::GetCallingUid();
78     }
79 
GetCallingTokenIDTest(const uint8_t * data,size_t size)80     void GetCallingTokenIDTest(const uint8_t* data, size_t size)
81     {
82         if (data == nullptr || size < sizeof(uint32_t)) {
83             return;
84         }
85 
86         IPCSkeleton skeleton = IPCSkeleton::GetInstance();
87         skeleton.GetCallingTokenID();
88     }
89 
GetFirstTokenIDTest(const uint8_t * data,size_t size)90     void GetFirstTokenIDTest(const uint8_t* data, size_t size)
91     {
92         if (data == nullptr || size < sizeof(uint32_t)) {
93             return;
94         }
95 
96         IPCSkeleton skeleton = IPCSkeleton::GetInstance();
97         skeleton.GetFirstTokenID();
98     }
99 
GetFirstFullTokenIDTest(const uint8_t * data,size_t size)100     void GetFirstFullTokenIDTest(const uint8_t* data, size_t size)
101     {
102         if (data == nullptr || size < sizeof(uint32_t)) {
103             return;
104         }
105 
106         IPCSkeleton::GetFirstFullTokenID();
107     }
108 
GetSelfTokenIDTest(const uint8_t * data,size_t size)109     void GetSelfTokenIDTest(const uint8_t* data, size_t size)
110     {
111         if (data == nullptr || size < sizeof(uint32_t)) {
112             return;
113         }
114 
115         IPCSkeleton::GetSelfTokenID();
116     }
117 
GetLocalDeviceIDTest(const uint8_t * data,size_t size)118     void GetLocalDeviceIDTest(const uint8_t* data, size_t size)
119     {
120         if (data == nullptr || size < sizeof(uint32_t)) {
121             return;
122         }
123 
124         IPCSkeleton::GetLocalDeviceID();
125     }
126 
GetCallingDeviceIDTest(const uint8_t * data,size_t size)127     void GetCallingDeviceIDTest(const uint8_t* data, size_t size)
128     {
129         if (data == nullptr || size < sizeof(uint32_t)) {
130             return;
131         }
132 
133         IPCSkeleton::GetCallingDeviceID();
134     }
135 
IsLocalCallingTest(const uint8_t * data,size_t size)136     void IsLocalCallingTest(const uint8_t* data, size_t size)
137     {
138         if (data == nullptr || size < sizeof(uint32_t)) {
139             return;
140         }
141 
142         IPCSkeleton::IsLocalCalling();
143     }
144 
GetInstanceTest(const uint8_t * data,size_t size)145     void GetInstanceTest(const uint8_t* data, size_t size)
146     {
147         if (data == nullptr || size < sizeof(uint32_t)) {
148             return;
149         }
150 
151         IPCSkeleton::GetInstance();
152     }
153 
GetContextObjectTest(const uint8_t * data,size_t size)154     void GetContextObjectTest(const uint8_t* data, size_t size)
155     {
156         if (data == nullptr || size < sizeof(uint32_t)) {
157             return;
158         }
159 
160         IPCSkeleton::GetContextObject();
161     }
162 
SetContextObjectTest(const uint8_t * data,size_t size)163     void SetContextObjectTest(const uint8_t* data, size_t size)
164     {
165         if (data == nullptr || size < sizeof(uint32_t)) {
166             return;
167         }
168 
169         sptr<IRemoteObject> object;
170         IPCSkeleton::SetContextObject(object);
171     }
172 
FlushCommandsTest(const uint8_t * data,size_t size)173     void FlushCommandsTest(const uint8_t* data, size_t size)
174     {
175         if (data == nullptr || size < sizeof(uint32_t)) {
176             return;
177         }
178 
179         IPCSkeleton::FlushCommands(nullptr);
180     }
181 
ResetCallingIdentityTest(const uint8_t * data,size_t size)182     void ResetCallingIdentityTest(const uint8_t* data, size_t size)
183     {
184         if (data == nullptr || size < sizeof(uint32_t)) {
185             return;
186         }
187 
188         IPCSkeleton::ResetCallingIdentity();
189     }
190 
SetCallingIdentityTest(const uint8_t * data,size_t size)191     void SetCallingIdentityTest(const uint8_t* data, size_t size)
192     {
193         if (data == nullptr || size < sizeof(uint32_t)) {
194             return;
195         }
196 
197         std::string identity = "identity";
198         IPCSkeleton::SetCallingIdentity(identity);
199     }
200 }
201 
202 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)203 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
204 {
205     /* Run your code on data */
206     OHOS::SetMaxWorkThreadNumTest(data, size);
207     OHOS::StopWorkThreadTest(data, size);
208     OHOS::GetCallingSidTest(data, size);
209     OHOS::GetCallingPidTest(data, size);
210     OHOS::GetCallingRealPidTest(data, size);
211     OHOS::GetCallingUidTest(data, size);
212     OHOS::GetCallingTokenIDTest(data, size);
213     OHOS::GetFirstTokenIDTest(data, size);
214     OHOS::GetFirstFullTokenIDTest(data, size);
215     OHOS::GetSelfTokenIDTest(data, size);
216     OHOS::GetLocalDeviceIDTest(data, size);
217     OHOS::GetCallingDeviceIDTest(data, size);
218     OHOS::IsLocalCallingTest(data, size);
219     OHOS::GetInstanceTest(data, size);
220     OHOS::GetContextObjectTest(data, size);
221     OHOS::SetContextObjectTest(data, size);
222     OHOS::FlushCommandsTest(data, size);
223     OHOS::ResetCallingIdentityTest(data, size);
224     OHOS::SetCallingIdentityTest(data, size);
225 
226     return 0;
227 }