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 "appmgrserviceinnersecond_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #include "app_mgr_service_inner.h"
23 #undef private
24
25 #include "ability_record.h"
26 #include "parcel.h"
27 #include "securec.h"
28
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AppExecFwk;
31
32 namespace OHOS {
33 namespace {
34 constexpr size_t FOO_MAX_LEN = 1024;
35 constexpr size_t U32_AT_SIZE = 4;
36 constexpr uint8_t ENABLE = 2;
37 constexpr size_t OFFSET_ZERO = 24;
38 constexpr size_t OFFSET_ONE = 16;
39 constexpr size_t OFFSET_TWO = 8;
40 }
GetU32Data(const char * ptr)41 uint32_t GetU32Data(const char* ptr)
42 {
43 // convert fuzz input data to an integer
44 return (ptr[0] << OFFSET_ZERO) | (ptr[1] << OFFSET_ONE) | (ptr[2] << OFFSET_TWO) | ptr[3];
45 }
GetFuzzAbilityToken()46 sptr<Token> GetFuzzAbilityToken()
47 {
48 sptr<Token> token = nullptr;
49 AbilityRequest abilityRequest;
50 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
51 abilityRequest.abilityInfo.name = "MainAbility";
52 abilityRequest.abilityInfo.type = AbilityType::PAGE;
53 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
54 if (abilityRecord) {
55 token = abilityRecord->GetToken();
56 }
57 return token;
58 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)59 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
60 {
61 AppMgrServiceInner* appMgrServiceInner = new AppMgrServiceInner();
62 sptr<IApplicationStateObserver> observer;
63 std::vector<std::string> bundleNameList;
64 appMgrServiceInner->RegisterApplicationStateObserver(observer, bundleNameList);
65 appMgrServiceInner->UnregisterApplicationStateObserver(observer);
66 std::vector<AppStateData> list;
67 appMgrServiceInner->GetForegroundApplications(list);
68 Parcel wantParcel;
69 Want* want = nullptr;
70 if (wantParcel.WriteBuffer(data, size)) {
71 want = Want::Unmarshalling(wantParcel);
72 }
73 sptr<IRemoteObject> token = GetFuzzAbilityToken();
74 BundleInfo bundleInfo;
75 int32_t userId = static_cast<int32_t>(GetU32Data(data));
76 appMgrServiceInner->StartUserTestProcess(*want, token, bundleInfo, userId);
77 HapModuleInfo hapModuleInfo;
78 appMgrServiceInner->GetHapModuleInfoForTestRunner(*want, token, bundleInfo, hapModuleInfo);
79 std::string msg(data, size);
80 std::string processName(data, size);
81 appMgrServiceInner->StartEmptyProcess(*want, token, bundleInfo, processName, userId);
82 int64_t resultCode = static_cast<int64_t>(GetU32Data(data));
83 std::string bundleName(data, size);
84 pid_t pid = static_cast<pid_t>(GetU32Data(data));
85 appMgrServiceInner->FinishUserTest(msg, resultCode, bundleName, pid);
86 std::shared_ptr<AppRunningRecord> appRecord;
87 appMgrServiceInner->FinishUserTestLocked(msg, resultCode, appRecord);
88 AbilityInfo abilityInfo;
89 appMgrServiceInner->StartSpecifiedAbility(*want, abilityInfo);
90 sptr<IStartSpecifiedAbilityResponse> response;
91 appMgrServiceInner->RegisterStartSpecifiedAbilityResponse(response);
92 int32_t recordId = static_cast<int32_t>(GetU32Data(data));
93 std::string flag(data, size);
94 appMgrServiceInner->ScheduleAcceptWantDone(recordId, *want, flag);
95 int64_t eventId = static_cast<int64_t>(GetU32Data(data));
96 appMgrServiceInner->HandleStartSpecifiedAbilityTimeOut(eventId);
97 Configuration config;
98 appMgrServiceInner->UpdateConfiguration(config);
99 sptr<IConfigurationObserver> configurationObserver;
100 appMgrServiceInner->RegisterConfigurationObserver(configurationObserver);
101 appMgrServiceInner->UnregisterConfigurationObserver(configurationObserver);
102 appMgrServiceInner->InitGlobalConfiguration();
103 appMgrServiceInner->GetConfiguration();
104 appMgrServiceInner->KillApplicationByRecord(appRecord);
105 int32_t innerEventId = static_cast<int32_t>(GetU32Data(data));
106 appMgrServiceInner->SendHiSysEvent(innerEventId, eventId);
107 std::vector<sptr<IRemoteObject>> tokens;
108 appMgrServiceInner->GetAbilityRecordsByProcessID(static_cast<int>(pid), tokens);
109 ApplicationInfo application;
110 bool debug;
111 appMgrServiceInner->GetApplicationInfoByProcessID(static_cast<int>(pid), application, debug);
112 appMgrServiceInner->VerifyProcessPermission("");
113 appMgrServiceInner->VerifyAPL();
114 std::string permissionName(data, size);
115 appMgrServiceInner->VerifyAccountPermission(permissionName, static_cast<int>(userId));
116 pid_t hostPid = static_cast<pid_t>(GetU32Data(data));
117 appMgrServiceInner->PreStartNWebSpawnProcess(hostPid);
118 std::string renderParam(data, size);
119 int32_t ipcFd = static_cast<int32_t>(GetU32Data(data));
120 int32_t sharedFd = static_cast<int32_t>(GetU32Data(data));
121 int32_t crashFd = static_cast<int32_t>(GetU32Data(data));
122 pid_t renderPid = static_cast<pid_t>(GetU32Data(data));
123 appMgrServiceInner->StartRenderProcess(hostPid, renderParam, ipcFd,
124 sharedFd, crashFd, renderPid);
125 sptr<IRenderScheduler> scheduler;
126 appMgrServiceInner->AttachRenderProcess(pid, scheduler);
127 std::shared_ptr<RenderRecord> renderRecord;
128 appMgrServiceInner->StartRenderProcessImpl(renderRecord, appRecord, renderPid);
129 int status = static_cast<int>(GetU32Data(data));
130 appMgrServiceInner->GetRenderProcessTerminationStatus(renderPid, status);
131 wptr<IRemoteObject> remote;
132 appMgrServiceInner->OnRenderRemoteDied(remote);
133 appMgrServiceInner->BuildStartFlags(*want, abilityInfo);
134 sptr<OHOS::Rosen::FocusChangeInfo> focusChangeInfo;
135 appMgrServiceInner->HandleFocused(focusChangeInfo);
136 appMgrServiceInner->HandleUnfocused(focusChangeInfo);
137 sptr<IQuickFixCallback> callback;
138 appMgrServiceInner->NotifyLoadRepairPatch(bundleName, callback);
139 appMgrServiceInner->NotifyHotReloadPage(bundleName, callback);
140 bool isContinuousTask = *data % ENABLE;
141 appMgrServiceInner->SetContinuousTaskProcess(static_cast<int32_t>(pid), isContinuousTask);
142 appMgrServiceInner->NotifyUnLoadRepairPatch(bundleName, callback);
143 return appMgrServiceInner->GetAppRunningStateByBundleName(bundleName);
144 }
145 }
146
147 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)148 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
149 {
150 /* Run your code on data */
151 if (data == nullptr) {
152 std::cout << "invalid data" << std::endl;
153 return 0;
154 }
155
156 /* Validate the length of size */
157 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
158 return 0;
159 }
160
161 char* ch = (char*)malloc(size + 1);
162 if (ch == nullptr) {
163 std::cout << "malloc failed." << std::endl;
164 return 0;
165 }
166
167 (void)memset_s(ch, size + 1, 0x00, size + 1);
168 if (memcpy_s(ch, size, data, size) != EOK) {
169 std::cout << "copy failed." << std::endl;
170 free(ch);
171 ch = nullptr;
172 return 0;
173 }
174
175 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
176 free(ch);
177 ch = nullptr;
178 return 0;
179 }
180