• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FOO_MAX_LEN = 1024;
39 constexpr size_t U32_AT_SIZE = 4;
40 constexpr uint8_t ENABLE = 2;
41 constexpr size_t OFFSET_ZERO = 24;
42 constexpr size_t OFFSET_ONE = 16;
43 constexpr size_t OFFSET_TWO = 8;
44 }
GetU32Data(const char * ptr)45 uint32_t GetU32Data(const char* ptr)
46 {
47     // convert fuzz input data to an integer
48     return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
49         ptr[INPUT_THREE];
50 }
GetFuzzAbilityToken()51 sptr<Token> GetFuzzAbilityToken()
52 {
53     sptr<Token> token = nullptr;
54     AbilityRequest abilityRequest;
55     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
56     abilityRequest.abilityInfo.name = "MainAbility";
57     abilityRequest.abilityInfo.type = AbilityType::PAGE;
58     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
59     if (abilityRecord) {
60         token = abilityRecord->GetToken();
61     }
62     return token;
63 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)64 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
65 {
66     AppMgrService* appMgrService = new AppMgrService();
67     std::shared_ptr<AppMgrServiceInner> innerService = std::make_shared<AppMgrServiceInner>();
68     appMgrService->appMgrServiceState_.serviceRunningState = ServiceRunningState::STATE_NOT_START;
69     appMgrService->SetInnerService(innerService);
70     appMgrService->OnStart();
71     sptr<IConfigurationObserver> configurationObserver;
72     appMgrService->RegisterConfigurationObserver(configurationObserver);
73     appMgrService->UnregisterConfigurationObserver(configurationObserver);
74     sptr<IApplicationStateObserver> applicationStateObserver;
75     appMgrService->RegisterApplicationStateObserver(applicationStateObserver);
76     appMgrService->UnregisterApplicationStateObserver(applicationStateObserver);
77     pid_t pid = static_cast<pid_t>(GetU32Data(data));
78     appMgrService->AddAppDeathRecipient(pid);
79     appMgrService->QueryServiceState();
80     int32_t recordId = static_cast<int32_t>(GetU32Data(data));
81     std::string permission(data, size);
82     appMgrService->CheckPermission(recordId, permission);
83     sptr<IRemoteObject> app = nullptr;
84     appMgrService->AttachApplication(app);
85     std::vector<BundleInfo> bundleInfos;
86     appMgrService->StartupResidentProcess(bundleInfos);
87     Parcel wantParcel;
88     Want* want = nullptr;
89     if (wantParcel.WriteBuffer(data, size)) {
90         want = Want::Unmarshalling(wantParcel);
91     }
92     std::string renderParam(data, size);
93     int32_t ipcFd = static_cast<int32_t>(GetU32Data(data));
94     int32_t sharedFd = static_cast<int32_t>(GetU32Data(data));
95     int32_t crashFd = static_cast<int32_t>(GetU32Data(data));
96     pid_t renderPid = static_cast<pid_t>(GetU32Data(data));
97     appMgrService->StartRenderProcess(renderParam, ipcFd, sharedFd, crashFd, renderPid);
98     appMgrService->PreStartNWebSpawnProcess();
99     sptr<IRemoteObject> scheduler = nullptr;
100     appMgrService->AttachRenderProcess(scheduler);
101     bool isContinuousTask = *data % ENABLE;
102     appMgrService->SetContinuousTaskProcess(static_cast<int32_t>(pid), isContinuousTask);
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(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 #ifdef ABILITY_COMMAND_FOR_TEST
136     appMgrService->BlockAppService();
137 #endif
138     appMgrService->ApplicationBackgrounded(recordId);
139     sptr<IRemoteObject> token = GetFuzzAbilityToken();
140     appMgrService->AbilityCleaned(token);
141     appMgrService->ClearUpApplicationData(bundleName);
142     appMgrService->ApplicationTerminated(recordId);
143     std::string msg(data, size);
144     int64_t resultCode = static_cast<int64_t>(GetU32Data(data));
145     appMgrService->FinishUserTest(msg, resultCode, bundleName);
146     appMgrService->OnStop();
147     return appMgrService->IsReady();
148 }
149 }
150 
151 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)152 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
153 {
154     /* Run your code on data */
155     if (data == nullptr) {
156         std::cout << "invalid data" << std::endl;
157         return 0;
158     }
159 
160     /* Validate the length of size */
161     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
162         return 0;
163     }
164 
165     char* ch = (char*)malloc(size + 1);
166     if (ch == nullptr) {
167         std::cout << "malloc failed." << std::endl;
168         return 0;
169     }
170 
171     (void)memset_s(ch, size + 1, 0x00, size + 1);
172     if (memcpy_s(ch, size, data, size) != EOK) {
173         std::cout << "copy failed." << std::endl;
174         free(ch);
175         ch = nullptr;
176         return 0;
177     }
178 
179     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
180     free(ch);
181     ch = nullptr;
182     return 0;
183 }
184