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 = 2;
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
MarshallingFuzzTest()83 void MarshallingFuzzTest()
84 {
85 InterruptGroupInfo interruptGroupInfo;
86 Parcel parcel;
87 interruptGroupInfo.Marshalling(parcel);
88 }
89
UnmarshallingFuzzTest()90 void UnmarshallingFuzzTest()
91 {
92 InterruptGroupInfo interruptGroupInfo;
93 Parcel in;
94 interruptGroupInfo.Unmarshalling(in);
95 }
96
97 TestFuncs g_testFuncs[TESTSIZE] = {
98 MarshallingFuzzTest,
99 UnmarshallingFuzzTest,
100 };
101
FuzzTest(const uint8_t * rawData,size_t size)102 bool FuzzTest(const uint8_t* rawData, size_t size)
103 {
104 if (rawData == nullptr) {
105 return false;
106 }
107
108 // initialize data
109 RAW_DATA = rawData;
110 g_dataSize = size;
111 g_pos = 0;
112
113 uint32_t code = GetData<uint32_t>();
114 uint32_t len = GetArrLength(g_testFuncs);
115 if (len > 0) {
116 g_testFuncs[code % len]();
117 } else {
118 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
119 }
120
121 return true;
122 }
123 } // namespace AudioStandard
124 } // namesapce OHOS
125
126 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)127 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
128 {
129 if (size < OHOS::AudioStandard::THRESHOLD) {
130 return 0;
131 }
132
133 OHOS::AudioStandard::FuzzTest(data, size);
134 return 0;
135 }
136