• 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 
43 namespace OHOS {
44 namespace AudioStandard {
45 using namespace std;
46 
47 static const uint8_t* RAW_DATA = nullptr;
48 static size_t g_dataSize = 0;
49 static size_t g_pos;
50 const size_t THRESHOLD = 10;
51 const uint8_t TESTSIZE = 2;
52 static const std::time_t AUDIO_SESSION_TIME_OUT_DURATION_TEST_S = 3;
53 
54 typedef void (*TestFuncs)();
55 
56 class AudioPolicyStateMonitorCallbackMocker : public AudioPolicyStateMonitorCallback {
57 public:
OnTimeOut()58     void OnTimeOut() override {}
59 };
60 
61 template<class T>
GetData()62 T GetData()
63 {
64     T object {};
65     size_t objectSize = sizeof(object);
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 
UnRegisterCallbackFuzzTest()87 void UnRegisterCallbackFuzzTest()
88 {
89     const shared_ptr<AudioPolicyStateMonitor> g_audioPolicyStateMonitor =
90         DelayedSingleton<AudioPolicyStateMonitor>::GetInstance();
91     auto cb = std::make_shared<AudioPolicyStateMonitorCallbackMocker>();
92     int32_t callbackTypeCount = static_cast<int32_t>(CallbackType::REPEAT) + 1;
93     CallbackType callbackType = static_cast<CallbackType>(GetData<uint8_t>() % callbackTypeCount);
94     int32_t cbId = g_audioPolicyStateMonitor->RegisterCallback(
95         cb,
96         AUDIO_SESSION_TIME_OUT_DURATION_TEST_S,
97         callbackType);
98     g_audioPolicyStateMonitor->UnRegisterCallback(cbId);
99 }
100 
FreeCbIdFuzzTest()101 void FreeCbIdFuzzTest()
102 {
103     auto audioPolicyStateMonitor = std::make_shared<AudioPolicyStateMonitor>();
104     int32_t cbId = GetData<int32_t>();
105     audioPolicyStateMonitor->FreeCbId(cbId);
106 }
107 
108 TestFuncs g_testFuncs[TESTSIZE] = {
109     UnRegisterCallbackFuzzTest,
110     FreeCbIdFuzzTest,
111 };
112 
FuzzTest(const uint8_t * rawData,size_t size)113 bool FuzzTest(const uint8_t* rawData, size_t size)
114 {
115     if (rawData == nullptr) {
116         return false;
117     }
118 
119     // initialize data
120     RAW_DATA = rawData;
121     g_dataSize = size;
122     g_pos = 0;
123 
124     uint32_t code = GetData<uint32_t>();
125     uint32_t len = GetArrLength(g_testFuncs);
126     if (len > 0) {
127         g_testFuncs[code % len]();
128     } else {
129         AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
130     }
131 
132     return true;
133 }
134 } // namespace AudioStandard
135 } // namesapce OHOS
136 
137 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)138 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
139 {
140     if (size < OHOS::AudioStandard::THRESHOLD) {
141         return 0;
142     }
143 
144     OHOS::AudioStandard::FuzzTest(data, size);
145     return 0;
146 }
147