• 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 size_t FOO_MAX_LEN = 1024;
34 constexpr size_t U32_AT_SIZE = 4;
35 constexpr uint8_t ENABLE = 2;
36 constexpr size_t OFFSET_ZERO = 24;
37 constexpr size_t OFFSET_ONE = 16;
38 constexpr size_t OFFSET_TWO = 8;
39 }
GetU32Data(const char * ptr)40 uint32_t GetU32Data(const char* ptr)
41 {
42     // convert fuzz input data to an integer
43     return (ptr[0] << OFFSET_ZERO) | (ptr[1] << OFFSET_ONE) | (ptr[2] << OFFSET_TWO) | ptr[3];
44 }
GetFuzzAbilityToken()45 sptr<Token> GetFuzzAbilityToken()
46 {
47     sptr<Token> token = nullptr;
48 
49     AbilityRequest abilityRequest;
50     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
51     abilityRequest.abilityInfo.name = "MainAbility";
52     abilityRequest.abilityInfo.type = AbilityType::PAGE;
53     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
54     if (abilityRecord) {
55         token = abilityRecord->GetToken();
56     }
57 
58     return token;
59 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)60 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
61 {
62     std::shared_ptr<AppMgrServiceInner> mgrServiceInner;
63     std::shared_ptr<AAFwk::TaskHandlerWrap> handler;
64     AmsMgrScheduler amsMgrScheduler(mgrServiceInner, handler);
65     sptr<IStartSpecifiedAbilityResponse> response;
66     amsMgrScheduler.RegisterStartSpecifiedAbilityResponse(response);
67     sptr<IRemoteObject> token = GetFuzzAbilityToken();
68     sptr<IRemoteObject> preToken = nullptr;
69     std::shared_ptr<AbilityInfo> abilityInfoptr;
70     std::shared_ptr<ApplicationInfo> appInfo;
71     std::shared_ptr<AAFwk::Want> wantptr;
72     amsMgrScheduler.LoadAbility(token, preToken, abilityInfoptr, appInfo, wantptr);
73     AppExecFwk::AbilityState state = AppExecFwk::AbilityState::ABILITY_STATE_READY;
74     amsMgrScheduler.UpdateAbilityState(token, state);
75     AppExecFwk::ExtensionState extensionState = AppExecFwk::ExtensionState::EXTENSION_STATE_READY;
76     amsMgrScheduler.UpdateExtensionState(token, extensionState);
77     bool clearMissionFlag = *data % ENABLE;
78     amsMgrScheduler.TerminateAbility(token, clearMissionFlag);
79     sptr<IAppStateCallback> callback;
80     amsMgrScheduler.RegisterAppStateCallback(callback);
81     int32_t visibility = static_cast<int32_t>(GetU32Data(data));
82     int32_t perceptibility = static_cast<int32_t>(GetU32Data(data));
83     int32_t connectionState = static_cast<int32_t>(GetU32Data(data));
84     amsMgrScheduler.AbilityBehaviorAnalysis(token, preToken, visibility, perceptibility, connectionState);
85     int32_t userId = static_cast<int32_t>(GetU32Data(data));
86     amsMgrScheduler.KillProcessesByUserId(userId);
87     std::string bundleName(data, size);
88     int accountId = static_cast<int>(GetU32Data(data));
89     amsMgrScheduler.KillProcessWithAccount(bundleName, accountId);
90     amsMgrScheduler.AbilityAttachTimeOut(token);
91     amsMgrScheduler.PrepareTerminate(token);
92     amsMgrScheduler.KillApplication(bundleName);
93     int uid = static_cast<int>(GetU32Data(data));
94     amsMgrScheduler.KillApplicationByUid(bundleName, uid);
95     amsMgrScheduler.KillApplicationSelf();
96     AppExecFwk::RunningProcessInfo info;
97     amsMgrScheduler.GetRunningProcessInfoByToken(token, info);
98     Parcel wantParcel;
99     Want* want = nullptr;
100     if (wantParcel.WriteBuffer(data, size)) {
101         want = Want::Unmarshalling(wantParcel);
102     }
103     AbilityInfo abilityInfo;
104     amsMgrScheduler.StartSpecifiedAbility(*want, abilityInfo);
105     int pid = static_cast<int>(GetU32Data(data));
106     AppExecFwk::ApplicationInfo application;
107     bool debug;
108     amsMgrScheduler.GetApplicationInfoByProcessID(pid, application, debug);
109     return amsMgrScheduler.IsReady();
110 }
111 }
112 
113 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)114 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
115 {
116     /* Run your code on data */
117     if (data == nullptr) {
118         std::cout << "invalid data" << std::endl;
119         return 0;
120     }
121 
122     /* Validate the length of size */
123     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
124         return 0;
125     }
126 
127     char* ch = (char*)malloc(size + 1);
128     if (ch == nullptr) {
129         std::cout << "malloc failed." << std::endl;
130         return 0;
131     }
132 
133     (void)memset_s(ch, size + 1, 0x00, size + 1);
134     if (memcpy_s(ch, size, data, size) != EOK) {
135         std::cout << "copy failed." << std::endl;
136         free(ch);
137         ch = nullptr;
138         return 0;
139     }
140 
141     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
142     free(ch);
143     ch = nullptr;
144     return 0;
145 }
146 
147