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