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 "distributedschedservicenine_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
NotifyCollaborateEventWithSessionsFuzzTest(const uint8_t * data,size_t size)42 void NotifyCollaborateEventWithSessionsFuzzTest(const uint8_t* data, size_t size)
43 {
44 if (data == nullptr || size < sizeof(int32_t)) {
45 return;
46 }
47
48 FuzzedDataProvider fdp(data, size);
49
50 std::list<ConnectAbilitySession> sessionsList;
51 int sessionCount = fdp.ConsumeIntegralInRange<int>(1, SESSION_COUNT_MAX);
52 for (int i = 0; i < sessionCount; ++i) {
53 CallerInfo callerInfo;
54 callerInfo.sourceDeviceId = fdp.ConsumeRandomLengthString();
55 callerInfo.uid = fdp.ConsumeIntegral<int32_t>();
56
57 AppExecFwk::ElementName element(
58 fdp.ConsumeRandomLengthString(),
59 fdp.ConsumeRandomLengthString(),
60 fdp.ConsumeRandomLengthString()
61 );
62 ConnectAbilitySession session(callerInfo.sourceDeviceId, fdp.ConsumeRandomLengthString(), callerInfo);
63 session.AddElement(element);
64 sessionsList.emplace_back(session);
65 }
66 DSchedEventState state = static_cast<DSchedEventState>(fdp.ConsumeIntegral<int32_t>());
67 int32_t ret = fdp.ConsumeIntegral<int32_t>();
68 DistributedSchedService::GetInstance().NotifyCollaborateEventWithSessions(sessionsList, state, ret);
69 }
70
GetCurSrcCollaborateEventFuzzTest(const uint8_t * data,size_t size)71 void GetCurSrcCollaborateEventFuzzTest(const uint8_t* data, size_t size)
72 {
73 if (data == nullptr || size < sizeof(int32_t)) {
74 return;
75 }
76
77 FuzzedDataProvider fdp(data, size);
78 CallerInfo callerInfo;
79 callerInfo.uid = fdp.ConsumeIntegral<int32_t>();
80 callerInfo.sourceDeviceId = fdp.ConsumeRandomLengthString();
81
82 AppExecFwk::ElementName element(
83 fdp.ConsumeRandomLengthString(),
84 fdp.ConsumeRandomLengthString(),
85 fdp.ConsumeRandomLengthString()
86 );
87 element.SetModuleName(fdp.ConsumeRandomLengthString());
88 DSchedEventState state = static_cast<DSchedEventState>(fdp.ConsumeIntegral<int32_t>());
89 int32_t ret = fdp.ConsumeIntegral<int32_t>();
90 EventNotify event;
91
92 DistributedSchedService::GetInstance().GetCurSrcCollaborateEvent(callerInfo, element, state, ret, event);
93 }
94
GetCurDestCollaborateEventFuzzTest(const uint8_t * data,size_t size)95 void GetCurDestCollaborateEventFuzzTest(const uint8_t* data, size_t size)
96 {
97 if (data == nullptr || size < sizeof(int32_t)) {
98 return;
99 }
100
101 FuzzedDataProvider fdp(data, size);
102 CallerInfo callerInfo;
103 callerInfo.uid = fdp.ConsumeIntegral<int32_t>();
104 callerInfo.sourceDeviceId = fdp.ConsumeRandomLengthString();
105 callerInfo.extraInfoJson["dmsCallerUidBundleName"] = fdp.ConsumeRandomLengthString();
106
107 AppExecFwk::ElementName element(
108 fdp.ConsumeRandomLengthString(),
109 fdp.ConsumeRandomLengthString(),
110 fdp.ConsumeRandomLengthString()
111 );
112 element.SetModuleName(fdp.ConsumeRandomLengthString());
113 DSchedEventState state = static_cast<DSchedEventState>(fdp.ConsumeIntegral<int32_t>());
114 int32_t ret = fdp.ConsumeIntegral<int32_t>();
115 EventNotify event;
116 DistributedSchedService::GetInstance().GetCurDestCollaborateEvent(callerInfo, element, state, ret, event);
117 }
118
CheckDistributedConnectLockedFuzzTest(const uint8_t * data,size_t size)119 void CheckDistributedConnectLockedFuzzTest(const uint8_t* data, size_t size)
120 {
121 if (data == nullptr || size < sizeof(int32_t)) {
122 return;
123 }
124
125 FuzzedDataProvider fdp(data, size);
126
127 CallerInfo callerInfo;
128 callerInfo.uid = fdp.ConsumeIntegral<int32_t>();
129 callerInfo.sourceDeviceId = fdp.ConsumeRandomLengthString();
130 DistributedSchedService::GetInstance().CheckDistributedConnectLocked(callerInfo);
131 }
132
DecreaseConnectLockedFuzzTest(const uint8_t * data,size_t size)133 void DecreaseConnectLockedFuzzTest(const uint8_t* data, size_t size)
134 {
135 if (data == nullptr || size < sizeof(int32_t)) {
136 return;
137 }
138
139 FuzzedDataProvider fdp(data, size);
140 int32_t uid = fdp.ConsumeIntegral<int32_t>();
141 DistributedSchedService::GetInstance().DecreaseConnectLocked(uid);
142 }
143 }
144 }
145 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)146 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
147 {
148 OHOS::DistributedSchedule::NotifyCollaborateEventWithSessionsFuzzTest(data, size);
149 OHOS::DistributedSchedule::GetCurSrcCollaborateEventFuzzTest(data, size);
150 OHOS::DistributedSchedule::GetCurDestCollaborateEventFuzzTest(data, size);
151 OHOS::DistributedSchedule::CheckDistributedConnectLockedFuzzTest(data, size);
152 OHOS::DistributedSchedule::DecreaseConnectLockedFuzzTest(data, size);
153 return 0;
154 }
155