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 "hpae_no_lock_queue.h"
42
43 namespace OHOS {
44 namespace AudioStandard {
45 using namespace std;
46
47 static const uint8_t* RAW_DATA = nullptr;
48 static size_t g_dataSize = 0;
49 static size_t g_pos;
50 const size_t THRESHOLD = 10;
51 const uint8_t TESTSIZE = 6;
52 const size_t TESTQUEUESIZE = 5;
53 static constexpr size_t TESTQUEUESIZETHREE = 3;
54 static int32_t NUM_2 = 2;
55
56 typedef void (*TestFuncs)();
57
58 template<class T>
GetData()59 T GetData()
60 {
61 T object {};
62 size_t objectSize = sizeof(object);
63 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
64 return object;
65 }
66 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
67 if (ret != EOK) {
68 return {};
69 }
70 g_pos += objectSize;
71 return object;
72 }
73
74 template<class T>
GetArrLength(T & arr)75 uint32_t GetArrLength(T& arr)
76 {
77 if (arr == nullptr) {
78 AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
79 return 0;
80 }
81 return sizeof(arr) / sizeof(arr[0]);
82 }
83
RequestNodeFuzzTest()84 void RequestNodeFuzzTest()
85 {
86 HPAE::RequestNode requestNode1;
87 HPAE::RequestNode requestNode2(requestNode1);
88 }
89
HpaeNoLockQueueFuzzTest()90 void HpaeNoLockQueueFuzzTest()
91 {
92 vector<uint32_t> requestCountList = {-1, 0, 1, 10000000, 10000001};
93 size_t maxRequestCount = GetData<uint32_t>() % requestCountList.size();
94 HPAE::HpaeNoLockQueue large_queue(maxRequestCount);
95 large_queue.IsFinishProcess();
96 }
97
PushRequestFuzzTest()98 void PushRequestFuzzTest()
99 {
100 std::atomic<int> gCount = 0;
101 auto countingRequest = [&gCount]() { gCount++; };
102 HPAE::HpaeNoLockQueue queue(TESTQUEUESIZETHREE);
103 for (int i = 0; i < TESTQUEUESIZETHREE; ++i) {
104 queue.PushRequest(countingRequest);
105 }
106 queue.PushRequest(countingRequest);
107 queue.HandleRequests();
108 }
109
ResetFuzzTest()110 void ResetFuzzTest()
111 {
112 std::unique_ptr<HPAE::HpaeNoLockQueue> queue_ = std::make_unique<HPAE::HpaeNoLockQueue>(TESTQUEUESIZE);
113 std::atomic<int> processed_count_ = 0;
114 std::atomic<int> gCount = 0;
115 auto countingRequest = [&gCount]() { gCount++; };
116
117 HPAE::HpaeNoLockQueue queue(TESTQUEUESIZE);
118 for (int i = 0; i < TESTQUEUESIZE; ++i) {
119 queue.PushRequest(countingRequest);
120 }
121 queue.Reset();
122 queue.HandleRequests();
123 }
124
GetRequsetFlagFuzzTest()125 void GetRequsetFlagFuzzTest()
126 {
127 std::unique_ptr<HPAE::HpaeNoLockQueue> queue_ = std::make_unique<HPAE::HpaeNoLockQueue>(GetData<size_t>());
128 uint64_t requestFlag = GetData<uint64_t>();
129 queue_->GetRequsetFlag(requestFlag);
130 }
131
PushRequestNodeFuzzTest()132 void PushRequestNodeFuzzTest()
133 {
134 std::unique_ptr<HPAE::HpaeNoLockQueue> queue_ = std::make_unique<HPAE::HpaeNoLockQueue>(GetData<size_t>());
135 uint64_t index = GetData<uint64_t>();
136 queue_->PushRequestNode(nullptr, index);
137 }
138
139 TestFuncs g_testFuncs[TESTSIZE] = {
140 RequestNodeFuzzTest,
141 HpaeNoLockQueueFuzzTest,
142 PushRequestFuzzTest,
143 ResetFuzzTest,
144 GetRequsetFlagFuzzTest,
145 PushRequestNodeFuzzTest,
146 };
147
FuzzTest(const uint8_t * rawData,size_t size)148 bool FuzzTest(const uint8_t* rawData, size_t size)
149 {
150 if (rawData == nullptr) {
151 return false;
152 }
153
154 // initialize data
155 RAW_DATA = rawData;
156 g_dataSize = size;
157 g_pos = 0;
158
159 uint32_t code = GetData<uint32_t>();
160 uint32_t len = GetArrLength(g_testFuncs);
161 if (len > 0) {
162 g_testFuncs[code % len]();
163 } else {
164 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
165 }
166
167 return true;
168 }
169 } // namespace AudioStandard
170 } // namesapce OHOS
171
172 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)173 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
174 {
175 if (size < OHOS::AudioStandard::THRESHOLD) {
176 return 0;
177 }
178
179 OHOS::AudioStandard::FuzzTest(data, size);
180 return 0;
181 }
182