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