1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "abilityeventhandler_fuzzer.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include <cstddef> 19eace7efcSopenharmony_ci#include <cstdint> 20eace7efcSopenharmony_ci 21eace7efcSopenharmony_ci#define private public 22eace7efcSopenharmony_ci#include "ability_event_handler.h" 23eace7efcSopenharmony_ci#include "interceptor/ability_interceptor_executer.h" 24eace7efcSopenharmony_ci#include "ability_running_info.h" 25eace7efcSopenharmony_ci#include "ability_scheduler_proxy.h" 26eace7efcSopenharmony_ci#include "ams_configuration_parameter.h" 27eace7efcSopenharmony_ci#undef private 28eace7efcSopenharmony_ci 29eace7efcSopenharmony_ci#include "ability_record.h" 30eace7efcSopenharmony_ci#include "data_ability_observer_interface.h" 31eace7efcSopenharmony_ci#include "data_ability_predicates.h" 32eace7efcSopenharmony_ci 33eace7efcSopenharmony_ciusing namespace OHOS::AAFwk; 34eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk; 35eace7efcSopenharmony_ci 36eace7efcSopenharmony_cinamespace OHOS { 37eace7efcSopenharmony_cinamespace { 38eace7efcSopenharmony_ciconstexpr int INPUT_ZERO = 0; 39eace7efcSopenharmony_ciconstexpr int INPUT_ONE = 1; 40eace7efcSopenharmony_ciconstexpr int INPUT_THREE = 3; 41eace7efcSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024; 42eace7efcSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4; 43eace7efcSopenharmony_ciconstexpr uint8_t ENABLE = 2; 44eace7efcSopenharmony_ciconstexpr int OFFSET_ZERO = 24; 45eace7efcSopenharmony_ciconstexpr int OFFSET_ONE = 16; 46eace7efcSopenharmony_ciconstexpr int OFFSET_TWO = 8; 47eace7efcSopenharmony_ciclass DataAbilityObserver : public IDataAbilityObserver { 48eace7efcSopenharmony_cipublic: 49eace7efcSopenharmony_ci DataAbilityObserver() = default; 50eace7efcSopenharmony_ci virtual ~DataAbilityObserver() = default; 51eace7efcSopenharmony_ci void OnChange() override 52eace7efcSopenharmony_ci {} 53eace7efcSopenharmony_ci sptr<IRemoteObject> AsObject() override 54eace7efcSopenharmony_ci { 55eace7efcSopenharmony_ci return {}; 56eace7efcSopenharmony_ci } 57eace7efcSopenharmony_ci}; 58eace7efcSopenharmony_ci} 59eace7efcSopenharmony_ci 60eace7efcSopenharmony_ciuint32_t GetU32Data(const char* ptr) 61eace7efcSopenharmony_ci{ 62eace7efcSopenharmony_ci // convert fuzz input data to an integer 63eace7efcSopenharmony_ci return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) | 64eace7efcSopenharmony_ci ptr[INPUT_THREE]; 65eace7efcSopenharmony_ci} 66eace7efcSopenharmony_ci 67eace7efcSopenharmony_cisptr<Token> GetFuzzAbilityToken() 68eace7efcSopenharmony_ci{ 69eace7efcSopenharmony_ci sptr<Token> token = nullptr; 70eace7efcSopenharmony_ci AbilityRequest abilityRequest; 71eace7efcSopenharmony_ci abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; 72eace7efcSopenharmony_ci abilityRequest.abilityInfo.name = "MainAbility"; 73eace7efcSopenharmony_ci abilityRequest.abilityInfo.type = AbilityType::DATA; 74eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); 75eace7efcSopenharmony_ci if (abilityRecord) { 76eace7efcSopenharmony_ci token = abilityRecord->GetToken(); 77eace7efcSopenharmony_ci } 78eace7efcSopenharmony_ci return token; 79eace7efcSopenharmony_ci} 80eace7efcSopenharmony_ci 81eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPI(const char* data, size_t size) 82eace7efcSopenharmony_ci{ 83eace7efcSopenharmony_ci bool boolParam = *data % ENABLE; 84eace7efcSopenharmony_ci int intParam = static_cast<int>(GetU32Data(data)); 85eace7efcSopenharmony_ci int32_t int32Param = static_cast<int32_t>(GetU32Data(data)); 86eace7efcSopenharmony_ci int64_t int64Param = static_cast<int64_t>(GetU32Data(data)); 87eace7efcSopenharmony_ci uint32_t uint32Param = GetU32Data(data); 88eace7efcSopenharmony_ci std::string stringParam(data, size); 89eace7efcSopenharmony_ci 90eace7efcSopenharmony_ci // fuzz for AbilityEventHandler 91eace7efcSopenharmony_ci std::shared_ptr<TaskHandlerWrap> runner; 92eace7efcSopenharmony_ci std::weak_ptr<AbilityManagerService> server; 93eace7efcSopenharmony_ci auto abilityEventHandler = std::make_shared<AbilityEventHandler>(runner, server); 94eace7efcSopenharmony_ci abilityEventHandler->ProcessLoadTimeOut(int64Param); 95eace7efcSopenharmony_ci abilityEventHandler->ProcessActiveTimeOut(int64Param); 96eace7efcSopenharmony_ci abilityEventHandler->ProcessInactiveTimeOut(int64Param); 97eace7efcSopenharmony_ci abilityEventHandler->ProcessForegroundTimeOut(int64Param); 98eace7efcSopenharmony_ci 99eace7efcSopenharmony_ci // fuzz for AbilityInterceptorExecuter 100eace7efcSopenharmony_ci auto abilityInterceptorExecuter = std::make_shared<AbilityInterceptorExecuter>(); 101eace7efcSopenharmony_ci std::shared_ptr<AbilityInterceptor> interceptor; 102eace7efcSopenharmony_ci abilityInterceptorExecuter->AddInterceptor(interceptor); 103eace7efcSopenharmony_ci Parcel wantParcel; 104eace7efcSopenharmony_ci Want* want = nullptr; 105eace7efcSopenharmony_ci if (wantParcel.WriteBuffer(data, size)) { 106eace7efcSopenharmony_ci want = Want::Unmarshalling(wantParcel); 107eace7efcSopenharmony_ci if (!want) { 108eace7efcSopenharmony_ci return false; 109eace7efcSopenharmony_ci } 110eace7efcSopenharmony_ci } 111eace7efcSopenharmony_ci 112eace7efcSopenharmony_ci AbilityInterceptorParam interceptorParam = AbilityInterceptorParam( 113eace7efcSopenharmony_ci *want, intParam, int32Param, boolParam, nullptr, nullptr); 114eace7efcSopenharmony_ci abilityInterceptorExecuter->DoProcess(interceptorParam); 115eace7efcSopenharmony_ci 116eace7efcSopenharmony_ci // fuzz for AbilityRunningInfo 117eace7efcSopenharmony_ci auto abilityRunningInfo = std::make_shared<AbilityRunningInfo>(); 118eace7efcSopenharmony_ci Parcel parcel; 119eace7efcSopenharmony_ci abilityRunningInfo->ReadFromParcel(parcel); 120eace7efcSopenharmony_ci abilityRunningInfo->Unmarshalling(parcel); 121eace7efcSopenharmony_ci abilityRunningInfo->Marshalling(parcel); 122eace7efcSopenharmony_ci 123eace7efcSopenharmony_ci // fuzz for AbilitySchedulerProxy 124eace7efcSopenharmony_ci sptr<IRemoteObject> impl = GetFuzzAbilityToken(); 125eace7efcSopenharmony_ci auto abilitySchedulerProxy = std::make_shared<AbilitySchedulerProxy>(impl); 126eace7efcSopenharmony_ci MessageParcel messageParcel; 127eace7efcSopenharmony_ci abilitySchedulerProxy->WriteInterfaceToken(messageParcel); 128eace7efcSopenharmony_ci LifeCycleStateInfo stateInfo; 129eace7efcSopenharmony_ci abilitySchedulerProxy->ScheduleAbilityTransaction(*want, stateInfo); 130eace7efcSopenharmony_ci abilitySchedulerProxy->SendResult(intParam, intParam, *want); 131eace7efcSopenharmony_ci abilitySchedulerProxy->ScheduleConnectAbility(*want); 132eace7efcSopenharmony_ci abilitySchedulerProxy->ScheduleDisconnectAbility(*want); 133eace7efcSopenharmony_ci abilitySchedulerProxy->ScheduleCommandAbility(*want, boolParam, intParam); 134eace7efcSopenharmony_ci abilitySchedulerProxy->ScheduleSaveAbilityState(); 135eace7efcSopenharmony_ci PacMap inState; 136eace7efcSopenharmony_ci abilitySchedulerProxy->ScheduleRestoreAbilityState(inState); 137eace7efcSopenharmony_ci Uri uri(stringParam); 138eace7efcSopenharmony_ci abilitySchedulerProxy->GetFileTypes(uri, stringParam); 139eace7efcSopenharmony_ci abilitySchedulerProxy->OpenFile(uri, stringParam); 140eace7efcSopenharmony_ci abilitySchedulerProxy->OpenRawFile(uri, stringParam); 141eace7efcSopenharmony_ci PacMap pacMap; 142eace7efcSopenharmony_ci abilitySchedulerProxy->Call(uri, stringParam, stringParam, pacMap); 143eace7efcSopenharmony_ci NativeRdb::DataAbilityPredicates predicates; 144eace7efcSopenharmony_ci abilitySchedulerProxy->Delete(uri, predicates); 145eace7efcSopenharmony_ci std::vector<std::string> columns; 146eace7efcSopenharmony_ci abilitySchedulerProxy->Query(uri, columns, predicates); 147eace7efcSopenharmony_ci abilitySchedulerProxy->GetType(uri); 148eace7efcSopenharmony_ci PacMap extras; 149eace7efcSopenharmony_ci abilitySchedulerProxy->Reload(uri, extras); 150eace7efcSopenharmony_ci sptr<IDataAbilityObserver> dataObserver(new DataAbilityObserver()); 151eace7efcSopenharmony_ci abilitySchedulerProxy->ScheduleRegisterObserver(uri, dataObserver); 152eace7efcSopenharmony_ci abilitySchedulerProxy->ScheduleUnregisterObserver(uri, dataObserver); 153eace7efcSopenharmony_ci abilitySchedulerProxy->ScheduleNotifyChange(uri); 154eace7efcSopenharmony_ci abilitySchedulerProxy->NormalizeUri(uri); 155eace7efcSopenharmony_ci abilitySchedulerProxy->DenormalizeUri(uri); 156eace7efcSopenharmony_ci std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> operations; 157eace7efcSopenharmony_ci abilitySchedulerProxy->ExecuteBatch(operations); 158eace7efcSopenharmony_ci abilitySchedulerProxy->ContinueAbility(stringParam, uint32Param); 159eace7efcSopenharmony_ci abilitySchedulerProxy->NotifyContinuationResult(int32Param); 160eace7efcSopenharmony_ci std::vector<std::string> stringVector; 161eace7efcSopenharmony_ci abilitySchedulerProxy->DumpAbilityInfo(stringVector, stringVector); 162eace7efcSopenharmony_ci abilitySchedulerProxy->CallRequest(); 163eace7efcSopenharmony_ci // fuzz for AmsConfigurationParameter 164eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().NonConfigFile(); 165eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().GetMissionSaveTime(); 166eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().GetOrientation(); 167eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().GetANRTimeOutTime(); 168eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().GetAMSTimeOutTime(); 169eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().GetMaxRestartNum(true); 170eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().GetDeviceType(); 171eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().GetBootAnimationTimeoutTime(); 172eace7efcSopenharmony_ci nlohmann::json Object; 173eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().LoadAppConfigurationForStartUpService(Object); 174eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().LoadAppConfigurationForMemoryThreshold(Object); 175eace7efcSopenharmony_ci AmsConfigurationParameter::GetInstance().LoadSystemConfiguration(Object); 176eace7efcSopenharmony_ci 177eace7efcSopenharmony_ci return true; 178eace7efcSopenharmony_ci} 179eace7efcSopenharmony_ci} 180eace7efcSopenharmony_ci 181eace7efcSopenharmony_ci/* Fuzzer entry point */ 182eace7efcSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 183eace7efcSopenharmony_ci{ 184eace7efcSopenharmony_ci /* Run your code on data */ 185eace7efcSopenharmony_ci if (data == nullptr) { 186eace7efcSopenharmony_ci std::cout << "invalid data" << std::endl; 187eace7efcSopenharmony_ci return 0; 188eace7efcSopenharmony_ci } 189eace7efcSopenharmony_ci 190eace7efcSopenharmony_ci /* Validate the length of size */ 191eace7efcSopenharmony_ci if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { 192eace7efcSopenharmony_ci return 0; 193eace7efcSopenharmony_ci } 194eace7efcSopenharmony_ci 195eace7efcSopenharmony_ci char* ch = (char*)malloc(size + 1); 196eace7efcSopenharmony_ci if (ch == nullptr) { 197eace7efcSopenharmony_ci std::cout << "malloc failed." << std::endl; 198eace7efcSopenharmony_ci return 0; 199eace7efcSopenharmony_ci } 200eace7efcSopenharmony_ci 201eace7efcSopenharmony_ci (void)memset_s(ch, size + 1, 0x00, size + 1); 202eace7efcSopenharmony_ci if (memcpy_s(ch, size, data, size) != EOK) { 203eace7efcSopenharmony_ci std::cout << "copy failed." << std::endl; 204eace7efcSopenharmony_ci free(ch); 205eace7efcSopenharmony_ci ch = nullptr; 206eace7efcSopenharmony_ci return 0; 207eace7efcSopenharmony_ci } 208eace7efcSopenharmony_ci 209eace7efcSopenharmony_ci OHOS::DoSomethingInterestingWithMyAPI(ch, size); 210eace7efcSopenharmony_ci free(ch); 211eace7efcSopenharmony_ci ch = nullptr; 212eace7efcSopenharmony_ci return 0; 213eace7efcSopenharmony_ci} 214eace7efcSopenharmony_ci 215