• 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 "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 "parcel.h"
26 #include "securec.h"
27 
28 using namespace OHOS::AAFwk;
29 using namespace OHOS::AppExecFwk;
30 
31 namespace OHOS {
32 namespace {
33 constexpr int INPUT_ZERO = 0;
34 constexpr int INPUT_ONE = 1;
35 constexpr int INPUT_THREE = 3;
36 constexpr size_t FOO_MAX_LEN = 1024;
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 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)64 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
65 {
66     std::shared_ptr<AppMgrServiceInner> mgrServiceInner;
67     std::shared_ptr<AAFwk::TaskHandlerWrap> handler;
68     AmsMgrScheduler amsMgrScheduler(mgrServiceInner, handler);
69     sptr<IStartSpecifiedAbilityResponse> response;
70     amsMgrScheduler.RegisterStartSpecifiedAbilityResponse(response);
71     sptr<IRemoteObject> token = GetFuzzAbilityToken();
72     sptr<IRemoteObject> preToken = nullptr;
73     std::shared_ptr<AbilityInfo> abilityInfoptr;
74     std::shared_ptr<ApplicationInfo> appInfo;
75     std::shared_ptr<AAFwk::Want> wantptr;
76     amsMgrScheduler.LoadAbility(token, preToken, abilityInfoptr, appInfo, wantptr);
77     AppExecFwk::AbilityState state = AppExecFwk::AbilityState::ABILITY_STATE_READY;
78     amsMgrScheduler.UpdateAbilityState(token, state);
79     AppExecFwk::ExtensionState extensionState = AppExecFwk::ExtensionState::EXTENSION_STATE_READY;
80     amsMgrScheduler.UpdateExtensionState(token, extensionState);
81     bool clearMissionFlag = *data % ENABLE;
82     amsMgrScheduler.TerminateAbility(token, clearMissionFlag);
83     sptr<IAppStateCallback> callback;
84     amsMgrScheduler.RegisterAppStateCallback(callback);
85     int32_t visibility = static_cast<int32_t>(GetU32Data(data));
86     int32_t perceptibility = static_cast<int32_t>(GetU32Data(data));
87     int32_t connectionState = static_cast<int32_t>(GetU32Data(data));
88     amsMgrScheduler.AbilityBehaviorAnalysis(token, preToken, visibility, perceptibility, connectionState);
89     int32_t userId = static_cast<int32_t>(GetU32Data(data));
90     amsMgrScheduler.KillProcessesByUserId(userId);
91     std::string bundleName(data, size);
92     int accountId = static_cast<int>(GetU32Data(data));
93     amsMgrScheduler.KillProcessWithAccount(bundleName, accountId);
94     amsMgrScheduler.AbilityAttachTimeOut(token);
95     amsMgrScheduler.PrepareTerminate(token);
96     amsMgrScheduler.KillApplication(bundleName);
97     int uid = static_cast<int>(GetU32Data(data));
98     amsMgrScheduler.KillApplicationByUid(bundleName, uid);
99     amsMgrScheduler.KillApplicationSelf();
100     AppExecFwk::RunningProcessInfo info;
101     amsMgrScheduler.GetRunningProcessInfoByToken(token, info);
102     Parcel wantParcel;
103     Want* want = nullptr;
104     if (wantParcel.WriteBuffer(data, size)) {
105         want = Want::Unmarshalling(wantParcel);
106     }
107     AbilityInfo abilityInfo;
108     amsMgrScheduler.StartSpecifiedAbility(*want, abilityInfo);
109     int pid = static_cast<int>(GetU32Data(data));
110     AppExecFwk::ApplicationInfo application;
111     bool debug;
112     amsMgrScheduler.GetApplicationInfoByProcessID(pid, application, debug);
113     return amsMgrScheduler.IsReady();
114 }
115 }
116 
117 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)118 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
119 {
120     /* Run your code on data */
121     if (data == nullptr) {
122         std::cout << "invalid data" << std::endl;
123         return 0;
124     }
125 
126     /* Validate the length of size */
127     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
128         return 0;
129     }
130 
131     char* ch = (char*)malloc(size + 1);
132     if (ch == nullptr) {
133         std::cout << "malloc failed." << std::endl;
134         return 0;
135     }
136 
137     (void)memset_s(ch, size + 1, 0x00, size + 1);
138     if (memcpy_s(ch, size, data, size) != EOK) {
139         std::cout << "copy failed." << std::endl;
140         free(ch);
141         ch = nullptr;
142         return 0;
143     }
144 
145     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
146     free(ch);
147     ch = nullptr;
148     return 0;
149 }
150 
151