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 "abilityfirstframestateobservermanager_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define protected public
22 #include "ability_first_frame_state_observer_manager.h"
23 #undef protected
24 #include "permission_verification.h"
25 #include "securec.h"
26
27 using namespace OHOS::AAFwk;
28 using namespace OHOS::AppExecFwk;
29
30 namespace OHOS {
31 namespace {
32 constexpr int INPUT_ZERO = 0;
33 constexpr int INPUT_ONE = 1;
34 constexpr int INPUT_THREE = 3;
35 constexpr uint8_t ENABLE = 2;
36 constexpr size_t U32_AT_SIZE = 4;
37 constexpr size_t OFFSET_ZERO = 24;
38 constexpr size_t OFFSET_ONE = 16;
39 constexpr size_t OFFSET_TWO = 8;
40 } // namespace
GetU32Data(const char * ptr)41 uint32_t GetU32Data(const char* ptr)
42 {
43 // convert fuzz input data to an integer
44 return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) |
45 (ptr[ENABLE] << OFFSET_TWO) | ptr[INPUT_THREE];
46 }
47
DoSomethingInterestingWithMyAPI(const char * data,size_t size)48 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
49 {
50 bool boolParam = *data % ENABLE;
51 std::string strParam(data, size);
52 std::shared_ptr<AbilityRecord> abilityRecord;
53 sptr<IRemoteBroker> observers;
54 sptr<IAbilityFirstFrameStateObserver> observer;
55 auto abilityFirstFrameStateObserverSet =
56 std::make_shared<AbilityFirstFrameStateObserverSet>(boolParam);
57 abilityFirstFrameStateObserverSet->AddAbilityFirstFrameStateObserver(
58 observer, strParam);
59 abilityFirstFrameStateObserverSet->RemoveAbilityFirstFrameStateObserver(observer);
60 abilityFirstFrameStateObserverSet->OnAbilityFirstFrameState(abilityRecord);
61 abilityFirstFrameStateObserverSet->AddObserverDeathRecipient(observers);
62 abilityFirstFrameStateObserverSet->RemoveObserverDeathRecipient(observers);
63 AbilityFirstFrameStateObserverManager& instance =
64 AbilityFirstFrameStateObserverManager::GetInstance();
65 instance.Init();
66 instance.RegisterAbilityFirstFrameStateObserver(observer, strParam);
67 instance.UnregisterAbilityFirstFrameStateObserver(observer);
68 instance.HandleOnFirstFrameState(abilityRecord);
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::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 + 1, 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 }
102