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