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 #include "i_standard_audio_zone_client.h"
43 #include "i_standard_audio_zone_client_fuzzer.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 = 1;
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
IStandardAudioZoneClientFuzzTest()84 void IStandardAudioZoneClientFuzzTest()
85 {
86 IStandardAudioZoneClientFuzz example;
87 example.hasBTPermission_ = GetData<uint8_t>() % NUM_2;
88 example.hasSystemPermission_ = GetData<uint8_t>() % NUM_2;
89 example.~IStandardAudioZoneClientFuzz();
90 }
91
92 TestFuncs g_testFuncs[TESTSIZE] = {
93 IStandardAudioZoneClientFuzzTest,
94 };
95
FuzzTest(const uint8_t * rawData,size_t size)96 bool FuzzTest(const uint8_t* rawData, size_t size)
97 {
98 if (rawData == nullptr) {
99 return false;
100 }
101
102 // initialize data
103 RAW_DATA = rawData;
104 g_dataSize = size;
105 g_pos = 0;
106
107 uint32_t code = GetData<uint32_t>();
108 uint32_t len = GetArrLength(g_testFuncs);
109 if (len > 0) {
110 g_testFuncs[code % len]();
111 } else {
112 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
113 }
114
115 return true;
116 }
117 } // namespace AudioStandard
118 } // namesapce OHOS
119
120 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)121 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
122 {
123 if (size < OHOS::AudioStandard::THRESHOLD) {
124 return 0;
125 }
126
127 OHOS::AudioStandard::FuzzTest(data, size);
128 return 0;
129 }
130