• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         DelayedSingleton<BundleActiveConfigReader>::GetInstance()->GetAppHighFrequencyPeriodThresholdConfig();
59         DelayedSingleton<BundleActiveConfigReader>::GetInstance()->LoadConfigFile(inputBundlName.c_str());
60         return true;
61     }
62 
BundleActiveServiceDumpFuzzTest(const char * data,size_t size)63     bool BundleActiveServiceDumpFuzzTest(const char* data, size_t size)
64     {
65         int32_t fd = static_cast<int32_t>(GetU32Data(data));
66         std::vector<std::u16string> args;
67         args = {to_utf16(std::to_string(GetU32Data(data)))};
68         DelayedSingleton<BundleActiveService>::GetInstance()->Dump(fd, args);
69         DelayedSingleton<BundleActiveService>::GetInstance()->AllowDump();
70 
71         std::vector<std::string> dumpOption = {
72             std::to_string(GetU32Data(data)), std::to_string(GetU32Data(data)),
73             std::to_string(GetU32Data(data)), std::to_string(GetU32Data(data)),
74             std::to_string(GetU32Data(data)), std::to_string(GetU32Data(data))};
75         std::vector<std::string> dumpInfo;
76         DelayedSingleton<BundleActiveService>::GetInstance()->ShellDump(dumpOption, dumpInfo);
77         dumpInfo.clear();
78         DelayedSingleton<BundleActiveService>::GetInstance()->DumpEvents(dumpOption, dumpInfo);
79         dumpInfo.clear();
80         DelayedSingleton<BundleActiveService>::GetInstance()->DumpPackageUsage(dumpOption, dumpInfo);
81         dumpInfo.clear();
82         DelayedSingleton<BundleActiveService>::GetInstance()->DumpModuleUsage(dumpOption, dumpInfo);
83         dumpInfo.clear();
84         DelayedSingleton<BundleActiveService>::GetInstance()->DumpHighFreqHourUsage(dumpOption, dumpInfo);
85         std::string result;
86         DelayedSingleton<BundleActiveService>::GetInstance()->DumpUsage(result);
87         return true;
88     }
89 } // namespace DeviceUsageStats
90 } // namespace OHOS
91 
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
94 {
95     /* Run your code on data */
96     if (data == nullptr) {
97         return 0;
98     }
99 
100     if (size < OHOS::DeviceUsageStats::U32_AT_SIZE) {
101         return 0;
102     }
103     char* ch = (char *)malloc(size + 1);
104     if (ch == nullptr) {
105         return 0;
106     }
107 
108     (void)memset_s(ch, size + 1, 0x00, size + 1);
109     if (memcpy_s(ch, size, data, size) != EOK) {
110         free(ch);
111         ch = nullptr;
112         return 0;
113     }
114 
115     OHOS::DeviceUsageStats::DoSomethingInterestingWithMyAPI(ch, size);
116     OHOS::DeviceUsageStats::BundleActiveServiceDumpFuzzTest(ch, size);
117     free(ch);
118     ch = nullptr;
119     return 0;
120 }
121 
122