• 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 #include "audio_effect_volume.h"
45 
46 namespace OHOS {
47 namespace AudioStandard {
48 using namespace std;
49 
50 static const uint8_t* RAW_DATA = nullptr;
51 static size_t g_dataSize = 0;
52 static size_t g_pos;
53 const size_t THRESHOLD = 10;
54 const uint8_t TESTSIZE = 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 (g_dataSize < g_pos) {
64         return object;
65     }
66     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
67         return object;
68     }
69     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
70     if (ret != EOK) {
71         return {};
72     }
73     g_pos += objectSize;
74     return object;
75 }
76 
77 template<class T>
GetArrLength(T & arr)78 uint32_t GetArrLength(T& arr)
79 {
80     if (arr == nullptr) {
81         AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
82         return 0;
83     }
84     return sizeof(arr) / sizeof(arr[0]);
85 }
86 
SetStreamVolumeFuzzTest()87 void SetStreamVolumeFuzzTest()
88 {
89     std::string sessionID = "123";
90     float streamVolume = GetData<float>();
91     std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
92     if (audioEffectVolume == nullptr) {
93         return;
94     }
95     audioEffectVolume->SetStreamVolume(sessionID, streamVolume);
96 }
97 
StreamVolumeDeleteFuzzTest()98 void StreamVolumeDeleteFuzzTest()
99 {
100     std::string sessionID = "123";
101     float streamVolume = GetData<float>();
102     std::shared_ptr<AudioEffectVolume> audioEffectVolume = AudioEffectVolume::GetInstance();
103     if (audioEffectVolume == nullptr) {
104         return;
105     }
106     audioEffectVolume->SetStreamVolume(sessionID, streamVolume);
107     audioEffectVolume->StreamVolumeDelete(sessionID);
108     audioEffectVolume->StreamVolumeDelete(sessionID);
109 }
110 
111 TestFuncs g_testFuncs[TESTSIZE] = {
112     SetStreamVolumeFuzzTest,
113     StreamVolumeDeleteFuzzTest,
114 };
115 
FuzzTest(const uint8_t * rawData,size_t size)116 void FuzzTest(const uint8_t* rawData, size_t size)
117 {
118     if (rawData == nullptr) {
119         return;
120     }
121 
122     // initialize data
123     RAW_DATA = rawData;
124     g_dataSize = size;
125     g_pos = 0;
126 
127     uint32_t code = GetData<uint32_t>();
128     uint32_t len = GetArrLength(g_testFuncs);
129     if (len > 0) {
130         g_testFuncs[code % len]();
131     } else {
132         AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
133     }
134 
135     return;
136 }
137 } // namespace AudioStandard
138 } // namesapce OHOS
139 
140 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)141 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
142 {
143     if (size < OHOS::AudioStandard::THRESHOLD) {
144         return 0;
145     }
146 
147     OHOS::AudioStandard::FuzzTest(data, size);
148     return 0;
149 }
150