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 #include "distributed_ability_manager_service.h"
18
19 #include <cstddef>
20 #include <cstdint>
21 #include <fuzzer/FuzzedDataProvider.h>
22
23 #include "base/continuationmgr_log.h"
24 #include "fuzz_util.h"
25
26 namespace OHOS {
27 namespace DistributedSchedule {
28 namespace {
29 constexpr int32_t CONTINUATION_MANAGER_SA_ID = 1404;
30 constexpr size_t THRESHOLD = 10;
31 constexpr uint16_t MAX_CALL_TRANSACTION = 510;
32 constexpr int32_t OFFSET = 4;
33 const std::u16string DMS_INTERFACE_TOKEN = u"OHOS.DistributedSchedule.IDistributedAbilityManager";
34 const std::string TAG = "ContinuationFuzz";
35 }
36
Convert2Uint32(const uint8_t * ptr)37 uint32_t Convert2Uint32(const uint8_t* ptr)
38 {
39 if (ptr == nullptr) {
40 return 0;
41 }
42 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); // this is a general method of converting in fuzz
43 }
44
FuzzUnregister(const uint8_t * rawData,size_t size)45 void FuzzUnregister(const uint8_t* rawData, size_t size)
46 {
47 FuzzUtil::MockPermission();
48 uint32_t code = Convert2Uint32(rawData);
49 rawData = rawData + OFFSET;
50 size = size - OFFSET;
51 FuzzedDataProvider fdp(rawData, size);
52 std::string str = fdp.ConsumeRandomLengthString();
53 MessageParcel data;
54 data.WriteInterfaceToken(DMS_INTERFACE_TOKEN);
55 data.WriteBuffer(str.c_str(), str.size());
56 data.RewindRead(0);
57 MessageParcel reply;
58 MessageOption option;
59 DistributedAbilityManagerService::GetInstance().OnRemoteRequest(code % MAX_CALL_TRANSACTION, data, reply, option);
60 }
61 }
62 }
63
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
66 {
67 if (size < OHOS::DistributedSchedule::THRESHOLD) {
68 return 0;
69 }
70
71 OHOS::DistributedSchedule::FuzzUnregister(data, size);
72 return 0;
73 }