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