• 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 "distributedschedservicethree_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 int32_t ON_DEMAND_REASON_ID_COUNT = 7;
38 
39 std::string GetDExtensionName(std::string bundleName, int32_t userId);
40 std::string GetDExtensionProcess(std::string bundleName, int32_t userId);
41 
OnStartFuzzTest(const uint8_t * data,size_t size)42 void OnStartFuzzTest(const uint8_t* data, size_t size)
43 {
44     if ((data == nullptr) || (size < sizeof(int32_t) + sizeof(int32_t))) {
45         return;
46     }
47     FuzzedDataProvider fdp(data, size);
48     std::string reasonName = fdp.ConsumeRandomLengthString();
49     std::string reasonValue = fdp.ConsumeRandomLengthString();
50     int32_t enumIdx = fdp.ConsumeIntegral<int32_t>() % ON_DEMAND_REASON_ID_COUNT;
51     OnDemandReasonId reasonId = static_cast<OnDemandReasonId>(enumIdx);
52     int32_t extraDataId = fdp.ConsumeIntegral<int32_t>();
53     SystemAbilityOnDemandReason reason(reasonId, reasonName, reasonValue, extraDataId);
54     DistributedSchedService::GetInstance().OnStart(reason);
55 }
56 
OnAddSystemAbilityFuzzTest(const uint8_t * data,size_t size)57 void OnAddSystemAbilityFuzzTest(const uint8_t* data, size_t size)
58 {
59     if ((data == nullptr) || (size < sizeof(int32_t))) {
60         return;
61     }
62     FuzzedDataProvider fdp(data, size);
63     int32_t systemAbilityId = fdp.ConsumeIntegral<int32_t>();
64     std::string deviceId = fdp.ConsumeRandomLengthString();
65     DistributedSchedService::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
66 }
67 
ContinueStateCallbackUnRegisterFuzzTest(const uint8_t * data,size_t size)68 void ContinueStateCallbackUnRegisterFuzzTest(const uint8_t* data, size_t size)
69 {
70     if ((data == nullptr) || (size < sizeof(int32_t))) {
71         return;
72     }
73     FuzzedDataProvider fdp(data, size);
74     int32_t missionId = fdp.ConsumeIntegral<int32_t>();
75     std::string bundleName = fdp.ConsumeRandomLengthString();
76     std::string moduleName = fdp.ConsumeRandomLengthString();
77     std::string abilityName = fdp.ConsumeRandomLengthString();
78     DistributedSchedService::GetInstance().ContinueStateCallbackUnRegister(missionId, bundleName,
79         moduleName, abilityName);
80 }
81 
ContinueMissionFuzzTest(const uint8_t * data,size_t size)82 void ContinueMissionFuzzTest(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     std::string srcDeviceId = fdp.ConsumeRandomLengthString();
89     std::string dstDeviceId = fdp.ConsumeRandomLengthString();
90     int32_t missionId = fdp.ConsumeIntegral<int32_t>();
91     sptr<IRemoteObject> callback = nullptr;
92     OHOS::AAFwk::WantParams wantParams;
93     DistributedSchedService::GetInstance().ContinueMission(srcDeviceId, dstDeviceId, missionId, callback, wantParams);
94 }
95 }
96 }
97 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)98 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
99 {
100     OHOS::DistributedSchedule::OnStartFuzzTest(data, size);
101     OHOS::DistributedSchedule::OnAddSystemAbilityFuzzTest(data, size);
102     OHOS::DistributedSchedule::ContinueStateCallbackUnRegisterFuzzTest(data, size);
103     OHOS::DistributedSchedule::ContinueMissionFuzzTest(data, size);
104     return 0;
105 }
106