• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bgtasksystemeventobserver_fuzzer.h"
17 #include "securec.h"
18 
19 #include "background_task_mgr_service.h"
20 #include "common_event_data.h"
21 #include "event_info.h"
22 #include "event_handler.h"
23 #include "event_runner.h"
24 #include "system_event_observer.h"
25 
26 
27 namespace OHOS {
28 namespace BackgroundTaskMgr {
29     constexpr int32_t U32_AT_SIZE = 4;
30     constexpr uint8_t TWENTYFOUR = 24;
31     constexpr uint8_t SIXTEEN = 16;
32     constexpr uint8_t EIGHT = 8;
33     constexpr int32_t THREE = 3;
34     constexpr int32_t TWO = 2;
35 
GetU32Data(const char * ptr)36     uint32_t GetU32Data(const char* ptr)
37     {
38         return (ptr[0] << TWENTYFOUR) | (ptr[1] << SIXTEEN) | (ptr[TWO] << EIGHT) | (ptr[THREE]);
39     }
40 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)41     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
42     {
43         GetU32Data(data);
44         EventFwk::MatchingSkills matchingSkills;
45         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
46         EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
47         auto systemEventListener = std::make_shared<SystemEventObserver>(commonEventSubscribeInfo);
48         systemEventListener->Subscribe();
49         systemEventListener->Unsubscribe();
50 
51         EventFwk::CommonEventData eventData = EventFwk::CommonEventData();
52         systemEventListener->OnReceiveEvent(eventData);
53 
54         auto handler = std::make_shared<OHOS::AppExecFwk::EventHandler>(nullptr);
55         systemEventListener->SetEventHandler(handler);
56         systemEventListener->OnReceiveEventContinuousTask(eventData);
57         auto bgContinuousTaskMgr = std::make_shared<BgContinuousTaskMgr>();
58         systemEventListener->SetBgContinuousTaskMgr(bgContinuousTaskMgr);
59         systemEventListener->OnReceiveEventContinuousTask(eventData);
60         AAFwk::Want want = AAFwk::Want();
61         want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
62         eventData.SetWant(want);
63         systemEventListener->OnReceiveEventContinuousTask(eventData);
64         want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED);
65         eventData.SetWant(want);
66         systemEventListener->OnReceiveEventContinuousTask(eventData);
67         want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
68         eventData.SetWant(want);
69         systemEventListener->OnReceiveEventContinuousTask(eventData);
70 
71         EventFwk::CommonEventData eventData2 = EventFwk::CommonEventData();
72         AAFwk::Want want2 = AAFwk::Want();
73         want2.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
74         eventData2.SetWant(want2);
75         systemEventListener->OnReceiveEventEfficiencyRes(eventData2);
76         return true;
77     }
78 } // BackgroundTaskMgr
79 } // OHOS
80 
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84     /* Run your code on data */
85     if (data == nullptr) {
86         return 0;
87     }
88 
89     if (size < OHOS::BackgroundTaskMgr::U32_AT_SIZE) {
90         return 0;
91     }
92 
93     char* ch = static_cast<char *>(malloc(size + 1));
94     if (ch == nullptr) {
95         return 0;
96     }
97 
98     (void)memset_s(ch, size + 1, 0x00, size + 1);
99     if (memcpy_s(ch, size + 1, data, size) != EOK) {
100         free(ch);
101         ch = nullptr;
102         return 0;
103     }
104 
105     OHOS::BackgroundTaskMgr::DoSomethingInterestingWithMyAPI(ch, size);
106     free(ch);
107     ch = nullptr;
108     return 0;
109 }
110