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 "bundleactivepowerstatecallbackproxy_fuzzer.h"
17 #include "accesstoken_kit.h"
18 #include "app_mgr_interface.h"
19
20 #include <securec.h>
21 #include "bundle_active_power_state_callback_service.h"
22 #include "power_state_callback_ipc_interface_code.h"
23 #include "power_state_machine_info.h"
24 #include "bundle_active_open_callback.h"
25 #include "bundle_active_service.h"
26 #include "bundle_active_client.h"
27 #include "bundle_active_stats_combiner.h"
28 #include "bundle_active_event_list.h"
29 #include "bundle_active_core.h"
30
31 #ifndef errno_t
32 typedef int errno_t;
33 #endif
34
35 #ifndef EOK
36 #define EOK 0
37 #endif
38
39 namespace OHOS {
40 namespace DeviceUsageStats {
41 static std::string g_defaultBundleName = "com.ohos.camera";
42 static std::string g_defaultMoudleName = "defaultmoudlename";
43 static std::string g_defaultFormName = "defaultformname";
44 static int32_t DEFAULT_DIMENSION = 4;
45 static int64_t DEFAULT_FORMID = 1;
46 constexpr uint32_t U32_AT_SIZE = 4;
47
48 const uint8_t* g_data = nullptr;
49 size_t g_size = 0;
50 size_t g_pos;
51
52 template<class T>
GetData()53 T GetData()
54 {
55 T object {};
56 size_t objectSize = sizeof(object);
57 if (g_data == nullptr || objectSize > g_size - g_pos) {
58 return object;
59 }
60 errno_t ret = memcpy_s(&object, objectSize, g_data + g_pos, objectSize);
61 if (ret != EOK) {
62 return {};
63 }
64 g_pos += objectSize;
65 return object;
66 }
67
GetStringFromData(int strlen)68 std::string GetStringFromData(int strlen)
69 {
70 if (strlen <= 0) {
71 return "";
72 }
73 char cstr[strlen];
74 cstr[strlen - 1] = '\0';
75 for (int i = 0; i < strlen - 1; i++) {
76 char tmp = GetData<char>();
77 if (tmp == '\0') {
78 tmp = '1';
79 }
80 cstr[i] = tmp;
81 }
82 std::string str(cstr);
83 return str;
84 }
85
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)86 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
87 {
88 g_data = data;
89 g_size = size;
90 g_pos = 0;
91 uint32_t code = GetData<uint32_t>();
92 PowerMgr::PowerState state = static_cast<PowerMgr::PowerState>(code);
93 auto bundleActiveCore = std::make_shared<BundleActiveCore>();
94 BundleActivePowerStateCallbackService BundleActivePowerStateCallbackService(bundleActiveCore);
95 BundleActivePowerStateCallbackService.OnPowerStateChanged(state);
96 return true;
97 }
98
BundleActiveClientFuzzTest(const uint8_t * data,size_t size)99 bool BundleActiveClientFuzzTest(const uint8_t* data, size_t size)
100 {
101 g_data = data;
102 g_size = size;
103 g_pos = 0;
104 bool result = false;
105 int32_t userId = GetData<int32_t>();
106 std::string inputBundleName = GetStringFromData(size);
107 sptr<IAppGroupCallback> appGroupCallback = nullptr;
108 int32_t intervalType = GetData<int32_t>();
109 int64_t beginTime = GetData<int64_t>();
110 int64_t endTime = GetData<int64_t>();
111
112 DelayedSingleton<BundleActiveClient>::GetInstance()->GetBundleActiveProxy();
113 DelayedSingleton<BundleActiveClient>::GetInstance()->RegisterAppGroupCallBack(appGroupCallback);
114 DelayedSingleton<BundleActiveClient>::GetInstance()->UnRegisterAppGroupCallBack(appGroupCallback);
115 DelayedSingleton<BundleActiveClient>::GetInstance()->IsBundleIdle(result, inputBundleName, userId);
116 BundleActiveEvent event(g_defaultBundleName, g_defaultMoudleName, g_defaultFormName,
117 DEFAULT_DIMENSION, DEFAULT_FORMID, BundleActiveEvent::FORM_IS_CLICKED);
118 DelayedSingleton<BundleActiveClient>::GetInstance()->ReportEvent(event, userId);
119
120 std::vector<BundleActivePackageStats> packageStats;
121 DelayedSingleton<BundleActiveClient>::GetInstance()->QueryBundleStatsInfoByInterval(packageStats,
122 intervalType, beginTime, endTime, userId);
123
124 std::vector<BundleActiveEvent> bundleActiveEvent;
125 DelayedSingleton<BundleActiveClient>::GetInstance()->QueryBundleEvents(bundleActiveEvent,
126 beginTime, endTime, userId);
127
128 int32_t newGroup = GetData<int32_t>();
129 std::string bundleName = GetStringFromData(size);
130 DelayedSingleton<BundleActiveClient>::GetInstance()->SetAppGroup(bundleName, newGroup, userId);
131
132 std::vector<BundleActiveEventStats> eventStats;
133 DelayedSingleton<BundleActiveClient>::GetInstance()->QueryDeviceEventStats(beginTime, endTime,
134 eventStats, userId);
135 DelayedSingleton<BundleActiveClient>::GetInstance()->QueryNotificationEventStats(beginTime, endTime,
136 eventStats, userId);
137 return true;
138 }
139
BundleActiveEventListFuzzTest(const uint8_t * data,size_t size)140 bool BundleActiveEventListFuzzTest(const uint8_t* data, size_t size)
141 {
142 g_data = data;
143 g_size = size;
144 g_pos = 0;
145 BundleActiveEventList right;
146 int64_t resultData = GetData<int64_t>();
147 auto combiner = std::make_shared<BundleActiveEventList>();
148 BundleActiveEvent event;
149 event.bundleName_ = GetStringFromData(size);
150 event.continuousTaskAbilityName_ = GetStringFromData(size);
151 event.timeStamp_ = GetData<int64_t>();
152
153 combiner->Size();
154 combiner->FindBestIndex(resultData);
155 combiner->Insert(event);
156 combiner->Merge(right);
157 combiner->Clear();
158 return true;
159 }
160
BundleActiveStatsCombinerFuzzTest(const uint8_t * data,size_t size)161 bool BundleActiveStatsCombinerFuzzTest(const uint8_t* data, size_t size)
162 {
163 g_data = data;
164 g_size = size;
165 g_pos = 0;
166 auto combiner = std::make_shared<BundleActiveStatsCombiner<BundleActivePackageStats>>();
167 auto stats = std::make_shared<BundleActivePeriodStats>();
168 auto packageStat = std::make_shared<BundleActivePackageStats>();
169 stats->bundleStats_.emplace("normal", packageStat);
170 packageStat = nullptr;
171 stats->bundleStats_.emplace("default", packageStat);
172 int64_t beginTime = GetData<int64_t>();
173 std::vector<BundleActivePackageStats> accumulatedResult;
174 combiner->combine(stats, accumulatedResult, beginTime);
175
176 auto eventCombiner = std::make_shared<BundleActiveStatsCombiner<BundleActiveEvent>>();
177 std::vector<BundleActiveEvent> activeEventResult;
178 eventCombiner->combine(stats, activeEventResult, beginTime);
179 return true;
180 }
181 } // namespace DeviceUsageStats
182 } // namespace OHOS
183
184 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)185 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
186 {
187 /* Run your code on data */
188 if (data == nullptr) {
189 return 0;
190 }
191
192 if (size < OHOS::DeviceUsageStats::U32_AT_SIZE) {
193 return 0;
194 }
195
196 OHOS::DeviceUsageStats::DoSomethingInterestingWithMyAPI(data, size);
197 OHOS::DeviceUsageStats::BundleActiveClientFuzzTest(data, size);
198 OHOS::DeviceUsageStats::BundleActiveEventListFuzzTest(data, size);
199 OHOS::DeviceUsageStats::BundleActiveStatsCombinerFuzzTest(data, size);
200 return 0;
201 }