1 /*
2  * Copyright (c) 2024 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 int32_t ABILITY_RECORD_ID;
40 }
GetU32Data(const char * ptr)41 uint32_t GetU32Data(const char* ptr)
42 {
43     // convert fuzz input data to an integer
44     return (ptr[0] << OFFSET_ZERO) | (ptr[1] << OFFSET_ONE) | (ptr[2] << OFFSET_TWO) | ptr[3];
45 }
GetFuzzAbilityToken()46 sptr<Token> GetFuzzAbilityToken()
47 {
48     sptr<Token> token = nullptr;
49 
50     AbilityRequest abilityRequest;
51     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
52     abilityRequest.abilityInfo.name = "MainAbility";
53     abilityRequest.abilityInfo.type = AbilityType::PAGE;
54     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
55     if (abilityRecord) {
56         token = abilityRecord->GetToken();
57         ABILITY_RECORD_ID = abilityRecord->GetAbilityRecordId();
58     }
59 
60     return token;
61 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)62 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
63 {
64     std::shared_ptr<ApplicationInfo> info;
65     std::shared_ptr<AMSEventHandler> eventHandler;
66     ModuleRunningRecord moduleRecord(info, eventHandler);
67     HapModuleInfo hapInfo;
68     moduleRecord.Init(hapInfo);
69     std::weak_ptr<AppMgrServiceInner> inner;
70     moduleRecord.SetAppMgrServiceInner(inner);
71     ModuleRecordState moduleState = ModuleRecordState::UNKNOWN_STATE;
72     moduleRecord.SetModuleRecordState(moduleState);
73     std::shared_ptr<AppLifeCycleDeal> appLifeCycleDeal;
74     moduleRecord.SetApplicationClient(appLifeCycleDeal);
75     sptr<IRemoteObject> token = GetFuzzAbilityToken();
76     moduleRecord.GetAbilityRunningRecordByToken(token);
77     std::shared_ptr<AbilityInfo> abilityInfo;
78     Parcel wantParcel;
79     std::shared_ptr<Want> want;
80     moduleRecord.AddAbility(token, abilityInfo, want, ABILITY_RECORD_ID);
81     int64_t eventId = static_cast<int64_t>(GetU32Data(data));
82     moduleRecord.GetAbilityRunningRecord(eventId);
83     std::shared_ptr<AbilityRunningRecord> record;
84     moduleRecord.LaunchAbility(record);
85     moduleRecord.LaunchPendingAbilities();
86     AppExecFwk::AbilityState state = AppExecFwk::AbilityState::ABILITY_STATE_READY;
87     moduleRecord.OnAbilityStateChanged(record, state);
88     bool isForce = *data % ENABLE;
89     std::shared_ptr<AppRunningRecord> appRunningRecord;
90     moduleRecord.TerminateAbility(appRunningRecord, token, isForce);
91     moduleRecord.AbilityTerminated(token);
92     moduleRecord.GetAbilityByTerminateLists(token);
93     uint32_t msg = GetU32Data(data);
94     int64_t timeOut = static_cast<int64_t>(GetU32Data(data));
95     moduleRecord.SendEvent(msg, timeOut, record);
96     moduleRecord.RemoveTerminateAbilityTimeoutTask(token);
97     moduleRecord.GetModuleName();
98     moduleRecord.GetPageAbilitySize();
99     moduleRecord.GetModuleRecordState();
100     moduleRecord.GetAppInfo();
101     moduleRecord.GetState();
102     return moduleRecord.IsLastAbilityRecord(token);
103 }
104 }
105 
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
108 {
109     /* Run your code on data */
110     if (data == nullptr) {
111         std::cout << "invalid data" << std::endl;
112         return 0;
113     }
114 
115     /* Validate the length of size */
116     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
117         return 0;
118     }
119 
120     char* ch = static_cast<char*>(malloc(size + 1));
121     if (ch == nullptr) {
122         std::cout << "malloc failed." << std::endl;
123         return 0;
124     }
125 
126     (void)memset_s(ch, size + 1, 0x00, size + 1);
127     if (memcpy_s(ch, size, data, size) != EOK) {
128         std::cout << "copy failed." << std::endl;
129         free(ch);
130         ch = nullptr;
131         return 0;
132     }
133 
134     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
135     free(ch);
136     ch = nullptr;
137     return 0;
138 }