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
45 namespace OHOS {
46 namespace AudioStandard {
47 using namespace std;
48
49 static const uint8_t* RAW_DATA = nullptr;
50 static size_t g_dataSize = 0;
51 static size_t g_pos;
52 const size_t THRESHOLD = 10;
53 const uint8_t TESTSIZE = 11;
54
55 typedef void (*TestFuncs)();
56
57 template<class T>
GetData()58 T GetData()
59 {
60 T object {};
61 size_t objectSize = sizeof(object);
62 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
63 return object;
64 }
65 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
66 if (ret != EOK) {
67 return {};
68 }
69 g_pos += objectSize;
70 return object;
71 }
72
73 template<class T>
GetArrLength(T & arr)74 uint32_t GetArrLength(T& arr)
75 {
76 if (arr == nullptr) {
77 AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
78 return 0;
79 }
80 return sizeof(arr) / sizeof(arr[0]);
81 }
82
FlushDfxMsgFuzzTest()83 void FlushDfxMsgFuzzTest()
84 {
85 AudioInterruptDfxCollector dfxCollector;
86 uint32_t index = 0;
87 uint32_t appUid = GetData<uint32_t>();
88 std::list<InterruptDfxInfo> dfxInfoList;
89 dfxCollector.dfxInfos_[index] = dfxInfoList;
90 dfxCollector.FlushDfxMsg(index, appUid);
91 }
92
WriteEffectMsgFuzzTest()93 void WriteEffectMsgFuzzTest()
94 {
95 InterruptDfxBuilder dfxBuilder;
96 uint8_t appstate = GetData<uint32_t>();
97 std::string bundleName = "com.ohos.test";
98 AudioInterrupt audioInterrupt;
99 int32_t hintTypeCount = static_cast<int32_t>(InterruptHint::INTERRUPT_HINT_UNMUTE) + 1;
100 InterruptHint hintType = static_cast<InterruptHint>(GetData<uint8_t>() % hintTypeCount);
101 dfxBuilder.WriteEffectMsg(appstate, bundleName, audioInterrupt, hintType);
102 }
103
GetDfxIndexesFuzzTest()104 void GetDfxIndexesFuzzTest()
105 {
106 AudioInterruptDfxCollector dfxCollector;
107 uint32_t index = 0;
108 std::list<InterruptDfxInfo> dfxInfoList;
109 dfxCollector.dfxInfos_[index] = dfxInfoList;
110 dfxCollector.GetDfxIndexes(index);
111 }
112
WriteActionMsgFuzzTest()113 void WriteActionMsgFuzzTest()
114 {
115 InterruptDfxBuilder dfxBuilder;
116 uint8_t infoIndex = GetData<uint32_t>();
117 uint8_t effectIdx = GetData<uint32_t>();
118 InterruptStage stage = INTERRUPT_STAGE_STOP;
119 dfxBuilder.WriteActionMsg(infoIndex, effectIdx, stage);
120 }
121
WriteInfoMsgFuzzTest()122 void WriteInfoMsgFuzzTest()
123 {
124 InterruptDfxBuilder dfxBuilder;
125 AudioInterrupt audioInterrupt;
126 AudioSessionStrategy strategy;
127 InterruptRole interruptType = INTERRUPT_ROLE_DEFAULT;
128 dfxBuilder.WriteInfoMsg(audioInterrupt, strategy, interruptType);
129 }
130
GetFloatValueFuzzTest()131 void GetFloatValueFuzzTest()
132 {
133 AudioSettingProvider &settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
134 float value = GetData<float>();
135 std::string key = "test";
136 std::string tableType = "test";
137 settingProvider.GetFloatValue(key, value, tableType);
138 }
139
IsValidKeyFuzzTest()140 void IsValidKeyFuzzTest()
141 {
142 AudioSettingProvider &settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
143 std::string key = "test";
144 settingProvider.IsValidKey(key);
145 }
146
SetDataShareReadyFuzzTest()147 void SetDataShareReadyFuzzTest()
148 {
149 AudioSettingProvider &settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
150 bool isDataShareReady = false;
151 settingProvider.SetDataShareReady(isDataShareReady);
152 }
153
ParseJsonArrayFuzzTest()154 void ParseJsonArrayFuzzTest()
155 {
156 AudioSettingProvider &settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
157 std::string input = "{'aa':'bb'}";
158 settingProvider.ParseJsonArray(input);
159 }
160
ParseFirstOfKeyFuzzTest()161 void ParseFirstOfKeyFuzzTest()
162 {
163 AudioSettingProvider &settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
164 size_t pos = 0;
165 size_t len = 1;
166 std::string input = "test";
167 settingProvider.ParseFirstOfKey(pos, len, input);
168 }
169
ParseSecondOfValueFuzzTest()170 void ParseSecondOfValueFuzzTest()
171 {
172 AudioSettingProvider &settingProvider = AudioSettingProvider::GetInstance(AUDIO_POLICY_SERVICE_ID);
173 size_t pos = 0;
174 size_t len = 1;
175 std::string input = "test";
176 settingProvider.ParseSecondOfValue(pos, len, input);
177 }
178
179 TestFuncs g_testFuncs[TESTSIZE] = {
180 FlushDfxMsgFuzzTest,
181 WriteEffectMsgFuzzTest,
182 GetDfxIndexesFuzzTest,
183 WriteActionMsgFuzzTest,
184 WriteInfoMsgFuzzTest,
185 GetFloatValueFuzzTest,
186 IsValidKeyFuzzTest,
187 SetDataShareReadyFuzzTest,
188 ParseJsonArrayFuzzTest,
189 ParseFirstOfKeyFuzzTest,
190 ParseSecondOfValueFuzzTest,
191 };
192
FuzzTest(const uint8_t * rawData,size_t size)193 bool FuzzTest(const uint8_t* rawData, size_t size)
194 {
195 if (rawData == nullptr) {
196 return false;
197 }
198
199 // initialize data
200 RAW_DATA = rawData;
201 g_dataSize = size;
202 g_pos = 0;
203
204 uint32_t code = GetData<uint32_t>();
205 uint32_t len = GetArrLength(g_testFuncs);
206 if (len > 0) {
207 g_testFuncs[code % len]();
208 } else {
209 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
210 }
211
212 return true;
213 }
214 } // namespace AudioStandard
215 } // namesapce OHOS
216
217 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)218 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
219 {
220 if (size < OHOS::AudioStandard::THRESHOLD) {
221 return 0;
222 }
223
224 OHOS::AudioStandard::FuzzTest(data, size);
225 return 0;
226 }
227