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 "unregisterabilitylifecyclecallback_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}
34class AbilityLifecycleCallbackFuzz : public AbilityLifecycleCallback {
35public:
36    explicit AbilityLifecycleCallbackFuzz() {};
37    virtual ~AbilityLifecycleCallbackFuzz() {};
38    void OnAbilityCreate(const std::shared_ptr<NativeReference>& ability) override {};
39    void OnWindowStageCreate(const std::shared_ptr<NativeReference>& ability,
40        const std::shared_ptr<NativeReference>& windowStage) override {};
41    void OnWindowStageDestroy(const std::shared_ptr<NativeReference>& ability,
42        const std::shared_ptr<NativeReference>& windowStage) override {};
43    void OnWindowStageActive(const std::shared_ptr<NativeReference>& ability,
44        const std::shared_ptr<NativeReference>& windowStage) override {};
45    void OnWindowStageInactive(const std::shared_ptr<NativeReference>& ability,
46        const std::shared_ptr<NativeReference>& windowStage) override {};
47    void OnAbilityDestroy(const std::shared_ptr<NativeReference>& ability) override {};
48    void OnAbilityForeground(const std::shared_ptr<NativeReference>& ability) override {};
49    void OnAbilityBackground(const std::shared_ptr<NativeReference>& ability) override {};
50    void OnAbilityContinue(const std::shared_ptr<NativeReference>& ability) override {};
51};
52bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
53{
54    auto context = ApplicationContext::GetInstance();
55    if (!context) {
56        return false;
57    }
58
59    std::shared_ptr<AbilityLifecycleCallbackFuzz> callback;
60    context->UnregisterAbilityLifecycleCallback(callback);
61
62    return true;
63}
64}
65
66/* Fuzzer entry point */
67extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
68{
69    /* Run your code on data */
70    if (data == nullptr) {
71        std::cout << "invalid data" << std::endl;
72        return 0;
73    }
74
75    /* Validate the length of size */
76    if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
77        return 0;
78    }
79
80    char* ch = (char*)malloc(size + 1);
81    if (ch == nullptr) {
82        std::cout << "malloc failed." << std::endl;
83        return 0;
84    }
85
86    (void)memset_s(ch, size + 1, 0x00, size + 1);
87    if (memcpy_s(ch, size, data, size) != EOK) {
88        std::cout << "copy failed." << std::endl;
89        free(ch);
90        ch = nullptr;
91        return 0;
92    }
93
94    OHOS::DoSomethingInterestingWithMyAPI(ch, size);
95    free(ch);
96    ch = nullptr;
97    return 0;
98}