• 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 
20 #include "audio_manager_base.h"
21 #include "audio_policy_manager_listener_stub.h"
22 #include "audio_server.h"
23 #include "message_parcel.h"
24 #include "audio_param_parser.h"
25 #include "audio_info.h"
26 #include "audio_source_type.h"
27 #include "audio_process_in_client.h"
28 #include "fast_audio_stream.h"
29 
30 using namespace std;
31 
32 namespace OHOS {
33 namespace AudioStandard {
34 shared_ptr<AudioProcessInClient> g_AudioProcessInClient = nullptr;
35 shared_ptr<FastAudioStream> g_FastAudioStream = nullptr;
36 static const uint8_t *RAW_DATA = nullptr;
37 static size_t g_dataSize = 0;
38 static size_t g_pos;
39 const size_t THRESHOLD = 10;
40 
41 /*
42 * describe: get data from outside untrusted data(RAW_DATA) which size is according to sizeof(T)
43 * tips: only support basic type
44 */
45 template<class T>
GetData()46 T GetData()
47 {
48     T object {};
49     size_t objectSize = sizeof(object);
50     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
51         return object;
52     }
53     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
54     if (ret != EOK) {
55         return {};
56     }
57     g_pos += objectSize;
58     return object;
59 }
60 
61 template<class T>
GetArrLength(T & arr)62 uint32_t GetArrLength(T& arr)
63 {
64     if (arr == nullptr) {
65         AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
66         return 0;
67     }
68     return sizeof(arr) / sizeof(arr[0]);
69 }
70 
GetAudioProcessInClient()71 void GetAudioProcessInClient()
72 {
73     if (g_AudioProcessInClient != nullptr) {
74         return;
75     }
76     AudioProcessConfig config;
77     config.appInfo.appPid = getpid();
78     config.appInfo.appUid = getuid();
79     config.audioMode = AUDIO_MODE_RECORD;
80     config.capturerInfo.sourceType = SOURCE_TYPE_MIC;
81     config.capturerInfo.capturerFlags = STREAM_FLAG_FAST;
82     config.streamInfo.channels = STEREO;
83     config.streamInfo.encoding = ENCODING_PCM;
84     config.streamInfo.format = SAMPLE_S16LE;
85     config.streamInfo.samplingRate = SAMPLE_RATE_48000;
86     g_FastAudioStream = std::make_shared<FastAudioStream>(config.streamType,
87         AUDIO_MODE_RECORD, config.appInfo.appUid);
88     g_AudioProcessInClient = AudioProcessInClient::Create(config, g_FastAudioStream);
89     if (g_AudioProcessInClient== nullptr) {
90         return;
91     }
92     g_AudioProcessInClient->Start();
93 
94     int32_t vol = GetData<int32_t>();
95     g_AudioProcessInClient->SetVolume(vol);
96 
97     uint32_t sessionID = GetData<uint32_t>();
98     g_AudioProcessInClient->GetSessionID(sessionID);
99 
100     size_t bufferSize = GetData<size_t>();
101     g_AudioProcessInClient->GetBufferSize(bufferSize);
102 
103     uint32_t frameCount = GetData<uint32_t>();
104     g_AudioProcessInClient->GetFrameCount(frameCount);
105 
106     uint64_t latency = GetData<uint32_t>();
107     g_AudioProcessInClient->GetLatency(latency);
108 
109     float volume = GetData<float>();
110     g_AudioProcessInClient->SetVolume(volume);
111     g_AudioProcessInClient->GetVolume();
112     g_AudioProcessInClient->SetDuckVolume(volume);
113     g_AudioProcessInClient->GetUnderflowCount();
114     g_AudioProcessInClient->GetOverflowCount();
115 
116     uint32_t flowCount = GetData<uint32_t>();
117     g_AudioProcessInClient->SetUnderflowCount(flowCount);
118     g_AudioProcessInClient->SetOverflowCount(flowCount);
119     g_AudioProcessInClient->GetFramesWritten();
120     g_AudioProcessInClient->GetFramesRead();
121 
122     int32_t frameSize = GetData<int32_t>();
123     g_AudioProcessInClient->SetPreferredFrameSize(frameSize);
124 }
125 
AudioClientUpdateLatencyTimestampTest()126 void AudioClientUpdateLatencyTimestampTest()
127 {
128     if (g_AudioProcessInClient != nullptr) {
129         return;
130     }
131     AudioProcessConfig config;
132     config.appInfo.appPid = getpid();
133     config.appInfo.appUid = getuid();
134     config.audioMode = AUDIO_MODE_RECORD;
135     config.capturerInfo.sourceType = SOURCE_TYPE_MIC;
136     config.capturerInfo.capturerFlags = STREAM_FLAG_FAST;
137     config.streamInfo.channels = STEREO;
138     config.streamInfo.encoding = ENCODING_PCM;
139     config.streamInfo.format = SAMPLE_S16LE;
140     config.streamInfo.samplingRate = SAMPLE_RATE_48000;
141     g_FastAudioStream = std::make_shared<FastAudioStream>(config.streamType,
142         AUDIO_MODE_RECORD, config.appInfo.appUid);
143     g_AudioProcessInClient = AudioProcessInClient::Create(config, g_FastAudioStream);
144     if (g_AudioProcessInClient== nullptr) {
145         return;
146     }
147 
148     bool isRenderer = GetData<bool>();
149     std::string timestamp = "123456";
150     g_AudioProcessInClient->UpdateLatencyTimestamp(timestamp, isRenderer);
151     g_AudioProcessInClient->Pause();
152     g_AudioProcessInClient->Resume();
153     g_AudioProcessInClient->Stop();
154 }
155 
156 typedef void (*TestFuncs[2])();
157 
158 TestFuncs g_testFuncs = {
159     GetAudioProcessInClient,
160     AudioClientUpdateLatencyTimestampTest,
161 };
162 
FuzzTest(const uint8_t * rawData,size_t size)163 bool FuzzTest(const uint8_t* rawData, size_t size)
164 {
165     if (rawData == nullptr) {
166         return false;
167     }
168 
169     // initialize data
170     RAW_DATA = rawData;
171     g_dataSize = size;
172     g_pos = 0;
173 
174     uint32_t code = GetData<uint32_t>();
175     uint32_t len = GetArrLength(g_testFuncs);
176     if (len > 0) {
177         g_testFuncs[code % len]();
178     } else {
179         AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
180     }
181 
182     return true;
183 }
184 } // namespace AudioStandard
185 } // namesapce OHOS
186 
187 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)188 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
189 {
190     if (size < OHOS::AudioStandard::THRESHOLD) {
191         return 0;
192     }
193 
194     OHOS::AudioStandard::FuzzTest(data, size);
195     return 0;
196 }
197