• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "distributedschedstubseven_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 const std::string TAG = "DistributedSchedFuzzTest";
38 
ConnectAbilityFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)39 void ConnectAbilityFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
40 {
41     if ((data == nullptr) || (size < sizeof(int32_t))) {
42         return;
43     }
44     FuzzUtil::MockPermission();
45     MessageParcel dataParcel;
46     MessageParcel reply;
47     MessageOption option;
48     AppExecFwk::CompatibleAbilityInfo compatibleAbilityInfo;
49     FuzzedDataProvider fdp(data, size);
50     int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
51     std::string str = fdp.ConsumeRandomLengthString();
52     std::vector<std::string> strVector = {str};
53     DistributedWant dstbWant;
54     std::string dstDeviceId = fdp.ConsumeRandomLengthString();
55     std::string bundleName = fdp.ConsumeRandomLengthString();
56     std::string abilityName = fdp.ConsumeRandomLengthString();
57 
58     dstbWant.SetDeviceId(dstDeviceId);
59     dstbWant.SetElementName(bundleName, abilityName);
60     const sptr<IRemoteObject> connect(new MockDistributedSched());
61 
62     PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &dstbWant);
63     PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &compatibleAbilityInfo);
64     PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, connect);
65     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
66     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
67     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
68     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
69     PARCEL_WRITE_HELPER_NORET(dataParcel, StringVector, strVector);
70     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
71     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
72     DistributedSchedService::GetInstance().ConnectAbilityFromRemoteInner(dataParcel, reply);
73 }
74 
DisconnectAbilityFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)75 void DisconnectAbilityFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
76 {
77     if ((data == nullptr) || (size < sizeof(int32_t))) {
78         return;
79     }
80     FuzzUtil::MockPermission();
81     MessageParcel dataParcel;
82     MessageParcel reply;
83     MessageOption option;
84     AppExecFwk::CompatibleAbilityInfo compatibleAbilityInfo;
85     FuzzedDataProvider fdp(data, size);
86     int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
87     std::string str = fdp.ConsumeRandomLengthString();
88 
89     sptr<IRemoteObject> connect(new MockDistributedSched());
90     PARCEL_WRITE_HELPER_NORET(dataParcel, RemoteObject, connect);
91     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
92     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
93     DistributedSchedService::GetInstance().DisconnectAbilityFromRemoteInner(dataParcel, reply);
94     DistributedSchedService::GetInstance().ProcessCallerDied(connect, int32Data);
95     DistributedSchedService::GetInstance().ProcessCalleeDied(connect);
96 }
97 
NotifyProcessDiedFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)98 void NotifyProcessDiedFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
99 {
100     if ((data == nullptr) || (size < sizeof(int32_t))) {
101         return;
102     }
103     FuzzUtil::MockPermission();
104     MessageParcel dataParcel;
105     MessageParcel reply;
106     MessageOption option;
107     FuzzedDataProvider fdp(data, size);
108     int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
109     std::string str = fdp.ConsumeRandomLengthString();
110 
111     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
112     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
113     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
114     DistributedSchedService::GetInstance().NotifyProcessDiedFromRemoteInner(dataParcel, reply);
115 }
116 
GetContinueInfoInnerFuzzTest(const uint8_t * data,size_t size)117 void GetContinueInfoInnerFuzzTest(const uint8_t* data, size_t size)
118 {
119     if ((data == nullptr) || (size < sizeof(int32_t))) {
120         return;
121     }
122     FuzzUtil::MockPermission();
123     MessageParcel dataParcel;
124     MessageParcel reply;
125     MessageOption option;
126     std::string str(reinterpret_cast<const char*>(data), size);
127 
128     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
129     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
130     DistributedSchedService::GetInstance().GetContinueInfoInner(dataParcel, reply);
131 }
132 
StopAbilityFromRemoteInnerFuzzTest(const uint8_t * data,size_t size)133 void StopAbilityFromRemoteInnerFuzzTest(const uint8_t* data, size_t size)
134 {
135     if ((data == nullptr) || (size < sizeof(int32_t))) {
136         return;
137     }
138     FuzzUtil::MockPermission();
139     MessageParcel dataParcel;
140     MessageParcel reply;
141     FuzzedDataProvider fdp(data, size);
142     int32_t int32Data = fdp.ConsumeIntegral<int32_t>();
143     std::string str = fdp.ConsumeRandomLengthString();
144     std::vector<std::string> strVector = {str};
145     DistributedWant dstbWant;
146     std::string dstDeviceId = fdp.ConsumeRandomLengthString();
147     std::string bundleName = fdp.ConsumeRandomLengthString();
148     std::string abilityName = fdp.ConsumeRandomLengthString();
149 
150     dstbWant.SetDeviceId(dstDeviceId);
151     dstbWant.SetElementName(bundleName, abilityName);
152 
153     PARCEL_WRITE_HELPER_NORET(dataParcel, Parcelable, &dstbWant);
154     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
155     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
156     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
157     PARCEL_WRITE_HELPER_NORET(dataParcel, Int32, int32Data);
158     PARCEL_WRITE_HELPER_NORET(dataParcel, StringVector, strVector);
159     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
160     PARCEL_WRITE_HELPER_NORET(dataParcel, String, str);
161     DistributedSchedService::GetInstance().StopAbilityFromRemoteInner(dataParcel, reply);
162 }
163 }
164 }
165 
166 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)167 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
168 {
169     OHOS::DistributedSchedule::ConnectAbilityFromRemoteInnerFuzzTest(data, size);
170     OHOS::DistributedSchedule::DisconnectAbilityFromRemoteInnerFuzzTest(data, size);
171     OHOS::DistributedSchedule::NotifyProcessDiedFromRemoteInnerFuzzTest(data, size);
172     OHOS::DistributedSchedule::GetContinueInfoInnerFuzzTest(data, size);
173     OHOS::DistributedSchedule::StopAbilityFromRemoteInnerFuzzTest(data, size);
174     return 0;
175 }
176