1 /*
2 * Copyright (c) 2025 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 <iostream>
17 #include <cstddef>
18 #include <cstdint>
19 #undef private
20 #include "audio_info.h"
21 #include "simd_utils.h"
22 #include "hpae_pcm_buffer.h"
23 #include "hpae_pcm_process.h"
24 #include "audio_log.h"
25 using namespace std;
26 using namespace OHOS::AudioStandard::HPAE;
27
28 namespace OHOS {
29 namespace AudioStandard {
30 using namespace std;
31 static const uint8_t *RAW_DATA = nullptr;
32 static size_t g_dataSize = 0;
33 static size_t g_pos;
34 const size_t THRESHOLD = 10;
35 typedef void (*TestPtr)(const uint8_t *, size_t);
36
37 template<class T>
GetData()38 T GetData()
39 {
40 T object {};
41 size_t objectSize = sizeof(object);
42 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
43 return object;
44 }
45 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
46 if (ret != EOK) {
47 return {};
48 }
49 g_pos += objectSize;
50 return object;
51 }
52
53 template<class T>
GetArrLength(T & arr)54 uint32_t GetArrLength(T& arr)
55 {
56 if (arr == nullptr) {
57 AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
58 return 0;
59 }
60 return sizeof(arr) / sizeof(arr[0]);
61 }
62
SizeFuzzTest()63 void SizeFuzzTest()
64 {
65 std::vector<float> pcmData = {GetData<float>(), GetData<float>(), GetData<float>(), GetData<float>(),
66 GetData<float>()};
67 float *begin = pcmData.data();
68 size_t size = pcmData.size();
69 auto hpaePcmProcess = std::make_shared<HpaePcmProcess>(begin, size);
70 hpaePcmProcess->Size();
71 }
72
BeginFuzzTest()73 void BeginFuzzTest()
74 {
75 std::vector<float> pcmData = {GetData<float>(), GetData<float>(), GetData<float>(), GetData<float>(),
76 GetData<float>()};
77 float *begin = pcmData.data();
78 size_t size = pcmData.size();
79 auto hpaePcmProcess = std::make_shared<HpaePcmProcess>(begin, size);
80 hpaePcmProcess->Begin();
81 }
82
EndFuzzTest()83 void EndFuzzTest()
84 {
85 std::vector<float> pcmData = {GetData<float>(), GetData<float>(), GetData<float>(), GetData<float>(),
86 GetData<float>()};
87 float *begin = pcmData.data();
88 size_t size = pcmData.size();
89 auto hpaePcmProcess = std::make_shared<HpaePcmProcess>(begin, size);
90 hpaePcmProcess->End();
91 }
92
ResetFuzzTest()93 void ResetFuzzTest()
94 {
95 std::vector<float> pcmData = {GetData<float>(), GetData<float>(), GetData<float>(), GetData<float>(),
96 GetData<float>()};
97 float *begin = pcmData.data();
98 size_t size = pcmData.size();
99 auto hpaePcmProcess = std::make_shared<HpaePcmProcess>(begin, size);
100 hpaePcmProcess->Reset();
101 }
102
GetErrNoFuzzTest()103 void GetErrNoFuzzTest()
104 {
105 std::vector<float> pcmData = {GetData<float>(), GetData<float>(), GetData<float>(), GetData<float>(),
106 GetData<float>()};
107 float *begin = pcmData.data();
108 size_t size = pcmData.size();
109 auto hpaePcmProcess = std::make_shared<HpaePcmProcess>(begin, size);
110 hpaePcmProcess->GetErrNo();
111 }
112
113 typedef void (*TestFuncs[5])();
114
115 TestFuncs g_testFuncs = {
116 SizeFuzzTest,
117 BeginFuzzTest,
118 EndFuzzTest,
119 ResetFuzzTest,
120 GetErrNoFuzzTest,
121 };
122
FuzzTest(const uint8_t * rawData,size_t size)123 bool FuzzTest(const uint8_t *rawData, size_t size)
124 {
125 if (rawData == nullptr) {
126 return false;
127 }
128
129 // initialize data
130 RAW_DATA = rawData;
131 g_dataSize = size;
132 g_pos = 0;
133
134 uint32_t code = GetData<uint32_t>();
135 uint32_t len = GetArrLength(g_testFuncs);
136 if (len > 0) {
137 g_testFuncs[code % len]();
138 } else {
139 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
140 }
141
142 return true;
143 }
144
145 } // namespace AudioStandard
146 } // namespace OHOS
147
148 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)149 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
150 {
151 if (size < OHOS::AudioStandard::THRESHOLD) {
152 return 0;
153 }
154
155 OHOS::AudioStandard::FuzzTest(data, size);
156 return 0;
157 }