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 "amsmgrscheduler_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #include "ams_mgr_scheduler.h"
23 #undef private
24 #include "ability_record.h"
25 #include "param.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 int INPUT_ZERO = 0;
35 constexpr int INPUT_ONE = 1;
36 constexpr int INPUT_THREE = 3;
37 constexpr size_t U32_AT_SIZE = 4;
38 constexpr uint8_t ENABLE = 2;
39 constexpr size_t OFFSET_ZERO = 24;
40 constexpr size_t OFFSET_ONE = 16;
41 constexpr size_t OFFSET_TWO = 8;
42 }
GetU32Data(const char * ptr)43 uint32_t GetU32Data(const char* ptr)
44 {
45 // convert fuzz input data to an integer
46 return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
47 ptr[INPUT_THREE];
48 }
GetFuzzAbilityToken()49 sptr<Token> GetFuzzAbilityToken()
50 {
51 sptr<Token> token = nullptr;
52
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
62 return token;
63 }
DoSomethingInterestingWithMyAPI1(sptr<IRemoteObject> token,sptr<IRemoteObject> preToken,const char * data)64 std::shared_ptr<AmsMgrScheduler> DoSomethingInterestingWithMyAPI1(sptr<IRemoteObject> token,
65 sptr<IRemoteObject> preToken, const char* data)
66 {
67 std::shared_ptr<AppMgrServiceInner> mgrServiceInner;
68 std::shared_ptr<AAFwk::TaskHandlerWrap> handler;
69 std::shared_ptr<AmsMgrScheduler> amsMgrScheduler = std::make_shared<AmsMgrScheduler>(mgrServiceInner, handler);
70 sptr<IStartSpecifiedAbilityResponse> response;
71 amsMgrScheduler->RegisterStartSpecifiedAbilityResponse(response);
72 std::shared_ptr<AbilityInfo> abilityInfoptr;
73 std::shared_ptr<ApplicationInfo> appInfo;
74 std::shared_ptr<AAFwk::Want> wantptr;
75 int32_t abilityRecordId = static_cast<int32_t>(GetU32Data(data));
76 AbilityRuntime::LoadParam loadParam;
77 loadParam.abilityRecordId = abilityRecordId;
78 loadParam.token = token;
79 loadParam.preToken = preToken;
80 auto loadParamPtr = std::make_shared<AbilityRuntime::LoadParam>(loadParam);
81 amsMgrScheduler->LoadAbility(abilityInfoptr, appInfo, wantptr, loadParamPtr);
82 bool clearMissionFlag = *data % ENABLE;
83 amsMgrScheduler->TerminateAbility(token, clearMissionFlag);
84 return amsMgrScheduler;
85 }
86
DoSomethingInterestingWithMyAPI(const char * data,size_t size)87 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
88 {
89 sptr<IRemoteObject> token = GetFuzzAbilityToken();
90 sptr<IRemoteObject> preToken = nullptr;
91 auto amsMgrScheduler = DoSomethingInterestingWithMyAPI1(token, preToken, data);
92 AppExecFwk::AbilityState state = AppExecFwk::AbilityState::ABILITY_STATE_READY;
93 amsMgrScheduler->UpdateAbilityState(token, state);
94 AppExecFwk::ExtensionState extensionState = AppExecFwk::ExtensionState::EXTENSION_STATE_READY;
95 amsMgrScheduler->UpdateExtensionState(token, extensionState);
96 bool clearMissionFlag = *data % ENABLE;
97 amsMgrScheduler->TerminateAbility(token, clearMissionFlag);
98 sptr<IAppStateCallback> callback;
99 amsMgrScheduler->RegisterAppStateCallback(callback);
100 int32_t userId = static_cast<int32_t>(GetU32Data(data));
101 amsMgrScheduler->KillProcessesByUserId(userId, false, nullptr);
102 std::string bundleName(data, size);
103 int accountId = static_cast<int>(GetU32Data(data));
104 amsMgrScheduler->KillProcessWithAccount(bundleName, accountId);
105 amsMgrScheduler->AbilityAttachTimeOut(token);
106 amsMgrScheduler->PrepareTerminate(token);
107 amsMgrScheduler->KillApplication(bundleName);
108 amsMgrScheduler->VerifyKillProcessPermission(bundleName);
109 int uid = static_cast<int>(GetU32Data(data));
110 amsMgrScheduler->KillApplicationByUid(bundleName, uid);
111 amsMgrScheduler->KillApplicationSelf();
112 AppExecFwk::RunningProcessInfo info;
113 amsMgrScheduler->GetRunningProcessInfoByToken(token, info);
114 Parcel wantParcel;
115 Want* want = nullptr;
116 if (wantParcel.WriteBuffer(data, size)) {
117 want = Want::Unmarshalling(wantParcel);
118 if (!want) {
119 return false;
120 }
121 }
122 AbilityInfo abilityInfo;
123 amsMgrScheduler->StartSpecifiedAbility(*want, abilityInfo);
124 int pid = static_cast<int>(GetU32Data(data));
125 AppExecFwk::ApplicationInfo application;
126 bool debug;
127 amsMgrScheduler->GetApplicationInfoByProcessID(pid, application, debug);
128 if (want) {
129 delete want;
130 want = nullptr;
131 }
132 bool enable = *data % ENABLE;
133 amsMgrScheduler->SetKeepAliveAppService(bundleName, enable, uid);
134 return amsMgrScheduler->IsReady();
135 }
136 }
137
138 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)139 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
140 {
141 /* Run your code on data */
142 if (data == nullptr) {
143 std::cout << "invalid data" << std::endl;
144 return 0;
145 }
146
147 /* Validate the length of size */
148 if (size < OHOS::U32_AT_SIZE) {
149 return 0;
150 }
151
152 char* ch = static_cast<char*>(malloc(size + 1));
153 if (ch == nullptr) {
154 std::cout << "malloc failed." << std::endl;
155 return 0;
156 }
157
158 (void)memset_s(ch, size + 1, 0x00, size + 1);
159 if (memcpy_s(ch, size, data, size) != EOK) {
160 std::cout << "copy failed." << std::endl;
161 free(ch);
162 ch = nullptr;
163 return 0;
164 }
165
166 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
167 free(ch);
168 ch = nullptr;
169 return 0;
170 }
171
172