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 "audiodevicemanager_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21 #include <memory>
22
23 #include "audio_device_manager.h"
24 #include "securec.h"
25
26 namespace OHOS {
27 namespace AVSession {
28 using namespace std;
29 static const int32_t MAX_CODE_LEN = 20;
30 static const int32_t MIN_SIZE_NUM = 10;
31 static const uint8_t *RAW_DATA = nullptr;
32 static size_t g_totalSize = 0;
33 static size_t g_sizePos;
34
35 /*
36 * describe: get data from FUZZ untrusted data(RAW_DATA) which size is according to sizeof(T)
37 * tips: only support basic type
38 */
39 template<class T>
GetData()40 T GetData()
41 {
42 T object {};
43 size_t objectSize = sizeof(object);
44 if (RAW_DATA == nullptr || objectSize > g_totalSize - g_sizePos) {
45 return object;
46 }
47 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_sizePos, objectSize);
48 if (ret != EOK) {
49 return {};
50 }
51 g_sizePos += objectSize;
52 return object;
53 }
54
GetString()55 std::string GetString()
56 {
57 size_t objectSize = (GetData<int8_t>() % MAX_CODE_LEN) + 1;
58 if (RAW_DATA == nullptr || objectSize > g_totalSize - g_sizePos) {
59 return "OVER_SIZE";
60 }
61 char object[objectSize + 1];
62 errno_t ret = memcpy_s(object, sizeof(object), RAW_DATA + g_sizePos, objectSize);
63 if (ret != EOK) {
64 return "";
65 }
66 g_sizePos += objectSize;
67 std::string output(object);
68 return output;
69 }
70
71 template<class T>
GetArrLength(T & arr)72 uint32_t GetArrLength(T& arr)
73 {
74 if (arr == nullptr) {
75 SLOGE("%{public}s: The array length is equal to 0", __func__);
76 return 0;
77 }
78 return sizeof(arr) / sizeof(arr[0]);
79 }
80
FuzzTest(const uint8_t * rawData,size_t size)81 bool FuzzTest(const uint8_t* rawData, size_t size)
82 {
83 if (rawData == nullptr) {
84 return false;
85 }
86
87 // initialize data
88 RAW_DATA = rawData;
89 g_totalSize = size;
90 g_sizePos = 0;
91
92 TestAudioDeviceManager();
93
94 return true;
95 }
96
TestAudioDeviceManager()97 void TestAudioDeviceManager()
98 {
99 std::string deviceId = GetString();
100 AudioDeviceManager::GetInstance().ClearRemoteAvSessionInfo(deviceId);
101 AudioDeviceManager::GetInstance().SendRemoteAvSessionInfo(deviceId);
102 AudioDeviceManager::GetInstance().GetDeviceId();
103 AudioDeviceManager::GetInstance().GetAudioState();
104 AudioDeviceManager::GetInstance().SetAudioState(GetData<int32_t>());
105
106 std::vector<std::shared_ptr<AudioStandard::AudioDeviceDescriptor>> desc;
107 AudioDeviceManager::GetInstance().RegisterPreferedOutputDeviceChangeCallback();
108 AudioDeviceManager::GetInstance().audioPreferedOutputDeviceChangeCallback_->OnPreferredOutputDeviceUpdated(desc);
109
110 AudioStandard::DeviceChangeAction deviceChangeAction;
111 AudioDeviceManager::GetInstance().RegisterAudioDeviceChangeCallback();
112 AudioDeviceManager::GetInstance().audioDeviceChangeCallback_->OnDeviceChange(deviceChangeAction);
113 }
114
115 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)116 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
117 {
118 if (size < MIN_SIZE_NUM) {
119 return 0;
120 }
121 /* Run your code on data */
122 FuzzTest(data, size);
123 return 0;
124 }
125 } // namespace AVSession
126 } // namespace OHOS
127