1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "appmgrservicefirst_fuzzer.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include <cstddef>
19eace7efcSopenharmony_ci#include <cstdint>
20eace7efcSopenharmony_ci
21eace7efcSopenharmony_ci#define private public
22eace7efcSopenharmony_ci#include "app_mgr_service.h"
23eace7efcSopenharmony_ci#undef private
24eace7efcSopenharmony_ci
25eace7efcSopenharmony_ci#include "ability_record.h"
26eace7efcSopenharmony_ci#include "parcel.h"
27eace7efcSopenharmony_ci#include "securec.h"
28eace7efcSopenharmony_ci#include "want.h"
29eace7efcSopenharmony_ci
30eace7efcSopenharmony_ciusing namespace OHOS::AAFwk;
31eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk;
32eace7efcSopenharmony_ci
33eace7efcSopenharmony_cinamespace OHOS {
34eace7efcSopenharmony_cinamespace {
35eace7efcSopenharmony_ciconstexpr int INPUT_ZERO = 0;
36eace7efcSopenharmony_ciconstexpr int INPUT_ONE = 1;
37eace7efcSopenharmony_ciconstexpr int INPUT_THREE = 3;
38eace7efcSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024;
39eace7efcSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4;
40eace7efcSopenharmony_ciconstexpr uint8_t ENABLE = 2;
41eace7efcSopenharmony_ciconstexpr size_t OFFSET_ZERO = 24;
42eace7efcSopenharmony_ciconstexpr size_t OFFSET_ONE = 16;
43eace7efcSopenharmony_ciconstexpr size_t OFFSET_TWO = 8;
44eace7efcSopenharmony_ci}
45eace7efcSopenharmony_ciuint32_t GetU32Data(const char* ptr)
46eace7efcSopenharmony_ci{
47eace7efcSopenharmony_ci    // convert fuzz input data to an integer
48eace7efcSopenharmony_ci    return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
49eace7efcSopenharmony_ci        ptr[INPUT_THREE];
50eace7efcSopenharmony_ci}
51eace7efcSopenharmony_cisptr<Token> GetFuzzAbilityToken()
52eace7efcSopenharmony_ci{
53eace7efcSopenharmony_ci    sptr<Token> token = nullptr;
54eace7efcSopenharmony_ci    AbilityRequest abilityRequest;
55eace7efcSopenharmony_ci    abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
56eace7efcSopenharmony_ci    abilityRequest.abilityInfo.name = "MainAbility";
57eace7efcSopenharmony_ci    abilityRequest.abilityInfo.type = AbilityType::PAGE;
58eace7efcSopenharmony_ci    std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
59eace7efcSopenharmony_ci    if (abilityRecord) {
60eace7efcSopenharmony_ci        token = abilityRecord->GetToken();
61eace7efcSopenharmony_ci    }
62eace7efcSopenharmony_ci    return token;
63eace7efcSopenharmony_ci}
64eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
65eace7efcSopenharmony_ci{
66eace7efcSopenharmony_ci    AppMgrService* appMgrService = new AppMgrService();
67eace7efcSopenharmony_ci    std::shared_ptr<AppMgrServiceInner> innerService = std::make_shared<AppMgrServiceInner>();
68eace7efcSopenharmony_ci    appMgrService->appMgrServiceState_.serviceRunningState = ServiceRunningState::STATE_NOT_START;
69eace7efcSopenharmony_ci    appMgrService->SetInnerService(innerService);
70eace7efcSopenharmony_ci    appMgrService->OnStart();
71eace7efcSopenharmony_ci    sptr<IConfigurationObserver> configurationObserver;
72eace7efcSopenharmony_ci    appMgrService->RegisterConfigurationObserver(configurationObserver);
73eace7efcSopenharmony_ci    appMgrService->UnregisterConfigurationObserver(configurationObserver);
74eace7efcSopenharmony_ci    sptr<IApplicationStateObserver> applicationStateObserver;
75eace7efcSopenharmony_ci    appMgrService->RegisterApplicationStateObserver(applicationStateObserver);
76eace7efcSopenharmony_ci    appMgrService->UnregisterApplicationStateObserver(applicationStateObserver);
77eace7efcSopenharmony_ci    pid_t pid = static_cast<pid_t>(GetU32Data(data));
78eace7efcSopenharmony_ci    appMgrService->AddAppDeathRecipient(pid);
79eace7efcSopenharmony_ci    appMgrService->QueryServiceState();
80eace7efcSopenharmony_ci    sptr<IRemoteObject> app = nullptr;
81eace7efcSopenharmony_ci    appMgrService->AttachApplication(app);
82eace7efcSopenharmony_ci    std::vector<BundleInfo> bundleInfos;
83eace7efcSopenharmony_ci    appMgrService->StartupResidentProcess(bundleInfos);
84eace7efcSopenharmony_ci    Parcel wantParcel;
85eace7efcSopenharmony_ci    Want* want = nullptr;
86eace7efcSopenharmony_ci    if (wantParcel.WriteBuffer(data, size)) {
87eace7efcSopenharmony_ci        want = Want::Unmarshalling(wantParcel);
88eace7efcSopenharmony_ci    }
89eace7efcSopenharmony_ci    std::string renderParam(data, size);
90eace7efcSopenharmony_ci    int32_t ipcFd = static_cast<int32_t>(GetU32Data(data));
91eace7efcSopenharmony_ci    int32_t sharedFd = static_cast<int32_t>(GetU32Data(data));
92eace7efcSopenharmony_ci    int32_t crashFd = static_cast<int32_t>(GetU32Data(data));
93eace7efcSopenharmony_ci    pid_t renderPid = static_cast<pid_t>(GetU32Data(data));
94eace7efcSopenharmony_ci    appMgrService->StartRenderProcess(renderParam, ipcFd, sharedFd, crashFd, renderPid);
95eace7efcSopenharmony_ci    appMgrService->PreStartNWebSpawnProcess();
96eace7efcSopenharmony_ci    sptr<IRemoteObject> scheduler = nullptr;
97eace7efcSopenharmony_ci    appMgrService->AttachRenderProcess(scheduler);
98eace7efcSopenharmony_ci    bool isContinuousTask = *data % ENABLE;
99eace7efcSopenharmony_ci    appMgrService->SetContinuousTaskProcess(static_cast<int32_t>(pid), isContinuousTask);
100eace7efcSopenharmony_ci    int32_t recordId = static_cast<int32_t>(GetU32Data(data));
101eace7efcSopenharmony_ci    appMgrService->ApplicationForegrounded(recordId);
102eace7efcSopenharmony_ci    appMgrService->AddAbilityStageDone(recordId);
103eace7efcSopenharmony_ci    int fd = static_cast<int>(GetU32Data(data));
104eace7efcSopenharmony_ci    std::vector<std::u16string> args;
105eace7efcSopenharmony_ci    appMgrService->Dump(fd, args);
106eace7efcSopenharmony_ci    std::string result(data, size);
107eace7efcSopenharmony_ci    appMgrService->Dump(args, result);
108eace7efcSopenharmony_ci    appMgrService->ShowHelp(args, result);
109eace7efcSopenharmony_ci    std::string flag(data, size);
110eace7efcSopenharmony_ci    appMgrService->ScheduleAcceptWantDone(recordId, *want, flag);
111eace7efcSopenharmony_ci    Configuration config;
112eace7efcSopenharmony_ci    appMgrService->UpdateConfiguration(config);
113eace7efcSopenharmony_ci    appMgrService->GetAmsMgr();
114eace7efcSopenharmony_ci    std::vector<RunningProcessInfo> info;
115eace7efcSopenharmony_ci    appMgrService->GetAllRunningProcesses(info);
116eace7efcSopenharmony_ci    int32_t userId = static_cast<int32_t>(GetU32Data(data));
117eace7efcSopenharmony_ci    appMgrService->GetProcessRunningInfosByUserId(info, userId);
118eace7efcSopenharmony_ci    int32_t level = static_cast<int32_t>(GetU32Data(data));
119eace7efcSopenharmony_ci    appMgrService->NotifyMemoryLevel(level);
120eace7efcSopenharmony_ci    std::vector<AppStateData> list;
121eace7efcSopenharmony_ci    appMgrService->GetForegroundApplications(list);
122eace7efcSopenharmony_ci    std::vector<sptr<IRemoteObject>> tokens;
123eace7efcSopenharmony_ci    appMgrService->GetAbilityRecordsByProcessID(static_cast<int>(pid), tokens);
124eace7efcSopenharmony_ci    int status = static_cast<int>(GetU32Data(data));
125eace7efcSopenharmony_ci    appMgrService->GetRenderProcessTerminationStatus(renderPid, status);
126eace7efcSopenharmony_ci    appMgrService->GetConfiguration(config);
127eace7efcSopenharmony_ci    std::string bundleName(data, size);
128eace7efcSopenharmony_ci    appMgrService->GetAppRunningStateByBundleName(bundleName);
129eace7efcSopenharmony_ci    sptr<IQuickFixCallback> callback;
130eace7efcSopenharmony_ci    appMgrService->NotifyLoadRepairPatch(bundleName, callback);
131eace7efcSopenharmony_ci    appMgrService->NotifyHotReloadPage(bundleName, callback);
132eace7efcSopenharmony_ci    appMgrService->NotifyUnLoadRepairPatch(bundleName, callback);
133eace7efcSopenharmony_ci    appMgrService->ApplicationBackgrounded(recordId);
134eace7efcSopenharmony_ci    sptr<IRemoteObject> token = GetFuzzAbilityToken();
135eace7efcSopenharmony_ci    appMgrService->AbilityCleaned(token);
136eace7efcSopenharmony_ci    int32_t appCloneIndex = static_cast<int32_t>(GetU32Data(data));
137eace7efcSopenharmony_ci    appMgrService->ClearUpApplicationData(bundleName, appCloneIndex);
138eace7efcSopenharmony_ci    appMgrService->ApplicationTerminated(recordId);
139eace7efcSopenharmony_ci    std::string msg(data, size);
140eace7efcSopenharmony_ci    int64_t resultCode = static_cast<int64_t>(GetU32Data(data));
141eace7efcSopenharmony_ci    appMgrService->FinishUserTest(msg, resultCode, bundleName);
142eace7efcSopenharmony_ci    appMgrService->OnStop();
143eace7efcSopenharmony_ci    return appMgrService->IsReady();
144eace7efcSopenharmony_ci}
145eace7efcSopenharmony_ci}
146eace7efcSopenharmony_ci
147eace7efcSopenharmony_ci/* Fuzzer entry point */
148eace7efcSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
149eace7efcSopenharmony_ci{
150eace7efcSopenharmony_ci    /* Run your code on data */
151eace7efcSopenharmony_ci    if (data == nullptr) {
152eace7efcSopenharmony_ci        std::cout << "invalid data" << std::endl;
153eace7efcSopenharmony_ci        return 0;
154eace7efcSopenharmony_ci    }
155eace7efcSopenharmony_ci
156eace7efcSopenharmony_ci    /* Validate the length of size */
157eace7efcSopenharmony_ci    if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
158eace7efcSopenharmony_ci        return 0;
159eace7efcSopenharmony_ci    }
160eace7efcSopenharmony_ci
161eace7efcSopenharmony_ci    char* ch = (char*)malloc(size + 1);
162eace7efcSopenharmony_ci    if (ch == nullptr) {
163eace7efcSopenharmony_ci        std::cout << "malloc failed." << std::endl;
164eace7efcSopenharmony_ci        return 0;
165eace7efcSopenharmony_ci    }
166eace7efcSopenharmony_ci
167eace7efcSopenharmony_ci    (void)memset_s(ch, size + 1, 0x00, size + 1);
168eace7efcSopenharmony_ci    if (memcpy_s(ch, size, data, size) != EOK) {
169eace7efcSopenharmony_ci        std::cout << "copy failed." << std::endl;
170eace7efcSopenharmony_ci        free(ch);
171eace7efcSopenharmony_ci        ch = nullptr;
172eace7efcSopenharmony_ci        return 0;
173eace7efcSopenharmony_ci    }
174eace7efcSopenharmony_ci
175eace7efcSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(ch, size);
176eace7efcSopenharmony_ci    free(ch);
177eace7efcSopenharmony_ci    ch = nullptr;
178eace7efcSopenharmony_ci    return 0;
179eace7efcSopenharmony_ci}
180