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_server.h"
44 #include "audio_effect_volume.h"
45 #include "futex_tool.h"
46 #include "format_converter.h"
47 #include "audio_dump_pcm.h"
48 #include "audio_dump_pcm_private.h"
49 #include "audio_zone_service.h"
50 #include "audio_schedule.h"
51
52 namespace OHOS {
53 namespace AudioStandard {
54 using namespace std;
55
56 static const uint8_t* RAW_DATA = nullptr;
57 static size_t g_dataSize = 0;
58 static size_t g_pos;
59 const size_t THRESHOLD = 10;
60 const uint8_t TESTSIZE = 5;
61
62 typedef void (*TestFuncs)();
63
64 template<class T>
GetData()65 T GetData()
66 {
67 T object {};
68 size_t objectSize = sizeof(object);
69 if (g_dataSize < g_pos) {
70 return object;
71 }
72 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
73 return object;
74 }
75 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
76 if (ret != EOK) {
77 return {};
78 }
79 g_pos += objectSize;
80 return object;
81 }
82
83 template<class T>
GetArrLength(T & arr)84 uint32_t GetArrLength(T& arr)
85 {
86 if (arr == nullptr) {
87 AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
88 return 0;
89 }
90 return sizeof(arr) / sizeof(arr[0]);
91 }
92
ResetProcessDataThreadPriorityFuzzTest()93 void ResetProcessDataThreadPriorityFuzzTest()
94 {
95 #ifdef RESSCHE_ENABLE
96 ResetProcessDataThreadPriority();
97 #endif
98 }
99
SetProcessDataThreadPriorityFuzzTest()100 void SetProcessDataThreadPriorityFuzzTest()
101 {
102 #ifdef RESSCHE_ENABLE
103 int32_t priority = GetData<int32_t>();
104 SetProcessDataThreadPriority(priority);
105 #endif
106 }
107
OnAddResSchedServiceFuzzTest()108 void OnAddResSchedServiceFuzzTest()
109 {
110 #ifdef RESSCHE_ENABLE
111 uint32_t audioServerPid = GetData<uint32_t>();
112 OnAddResSchedService(audioServerPid);
113 #endif
114 }
115
SetEndpointThreadPriorityFuzzTest()116 void SetEndpointThreadPriorityFuzzTest()
117 {
118 #ifdef RESSCHE_ENABLE
119 SetEndpointThreadPriority();
120 #endif
121 }
122
ResetEndpointThreadPriorityFuzzTest()123 void ResetEndpointThreadPriorityFuzzTest()
124 {
125 #ifdef RESSCHE_ENABLE
126 ResetEndpointThreadPriority();
127 #endif
128 }
129
130 TestFuncs g_testFuncs[TESTSIZE] = {
131 ResetProcessDataThreadPriorityFuzzTest,
132 SetProcessDataThreadPriorityFuzzTest,
133 OnAddResSchedServiceFuzzTest,
134 SetEndpointThreadPriorityFuzzTest,
135 ResetEndpointThreadPriorityFuzzTest,
136 };
137
FuzzTest(const uint8_t * rawData,size_t size)138 void FuzzTest(const uint8_t* rawData, size_t size)
139 {
140 if (rawData == nullptr) {
141 return;
142 }
143
144 // initialize data
145 RAW_DATA = rawData;
146 g_dataSize = size;
147 g_pos = 0;
148
149 uint32_t code = GetData<uint32_t>();
150 uint32_t len = GetArrLength(g_testFuncs);
151 if (len > 0) {
152 g_testFuncs[code % len]();
153 } else {
154 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
155 }
156
157 return;
158 }
159 } // namespace AudioStandard
160 } // namesapce OHOS
161
162 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)163 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
164 {
165 if (size < OHOS::AudioStandard::THRESHOLD) {
166 return 0;
167 }
168
169 OHOS::AudioStandard::FuzzTest(data, size);
170 return 0;
171 }
172