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
51 namespace OHOS {
52 namespace AudioStandard {
53 using namespace std;
54
55 static const uint8_t* RAW_DATA = nullptr;
56 static size_t g_dataSize = 0;
57 static size_t g_pos;
58 const size_t THRESHOLD = 10;
59 const uint8_t TESTSIZE = 2;
60
61 typedef void (*TestFuncs)();
62
63 template<class T>
GetData()64 T GetData()
65 {
66 T object {};
67 size_t objectSize = sizeof(object);
68 if (g_dataSize <= g_pos) {
69 return object;
70 }
71 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
72 return object;
73 }
74 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
75 if (ret != EOK) {
76 return {};
77 }
78 g_pos += objectSize;
79 return object;
80 }
81
82 template<class T>
GetArrLength(T & arr)83 uint32_t GetArrLength(T& arr)
84 {
85 if (arr == nullptr) {
86 AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
87 return 0;
88 }
89 return sizeof(arr) / sizeof(arr[0]);
90 }
91
SetThreadQosLevelAsyncFuzzTest()92 void SetThreadQosLevelAsyncFuzzTest()
93 {
94 #ifdef QOSMANAGER_ENABLE
95 SetThreadQosLevelAsync(1);
96 #endif
97 }
98
SetThreadQosLevelWithTidFuzzTest()99 void SetThreadQosLevelWithTidFuzzTest()
100 {
101 #ifdef QOSMANAGER_ENABLE
102 int32_t pid = GetData<int32_t>();
103 int32_t tid = GetData<int32_t>();
104 SetThreadQosLevelWithTid(pid, tid, 1);
105 #endif
106 }
107
108 TestFuncs g_testFuncs[TESTSIZE] = {
109 SetThreadQosLevelAsyncFuzzTest,
110 SetThreadQosLevelWithTidFuzzTest,
111 };
112
FuzzTest(const uint8_t * rawData,size_t size)113 void FuzzTest(const uint8_t* rawData, size_t size)
114 {
115 if (rawData == nullptr) {
116 return;
117 }
118
119 // initialize data
120 RAW_DATA = rawData;
121 g_dataSize = size;
122 g_pos = 0;
123
124 uint32_t code = GetData<uint32_t>();
125 uint32_t len = GetArrLength(g_testFuncs);
126 if (len > 0) {
127 g_testFuncs[code % len]();
128 } else {
129 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
130 }
131
132 return;
133 }
134 } // namespace AudioStandard
135 } // namesapce OHOS
136
137 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)138 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
139 {
140 //Ensure that the incoming data is large enough
141 if (size < OHOS::AudioStandard::THRESHOLD) {
142 return 0;
143 }
144
145 OHOS::AudioStandard::FuzzTest(data, size);
146 return 0;
147 }
148