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 
25 using namespace OHOS::AAFwk;
26 using namespace OHOS::AppExecFwk;
27 using namespace OHOS::AbilityRuntime;
28 
29 namespace OHOS {
30 namespace {
31 constexpr size_t FOO_MAX_LEN = 1024;
32 constexpr size_t U32_AT_SIZE = 4;
33 } // namespace
34 class AbilityLifecycleCallbackFuzz : public AbilityLifecycleCallback {
35 public:
AbilityLifecycleCallbackFuzz()36     explicit AbilityLifecycleCallbackFuzz(){};
~AbilityLifecycleCallbackFuzz()37     virtual ~AbilityLifecycleCallbackFuzz(){};
38     void
OnAbilityCreate(const std::shared_ptr<NativeReference> & ability)39     OnAbilityCreate(const std::shared_ptr<NativeReference> &ability) override{};
OnWindowStageCreate(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)40     void OnWindowStageCreate(
41         const std::shared_ptr<NativeReference> &ability,
42         const std::shared_ptr<NativeReference> &windowStage) override{};
OnWindowStageDestroy(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)43     void OnWindowStageDestroy(
44         const std::shared_ptr<NativeReference> &ability,
45         const std::shared_ptr<NativeReference> &windowStage) override{};
OnWindowStageActive(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)46     void OnWindowStageActive(
47         const std::shared_ptr<NativeReference> &ability,
48         const std::shared_ptr<NativeReference> &windowStage) override{};
OnWindowStageInactive(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)49     void OnWindowStageInactive(
50         const std::shared_ptr<NativeReference> &ability,
51         const std::shared_ptr<NativeReference> &windowStage) override{};
52     void
OnAbilityDestroy(const std::shared_ptr<NativeReference> & ability)53     OnAbilityDestroy(const std::shared_ptr<NativeReference> &ability) override{};
OnAbilityForeground(const std::shared_ptr<NativeReference> & ability)54     void OnAbilityForeground(
55         const std::shared_ptr<NativeReference> &ability) override{};
OnAbilityBackground(const std::shared_ptr<NativeReference> & ability)56     void OnAbilityBackground(
57         const std::shared_ptr<NativeReference> &ability) override{};
58     void
OnAbilityContinue(const std::shared_ptr<NativeReference> & ability)59     OnAbilityContinue(const std::shared_ptr<NativeReference> &ability) override{};
60 };
DoSomethingInterestingWithMyAPI(const char * data,size_t size)61 bool 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 */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "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 = static_cast<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 }