• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
17 #include <cstddef>
18 #include <cstdint>
19 #include "audio_effect_service.h"
20 #include "audio_inner_call.h"
21 using namespace std;
22 
23 namespace OHOS {
24 namespace AudioStandard {
25 using namespace std;
26 static const uint8_t *RAW_DATA = nullptr;
27 static size_t g_dataSize = 0;
28 static size_t g_pos;
29 const size_t FUZZ_INPUT_SIZE_THRESHOLD = 10;
30 typedef void (*TestPtr)();
31 
32 const vector<DeviceType> g_testDeviceTypes = {
33     DEVICE_TYPE_NONE,
34     DEVICE_TYPE_INVALID,
35     DEVICE_TYPE_EARPIECE,
36     DEVICE_TYPE_SPEAKER,
37     DEVICE_TYPE_WIRED_HEADSET,
38     DEVICE_TYPE_WIRED_HEADPHONES,
39     DEVICE_TYPE_BLUETOOTH_SCO,
40     DEVICE_TYPE_BLUETOOTH_A2DP,
41     DEVICE_TYPE_BLUETOOTH_A2DP_IN,
42     DEVICE_TYPE_MIC,
43     DEVICE_TYPE_WAKEUP,
44     DEVICE_TYPE_USB_HEADSET,
45     DEVICE_TYPE_DP,
46     DEVICE_TYPE_REMOTE_CAST,
47     DEVICE_TYPE_USB_DEVICE,
48     DEVICE_TYPE_ACCESSORY,
49     DEVICE_TYPE_REMOTE_DAUDIO,
50     DEVICE_TYPE_HDMI,
51     DEVICE_TYPE_LINE_DIGITAL,
52     DEVICE_TYPE_NEARLINK,
53     DEVICE_TYPE_NEARLINK_IN,
54     DEVICE_TYPE_FILE_SINK,
55     DEVICE_TYPE_FILE_SOURCE,
56     DEVICE_TYPE_EXTERN_CABLE,
57     DEVICE_TYPE_DEFAULT,
58     DEVICE_TYPE_USB_ARM_HEADSET,
59     DEVICE_TYPE_MAX
60 };
61 
62 template<class T>
GetArrLength(T & arr)63 uint32_t GetArrLength(T &arr)
64 {
65     if (arr == nullptr) {
66         AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
67         return 0;
68     }
69     return sizeof(arr) / sizeof(arr[0]);
70 }
71 
72 template<class T>
GetData()73 T GetData()
74 {
75     T object {};
76     size_t objectSize = sizeof(object);
77     if (g_dataSize < g_pos) {
78         return object;
79     }
80     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
81         return object;
82     }
83     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
84     if (ret != EOK) {
85         return {};
86     }
87     g_pos += objectSize;
88     return object;
89 }
90 
AudioEffectServiceFuzzTest()91 void AudioEffectServiceFuzzTest()
92 {
93     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
94     if (audioEffectService == nullptr) {
95         return;
96     }
97     audioEffectService->EffectServiceInit();
98     audioEffectService->BuildAvailableAEConfig();
99 }
100 
AudioEffectServiceGetAvailableEffectsFuzzTest()101 void AudioEffectServiceGetAvailableEffectsFuzzTest()
102 {
103     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
104     if (audioEffectService == nullptr) {
105         return;
106     }
107 
108     std::vector<Effect> availableEffects;
109     audioEffectService->GetAvailableEffects(availableEffects);
110 }
111 
AudioEffectServiceGetOriginalEffectConfigFuzzTest()112 void AudioEffectServiceGetOriginalEffectConfigFuzzTest()
113 {
114     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
115     if (audioEffectService == nullptr) {
116         return;
117     }
118 
119     OriginalEffectConfig oriEffectConfig;
120     audioEffectService->GetOriginalEffectConfig(oriEffectConfig);
121 }
122 
AudioEffectServiceUpdateAvailableEffectsFuzzTest()123 void AudioEffectServiceUpdateAvailableEffectsFuzzTest()
124 {
125     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
126     if (audioEffectService == nullptr) {
127         return;
128     }
129 
130     std::vector<Effect> newAvailableEffects;
131     audioEffectService->UpdateAvailableEffects(newAvailableEffects);
132 }
133 
AudioEffectServiceQueryEffectManagerSceneModeFuzzTest()134 void AudioEffectServiceQueryEffectManagerSceneModeFuzzTest()
135 {
136     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
137     if (audioEffectService == nullptr) {
138         return;
139     }
140 
141     SupportedEffectConfig supportedEffectConfig;
142     audioEffectService->QueryEffectManagerSceneMode(supportedEffectConfig);
143 }
144 
AudioEffectServiceGetSupportedEffectConfigFuzzTest()145 void AudioEffectServiceGetSupportedEffectConfigFuzzTest()
146 {
147     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
148     if (audioEffectService == nullptr) {
149         return;
150     }
151 
152     SupportedEffectConfig supportedEffectConfig;
153     audioEffectService->GetSupportedEffectConfig(supportedEffectConfig);
154 }
155 
AudioEffectServiceSetMasterSinkAvailableFuzzTest()156 void AudioEffectServiceSetMasterSinkAvailableFuzzTest()
157 {
158     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
159     if (audioEffectService == nullptr) {
160         return;
161     }
162 
163     audioEffectService->SetMasterSinkAvailable();
164     audioEffectService->SetEffectChainManagerAvailable();
165     audioEffectService->CanLoadEffectSinks();
166 }
167 
AudioEffectServiceConstructEffectChainModeFuzzTest()168 void AudioEffectServiceConstructEffectChainModeFuzzTest()
169 {
170     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
171     if (audioEffectService == nullptr) {
172         return;
173     }
174 
175     Device device1;
176     Device device2;
177     device2.chain = "chain";
178     device2.type = "DEVICE_TYPE_DEFAULT";
179     StreamEffectMode mode;
180     mode.mode = "mode";
181     mode.devicePort.push_back(device1);
182     mode.devicePort.push_back(device2);
183     std::string sceneType = "sceneType";
184     EffectChainManagerParam effectChainMgrParam;
185     audioEffectService->ConstructEffectChainMode(mode, sceneType, effectChainMgrParam);
186 }
187 
AudioEffectServiceConstructEffectChainManagerParamFuzzTest()188 void AudioEffectServiceConstructEffectChainManagerParamFuzzTest()
189 {
190     static const vector<ScenePriority> testScenePriorities = {
191         DEFAULT_SCENE,
192         PRIOR_SCENE,
193         NORMAL_SCENE,
194     };
195     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
196     if (audioEffectService == nullptr || testScenePriorities.empty()) {
197         return;
198     }
199 
200     Stream stream1;
201     Stream stream2;
202     stream1.scene = "test";
203     stream1.priority = testScenePriorities[GetData<uint32_t>() % testScenePriorities.size()];
204     stream2.scene = "test";
205     stream2.priority = testScenePriorities[GetData<uint32_t>() % testScenePriorities.size()];
206     EffectChainManagerParam effectChainManagerParam;
207     audioEffectService->supportedEffectConfig_.postProcessNew.stream.push_back(stream1);
208     audioEffectService->supportedEffectConfig_.postProcessNew.stream.push_back(stream2);
209     audioEffectService->ConstructEffectChainManagerParam(effectChainManagerParam);
210 }
211 
AudioEffectServiceConstructEnhanceChainManagerParamFuzzTest()212 void AudioEffectServiceConstructEnhanceChainManagerParamFuzzTest()
213 {
214     static const vector<ScenePriority> testScenePriorities = {
215         DEFAULT_SCENE,
216         PRIOR_SCENE,
217         NORMAL_SCENE,
218     };
219     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
220     if (audioEffectService == nullptr || testScenePriorities.empty()) {
221         return;
222     }
223 
224     Stream stream1;
225     Stream stream2;
226     stream1.scene = "test";
227     stream1.priority = testScenePriorities[GetData<uint32_t>() % testScenePriorities.size()];
228     stream2.scene = "test";
229     stream2.priority = testScenePriorities[GetData<uint32_t>() % testScenePriorities.size()];
230     EffectChainManagerParam effectChainManagerParam;
231     audioEffectService->supportedEffectConfig_.preProcessNew.stream.push_back(stream1);
232     audioEffectService->supportedEffectConfig_.preProcessNew.stream.push_back(stream2);
233     audioEffectService->ConstructEnhanceChainManagerParam(effectChainManagerParam);
234 }
235 
AudioEffectServiceAddSupportedPropertyByDeviceInnerFuzzTest()236 void AudioEffectServiceAddSupportedPropertyByDeviceInnerFuzzTest()
237 {
238     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
239     if (audioEffectService == nullptr || g_testDeviceTypes.empty()) {
240         return;
241     }
242 
243     std::set<std::pair<std::string, std::string>> mergedSet;
244     std::unordered_map<std::string, std::set<std::pair<std::string, std::string>>> device2PropertySet;
245     std::set<std::pair<std::string, std::string>> device2Property;
246     device2PropertySet.insert({"DEVICE_TYPE_DEFAULT", device2Property});
247     DeviceType deviceType = g_testDeviceTypes[GetData<uint32_t>() % g_testDeviceTypes.size()];
248     audioEffectService->AddSupportedPropertyByDeviceInner(deviceType, mergedSet, device2PropertySet);
249 }
250 
AudioEffectServiceAddSupportedAudioEffectPropertyByDeviceFuzzTest()251 void AudioEffectServiceAddSupportedAudioEffectPropertyByDeviceFuzzTest()
252 {
253     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
254     if (audioEffectService == nullptr || g_testDeviceTypes.empty()) {
255         return;
256     }
257 
258     std::set<std::pair<std::string, std::string>> mergedSet;
259     DeviceType deviceType = g_testDeviceTypes[GetData<uint32_t>() % g_testDeviceTypes.size()];
260     audioEffectService->AddSupportedAudioEffectPropertyByDevice(deviceType, mergedSet);
261 }
262 
AudioEffectServiceAddSupportedAudioEnhancePropertyByDeviceFuzzTest()263 void AudioEffectServiceAddSupportedAudioEnhancePropertyByDeviceFuzzTest()
264 {
265     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
266     if (audioEffectService == nullptr || g_testDeviceTypes.empty()) {
267         return;
268     }
269 
270     std::set<std::pair<std::string, std::string>> mergedSet;
271     DeviceType deviceType = g_testDeviceTypes[GetData<uint32_t>() % g_testDeviceTypes.size()];
272     audioEffectService->AddSupportedAudioEnhancePropertyByDevice(deviceType, mergedSet);
273 }
274 
AudioEffectServiceUpdateUnavailableEffectChainsFuzzTest()275 void AudioEffectServiceUpdateUnavailableEffectChainsFuzzTest()
276 {
277     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
278     if (audioEffectService == nullptr || g_testDeviceTypes.empty()) {
279         return;
280     }
281 
282     Stream newStream;
283     ProcessNew processNew;
284     StreamEffectMode streamEffect;
285     newStream.streamEffectMode.push_back(streamEffect);
286     processNew.stream.push_back(newStream);
287     std::vector<std::string> availableLayout;
288     audioEffectService->UpdateUnavailableEffectChains(availableLayout, processNew);
289 }
290 
AudioEffectServiceUpdateSupportedEffectPropertyFuzzTest()291 void AudioEffectServiceUpdateSupportedEffectPropertyFuzzTest()
292 {
293     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
294     if (audioEffectService == nullptr || g_testDeviceTypes.empty()) {
295         return;
296     }
297 
298     Device device;
299     Effect effect;
300     EffectChain effectChain;
301     std::unordered_map<std::string, std::set<std::pair<std::string, std::string>>> device2PropertySet;
302     device.chain = "test";
303     effectChain.name = "test";
304     effectChain.apply.push_back("test");
305     effect.effectProperty.push_back("test1");
306     std::set<std::pair<std::string, std::string>> deviceSet = {{"test1", "value1"}, {"test2", "value2"}};
307     device2PropertySet.insert({"test", deviceSet});
308     audioEffectService->supportedEffectConfig_.effectChains.push_back(effectChain);
309     audioEffectService->UpdateSupportedEffectProperty(device, device2PropertySet);
310 }
311 
AudioEffectServiceUpdateAvailableAEConfigFuzzTest()312 void AudioEffectServiceUpdateAvailableAEConfigFuzzTest()
313 {
314     std::shared_ptr<AudioEffectService> audioEffectService = std::make_shared<AudioEffectService>();
315     if (audioEffectService == nullptr) {
316         return;
317     }
318     OriginalEffectConfig aeConfig;
319     SceneMappingItem sceneMappingItem;
320     PreStreamScene preStreamScene;
321     preStreamScene.stream = "SCENE_VOIP_UP";
322     preStreamScene.mode.push_back("ENHANCE_NONE");
323     aeConfig.preProcess.defaultScenes.push_back(preStreamScene);
324     aeConfig.preProcess.priorScenes.push_back(preStreamScene);
325     aeConfig.preProcess.normalScenes.push_back(preStreamScene);
326     aeConfig.postProcess.sceneMap.push_back(sceneMappingItem);
327     audioEffectService->UpdateAvailableAEConfig(aeConfig);
328 }
329 
330 TestPtr g_testPtrs[] = {
331     AudioEffectServiceFuzzTest,
332     AudioEffectServiceGetAvailableEffectsFuzzTest,
333     AudioEffectServiceGetOriginalEffectConfigFuzzTest,
334     AudioEffectServiceUpdateAvailableEffectsFuzzTest,
335     AudioEffectServiceQueryEffectManagerSceneModeFuzzTest,
336     AudioEffectServiceGetSupportedEffectConfigFuzzTest,
337     AudioEffectServiceSetMasterSinkAvailableFuzzTest,
338     AudioEffectServiceConstructEffectChainModeFuzzTest,
339     AudioEffectServiceConstructEffectChainManagerParamFuzzTest,
340     AudioEffectServiceConstructEnhanceChainManagerParamFuzzTest,
341     AudioEffectServiceAddSupportedPropertyByDeviceInnerFuzzTest,
342     AudioEffectServiceAddSupportedAudioEffectPropertyByDeviceFuzzTest,
343     AudioEffectServiceAddSupportedAudioEnhancePropertyByDeviceFuzzTest,
344     AudioEffectServiceUpdateUnavailableEffectChainsFuzzTest,
345     AudioEffectServiceUpdateSupportedEffectPropertyFuzzTest,
346     AudioEffectServiceUpdateAvailableAEConfigFuzzTest,
347 };
348 
FuzzTest(const uint8_t * rawData,size_t size)349 void FuzzTest(const uint8_t *rawData, size_t size)
350 {
351     if (rawData == nullptr) {
352         return;
353     }
354 
355     RAW_DATA = rawData;
356     g_dataSize = size;
357     g_pos = 0;
358 
359     uint32_t code = GetData<uint32_t>();
360     uint32_t len = GetArrLength(g_testPtrs);
361     if (len > 0) {
362         g_testPtrs[code % len]();
363     } else {
364         AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
365     }
366     return;
367 }
368 } // namespace AudioStandard
369 } // namesapce OHOS
370 
371 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)372 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
373 {
374     if (size < OHOS::AudioStandard::FUZZ_INPUT_SIZE_THRESHOLD) {
375         return 0;
376     }
377     OHOS::AudioStandard::FuzzTest(data, size);
378     return 0;
379 }