1/* 2 * Copyright (c) 2024 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 "registerabilitylifecyclecallback_fuzzer.h" 17 18#include <cstddef> 19#include <cstdint> 20 21#include "ability_lifecycle_callback.h" 22#include "application_context.h" 23#include "securec.h" 24 25using namespace OHOS::AAFwk; 26using namespace OHOS::AppExecFwk; 27using namespace OHOS::AbilityRuntime; 28 29namespace OHOS { 30namespace { 31constexpr size_t FOO_MAX_LEN = 1024; 32constexpr size_t U32_AT_SIZE = 4; 33} // namespace 34class AbilityLifecycleCallbackFuzz : public AbilityLifecycleCallback { 35public: 36 explicit AbilityLifecycleCallbackFuzz(){}; 37 virtual ~AbilityLifecycleCallbackFuzz(){}; 38 void 39 OnAbilityCreate(const std::shared_ptr<NativeReference> &ability) override{}; 40 void OnWindowStageCreate( 41 const std::shared_ptr<NativeReference> &ability, 42 const std::shared_ptr<NativeReference> &windowStage) override{}; 43 void OnWindowStageDestroy( 44 const std::shared_ptr<NativeReference> &ability, 45 const std::shared_ptr<NativeReference> &windowStage) override{}; 46 void OnWindowStageActive( 47 const std::shared_ptr<NativeReference> &ability, 48 const std::shared_ptr<NativeReference> &windowStage) override{}; 49 void OnWindowStageInactive( 50 const std::shared_ptr<NativeReference> &ability, 51 const std::shared_ptr<NativeReference> &windowStage) override{}; 52 void 53 OnAbilityDestroy(const std::shared_ptr<NativeReference> &ability) override{}; 54 void OnAbilityForeground( 55 const std::shared_ptr<NativeReference> &ability) override{}; 56 void OnAbilityBackground( 57 const std::shared_ptr<NativeReference> &ability) override{}; 58 void 59 OnAbilityContinue(const std::shared_ptr<NativeReference> &ability) override{}; 60}; 61bool DoSomethingInterestingWithMyAPI(const char *data, size_t size) 62{ 63 auto context = ApplicationContext::GetInstance(); 64 if (!context) { 65 return false; 66 } 67 std::shared_ptr<AbilityLifecycleCallbackFuzz> callback; 68 context->RegisterAbilityLifecycleCallback(callback); 69 return true; 70} 71} // namespace OHOS 72 73/* Fuzzer entry point */ 74extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 75{ 76 /* Run your code on data */ 77 if (data == nullptr) { 78 std::cout << "invalid data" << std::endl; 79 return 0; 80 } 81 /* Validate the length of size */ 82 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) { 83 return 0; 84 } 85 char *ch = (char *)malloc(size + 1); 86 if (ch == nullptr) { 87 std::cout << "malloc failed." << std::endl; 88 return 0; 89 } 90 (void)memset_s(ch, size + 1, 0x00, size + 1); 91 if (memcpy_s(ch, size, data, size) != EOK) { 92 std::cout << "copy failed." << std::endl; 93 free(ch); 94 ch = nullptr; 95 return 0; 96 } 97 OHOS::DoSomethingInterestingWithMyAPI(ch, size); 98 free(ch); 99 ch = nullptr; 100 return 0; 101}