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