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 int uid = static_cast<int>(GetU32Data(data));
109 amsMgrScheduler->KillApplicationByUid(bundleName, uid);
110 amsMgrScheduler->KillApplicationSelf();
111 AppExecFwk::RunningProcessInfo info;
112 amsMgrScheduler->GetRunningProcessInfoByToken(token, info);
113 Parcel wantParcel;
114 Want* want = nullptr;
115 if (wantParcel.WriteBuffer(data, size)) {
116 want = Want::Unmarshalling(wantParcel);
117 if (!want) {
118 return false;
119 }
120 }
121 AbilityInfo abilityInfo;
122 amsMgrScheduler->StartSpecifiedAbility(*want, abilityInfo);
123 int pid = static_cast<int>(GetU32Data(data));
124 AppExecFwk::ApplicationInfo application;
125 bool debug;
126 amsMgrScheduler->GetApplicationInfoByProcessID(pid, application, debug);
127 if (want) {
128 delete want;
129 want = nullptr;
130 }
131 return amsMgrScheduler->IsReady();
132 }
133 }
134
135 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)136 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
137 {
138 /* Run your code on data */
139 if (data == nullptr) {
140 std::cout << "invalid data" << std::endl;
141 return 0;
142 }
143
144 /* Validate the length of size */
145 if (size < OHOS::U32_AT_SIZE) {
146 return 0;
147 }
148
149 char* ch = static_cast<char*>(malloc(size + 1));
150 if (ch == nullptr) {
151 std::cout << "malloc failed." << std::endl;
152 return 0;
153 }
154
155 (void)memset_s(ch, size + 1, 0x00, size + 1);
156 if (memcpy_s(ch, size, data, size) != EOK) {
157 std::cout << "copy failed." << std::endl;
158 free(ch);
159 ch = nullptr;
160 return 0;
161 }
162
163 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
164 free(ch);
165 ch = nullptr;
166 return 0;
167 }
168
169