1 /*
2 * Copyright (c) 2025 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 "distributedschedservicefive_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include <singleton.h>
22
23 #include "distributed_sched_interface.h"
24 #include "distributed_sched_service.h"
25 #include "distributed_sched_stub.h"
26 #include "distributedWant/distributed_want.h"
27 #include "mock_fuzz_util.h"
28 #include "mock_distributed_sched.h"
29 #include "parcel_helper.h"
30 #include "dms_continue_time_dumper.h"
31
32 using namespace OHOS::AAFwk;
33 using namespace OHOS::AppExecFwk;
34
35 namespace OHOS {
36 namespace DistributedSchedule {
37 constexpr int SESSION_COUNT_MAX = 10;
38
39 std::string GetDExtensionName(std::string bundleName, int32_t userId);
40 std::string GetDExtensionProcess(std::string bundleName, int32_t userId);
41
StartLocalAbilityFuzzTest(const uint8_t * data,size_t size)42 void StartLocalAbilityFuzzTest(const uint8_t* data, size_t size)
43 {
44 if ((data == nullptr) || (size < sizeof(int32_t)) + (size < sizeof(int64_t))) {
45 return;
46 }
47 FuzzedDataProvider fdp(data, size);
48 DistributedSchedService::FreeInstallInfo info;
49 int64_t taskId = fdp.ConsumeIntegral<int64_t>();
50 int32_t resultCode = fdp.ConsumeIntegral<int32_t>();
51 DistributedSchedService::GetInstance().StartLocalAbility(info, taskId, resultCode);
52 }
53
SetMissionContinueStateFuzzTest(const uint8_t * data,size_t size)54 void SetMissionContinueStateFuzzTest(const uint8_t* data, size_t size)
55 {
56 if ((data == nullptr) || (size < sizeof(int32_t)) + (size < sizeof(int32_t))) {
57 return;
58 }
59 FuzzedDataProvider fdp(data, size);
60 int32_t missionId = fdp.ConsumeIntegral<int32_t>();
61 AAFwk::ContinueState state = static_cast<AAFwk::ContinueState>(fdp.ConsumeIntegral<int32_t>());
62 int32_t callingUid = fdp.ConsumeIntegral<int32_t>();
63 DistributedSchedService::GetInstance().SetMissionContinueState(missionId, state, callingUid);
64 }
65
TryConnectRemoteAbilityFuzzTest(const uint8_t * data,size_t size)66 void TryConnectRemoteAbilityFuzzTest(const uint8_t* data, size_t size)
67 {
68 if ((data == nullptr) || (size < sizeof(int32_t))) {
69 return;
70 }
71 FuzzedDataProvider fdp(data, size);
72 AAFwk::Want want;
73 sptr<IRemoteObject> connect = nullptr;
74 CallerInfo callerInfo;
75 callerInfo.sourceDeviceId = fdp.ConsumeRandomLengthString();
76 callerInfo.uid = fdp.ConsumeIntegral<int32_t>();
77 callerInfo.pid = fdp.ConsumeIntegral<int32_t>();
78 callerInfo.callerType = fdp.ConsumeIntegral<int32_t>();
79 DistributedSchedService::GetInstance().TryConnectRemoteAbility(want, connect, callerInfo);
80 }
81
NotifyContinuateEventResultFuzzTest(const uint8_t * data,size_t size)82 void NotifyContinuateEventResultFuzzTest(const uint8_t* data, size_t size)
83 {
84 if ((data == nullptr) || (size < sizeof(int32_t))) {
85 return;
86 }
87 FuzzedDataProvider fdp(data, size);
88 int32_t resultCode = fdp.ConsumeIntegral<int32_t>();
89 EventNotify eventNotify;
90 DistributedSchedService::GetInstance().NotifyContinuateEventResult(resultCode, eventNotify);
91 }
92
GetUidLockedFuzzTest(const uint8_t * data,size_t size)93 void GetUidLockedFuzzTest(const uint8_t* data, size_t size)
94 {
95 if (data == nullptr || size < sizeof(int32_t)) {
96 return;
97 }
98
99 FuzzedDataProvider fdp(data, size);
100 std::list<ConnectAbilitySession> sessionsList;
101 int sessionCount = fdp.ConsumeIntegralInRange<int>(0, SESSION_COUNT_MAX);
102 for (int i = 0; i < sessionCount; ++i) {
103 CallerInfo callerInfo;
104 callerInfo.uid = fdp.ConsumeIntegral<int32_t>();
105 callerInfo.sourceDeviceId = fdp.ConsumeRandomLengthString();
106
107 AppExecFwk::ElementName element(
108 fdp.ConsumeRandomLengthString(),
109 fdp.ConsumeRandomLengthString(),
110 fdp.ConsumeRandomLengthString()
111 );
112
113 ConnectAbilitySession session(callerInfo.sourceDeviceId, fdp.ConsumeRandomLengthString(), callerInfo);
114 session.AddElement(element);
115 sessionsList.emplace_back(session);
116 }
117 DistributedSchedService::GetInstance().GetUidLocked(sessionsList);
118 }
119 }
120 }
121 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)122 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
123 {
124 OHOS::DistributedSchedule::StartLocalAbilityFuzzTest(data, size);
125 OHOS::DistributedSchedule::SetMissionContinueStateFuzzTest(data, size);
126 OHOS::DistributedSchedule::TryConnectRemoteAbilityFuzzTest(data, size);
127 OHOS::DistributedSchedule::NotifyContinuateEventResultFuzzTest(data, size);
128 OHOS::DistributedSchedule::GetUidLockedFuzzTest(data, size);
129 return 0;
130 }