• 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 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     pid_t pid = static_cast<pid_t>(GetU32Data(data));
77     appMgrService->AddAppDeathRecipient(pid);
78     appMgrService->QueryServiceState();
79     sptr<IRemoteObject> app = nullptr;
80     appMgrService->AttachApplication(app);
81     std::vector<BundleInfo> bundleInfos;
82     appMgrService->StartupResidentProcess(bundleInfos);
83     Parcel wantParcel;
84     Want* want = nullptr;
85     if (wantParcel.WriteBuffer(data, size)) {
86         want = Want::Unmarshalling(wantParcel);
87     }
88     std::string renderParam(data, size);
89     int32_t ipcFd = static_cast<int32_t>(GetU32Data(data));
90     int32_t sharedFd = static_cast<int32_t>(GetU32Data(data));
91     int32_t crashFd = static_cast<int32_t>(GetU32Data(data));
92     pid_t renderPid = static_cast<pid_t>(GetU32Data(data));
93     appMgrService->StartRenderProcess(renderParam, ipcFd, sharedFd, crashFd, renderPid);
94     appMgrService->PreStartNWebSpawnProcess();
95     sptr<IRemoteObject> scheduler = nullptr;
96     appMgrService->AttachRenderProcess(scheduler);
97     bool isContinuousTask = *data % ENABLE;
98     appMgrService->SetContinuousTaskProcess(static_cast<int32_t>(pid), isContinuousTask);
99     int32_t recordId = static_cast<int32_t>(GetU32Data(data));
100     appMgrService->ApplicationForegrounded(recordId);
101     appMgrService->AddAbilityStageDone(recordId);
102     int fd = static_cast<int>(GetU32Data(data));
103     std::vector<std::u16string> args;
104     appMgrService->Dump(fd, args);
105     std::string result(data, size);
106     appMgrService->Dump(args, result);
107     appMgrService->ShowHelp(args, result);
108     std::string flag(data, size);
109     appMgrService->ScheduleAcceptWantDone(recordId, *want, flag);
110     Configuration config;
111     appMgrService->UpdateConfiguration(config);
112     appMgrService->GetAmsMgr();
113     std::vector<RunningProcessInfo> info;
114     appMgrService->GetAllRunningProcesses(info);
115     int32_t userId = static_cast<int32_t>(GetU32Data(data));
116     appMgrService->GetProcessRunningInfosByUserId(info, userId);
117     int32_t level = static_cast<int32_t>(GetU32Data(data));
118     appMgrService->NotifyMemoryLevel(level);
119     std::vector<AppStateData> list;
120     appMgrService->GetForegroundApplications(list);
121     std::vector<sptr<IRemoteObject>> tokens;
122     appMgrService->GetAbilityRecordsByProcessID(static_cast<int>(pid), tokens);
123     int status = static_cast<int>(GetU32Data(data));
124     appMgrService->GetRenderProcessTerminationStatus(renderPid, status);
125     appMgrService->GetConfiguration(config);
126     std::string bundleName(data, size);
127     appMgrService->GetAppRunningStateByBundleName(bundleName);
128     sptr<IQuickFixCallback> callback;
129     appMgrService->NotifyLoadRepairPatch(bundleName, callback);
130     appMgrService->NotifyHotReloadPage(bundleName, callback);
131     appMgrService->NotifyUnLoadRepairPatch(bundleName, callback);
132     appMgrService->ApplicationBackgrounded(recordId);
133     sptr<IRemoteObject> token = GetFuzzAbilityToken();
134     appMgrService->AbilityCleaned(token);
135     int32_t appCloneIndex = static_cast<int32_t>(GetU32Data(data));
136     appMgrService->ClearUpApplicationData(bundleName, appCloneIndex);
137     appMgrService->ApplicationTerminated(recordId);
138     std::string msg(data, size);
139     int64_t resultCode = static_cast<int64_t>(GetU32Data(data));
140     appMgrService->FinishUserTest(msg, resultCode, bundleName);
141     appMgrService->OnStop();
142     return appMgrService->IsReady();
143 }
144 }
145 
146 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)147 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
148 {
149     /* Run your code on data */
150     if (data == nullptr) {
151         std::cout << "invalid data" << std::endl;
152         return 0;
153     }
154 
155     /* Validate the length of size */
156     if (size < OHOS::U32_AT_SIZE) {
157         return 0;
158     }
159 
160     char* ch = static_cast<char*>(malloc(size + 1));
161     if (ch == nullptr) {
162         std::cout << "malloc failed." << std::endl;
163         return 0;
164     }
165 
166     (void)memset_s(ch, size + 1, 0x00, size + 1);
167     if (memcpy_s(ch, size, data, size) != EOK) {
168         std::cout << "copy failed." << std::endl;
169         free(ch);
170         ch = nullptr;
171         return 0;
172     }
173 
174     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
175     free(ch);
176     ch = nullptr;
177     return 0;
178 }
179