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 "applifecycledeal_fuzzer.h" 17 18#include <cstddef> 19#include <cstdint> 20 21#include "app_lifecycle_deal.h" 22#include "ability_record.h" 23#include "message_parcel.h" 24#include "securec.h" 25 26using namespace OHOS::AAFwk; 27using namespace OHOS::AppExecFwk; 28 29namespace OHOS { 30namespace { 31constexpr int INPUT_ZERO = 0; 32constexpr int INPUT_ONE = 1; 33constexpr int INPUT_TWO = 2; 34constexpr int INPUT_THREE = 3; 35constexpr size_t FOO_MAX_LEN = 1024; 36constexpr size_t U32_AT_SIZE = 4; 37constexpr size_t OFFSET_ZERO = 24; 38constexpr size_t OFFSET_ONE = 16; 39constexpr size_t OFFSET_TWO = 8; 40} 41uint32_t GetU32Data(const char* ptr) 42{ 43 // convert fuzz input data to an integer 44 return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) | 45 ptr[INPUT_THREE]; 46} 47sptr<Token> GetFuzzAbilityToken() 48{ 49 sptr<Token> token = nullptr; 50 51 AbilityRequest abilityRequest; 52 abilityRequest.appInfo.bundleName = "com.example.fuzzTest"; 53 abilityRequest.abilityInfo.name = "MainAbility"; 54 abilityRequest.abilityInfo.type = AbilityType::PAGE; 55 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest); 56 if (abilityRecord) { 57 token = abilityRecord->GetToken(); 58 } 59 60 return token; 61} 62bool DoSomethingInterestingWithMyAPI(const char* data, size_t size) 63{ 64 AppLifeCycleDeal appLifeCycleDeal; 65 sptr<IAppScheduler> thread = nullptr; 66 appLifeCycleDeal.SetApplicationClient(thread); 67 std::shared_ptr<AbilityRunningRecord> ability = nullptr; 68 appLifeCycleDeal.LaunchAbility(ability); 69 AppLaunchData launchData; 70 Configuration config; 71 appLifeCycleDeal.LaunchApplication(launchData, config); 72 HapModuleInfo abilityStage; 73 appLifeCycleDeal.AddAbilityStage(abilityStage); 74 int32_t timeLevel = static_cast<int32_t>(GetU32Data(data)); 75 appLifeCycleDeal.ScheduleTrimMemory(timeLevel); 76 int32_t level = static_cast<int32_t>(GetU32Data(data)); 77 appLifeCycleDeal.ScheduleMemoryLevel(level); 78 sptr<IRemoteObject> token = GetFuzzAbilityToken(); 79 appLifeCycleDeal.ScheduleCleanAbility(token); 80 Want want; 81 std::string bundleName(data, size); 82 appLifeCycleDeal.ScheduleAcceptWant(want, bundleName); 83 sptr<IQuickFixCallback> callback = nullptr; 84 int32_t recordId = 0; 85 appLifeCycleDeal.NotifyLoadRepairPatch(bundleName, callback, recordId); 86 appLifeCycleDeal.NotifyHotReloadPage(callback, recordId); 87 appLifeCycleDeal.NotifyUnLoadRepairPatch(bundleName, callback, recordId); 88 appLifeCycleDeal.GetApplicationClient(); 89 appLifeCycleDeal.LowMemoryWarning(); 90 appLifeCycleDeal.ScheduleForegroundRunning(); 91 appLifeCycleDeal.ScheduleBackgroundRunning(); 92 appLifeCycleDeal.ScheduleProcessSecurityExit(); 93 appLifeCycleDeal.ScheduleTerminate(); 94 return (appLifeCycleDeal.UpdateConfiguration(config) == 0); 95} 96} 97 98/* Fuzzer entry point */ 99extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 100{ 101 /* Run your code on data */ 102 if (data == nullptr) { 103 std::cout << "invalid data" << std::endl; 104 return 0; 105 } 106 107 /* Validate the length of size */ 108 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { 109 return 0; 110 } 111 112 char* ch = (char*)malloc(size + 1); 113 if (ch == nullptr) { 114 std::cout << "malloc failed." << std::endl; 115 return 0; 116 } 117 118 (void)memset_s(ch, size + 1, 0x00, size + 1); 119 if (memcpy_s(ch, size, data, size) != EOK) { 120 std::cout << "copy failed." << std::endl; 121 free(ch); 122 ch = nullptr; 123 return 0; 124 } 125 126 OHOS::DoSomethingInterestingWithMyAPI(ch, size); 127 free(ch); 128 ch = nullptr; 129 return 0; 130} 131