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 "amsmgrscheduler_fuzzer.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include <cstddef> 19eace7efcSopenharmony_ci#include <cstdint> 20eace7efcSopenharmony_ci 21eace7efcSopenharmony_ci#define private public 22eace7efcSopenharmony_ci#include "ams_mgr_scheduler.h" 23eace7efcSopenharmony_ci#undef private 24eace7efcSopenharmony_ci#include "ability_record.h" 25eace7efcSopenharmony_ci#include "param.h" 26eace7efcSopenharmony_ci#include "parcel.h" 27eace7efcSopenharmony_ci#include "securec.h" 28eace7efcSopenharmony_ci 29eace7efcSopenharmony_ciusing namespace OHOS::AAFwk; 30eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk; 31eace7efcSopenharmony_ci 32eace7efcSopenharmony_cinamespace OHOS { 33eace7efcSopenharmony_cinamespace { 34eace7efcSopenharmony_ciconstexpr int INPUT_ZERO = 0; 35eace7efcSopenharmony_ciconstexpr int INPUT_ONE = 1; 36eace7efcSopenharmony_ciconstexpr int INPUT_THREE = 3; 37eace7efcSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024; 38eace7efcSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4; 39eace7efcSopenharmony_ciconstexpr uint8_t ENABLE = 2; 40eace7efcSopenharmony_ciconstexpr size_t OFFSET_ZERO = 24; 41eace7efcSopenharmony_ciconstexpr size_t OFFSET_ONE = 16; 42eace7efcSopenharmony_ciconstexpr size_t OFFSET_TWO = 8; 43eace7efcSopenharmony_ci} 44eace7efcSopenharmony_ciuint32_t GetU32Data(const char* ptr) 45eace7efcSopenharmony_ci{ 46eace7efcSopenharmony_ci // convert fuzz input data to an integer 47eace7efcSopenharmony_ci return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) | 48eace7efcSopenharmony_ci ptr[INPUT_THREE]; 49eace7efcSopenharmony_ci} 50eace7efcSopenharmony_cisptr<Token> GetFuzzAbilityToken() 51eace7efcSopenharmony_ci{ 52eace7efcSopenharmony_ci sptr<Token> token = nullptr; 53eace7efcSopenharmony_ci 54eace7efcSopenharmony_ci AbilityRequest abilityRequest; 55eace7efcSopenharmony_ci abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; 56eace7efcSopenharmony_ci abilityRequest.abilityInfo.name = "MainAbility"; 57eace7efcSopenharmony_ci abilityRequest.abilityInfo.type = AbilityType::PAGE; 58eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); 59eace7efcSopenharmony_ci if (abilityRecord) { 60eace7efcSopenharmony_ci token = abilityRecord->GetToken(); 61eace7efcSopenharmony_ci } 62eace7efcSopenharmony_ci 63eace7efcSopenharmony_ci return token; 64eace7efcSopenharmony_ci} 65eace7efcSopenharmony_cistd::shared_ptr<AmsMgrScheduler> DoSomethingInterestingWithMyAPI1(sptr<IRemoteObject> token, 66eace7efcSopenharmony_ci sptr<IRemoteObject> preToken, const char* data) 67eace7efcSopenharmony_ci{ 68eace7efcSopenharmony_ci std::shared_ptr<AppMgrServiceInner> mgrServiceInner; 69eace7efcSopenharmony_ci std::shared_ptr<AAFwk::TaskHandlerWrap> handler; 70eace7efcSopenharmony_ci std::shared_ptr<AmsMgrScheduler> amsMgrScheduler = std::make_shared<AmsMgrScheduler>(mgrServiceInner, handler); 71eace7efcSopenharmony_ci sptr<IStartSpecifiedAbilityResponse> response; 72eace7efcSopenharmony_ci amsMgrScheduler->RegisterStartSpecifiedAbilityResponse(response); 73eace7efcSopenharmony_ci std::shared_ptr<AbilityInfo> abilityInfoptr; 74eace7efcSopenharmony_ci std::shared_ptr<ApplicationInfo> appInfo; 75eace7efcSopenharmony_ci std::shared_ptr<AAFwk::Want> wantptr; 76eace7efcSopenharmony_ci int32_t abilityRecordId = static_cast<int32_t>(GetU32Data(data)); 77eace7efcSopenharmony_ci AbilityRuntime::LoadParam loadParam; 78eace7efcSopenharmony_ci loadParam.abilityRecordId = abilityRecordId; 79eace7efcSopenharmony_ci loadParam.token = token; 80eace7efcSopenharmony_ci loadParam.preToken = preToken; 81eace7efcSopenharmony_ci auto loadParamPtr = std::make_shared<AbilityRuntime::LoadParam>(loadParam); 82eace7efcSopenharmony_ci amsMgrScheduler->LoadAbility(abilityInfoptr, appInfo, wantptr, loadParamPtr); 83eace7efcSopenharmony_ci bool clearMissionFlag = *data % ENABLE; 84eace7efcSopenharmony_ci amsMgrScheduler->TerminateAbility(token, clearMissionFlag); 85eace7efcSopenharmony_ci return amsMgrScheduler; 86eace7efcSopenharmony_ci} 87eace7efcSopenharmony_ci 88eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPI(const char* data, size_t size) 89eace7efcSopenharmony_ci{ 90eace7efcSopenharmony_ci sptr<IRemoteObject> token = GetFuzzAbilityToken(); 91eace7efcSopenharmony_ci sptr<IRemoteObject> preToken = nullptr; 92eace7efcSopenharmony_ci auto amsMgrScheduler = DoSomethingInterestingWithMyAPI1(token, preToken, data); 93eace7efcSopenharmony_ci AppExecFwk::AbilityState state = AppExecFwk::AbilityState::ABILITY_STATE_READY; 94eace7efcSopenharmony_ci amsMgrScheduler->UpdateAbilityState(token, state); 95eace7efcSopenharmony_ci AppExecFwk::ExtensionState extensionState = AppExecFwk::ExtensionState::EXTENSION_STATE_READY; 96eace7efcSopenharmony_ci amsMgrScheduler->UpdateExtensionState(token, extensionState); 97eace7efcSopenharmony_ci bool clearMissionFlag = *data % ENABLE; 98eace7efcSopenharmony_ci amsMgrScheduler->TerminateAbility(token, clearMissionFlag); 99eace7efcSopenharmony_ci sptr<IAppStateCallback> callback; 100eace7efcSopenharmony_ci amsMgrScheduler->RegisterAppStateCallback(callback); 101eace7efcSopenharmony_ci int32_t userId = static_cast<int32_t>(GetU32Data(data)); 102eace7efcSopenharmony_ci amsMgrScheduler->KillProcessesByUserId(userId); 103eace7efcSopenharmony_ci std::string bundleName(data, size); 104eace7efcSopenharmony_ci int accountId = static_cast<int>(GetU32Data(data)); 105eace7efcSopenharmony_ci amsMgrScheduler->KillProcessWithAccount(bundleName, accountId); 106eace7efcSopenharmony_ci amsMgrScheduler->AbilityAttachTimeOut(token); 107eace7efcSopenharmony_ci amsMgrScheduler->PrepareTerminate(token); 108eace7efcSopenharmony_ci amsMgrScheduler->KillApplication(bundleName); 109eace7efcSopenharmony_ci int uid = static_cast<int>(GetU32Data(data)); 110eace7efcSopenharmony_ci amsMgrScheduler->KillApplicationByUid(bundleName, uid); 111eace7efcSopenharmony_ci amsMgrScheduler->KillApplicationSelf(); 112eace7efcSopenharmony_ci AppExecFwk::RunningProcessInfo info; 113eace7efcSopenharmony_ci amsMgrScheduler->GetRunningProcessInfoByToken(token, info); 114eace7efcSopenharmony_ci Parcel wantParcel; 115eace7efcSopenharmony_ci Want* want = nullptr; 116eace7efcSopenharmony_ci if (wantParcel.WriteBuffer(data, size)) { 117eace7efcSopenharmony_ci want = Want::Unmarshalling(wantParcel); 118eace7efcSopenharmony_ci if (!want) { 119eace7efcSopenharmony_ci return false; 120eace7efcSopenharmony_ci } 121eace7efcSopenharmony_ci } 122eace7efcSopenharmony_ci AbilityInfo abilityInfo; 123eace7efcSopenharmony_ci amsMgrScheduler->StartSpecifiedAbility(*want, abilityInfo); 124eace7efcSopenharmony_ci int pid = static_cast<int>(GetU32Data(data)); 125eace7efcSopenharmony_ci AppExecFwk::ApplicationInfo application; 126eace7efcSopenharmony_ci bool debug; 127eace7efcSopenharmony_ci amsMgrScheduler->GetApplicationInfoByProcessID(pid, application, debug); 128eace7efcSopenharmony_ci if (want) { 129eace7efcSopenharmony_ci delete want; 130eace7efcSopenharmony_ci want = nullptr; 131eace7efcSopenharmony_ci } 132eace7efcSopenharmony_ci return amsMgrScheduler->IsReady(); 133eace7efcSopenharmony_ci} 134eace7efcSopenharmony_ci} 135eace7efcSopenharmony_ci 136eace7efcSopenharmony_ci/* Fuzzer entry point */ 137eace7efcSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 138eace7efcSopenharmony_ci{ 139eace7efcSopenharmony_ci /* Run your code on data */ 140eace7efcSopenharmony_ci if (data == nullptr) { 141eace7efcSopenharmony_ci std::cout << "invalid data" << std::endl; 142eace7efcSopenharmony_ci return 0; 143eace7efcSopenharmony_ci } 144eace7efcSopenharmony_ci 145eace7efcSopenharmony_ci /* Validate the length of size */ 146eace7efcSopenharmony_ci if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { 147eace7efcSopenharmony_ci return 0; 148eace7efcSopenharmony_ci } 149eace7efcSopenharmony_ci 150eace7efcSopenharmony_ci char* ch = (char*)malloc(size + 1); 151eace7efcSopenharmony_ci if (ch == nullptr) { 152eace7efcSopenharmony_ci std::cout << "malloc failed." << std::endl; 153eace7efcSopenharmony_ci return 0; 154eace7efcSopenharmony_ci } 155eace7efcSopenharmony_ci 156eace7efcSopenharmony_ci (void)memset_s(ch, size + 1, 0x00, size + 1); 157eace7efcSopenharmony_ci if (memcpy_s(ch, size, data, size) != EOK) { 158eace7efcSopenharmony_ci std::cout << "copy failed." << std::endl; 159eace7efcSopenharmony_ci free(ch); 160eace7efcSopenharmony_ci ch = nullptr; 161eace7efcSopenharmony_ci return 0; 162eace7efcSopenharmony_ci } 163eace7efcSopenharmony_ci 164eace7efcSopenharmony_ci OHOS::DoSomethingInterestingWithMyAPI(ch, size); 165eace7efcSopenharmony_ci free(ch); 166eace7efcSopenharmony_ci ch = nullptr; 167eace7efcSopenharmony_ci return 0; 168eace7efcSopenharmony_ci} 169eace7efcSopenharmony_ci 170