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 
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 }
34 class AbilityLifecycleCallbackFuzz : public AbilityLifecycleCallback {
35 public:
AbilityLifecycleCallbackFuzz()36     explicit AbilityLifecycleCallbackFuzz() {};
~AbilityLifecycleCallbackFuzz()37     virtual ~AbilityLifecycleCallbackFuzz() {};
OnAbilityCreate(const std::shared_ptr<NativeReference> & ability)38     void OnAbilityCreate(const std::shared_ptr<NativeReference>& ability) override {};
OnWindowStageCreate(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)39     void OnWindowStageCreate(const std::shared_ptr<NativeReference>& ability,
40         const std::shared_ptr<NativeReference>& windowStage) override {};
OnWindowStageDestroy(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)41     void OnWindowStageDestroy(const std::shared_ptr<NativeReference>& ability,
42         const std::shared_ptr<NativeReference>& windowStage) override {};
OnWindowStageActive(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)43     void OnWindowStageActive(const std::shared_ptr<NativeReference>& ability,
44         const std::shared_ptr<NativeReference>& windowStage) override {};
OnWindowStageInactive(const std::shared_ptr<NativeReference> & ability,const std::shared_ptr<NativeReference> & windowStage)45     void OnWindowStageInactive(const std::shared_ptr<NativeReference>& ability,
46         const std::shared_ptr<NativeReference>& windowStage) override {};
OnAbilityDestroy(const std::shared_ptr<NativeReference> & ability)47     void OnAbilityDestroy(const std::shared_ptr<NativeReference>& ability) override {};
OnAbilityForeground(const std::shared_ptr<NativeReference> & ability)48     void OnAbilityForeground(const std::shared_ptr<NativeReference>& ability) override {};
OnAbilityBackground(const std::shared_ptr<NativeReference> & ability)49     void OnAbilityBackground(const std::shared_ptr<NativeReference>& ability) override {};
OnAbilityContinue(const std::shared_ptr<NativeReference> & ability)50     void OnAbilityContinue(const std::shared_ptr<NativeReference>& ability) override {};
51 };
DoSomethingInterestingWithMyAPI(const char * data,size_t size)52 bool 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 */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "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 = static_cast<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 }