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