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_device_info.h"
22 #include "audio_utils.h"
23 #include "accesstoken_kit.h"
24 #include "nativetoken_kit.h"
25 #include "token_setproc.h"
26 #include "access_token.h"
27 #include "audio_channel_blend.h"
28 #include "volume_ramp.h"
29 #include "audio_speed.h"
30
31 #include "hpae_capture_effect_node.h"
32 #include "hpae_source_input_cluster.h"
33 #include "hpae_format_convert.h"
34 #include "audio_effect_log.h"
35
36 namespace OHOS {
37 namespace AudioStandard {
38 using namespace std;
39 using namespace HPAE;
40
41 static const uint8_t* RAW_DATA = nullptr;
42 static size_t g_dataSize = 0;
43 static size_t g_pos;
44 static std::string g_rootCapturerPath = "/data/source_file_io_48000_2_s16le.pcm";
45 const char* DEFAULT_TEST_DEVICE_CLASS = "file_io";
46 const char* DEFAULT_TEST_DEVICE_NETWORKID = "LocalDevice";
47 constexpr size_t THRESHOLD = 10;
48 constexpr uint8_t TESTSIZE = 2;
49
50 const uint32_t DEFAULT_FRAME_LENGTH = 960;
51 const uint32_t DEFAULT_NODE_ID = 1243;
52
53 template<class T>
GetData()54 T GetData()
55 {
56 T object {};
57 size_t objectSize = sizeof(object);
58 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
59 return object;
60 }
61 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
62 if (ret != EOK) {
63 return {};
64 }
65 g_pos += objectSize;
66 return object;
67 }
68
69 template<class T>
GetArrLength(T & arr)70 uint32_t GetArrLength(T& arr)
71 {
72 if (arr == nullptr) {
73 AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
74 return 0;
75 }
76 return sizeof(arr) / sizeof(arr[0]);
77 }
78
GetTestNodeInfo(HpaeNodeInfo & nodeInfo)79 static void GetTestNodeInfo(HpaeNodeInfo &nodeInfo)
80 {
81 nodeInfo.nodeId = DEFAULT_NODE_ID;
82 nodeInfo.frameLen = DEFAULT_FRAME_LENGTH;
83 nodeInfo.samplingRate = SAMPLE_RATE_48000;
84 nodeInfo.channels = STEREO;
85 nodeInfo.format = SAMPLE_S16LE;
86 nodeInfo.sceneType = HPAE_SCENE_RECORD;
87 nodeInfo.sourceBufferType = HPAE_SOURCE_BUFFER_TYPE_MIC;
88 }
89
ConnectWithInfoFuzzTest()90 void ConnectWithInfoFuzzTest()
91 {
92 HpaeNodeInfo nodeInfo;
93 GetTestNodeInfo(nodeInfo);
94 std::shared_ptr<HpaeCaptureEffectNode> hpaeCaptureEffectNode = std::make_shared<HpaeCaptureEffectNode>(nodeInfo);
95 std::shared_ptr<HpaeSourceInputCluster> hpaeSourceInputCluster = std::make_shared<HpaeSourceInputCluster>(nodeInfo);
96 hpaeCaptureEffectNode->ConnectWithInfo(hpaeSourceInputCluster, nodeInfo);
97 hpaeCaptureEffectNode->DisConnectWithInfo(hpaeSourceInputCluster, nodeInfo);
98 }
99
100
CaptureEffectFuzzTest()101 void CaptureEffectFuzzTest()
102 {
103 HpaeNodeInfo nodeInfo;
104 GetTestNodeInfo(nodeInfo);
105 std::shared_ptr<HpaeCaptureEffectNode> hpaeCaptureEffectNode = std::make_shared<HpaeCaptureEffectNode>(nodeInfo);
106 hpaeCaptureEffectNode->GetCapturerEffectConfig(nodeInfo);
107 CaptureEffectAttr attr = {
108 .micChannels = GetData<uint32_t>(),
109 .ecChannels = GetData<uint32_t>(),
110 .micRefChannels = GetData<uint32_t>(),
111 };
112 uint64_t sceneKeyCode = GetData<uint64_t>();
113 hpaeCaptureEffectNode->CaptureEffectCreate(sceneKeyCode, attr);
114 hpaeCaptureEffectNode->CaptureEffectRelease(sceneKeyCode);
115 }
116
117 typedef void (*TestFuncs)();
118 TestFuncs g_testFuncs[TESTSIZE] = {
119 ConnectWithInfoFuzzTest,
120 CaptureEffectFuzzTest,
121 };
122
FuzzTest(const uint8_t * rawData,size_t size)123 bool FuzzTest(const uint8_t* rawData, size_t size)
124 {
125 if (rawData == nullptr) {
126 return false;
127 }
128
129 // initialize data
130 RAW_DATA = rawData;
131 g_dataSize = size;
132 g_pos = 0;
133
134 uint32_t code = GetData<uint32_t>();
135 uint32_t len = GetArrLength(g_testFuncs);
136 if (len > 0) {
137 g_testFuncs[code % len]();
138 } else {
139 AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
140 }
141
142 return true;
143 }
144 } // namespace AudioStandard
145 } // namesapce OHOS
146
147 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)148 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
149 {
150 if (size < OHOS::AudioStandard::THRESHOLD) {
151 return 0;
152 }
153
154 OHOS::AudioStandard::FuzzTest(data, size);
155 return 0;
156 }
157