1 /*
2 * Copyright (c) 2024 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 "workschedulerstopwork_fuzzer.h"
17
18 #include "iwork_sched_service.h"
19 #include "work_scheduler_service.h"
20 #include "work_sched_common.h"
21 #include "work_condition.h"
22 #include "work_queue_manager.h"
23 #include "work_policy_manager.h"
24 #include "work_standby_state_change_callback.h"
25 #include "work_bundle_group_change_callback.h"
26 #include "work_conn_manager.h"
27
DecStrongRef(void const * obj)28 void OHOS::RefBase::DecStrongRef(void const* obj) {}
29
30 namespace OHOS {
31 namespace WorkScheduler {
32 const std::u16string WORK_SCHEDULER_STUB_TOKEN = u"ohos.workscheduler.iworkschedservice";
33 static std::shared_ptr<WorkSchedulerService> workSchedulerService_;
34
GetUidByBundleName(const std::string & bundleName,int32_t & uid)35 bool WorkSchedulerService::GetUidByBundleName(const std::string &bundleName, int32_t &uid)
36 {
37 return true;
38 }
39
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)40 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
41 {
42 MessageParcel dataMessageParcel;
43 dataMessageParcel.WriteInterfaceToken(WORK_SCHEDULER_STUB_TOKEN);
44 dataMessageParcel.WriteBuffer(data, size);
45 dataMessageParcel.RewindRead(0);
46 MessageParcel reply;
47 MessageOption option;
48 workSchedulerService_ = DelayedSingleton<WorkSchedulerService>::GetInstance();
49 uint32_t code = static_cast<int32_t>(IWorkSchedServiceIpcCode::COMMAND_STOP_WORK);
50 WorkInfo workInfo = WorkInfo();
51 int32_t workId = 1;
52 workInfo.SetWorkId(workId);
53 workInfo.SetElement("bundle_name", "ability_name");
54 workInfo.RequestPersisted(true);
55 workInfo.RequestStorageLevel(WorkCondition::Storage::STORAGE_LEVEL_LOW_OR_OKAY);
56 if (!dataMessageParcel.WriteParcelable(&workInfo)) {
57 return false;
58 }
59 workSchedulerService_->OnStart();
60 workSchedulerService_->InitBgTaskSubscriber();
61 if (!workSchedulerService_->ready_) {
62 workSchedulerService_->ready_ = true;
63 }
64 if (workSchedulerService_->workQueueManager_ == nullptr) {
65 workSchedulerService_->workQueueManager_ = std::make_shared<WorkQueueManager>(workSchedulerService_);
66 }
67 if (workSchedulerService_->workPolicyManager_ == nullptr) {
68 workSchedulerService_->workPolicyManager_ = std::make_shared<WorkPolicyManager>(workSchedulerService_);
69 workSchedulerService_->workPolicyManager_->Init(workSchedulerService_->eventRunner_);
70 }
71 if (workSchedulerService_->workPolicyManager_->workConnManager_ == nullptr) {
72 workSchedulerService_->workPolicyManager_->workConnManager_ = std::make_shared<WorkConnManager>();
73 }
74 if (workSchedulerService_->standbyStateObserver_ == nullptr) {
75 workSchedulerService_->standbyStateObserver_ =
76 new (std::nothrow) WorkStandbyStateChangeCallback(workSchedulerService_->workQueueManager_);
77 }
78 if (workSchedulerService_->groupObserver_ == nullptr) {
79 workSchedulerService_->groupObserver_ =
80 new (std::nothrow) WorkBundleGroupChangeCallback(workSchedulerService_->workQueueManager_);
81 }
82 workSchedulerService_->StartWork(workInfo);
83 if (workSchedulerService_->checkBundle_) {
84 workSchedulerService_->checkBundle_ = false;
85 }
86 workSchedulerService_->OnRemoteRequest(code, dataMessageParcel, reply, option);
87 workSchedulerService_->OnStop();
88 return true;
89 }
90 } // WorkScheduler
91 } // OHOS
92
93 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)94 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
95 {
96 /* Run your code on data */
97 OHOS::WorkScheduler::DoSomethingInterestingWithMyAPI(data, size);
98 return 0;
99 }