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 "workscheduleservice_fuzzer.h"
17
18 #define private public
19 #include "system_ability_definition.h"
20 #include "iservice_registry.h"
21 #include "work_sched_service_stub.h"
22 #include "ffrt.h"
23
24 namespace OHOS {
25 namespace WorkScheduler {
26 constexpr int32_t MIN_LEN = 4;
27 constexpr int32_t MAX_CODE_TEST = 15; // current max code is 7
28 static bool isInited = false;
29 ffrt::mutex mutexLock;
30 sptr<IRemoteObject> remoteObject;
31
DoInit()32 bool DoInit()
33 {
34 std::lock_guard<ffrt::mutex> lock(mutexLock);
35 if (remoteObject != nullptr) {
36 return true;
37 }
38 sptr<ISystemAbilityManager> SystemAbilityManager =
39 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40 if (SystemAbilityManager == nullptr) {
41 return false;
42 }
43 remoteObject = SystemAbilityManager->GetSystemAbility(WORK_SCHEDULE_SERVICE_ID);
44 if (remoteObject == nullptr) {
45 return false;
46 }
47 return true;
48 }
49
OnRemoteRequest(uint32_t code,MessageParcel & data)50 int32_t OnRemoteRequest(uint32_t code, MessageParcel& data)
51 {
52 MessageParcel reply;
53 MessageOption option;
54 int32_t ret = remoteObject->SendRequest(code, data, reply, option);
55 return ret;
56 }
57
IpcFuzzTest(const uint8_t * data,size_t size)58 void IpcFuzzTest(const uint8_t* data, size_t size)
59 {
60 if (size <= MIN_LEN) {
61 return;
62 }
63
64 MessageParcel dataMessageParcel;
65 if (!dataMessageParcel.WriteInterfaceToken(WorkSchedServiceStub::GetDescriptor())) {
66 return;
67 }
68
69 uint32_t code = *(reinterpret_cast<const uint32_t*>(data));
70 if (code > MAX_CODE_TEST) {
71 return;
72 }
73 size -= sizeof(uint32_t);
74
75 dataMessageParcel.WriteBuffer(data + sizeof(uint32_t), size);
76 dataMessageParcel.RewindRead(0);
77
78 if (!isInited) {
79 isInited = DoInit();
80 }
81 if (isInited) {
82 OnRemoteRequest(code, dataMessageParcel);
83 }
84 }
85 } // namespace WorkScheduler
86 } // namespace OHOS
87
88 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
90 {
91 /* Run your code on data */
92 OHOS::WorkScheduler::IpcFuzzTest(data, size);
93 return 0;
94 }
95