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 #include <cstring>
20 #include "audio_info.h"
21 #include "audio_policy_server.h"
22 #include "audio_policy_service.h"
23 #include "audio_device_info.h"
24 #include "audio_utils.h"
25 #include "accesstoken_kit.h"
26 #include "nativetoken_kit.h"
27 #include "token_setproc.h"
28 #include "access_token.h"
29 #include "audio_channel_blend.h"
30 #include "volume_ramp.h"
31 #include "audio_speed.h"
32
33 #include "audio_policy_utils.h"
34 #include "audio_stream_descriptor.h"
35 #include "audio_limiter_manager.h"
36 #include "dfx_msg_manager.h"
37
38 #include "audio_source_clock.h"
39 #include "capturer_clock_manager.h"
40 #include "hpae_policy_manager.h"
41 #include "audio_policy_state_monitor.h"
42 #include "audio_device_info.h"
43 #include "audio_spatialization_service.h"
44 #include "suspend/sync_sleep_callback_ipc_interface_code.h"
45 #include "hibernate/sync_hibernate_callback_ipc_interface_code.h"
46
47 namespace OHOS {
48 namespace AudioStandard {
49 using namespace std;
50
51 static const uint8_t* RAW_DATA = nullptr;
52 static size_t g_dataSize = 0;
53 static size_t g_pos;
54 const size_t THRESHOLD = 10;
55 const uint8_t TESTSIZE = 3;
56
57 typedef void (*TestFuncs)();
58
59 template<class T>
GetData()60 T GetData()
61 {
62 T object {};
63 size_t objectSize = sizeof(object);
64 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
65 return object;
66 }
67 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
68 if (ret != EOK) {
69 return {};
70 }
71 g_pos += objectSize;
72 return object;
73 }
74
75 template<class T>
GetArrLength(T & arr)76 uint32_t GetArrLength(T& arr)
77 {
78 if (arr == nullptr) {
79 AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
80 return 0;
81 }
82 return sizeof(arr) / sizeof(arr[0]);
83 }
84
GetEcSamplingRateFuzzTest()85 void GetEcSamplingRateFuzzTest()
86 {
87 std::vector<const char*> deviceList = {
88 USB_CLASS,
89 DP_CLASS,
90 };
91 uint32_t deviceListCount = GetData<uint32_t>() % deviceList.size();
92 string halName = deviceList[deviceListCount];
93 std::shared_ptr<PipeStreamPropInfo> outModuleInfo = std::make_shared<PipeStreamPropInfo>();
94 AudioEcManager& ecManager(AudioEcManager::GetInstance());
95 outModuleInfo->sampleRate_ = GetData<uint32_t>();
96 ecManager.GetEcSamplingRate(halName, outModuleInfo);
97 }
98
GetEcChannelsFuzzTest()99 void GetEcChannelsFuzzTest()
100 {
101 std::vector<const char*> deviceList = {
102 USB_CLASS,
103 DP_CLASS,
104 };
105 uint32_t deviceListCount = GetData<uint32_t>() % deviceList.size();
106 string halName = deviceList[deviceListCount];
107 std::shared_ptr<PipeStreamPropInfo> outModuleInfo = std::make_shared<PipeStreamPropInfo>();
108 AudioEcManager& ecManager(AudioEcManager::GetInstance());
109 outModuleInfo->channelLayout_ = CH_LAYOUT_STEREO;
110 std::vector<std::string> insertList = {"", to_string(GetData<uint32_t>())};
111 uint32_t insertListCount = GetData<uint32_t>() % insertList.size();
112 ecManager.dpSinkModuleInfo_.channels = insertList[insertListCount];
113 ecManager.GetEcChannels(halName, outModuleInfo);
114 }
115
GetEcFormatFuzzTest()116 void GetEcFormatFuzzTest()
117 {
118 std::vector<const char*> deviceList = {
119 USB_CLASS,
120 DP_CLASS,
121 };
122 uint32_t deviceListCount = GetData<uint32_t>() % deviceList.size();
123 string halName = deviceList[deviceListCount];
124 std::shared_ptr<PipeStreamPropInfo> outModuleInfo = std::make_shared<PipeStreamPropInfo>();
125 AudioEcManager& ecManager(AudioEcManager::GetInstance());
126 outModuleInfo->format_ = SAMPLE_S32LE;
127 std::vector<std::string> insertList = {"", to_string(GetData<uint32_t>())};
128 uint32_t insertListCount = GetData<uint32_t>() % insertList.size();
129 ecManager.dpSinkModuleInfo_.format = insertList[insertListCount];
130 ecManager.GetEcFormat(halName, outModuleInfo);
131 }
132
133 TestFuncs g_testFuncs[TESTSIZE] = {
134 GetEcSamplingRateFuzzTest,
135 GetEcChannelsFuzzTest,
136 GetEcFormatFuzzTest,
137 };
138
FuzzTest(const uint8_t * rawData,size_t size)139 bool FuzzTest(const uint8_t* rawData, size_t size)
140 {
141 if (rawData == nullptr) {
142 return false;
143 }
144
145 // initialize data
146 RAW_DATA = rawData;
147 g_dataSize = size;
148 g_pos = 0;
149
150 uint32_t code = GetData<uint32_t>();
151 uint32_t len = GetArrLength(g_testFuncs);
152 if (len > 0) {
153 g_testFuncs[code % len]();
154 } else {
155 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
156 }
157
158 return true;
159 }
160 } // namespace AudioStandard
161 } // namesapce OHOS
162
163 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)164 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
165 {
166 if (size < OHOS::AudioStandard::THRESHOLD) {
167 return 0;
168 }
169
170 OHOS::AudioStandard::FuzzTest(data, size);
171 return 0;
172 }
173