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 pid_t renderPid = static_cast<pid_t>(GetU32Data(data));
122 appMgrServiceInner->StartRenderProcess(hostPid, renderParam, ipcFd, sharedFd, renderPid);
123 sptr<IRenderScheduler> scheduler;
124 appMgrServiceInner->AttachRenderProcess(pid, scheduler);
125 std::shared_ptr<RenderRecord> renderRecord;
126 appMgrServiceInner->StartRenderProcessImpl(renderRecord, appRecord, renderPid);
127 int status = static_cast<int>(GetU32Data(data));
128 appMgrServiceInner->GetRenderProcessTerminationStatus(renderPid, status);
129 wptr<IRemoteObject> remote;
130 appMgrServiceInner->OnRenderRemoteDied(remote);
131 appMgrServiceInner->BuildStartFlags(*want, abilityInfo);
132 sptr<OHOS::Rosen::FocusChangeInfo> focusChangeInfo;
133 appMgrServiceInner->HandleFocused(focusChangeInfo);
134 appMgrServiceInner->HandleUnfocused(focusChangeInfo);
135 sptr<IQuickFixCallback> callback;
136 appMgrServiceInner->NotifyLoadRepairPatch(bundleName, callback);
137 appMgrServiceInner->NotifyHotReloadPage(bundleName, callback);
138 bool isContinuousTask = *data % ENABLE;
139 appMgrServiceInner->SetContinuousTaskProcess(static_cast<int32_t>(pid), isContinuousTask);
140 appMgrServiceInner->NotifyUnLoadRepairPatch(bundleName, callback);
141 return appMgrServiceInner->GetAppRunningStateByBundleName(bundleName);
142 }
143 }
144
145 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)146 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
147 {
148 /* Run your code on data */
149 if (data == nullptr) {
150 std::cout << "invalid data" << std::endl;
151 return 0;
152 }
153
154 /* Validate the length of size */
155 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
156 return 0;
157 }
158
159 char* ch = (char *)malloc(size + 1);
160 if (ch == nullptr) {
161 std::cout << "malloc failed." << std::endl;
162 return 0;
163 }
164
165 (void)memset_s(ch, size + 1, 0x00, size + 1);
166 if (memcpy_s(ch, size, data, size) != EOK) {
167 std::cout << "copy failed." << std::endl;
168 free(ch);
169 ch = nullptr;
170 return 0;
171 }
172
173 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
174 free(ch);
175 ch = nullptr;
176 return 0;
177 }
178