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 FOO_MAX_LEN = 1024;
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
54 AbilityRequest abilityRequest;
55 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
56 abilityRequest.abilityInfo.name = "MainAbility";
57 abilityRequest.abilityInfo.type = AbilityType::PAGE;
58 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
59 if (abilityRecord) {
60 token = abilityRecord->GetToken();
61 }
62
63 return token;
64 }
DoSomethingInterestingWithMyAPI1(sptr<IRemoteObject> token,sptr<IRemoteObject> preToken,const char * data)65 std::shared_ptr<AmsMgrScheduler> DoSomethingInterestingWithMyAPI1(sptr<IRemoteObject> token,
66 sptr<IRemoteObject> preToken, const char* data)
67 {
68 std::shared_ptr<AppMgrServiceInner> mgrServiceInner;
69 std::shared_ptr<AAFwk::TaskHandlerWrap> handler;
70 std::shared_ptr<AmsMgrScheduler> amsMgrScheduler = std::make_shared<AmsMgrScheduler>(mgrServiceInner, handler);
71 sptr<IStartSpecifiedAbilityResponse> response;
72 amsMgrScheduler->RegisterStartSpecifiedAbilityResponse(response);
73 std::shared_ptr<AbilityInfo> abilityInfoptr;
74 std::shared_ptr<ApplicationInfo> appInfo;
75 std::shared_ptr<AAFwk::Want> wantptr;
76 int32_t abilityRecordId = static_cast<int32_t>(GetU32Data(data));
77 AbilityRuntime::LoadParam loadParam;
78 loadParam.abilityRecordId = abilityRecordId;
79 loadParam.token = token;
80 loadParam.preToken = preToken;
81 auto loadParamPtr = std::make_shared<AbilityRuntime::LoadParam>(loadParam);
82 amsMgrScheduler->LoadAbility(abilityInfoptr, appInfo, wantptr, loadParamPtr);
83 bool clearMissionFlag = *data % ENABLE;
84 amsMgrScheduler->TerminateAbility(token, clearMissionFlag);
85 return amsMgrScheduler;
86 }
87
DoSomethingInterestingWithMyAPI(const char * data,size_t size)88 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
89 {
90 sptr<IRemoteObject> token = GetFuzzAbilityToken();
91 sptr<IRemoteObject> preToken = nullptr;
92 auto amsMgrScheduler = DoSomethingInterestingWithMyAPI1(token, preToken, data);
93 AppExecFwk::AbilityState state = AppExecFwk::AbilityState::ABILITY_STATE_READY;
94 amsMgrScheduler->UpdateAbilityState(token, state);
95 AppExecFwk::ExtensionState extensionState = AppExecFwk::ExtensionState::EXTENSION_STATE_READY;
96 amsMgrScheduler->UpdateExtensionState(token, extensionState);
97 bool clearMissionFlag = *data % ENABLE;
98 amsMgrScheduler->TerminateAbility(token, clearMissionFlag);
99 sptr<IAppStateCallback> callback;
100 amsMgrScheduler->RegisterAppStateCallback(callback);
101 int32_t visibility = static_cast<int32_t>(GetU32Data(data));
102 int32_t perceptibility = static_cast<int32_t>(GetU32Data(data));
103 int32_t connectionState = static_cast<int32_t>(GetU32Data(data));
104 amsMgrScheduler->AbilityBehaviorAnalysis(token, preToken, visibility, perceptibility, connectionState);
105 int32_t userId = static_cast<int32_t>(GetU32Data(data));
106 amsMgrScheduler->KillProcessesByUserId(userId);
107 std::string bundleName(data, size);
108 int accountId = static_cast<int>(GetU32Data(data));
109 amsMgrScheduler->KillProcessWithAccount(bundleName, accountId);
110 amsMgrScheduler->AbilityAttachTimeOut(token);
111 amsMgrScheduler->PrepareTerminate(token);
112 amsMgrScheduler->KillApplication(bundleName);
113 int uid = static_cast<int>(GetU32Data(data));
114 amsMgrScheduler->KillApplicationByUid(bundleName, uid);
115 amsMgrScheduler->KillApplicationSelf();
116 AppExecFwk::RunningProcessInfo info;
117 amsMgrScheduler->GetRunningProcessInfoByToken(token, info);
118 Parcel wantParcel;
119 Want* want = nullptr;
120 if (wantParcel.WriteBuffer(data, size)) {
121 want = Want::Unmarshalling(wantParcel);
122 if (!want) {
123 return false;
124 }
125 }
126 AbilityInfo abilityInfo;
127 amsMgrScheduler->StartSpecifiedAbility(*want, abilityInfo);
128 int pid = static_cast<int>(GetU32Data(data));
129 AppExecFwk::ApplicationInfo application;
130 bool debug;
131 amsMgrScheduler->GetApplicationInfoByProcessID(pid, application, debug);
132 if (want) {
133 delete want;
134 want = nullptr;
135 }
136 return amsMgrScheduler->IsReady();
137 }
138 }
139
140 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)141 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
142 {
143 /* Run your code on data */
144 if (data == nullptr) {
145 std::cout << "invalid data" << std::endl;
146 return 0;
147 }
148
149 /* Validate the length of size */
150 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
151 return 0;
152 }
153
154 char* ch = static_cast<char*>(malloc(size + 1));
155 if (ch == nullptr) {
156 std::cout << "malloc failed." << std::endl;
157 return 0;
158 }
159
160 (void)memset_s(ch, size + 1, 0x00, size + 1);
161 if (memcpy_s(ch, size, data, size) != EOK) {
162 std::cout << "copy failed." << std::endl;
163 free(ch);
164 ch = nullptr;
165 return 0;
166 }
167
168 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
169 free(ch);
170 ch = nullptr;
171 return 0;
172 }
173
174