1 /*
2 * Copyright (c) 2022 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 "appmgrservicefirst_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #include "app_mgr_service.h"
23 #undef private
24
25 #include "ability_record.h"
26 #include "parcel.h"
27 #include "securec.h"
28 #include "want.h"
29
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::AppExecFwk;
32
33 namespace OHOS {
34 namespace {
35 constexpr int INPUT_ZERO = 0;
36 constexpr int INPUT_ONE = 1;
37 constexpr int INPUT_THREE = 3;
38 constexpr size_t U32_AT_SIZE = 4;
39 constexpr uint8_t ENABLE = 2;
40 constexpr size_t OFFSET_ZERO = 24;
41 constexpr size_t OFFSET_ONE = 16;
42 constexpr size_t OFFSET_TWO = 8;
43 }
GetU32Data(const char * ptr)44 uint32_t GetU32Data(const char* ptr)
45 {
46 // convert fuzz input data to an integer
47 return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
48 ptr[INPUT_THREE];
49 }
GetFuzzAbilityToken()50 sptr<Token> GetFuzzAbilityToken()
51 {
52 sptr<Token> token = nullptr;
53 AbilityRequest abilityRequest;
54 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
55 abilityRequest.abilityInfo.name = "MainAbility";
56 abilityRequest.abilityInfo.type = AbilityType::PAGE;
57 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
58 if (abilityRecord) {
59 token = abilityRecord->GetToken();
60 }
61 return token;
62 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)63 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
64 {
65 AppMgrService* appMgrService = new AppMgrService();
66 std::shared_ptr<AppMgrServiceInner> innerService = std::make_shared<AppMgrServiceInner>();
67 appMgrService->appMgrServiceState_.serviceRunningState = ServiceRunningState::STATE_NOT_START;
68 appMgrService->SetInnerService(innerService);
69 appMgrService->OnStart();
70 sptr<IConfigurationObserver> configurationObserver;
71 appMgrService->RegisterConfigurationObserver(configurationObserver);
72 appMgrService->UnregisterConfigurationObserver(configurationObserver);
73 sptr<IApplicationStateObserver> applicationStateObserver;
74 appMgrService->RegisterApplicationStateObserver(applicationStateObserver);
75 appMgrService->UnregisterApplicationStateObserver(applicationStateObserver);
76 sptr<INativeChildNotify> nativeChildNotify;
77 appMgrService->RegisterNativeChildExitNotify(nativeChildNotify);
78 appMgrService->UnregisterNativeChildExitNotify(nativeChildNotify);
79 pid_t pid = static_cast<pid_t>(GetU32Data(data));
80 appMgrService->AddAppDeathRecipient(pid);
81 appMgrService->QueryServiceState();
82 sptr<IRemoteObject> app = nullptr;
83 appMgrService->AttachApplication(app);
84 std::vector<BundleInfo> bundleInfos;
85 appMgrService->StartupResidentProcess(bundleInfos);
86 Parcel wantParcel;
87 Want* want = nullptr;
88 if (wantParcel.WriteBuffer(data, size)) {
89 want = Want::Unmarshalling(wantParcel);
90 }
91 std::string renderParam(data, size);
92 int32_t ipcFd = static_cast<int32_t>(GetU32Data(data));
93 int32_t sharedFd = static_cast<int32_t>(GetU32Data(data));
94 int32_t crashFd = static_cast<int32_t>(GetU32Data(data));
95 pid_t renderPid = static_cast<pid_t>(GetU32Data(data));
96 appMgrService->StartRenderProcess(renderParam, ipcFd, sharedFd, crashFd, renderPid);
97 appMgrService->PreStartNWebSpawnProcess();
98 sptr<IRemoteObject> scheduler = nullptr;
99 appMgrService->AttachRenderProcess(scheduler);
100 bool isContinuousTask = *data % ENABLE;
101 appMgrService->SetContinuousTaskProcess(static_cast<int32_t>(pid), isContinuousTask);
102 int32_t recordId = static_cast<int32_t>(GetU32Data(data));
103 appMgrService->ApplicationForegrounded(recordId);
104 appMgrService->AddAbilityStageDone(recordId);
105 int fd = static_cast<int>(GetU32Data(data));
106 std::vector<std::u16string> args;
107 appMgrService->Dump(fd, args);
108 std::string result(data, size);
109 appMgrService->Dump(args, result);
110 appMgrService->ShowHelp(args, result);
111 std::string flag(data, size);
112 appMgrService->ScheduleAcceptWantDone(recordId, *want, flag);
113 Configuration config;
114 appMgrService->UpdateConfiguration(config);
115 appMgrService->GetAmsMgr();
116 std::vector<RunningProcessInfo> info;
117 appMgrService->GetAllRunningProcesses(info);
118 int32_t userId = static_cast<int32_t>(GetU32Data(data));
119 appMgrService->GetProcessRunningInfosByUserId(info, userId);
120 int32_t level = static_cast<int32_t>(GetU32Data(data));
121 appMgrService->NotifyMemoryLevel(level);
122 std::vector<AppStateData> list;
123 appMgrService->GetForegroundApplications(list);
124 std::vector<sptr<IRemoteObject>> tokens;
125 appMgrService->GetAbilityRecordsByProcessID(static_cast<int>(pid), tokens);
126 int status = static_cast<int>(GetU32Data(data));
127 appMgrService->GetRenderProcessTerminationStatus(renderPid, status);
128 appMgrService->GetConfiguration(config);
129 std::string bundleName(data, size);
130 appMgrService->GetAppRunningStateByBundleName(bundleName);
131 sptr<IQuickFixCallback> callback;
132 appMgrService->NotifyLoadRepairPatch(bundleName, callback);
133 appMgrService->NotifyHotReloadPage(bundleName, callback);
134 appMgrService->NotifyUnLoadRepairPatch(bundleName, callback);
135 appMgrService->ApplicationBackgrounded(recordId);
136 sptr<IRemoteObject> token = GetFuzzAbilityToken();
137 appMgrService->AbilityCleaned(token);
138 int32_t appCloneIndex = static_cast<int32_t>(GetU32Data(data));
139 appMgrService->ClearUpApplicationData(bundleName, appCloneIndex);
140 appMgrService->ApplicationTerminated(recordId);
141 std::string msg(data, size);
142 int64_t resultCode = static_cast<int64_t>(GetU32Data(data));
143 appMgrService->FinishUserTest(msg, resultCode, bundleName);
144 appMgrService->OnStop();
145 return appMgrService->IsReady();
146 }
147 }
148
149 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)150 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
151 {
152 /* Run your code on data */
153 if (data == nullptr) {
154 std::cout << "invalid data" << std::endl;
155 return 0;
156 }
157
158 /* Validate the length of size */
159 if (size < OHOS::U32_AT_SIZE) {
160 return 0;
161 }
162
163 char* ch = static_cast<char*>(malloc(size + 1));
164 if (ch == nullptr) {
165 std::cout << "malloc failed." << std::endl;
166 return 0;
167 }
168
169 (void)memset_s(ch, size + 1, 0x00, size + 1);
170 if (memcpy_s(ch, size, data, size) != EOK) {
171 std::cout << "copy failed." << std::endl;
172 free(ch);
173 ch = nullptr;
174 return 0;
175 }
176
177 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
178 free(ch);
179 ch = nullptr;
180 return 0;
181 }
182