• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 = 5;
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 (g_dataSize < g_pos) {
63         return object;
64     }
65     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
66         return object;
67     }
68     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
69     if (ret != EOK) {
70         return {};
71     }
72     g_pos += objectSize;
73     return object;
74 }
75 
76 template<class T>
GetArrLength(T & arr)77 uint32_t GetArrLength(T& arr)
78 {
79     if (arr == nullptr) {
80         AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
81         return 0;
82     }
83     return sizeof(arr) / sizeof(arr[0]);
84 }
85 
DumpFuzzTest()86 void DumpFuzzTest()
87 {
88     std::shared_ptr<AudioPipeInfo> pipeInfo = std::make_shared<AudioPipeInfo>();
89     if (pipeInfo ==nullptr) {
90         return;
91     }
92     int32_t audioPipeRoleCount = static_cast<int32_t>(AudioPipeRole::PIPE_ROLE_NONE) + 1;
93     pipeInfo->pipeRole_ = static_cast<AudioPipeRole>(GetData<uint8_t>() % audioPipeRoleCount);
94     AudioPipeInfo audioPipeInfo(pipeInfo);
95     std::shared_ptr<AudioStreamDescriptor> streamDesc = std::make_shared<AudioStreamDescriptor>();
96     if (streamDesc ==nullptr) {
97         return;
98     }
99     audioPipeInfo.streamDescriptors_.push_back(streamDesc);
100     std::string dumpString = "";
101     audioPipeInfo.Dump(dumpString);
102 }
103 
DumpCommonAttrsFuzzTest()104 void DumpCommonAttrsFuzzTest()
105 {
106     AudioPipeInfo audioPipeInfo;
107     std::string dumpString = "";
108     audioPipeInfo.DumpCommonAttrs(dumpString);
109 }
110 
DumpOutputAttrsFuzzTest()111 void DumpOutputAttrsFuzzTest()
112 {
113     AudioPipeInfo audioPipeInfo;
114     std::string dumpString = "";
115     audioPipeInfo.DumpOutputAttrs(dumpString);
116 }
117 
DumpInputAttrsFuzzTest()118 void DumpInputAttrsFuzzTest()
119 {
120     AudioPipeInfo audioPipeInfo;
121     std::string dumpString = "";
122     audioPipeInfo.DumpInputAttrs(dumpString);
123 }
124 
AudioPipeInfoFuzzTest()125 void AudioPipeInfoFuzzTest()
126 {
127     std::shared_ptr<AudioPipeInfo> pipeInfo = std::make_shared<AudioPipeInfo>();
128     AudioPipeInfo audioPipeInfo(pipeInfo);
129 }
130 
131 TestFuncs g_testFuncs[TESTSIZE] = {
132     DumpFuzzTest,
133     DumpCommonAttrsFuzzTest,
134     DumpOutputAttrsFuzzTest,
135     DumpInputAttrsFuzzTest,
136     AudioPipeInfoFuzzTest,
137 };
138 
FuzzTest(const uint8_t * rawData,size_t size)139 void FuzzTest(const uint8_t* rawData, size_t size)
140 {
141     if (rawData == nullptr) {
142         return;
143     }
144 
145     // initialize data
146     RAW_DATA = rawData;
147     g_dataSize = size;
148     g_pos = 0;
149 
150     uint32_t code = GetData<uint32_t>();
151     uint32_t len = GetArrLength(g_testFuncs);
152     if (len > 0) {
153         g_testFuncs[code % len]();
154     } else {
155         AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
156     }
157 
158     return;
159 }
160 } // namespace AudioStandard
161 } // namesapce OHOS
162 
163 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)164 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
165 {
166     if (size < OHOS::AudioStandard::THRESHOLD) {
167         return 0;
168     }
169 
170     OHOS::AudioStandard::FuzzTest(data, size);
171     return 0;
172 }
173