• 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 "modulerunningrecord_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #include "module_running_record.h"
23 #undef private
24 #include "ability_record.h"
25 #include "message_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<ApplicationInfo> info;
63     std::shared_ptr<AMSEventHandler> eventHandler;
64     ModuleRunningRecord moduleRecord(info, eventHandler);
65     HapModuleInfo hapInfo;
66     moduleRecord.Init(hapInfo);
67     std::weak_ptr<AppMgrServiceInner> inner;
68     moduleRecord.SetAppMgrServiceInner(inner);
69     ModuleRecordState moduleState = ModuleRecordState::UNKNOWN_STATE;
70     moduleRecord.SetModuleRecordState(moduleState);
71     std::shared_ptr<AppLifeCycleDeal> appLifeCycleDeal;
72     moduleRecord.SetApplicationClient(appLifeCycleDeal);
73     sptr<IRemoteObject> token = GetFuzzAbilityToken();
74     moduleRecord.GetAbilityRunningRecordByToken(token);
75     std::shared_ptr<AbilityInfo> abilityInfo;
76     Parcel wantParcel;
77     std::shared_ptr<Want> want;
78     moduleRecord.AddAbility(token, abilityInfo, want);
79     int64_t eventId = static_cast<int64_t>(GetU32Data(data));
80     moduleRecord.GetAbilityRunningRecord(eventId);
81     std::shared_ptr<AbilityRunningRecord> record;
82     moduleRecord.LaunchAbility(record);
83     moduleRecord.LaunchPendingAbilities();
84     AppExecFwk::AbilityState state = AppExecFwk::AbilityState::ABILITY_STATE_READY;
85     moduleRecord.OnAbilityStateChanged(record, state);
86     bool isForce = *data % ENABLE;
87     std::shared_ptr<AppRunningRecord> appRunningRecord;
88     moduleRecord.TerminateAbility(appRunningRecord, token, isForce);
89     moduleRecord.AbilityTerminated(token);
90     moduleRecord.GetAbilityByTerminateLists(token);
91     uint32_t msg = GetU32Data(data);
92     int64_t timeOut = static_cast<int64_t>(GetU32Data(data));
93     moduleRecord.SendEvent(msg, timeOut, record);
94     moduleRecord.RemoveTerminateAbilityTimeoutTask(token);
95     moduleRecord.GetModuleName();
96     moduleRecord.GetPageAbilitySize();
97     moduleRecord.GetModuleRecordState();
98     moduleRecord.GetAppInfo();
99     moduleRecord.GetState();
100     return moduleRecord.IsLastAbilityRecord(token);
101 }
102 }
103 
104 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)105 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
106 {
107     /* Run your code on data */
108     if (data == nullptr) {
109         std::cout << "invalid data" << std::endl;
110         return 0;
111     }
112 
113     /* Validate the length of size */
114     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
115         return 0;
116     }
117 
118     char* ch = (char *)malloc(size + 1);
119     if (ch == nullptr) {
120         std::cout << "malloc failed." << std::endl;
121         return 0;
122     }
123 
124     (void)memset_s(ch, size + 1, 0x00, size + 1);
125     if (memcpy_s(ch, size, data, size) != EOK) {
126         std::cout << "copy failed." << std::endl;
127         free(ch);
128         ch = nullptr;
129         return 0;
130     }
131 
132     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
133     free(ch);
134     ch = nullptr;
135     return 0;
136 }
137