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