1 /*
2 * Copyright (c) 2023 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 "bundleactiveobserver_fuzzer.h"
17
18 #include "accesstoken_kit.h"
19 #include "app_mgr_interface.h"
20
21 #include "system_ability_definition.h"
22 #include "iservice_registry.h"
23 #include "bundle_active_service.h"
24 #include "iapp_group_callback.h"
25 #include "bundle_active_config_reader.h"
26
27 namespace OHOS {
28 namespace DeviceUsageStats {
29 constexpr uint32_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 uint8_t TWO = 2;
34 constexpr uint8_t THREE = 3;
35 bool g_isInited = false;
36
GetU32Data(const char * ptr)37 uint32_t GetU32Data(const char* ptr)
38 {
39 return (ptr[0] << TWENTYFOUR) | (ptr[1] << SIXTEEN) | (ptr[TWO] << EIGHT) | (ptr[THREE]);
40 }
41
DoSomethingInterestingWithMyAPI(const char * data,size_t size)42 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
43 {
44 DelayedSingleton<FuzztestHelper>::GetInstance()->NativeTokenGet();
45 if (!g_isInited) {
46 DelayedSingleton<BundleActiveService>::GetInstance()->InitService();
47 g_isInited = true;
48 }
49 sptr<IAppGroupCallback> appGroupCallback = nullptr;
50 DelayedSingleton<BundleActiveService>::GetInstance()->RegisterAppGroupCallBack(appGroupCallback);
51 DelayedSingleton<BundleActiveService>::GetInstance()->UnRegisterAppGroupCallBack(appGroupCallback);
52 bool result = false;
53 int32_t userId = static_cast<int32_t>(GetU32Data(data));
54 std::string inputBundlName(data);
55 DelayedSingleton<BundleActiveService>::GetInstance()->IsBundleIdle(result, inputBundlName, userId);
56 DelayedSingleton<BundleActiveService>::GetInstance()->IsBundleUsePeriod(result, inputBundlName, userId);
57 DelayedSingleton<BundleActiveConfigReader>::GetInstance()->GetApplicationUsePeriodicallyConfig();
58 return true;
59 }
60
BundleActiveServiceDumpFuzzTest(const char * data,size_t size)61 bool BundleActiveServiceDumpFuzzTest(const char* data, size_t size)
62 {
63 int32_t fd = static_cast<int32_t>(GetU32Data(data));
64 std::vector<std::u16string> args;
65 args = {to_utf16(std::to_string(GetU32Data(data)))};
66 DelayedSingleton<BundleActiveService>::GetInstance()->Dump(fd, args);
67 DelayedSingleton<BundleActiveService>::GetInstance()->AllowDump();
68
69 std::vector<std::string> dumpOption = {
70 std::to_string(GetU32Data(data)), std::to_string(GetU32Data(data)),
71 std::to_string(GetU32Data(data)), std::to_string(GetU32Data(data)),
72 std::to_string(GetU32Data(data)), std::to_string(GetU32Data(data))};
73 std::vector<std::string> dumpInfo;
74 DelayedSingleton<BundleActiveService>::GetInstance()->ShellDump(dumpOption, dumpInfo);
75 dumpInfo.clear();
76 DelayedSingleton<BundleActiveService>::GetInstance()->DumpEvents(dumpOption, dumpInfo);
77 dumpInfo.clear();
78 DelayedSingleton<BundleActiveService>::GetInstance()->DumpPackageUsage(dumpOption, dumpInfo);
79 dumpInfo.clear();
80 DelayedSingleton<BundleActiveService>::GetInstance()->DumpModuleUsage(dumpOption, dumpInfo);
81 std::string result;
82 DelayedSingleton<BundleActiveService>::GetInstance()->DumpUsage(result);
83 return true;
84 }
85 } // namespace DeviceUsageStats
86 } // namespace OHOS
87
88 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
90 {
91 /* Run your code on data */
92 if (data == nullptr) {
93 return 0;
94 }
95
96 if (size < OHOS::DeviceUsageStats::U32_AT_SIZE) {
97 return 0;
98 }
99 char* ch = (char *)malloc(size + 1);
100 if (ch == nullptr) {
101 return 0;
102 }
103
104 (void)memset_s(ch, size + 1, 0x00, size + 1);
105 if (memcpy_s(ch, size, data, size) != EOK) {
106 free(ch);
107 ch = nullptr;
108 return 0;
109 }
110
111 OHOS::DeviceUsageStats::DoSomethingInterestingWithMyAPI(ch, size);
112 OHOS::DeviceUsageStats::BundleActiveServiceDumpFuzzTest(ch, size);
113 free(ch);
114 ch = nullptr;
115 return 0;
116 }
117
118