1 /*
2 * Copyright (c) 2023 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 "bootanimation_fuzzer.h"
17
18 #include <cstdint>
19 #include <securec.h>
20
21 #include "boot_animation_utils.h"
22
23 namespace OHOS {
24 namespace {
25 const uint8_t* g_data = nullptr;
26 size_t g_size = 0;
27 size_t g_pos;
28 }
29
30 /*
31 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
32 * tips: only support basic type
33 */
34 template<class T>
GetData()35 T GetData()
36 {
37 T object {};
38 size_t objectSize = sizeof(object);
39 if (g_data == nullptr || objectSize > g_size - g_pos) {
40 return object;
41 }
42 errno_t ret = memcpy_s(&object, objectSize, g_data + g_pos, objectSize);
43 if (ret != EOK) {
44 return {};
45 }
46 g_pos += objectSize;
47 return object;
48 }
49
BootAnimationUtilsFuzzerTest()50 void BootAnimationUtilsFuzzerTest()
51 {
52 // get data
53 bool isEnabled = GetData<bool>();
54 BootAnimationUtils::SetBootAnimationSoundEnabled(isEnabled);
55 BootAnimationUtils::GetBootAnimationSoundEnabled();
56 }
57
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)58 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
59 {
60 if (data == nullptr) {
61 return false;
62 }
63 // initialize
64 g_data = data;
65 g_size = size;
66 g_pos = 0;
67
68 BootAnimationUtilsFuzzerTest();
69 return true;
70 }
71 }
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 OHOS::DoSomethingInterestingWithMyAPI(data, size);
78 return 0;
79 }
80
81