1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2024 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 "uiabilitylifecyclemanagera_fuzzer.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include <cstddef> 19eace7efcSopenharmony_ci#include <cstdint> 20eace7efcSopenharmony_ci 21eace7efcSopenharmony_ci#define private public 22eace7efcSopenharmony_ci#include "ui_ability_lifecycle_manager.h" 23eace7efcSopenharmony_ci#undef private 24eace7efcSopenharmony_ci 25eace7efcSopenharmony_ci#include "ability_record.h" 26eace7efcSopenharmony_ci 27eace7efcSopenharmony_ciusing namespace OHOS::AAFwk; 28eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk; 29eace7efcSopenharmony_ci 30eace7efcSopenharmony_cinamespace OHOS { 31eace7efcSopenharmony_cinamespace { 32eace7efcSopenharmony_ciconstexpr int INPUT_ZERO = 0; 33eace7efcSopenharmony_ciconstexpr int INPUT_ONE = 1; 34eace7efcSopenharmony_ciconstexpr int INPUT_TWO = 2; 35eace7efcSopenharmony_ciconstexpr int INPUT_THREE = 3; 36eace7efcSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024; 37eace7efcSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4; 38eace7efcSopenharmony_ciconstexpr size_t OFFSET_ZERO = 24; 39eace7efcSopenharmony_ciconstexpr size_t OFFSET_ONE = 16; 40eace7efcSopenharmony_ciconstexpr size_t OFFSET_TWO = 8; 41eace7efcSopenharmony_ciconstexpr uint8_t ENABLE = 2; 42eace7efcSopenharmony_ci} // namespace 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[INPUT_TWO] << OFFSET_TWO) | 48eace7efcSopenharmony_ci ptr[INPUT_THREE]; 49eace7efcSopenharmony_ci} 50eace7efcSopenharmony_ci 51eace7efcSopenharmony_cisptr<Token> GetFuzzAbilityToken() 52eace7efcSopenharmony_ci{ 53eace7efcSopenharmony_ci sptr<Token> token = nullptr; 54eace7efcSopenharmony_ci AbilityRequest abilityRequest; 55eace7efcSopenharmony_ci abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; 56eace7efcSopenharmony_ci abilityRequest.abilityInfo.name = "MainAbility"; 57eace7efcSopenharmony_ci abilityRequest.abilityInfo.type = AbilityType::DATA; 58eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); 59eace7efcSopenharmony_ci if (abilityRecord) { 60eace7efcSopenharmony_ci token = abilityRecord->GetToken(); 61eace7efcSopenharmony_ci } 62eace7efcSopenharmony_ci return token; 63eace7efcSopenharmony_ci} 64eace7efcSopenharmony_ci 65eace7efcSopenharmony_civoid DoSomethingInterestingWithMyAPI1(const char *data, size_t size) 66eace7efcSopenharmony_ci{ 67eace7efcSopenharmony_ci int32_t userId = static_cast<int32_t>(GetU32Data(data)); 68eace7efcSopenharmony_ci auto uIAbilityLifecycleManager = std::make_shared<UIAbilityLifecycleManager>(userId); 69eace7efcSopenharmony_ci Want want1; 70eace7efcSopenharmony_ci bool boolParam = *data % ENABLE; 71eace7efcSopenharmony_ci std::string strParam(data, size); 72eace7efcSopenharmony_ci int32_t int32Param = static_cast<int32_t>(GetU32Data(data)); 73eace7efcSopenharmony_ci uint32_t requestId = static_cast<uint32_t>(GetU32Data(data)); 74eace7efcSopenharmony_ci uIAbilityLifecycleManager->OnAcceptWantResponse(want1, strParam, requestId); 75eace7efcSopenharmony_ci uIAbilityLifecycleManager->OnStartSpecifiedProcessResponse(want1, strParam, requestId); 76eace7efcSopenharmony_ci uIAbilityLifecycleManager->OnStartSpecifiedAbilityTimeoutResponse(want1, requestId); 77eace7efcSopenharmony_ci uIAbilityLifecycleManager->OnStartSpecifiedProcessTimeoutResponse(want1, requestId); 78eace7efcSopenharmony_ci uIAbilityLifecycleManager->StartSpecifiedAbilityBySCB(want1); 79eace7efcSopenharmony_ci sptr<IRemoteObject> callStub; 80eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord4; 81eace7efcSopenharmony_ci uIAbilityLifecycleManager->CallRequestDone(abilityRecord4, callStub); 82eace7efcSopenharmony_ci sptr<IAbilityConnection> connect; 83eace7efcSopenharmony_ci AppExecFwk::ElementName element; 84eace7efcSopenharmony_ci sptr<IRemoteObject> token = GetFuzzAbilityToken(); 85eace7efcSopenharmony_ci uIAbilityLifecycleManager->ReleaseCallLocked(connect, element); 86eace7efcSopenharmony_ci std::shared_ptr<CallRecord> callRecord; 87eace7efcSopenharmony_ci uIAbilityLifecycleManager->OnCallConnectDied(callRecord); 88eace7efcSopenharmony_ci uIAbilityLifecycleManager->GetSessionIdByAbilityToken(token); 89eace7efcSopenharmony_ci std::vector<std::string> abilityList; 90eace7efcSopenharmony_ci uIAbilityLifecycleManager->GetActiveAbilityList(int32Param, abilityList, int32Param); 91eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord5; 92eace7efcSopenharmony_ci uIAbilityLifecycleManager->PrepareTerminateAbility(abilityRecord5); 93eace7efcSopenharmony_ci uIAbilityLifecycleManager->GetAbilityRecordsById(int32Param); 94eace7efcSopenharmony_ci uIAbilityLifecycleManager->CheckAbilityNumber(strParam, strParam, strParam); 95eace7efcSopenharmony_ci uIAbilityLifecycleManager->MoreAbilityNumbersSendEventInfo(int32Param, strParam, strParam, strParam); 96eace7efcSopenharmony_ci AppInfo info; 97eace7efcSopenharmony_ci info.state = AppState::TERMINATED; 98eace7efcSopenharmony_ci uIAbilityLifecycleManager->OnAppStateChanged(info); 99eace7efcSopenharmony_ci info.state = AppState::END; 100eace7efcSopenharmony_ci uIAbilityLifecycleManager->OnAppStateChanged(info); 101eace7efcSopenharmony_ci} 102eace7efcSopenharmony_ci 103eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPI(const char *data, size_t size) 104eace7efcSopenharmony_ci{ 105eace7efcSopenharmony_ci int32_t userId = static_cast<int32_t>(GetU32Data(data)); 106eace7efcSopenharmony_ci auto uIAbilityLifecycleManager = std::make_shared<UIAbilityLifecycleManager>(userId); 107eace7efcSopenharmony_ci std::string strParam(data, size); 108eace7efcSopenharmony_ci int32_t uid = static_cast<int32_t>(GetU32Data(data)); 109eace7efcSopenharmony_ci uIAbilityLifecycleManager->SignRestartAppFlag(uid); 110eace7efcSopenharmony_ci AbilityRequest abilityRequest; 111eace7efcSopenharmony_ci sptr<SessionInfo> sessionInfo; 112eace7efcSopenharmony_ci uint32_t sceneFlag = GetU32Data(data); 113eace7efcSopenharmony_ci bool boolParam = *data % ENABLE; 114eace7efcSopenharmony_ci uIAbilityLifecycleManager->StartUIAbility(abilityRequest, sessionInfo, sceneFlag, boolParam); 115eace7efcSopenharmony_ci sptr<IRemoteObject> token = GetFuzzAbilityToken(); 116eace7efcSopenharmony_ci int intParam = static_cast<int>(GetU32Data(data)); 117eace7efcSopenharmony_ci AppExecFwk::PacMap saveData; 118eace7efcSopenharmony_ci uIAbilityLifecycleManager->AbilityTransactionDone(token, intParam, saveData); 119eace7efcSopenharmony_ci sptr<IAbilityScheduler> scheduler; 120eace7efcSopenharmony_ci uIAbilityLifecycleManager->AttachAbilityThread(scheduler, token); 121eace7efcSopenharmony_ci int32_t int32Param = static_cast<int32_t>(GetU32Data(data)); 122eace7efcSopenharmony_ci uIAbilityLifecycleManager->OnAbilityRequestDone(token, int32Param); 123eace7efcSopenharmony_ci uIAbilityLifecycleManager->IsContainsAbility(token); 124eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord; 125eace7efcSopenharmony_ci uIAbilityLifecycleManager->NotifySCBToMinimizeUIAbility(abilityRecord, token); 126eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord1; 127eace7efcSopenharmony_ci uIAbilityLifecycleManager->MinimizeUIAbility(abilityRecord1, boolParam, sceneFlag); 128eace7efcSopenharmony_ci sptr<SessionInfo> sessionInfo1; 129eace7efcSopenharmony_ci uIAbilityLifecycleManager->GetUIAbilityRecordBySessionInfo(sessionInfo1); 130eace7efcSopenharmony_ci Want *want; 131eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord2; 132eace7efcSopenharmony_ci uIAbilityLifecycleManager->CloseUIAbility(abilityRecord2, intParam, want, boolParam); 133eace7efcSopenharmony_ci sptr<IRemoteObject> rootSceneSession; 134eace7efcSopenharmony_ci uIAbilityLifecycleManager->SetRootSceneSession(rootSceneSession); 135eace7efcSopenharmony_ci AbilityRequest abilityRequest1; 136eace7efcSopenharmony_ci uIAbilityLifecycleManager->NotifySCBToStartUIAbility(abilityRequest1); 137eace7efcSopenharmony_ci sptr<SessionInfo> sessionInfo2; 138eace7efcSopenharmony_ci AbilityRequest abilityRequest2; 139eace7efcSopenharmony_ci uIAbilityLifecycleManager->NotifySCBToPreStartUIAbility(abilityRequest2, sessionInfo2); 140eace7efcSopenharmony_ci uint32_t msgId = static_cast<uint32_t>(GetU32Data(data)); 141eace7efcSopenharmony_ci int64_t abilityRecordId = static_cast<int64_t>(GetU32Data(data)); 142eace7efcSopenharmony_ci uIAbilityLifecycleManager->OnTimeOut(msgId, abilityRecordId, boolParam); 143eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> abilityRecord3; 144eace7efcSopenharmony_ci AbilityRequest abilityRequest3; 145eace7efcSopenharmony_ci uIAbilityLifecycleManager->OnAbilityDied(abilityRecord3); 146eace7efcSopenharmony_ci uIAbilityLifecycleManager->ResolveLocked(abilityRequest3); 147eace7efcSopenharmony_ci sptr<SessionInfo> sessionInfo3; 148eace7efcSopenharmony_ci uIAbilityLifecycleManager->CallUIAbilityBySCB(sessionInfo3, boolParam); 149eace7efcSopenharmony_ci DoSomethingInterestingWithMyAPI1(data, size); 150eace7efcSopenharmony_ci 151eace7efcSopenharmony_ci return true; 152eace7efcSopenharmony_ci} 153eace7efcSopenharmony_ci} // namespace OHOS 154eace7efcSopenharmony_ci 155eace7efcSopenharmony_ci/* Fuzzer entry point */ 156eace7efcSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 157eace7efcSopenharmony_ci{ 158eace7efcSopenharmony_ci /* Run your code on data */ 159eace7efcSopenharmony_ci if (data == nullptr) { 160eace7efcSopenharmony_ci std::cout << "invalid data" << std::endl; 161eace7efcSopenharmony_ci return 0; 162eace7efcSopenharmony_ci } 163eace7efcSopenharmony_ci 164eace7efcSopenharmony_ci /* Validate the length of size */ 165eace7efcSopenharmony_ci if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { 166eace7efcSopenharmony_ci return 0; 167eace7efcSopenharmony_ci } 168eace7efcSopenharmony_ci 169eace7efcSopenharmony_ci char *ch = (char *)malloc(size + 1); 170eace7efcSopenharmony_ci if (ch == nullptr) { 171eace7efcSopenharmony_ci std::cout << "malloc failed." << std::endl; 172eace7efcSopenharmony_ci return 0; 173eace7efcSopenharmony_ci } 174eace7efcSopenharmony_ci 175eace7efcSopenharmony_ci (void)memset_s(ch, size + 1, 0x00, size + 1); 176eace7efcSopenharmony_ci if (memcpy_s(ch, size + 1, data, size) != EOK) { 177eace7efcSopenharmony_ci std::cout << "copy failed." << std::endl; 178eace7efcSopenharmony_ci free(ch); 179eace7efcSopenharmony_ci ch = nullptr; 180eace7efcSopenharmony_ci return 0; 181eace7efcSopenharmony_ci } 182eace7efcSopenharmony_ci 183eace7efcSopenharmony_ci OHOS::DoSomethingInterestingWithMyAPI(ch, size); 184eace7efcSopenharmony_ci free(ch); 185eace7efcSopenharmony_ci ch = nullptr; 186eace7efcSopenharmony_ci return 0; 187eace7efcSopenharmony_ci}