• 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 "continuationmanager_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "dtbschedmgr_log.h"
22 #include "distributed_ability_manager_interface.h"
23 #include "distributed_ability_manager_stub.h"
24 #include "distributed_ability_manager_service.h"
25 #include "fuzz_util.h"
26 
27 namespace OHOS {
28 namespace DistributedSchedule {
29 namespace {
30     constexpr int32_t DISTRIBUTED_SCHED_SA_ID = 1401;
31     constexpr size_t THRESHOLD = 10;
32     constexpr uint16_t MAX_CALL_TRANSACTION = 510;
33     constexpr int32_t OFFSET = 4;
34     const std::u16string DMS_INTERFACE_TOKEN = u"OHOS.DistributedSchedule.IDistributedAbilityManager";
35     const std::u16string DMS_STUB_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
36     const std::string TAG = "ContinuationFuzz";
37 }
38 
Convert2Uint32(const uint8_t * ptr)39 uint32_t Convert2Uint32(const uint8_t* ptr)
40 {
41     if (ptr == nullptr) {
42         return 0;
43     }
44     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); // this is a general method of converting in fuzz
45 }
46 
FuzzUnregister(const uint8_t * rawData,size_t size)47 void FuzzUnregister(const uint8_t* rawData, size_t size)
48 {
49     FuzzUtil::MockPermission();
50     uint32_t code = Convert2Uint32(rawData);
51     rawData = rawData + OFFSET;
52     size = size - OFFSET;
53     MessageParcel data;
54     data.WriteInterfaceToken(DMS_STUB_INTERFACE_TOKEN);
55     data.WriteBuffer(rawData, size);
56     data.RewindRead(0);
57     MessageParcel reply;
58     MessageOption option;
59     auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
60     if (systemAbilityMgr == nullptr) {
61         HILOGE("system ability manager is nullptr.");
62         return;
63     }
64     auto remoteObj = systemAbilityMgr->GetSystemAbility(DISTRIBUTED_SCHED_SA_ID);
65     if (remoteObj == nullptr) {
66         HILOGE("failed to get form manager service");
67         return;
68     }
69     remoteObj->SendRequest(code % MAX_CALL_TRANSACTION, data, reply, option);
70 }
71 }
72 }
73 
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77     if (size < OHOS::DistributedSchedule::THRESHOLD) {
78         return 0;
79     }
80 
81     OHOS::DistributedSchedule::FuzzUnregister(data, size);
82     return 0;
83 }