• 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 "abilityeventhandler_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #include "ability_event_handler.h"
23 #include "ability_interceptor_executer.h"
24 #include "ability_running_info.h"
25 #include "ability_scheduler_proxy.h"
26 #include "ams_configuration_parameter.h"
27 #undef private
28 
29 #include "ability_record.h"
30 #include "data_ability_observer_interface.h"
31 #include "data_ability_predicates.h"
32 
33 using namespace OHOS::AAFwk;
34 using namespace OHOS::AppExecFwk;
35 
36 namespace OHOS {
37 namespace {
38 constexpr size_t FOO_MAX_LEN = 1024;
39 constexpr size_t U32_AT_SIZE = 4;
40 constexpr uint8_t ENABLE = 2;
41 class DataAbilityObserver : public IDataAbilityObserver {
42 public:
43     DataAbilityObserver() = default;
44     virtual ~DataAbilityObserver() = default;
OnChange()45     void OnChange() override
46     {}
AsObject()47     sptr<IRemoteObject> AsObject() override
48     {
49         return {};
50     }
51 };
52 }
53 
GetU32Data(const char * ptr)54 uint32_t GetU32Data(const char* ptr)
55 {
56     // convert fuzz input data to an integer
57     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
58 }
59 
GetFuzzAbilityToken()60 sptr<Token> GetFuzzAbilityToken()
61 {
62     sptr<Token> token = nullptr;
63     AbilityRequest abilityRequest;
64     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
65     abilityRequest.abilityInfo.name = "MainAbility";
66     abilityRequest.abilityInfo.type = AbilityType::DATA;
67     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
68     if (abilityRecord) {
69         token = abilityRecord->GetToken();
70     }
71     return token;
72 }
73 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)74 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
75 {
76     bool boolParam = *data % ENABLE;
77     int intParam = static_cast<int>(GetU32Data(data));
78     int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
79     int64_t int64Param = static_cast<int64_t>(GetU32Data(data));
80     uint32_t uint32Param = GetU32Data(data);
81     std::string stringParam(data, size);
82 
83     // fuzz for AbilityEventHandler
84     std::shared_ptr<AppExecFwk::EventRunner> runner;
85     std::weak_ptr<AbilityManagerService> server;
86     auto abilityEventHandler = std::make_shared<AbilityEventHandler>(runner, server);
87     abilityEventHandler->ProcessLoadTimeOut(int64Param);
88     abilityEventHandler->ProcessActiveTimeOut(int64Param);
89     abilityEventHandler->ProcessInactiveTimeOut(int64Param);
90     abilityEventHandler->ProcessForegroundTimeOut(int64Param);
91     abilityEventHandler->ProcessBackgroundTimeOut(int64Param);
92 
93     // fuzz for AbilityInterceptorExecuter
94     auto abilityInterceptorExecuter = std::make_shared<AbilityInterceptorExecuter>();
95     std::shared_ptr<AbilityInterceptor> interceptor;
96     abilityInterceptorExecuter->AddInterceptor(interceptor);
97     Parcel wantParcel;
98     Want *want = nullptr;
99     if (wantParcel.WriteBuffer(data, size)) {
100         want = Want::Unmarshalling(wantParcel);
101         if (!want) {
102             return false;
103         }
104     }
105     abilityInterceptorExecuter->DoProcess(*want, intParam, int32Param, boolParam);
106 
107     // fuzz for AbilityRunningInfo
108     auto abilityRunningInfo = std::make_shared<AbilityRunningInfo>();
109     Parcel parcel;
110     abilityRunningInfo->ReadFromParcel(parcel);
111     abilityRunningInfo->Unmarshalling(parcel);
112     abilityRunningInfo->Marshalling(parcel);
113 
114     // fuzz for AbilitySchedulerProxy
115     sptr<IRemoteObject> impl = GetFuzzAbilityToken();
116     auto abilitySchedulerProxy = std::make_shared<AbilitySchedulerProxy>(impl);
117     MessageParcel messageParcel;
118     abilitySchedulerProxy->WriteInterfaceToken(messageParcel);
119     LifeCycleStateInfo stateInfo;
120     abilitySchedulerProxy->ScheduleAbilityTransaction(*want, stateInfo);
121     abilitySchedulerProxy->SendResult(intParam, intParam, *want);
122     abilitySchedulerProxy->ScheduleConnectAbility(*want);
123     abilitySchedulerProxy->ScheduleDisconnectAbility(*want);
124     abilitySchedulerProxy->ScheduleCommandAbility(*want, boolParam, intParam);
125     abilitySchedulerProxy->ScheduleSaveAbilityState();
126     PacMap inState;
127     abilitySchedulerProxy->ScheduleRestoreAbilityState(inState);
128     Uri uri(stringParam);
129     abilitySchedulerProxy->GetFileTypes(uri, stringParam);
130     abilitySchedulerProxy->OpenFile(uri, stringParam);
131     abilitySchedulerProxy->OpenRawFile(uri, stringParam);
132     PacMap pacMap;
133     abilitySchedulerProxy->Call(uri, stringParam, stringParam, pacMap);
134     NativeRdb::DataAbilityPredicates predicates;
135     abilitySchedulerProxy->Delete(uri, predicates);
136     std::vector<std::string> columns;
137     abilitySchedulerProxy->Query(uri, columns, predicates);
138     abilitySchedulerProxy->GetType(uri);
139     PacMap extras;
140     abilitySchedulerProxy->Reload(uri, extras);
141     sptr<IDataAbilityObserver> dataObserver(new DataAbilityObserver());
142     abilitySchedulerProxy->ScheduleRegisterObserver(uri, dataObserver);
143     abilitySchedulerProxy->ScheduleUnregisterObserver(uri, dataObserver);
144     abilitySchedulerProxy->ScheduleNotifyChange(uri);
145     abilitySchedulerProxy->NormalizeUri(uri);
146     abilitySchedulerProxy->DenormalizeUri(uri);
147     std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> operations;
148     abilitySchedulerProxy->ExecuteBatch(operations);
149     abilitySchedulerProxy->ContinueAbility(stringParam, uint32Param);
150     abilitySchedulerProxy->NotifyContinuationResult(int32Param);
151     std::vector<std::string> stringVector;
152     abilitySchedulerProxy->DumpAbilityInfo(stringVector, stringVector);
153     abilitySchedulerProxy->CallRequest();
154 #ifdef ABILITY_COMMAND_FOR_TEST
155     abilitySchedulerProxy->BlockAbility();
156 #endif
157     // fuzz for AmsConfigurationParameter
158     auto amsConfigurationParameter = std::make_shared<AmsConfigurationParameter>();
159     amsConfigurationParameter->Parse();
160     amsConfigurationParameter->NonConfigFile();
161     amsConfigurationParameter->GetMissionSaveTime();
162     amsConfigurationParameter->GetOrientation();
163     amsConfigurationParameter->GetANRTimeOutTime();
164     amsConfigurationParameter->GetAMSTimeOutTime();
165     amsConfigurationParameter->GetMaxRestartNum(true);
166     amsConfigurationParameter->GetDeviceType();
167     amsConfigurationParameter->GetBootAnimationTimeoutTime();
168     nlohmann::json Object;
169     amsConfigurationParameter->LoadAppConfigurationForStartUpService(Object);
170     amsConfigurationParameter->LoadAppConfigurationForMemoryThreshold(Object);
171     amsConfigurationParameter->LoadSystemConfiguration(Object);
172 
173     return true;
174 }
175 }
176 
177 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)178 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
179 {
180     /* Run your code on data */
181     if (data == nullptr) {
182         std::cout << "invalid data" << std::endl;
183         return 0;
184     }
185 
186     /* Validate the length of size */
187     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
188         return 0;
189     }
190 
191     char* ch = (char *)malloc(size + 1);
192     if (ch == nullptr) {
193         std::cout << "malloc failed." << std::endl;
194         return 0;
195     }
196 
197     (void)memset_s(ch, size + 1, 0x00, size + 1);
198     if (memcpy_s(ch, size, data, size) != EOK) {
199         std::cout << "copy failed." << std::endl;
200         free(ch);
201         ch = nullptr;
202         return 0;
203     }
204 
205     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
206     free(ch);
207     ch = nullptr;
208     return 0;
209 }
210 
211