• 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 #include "futex_tool.h"
46 #include "format_converter.h"
47 #include "audio_dump_pcm.h"
48 #include "audio_dump_pcm_private.h"
49 #include "audio_zone_service.h"
50 #include "audio_xml_parser.h"
51 
52 namespace OHOS {
53 namespace AudioStandard {
54 using namespace std;
55 
56 static const uint8_t* RAW_DATA = nullptr;
57 static size_t g_dataSize = 0;
58 static size_t g_pos;
59 const size_t THRESHOLD = 10;
60 const uint8_t TESTSIZE = 3;
61 
62 typedef void (*TestFuncs)();
63 
64 template<class T>
GetData()65 T GetData()
66 {
67     T object {};
68     size_t objectSize = sizeof(object);
69     if (g_dataSize < g_pos) {
70         return object;
71     }
72     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
73         return object;
74     }
75     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
76     if (ret != EOK) {
77         return {};
78     }
79     g_pos += objectSize;
80     return object;
81 }
82 
83 template<class T>
GetArrLength(T & arr)84 uint32_t GetArrLength(T& arr)
85 {
86     if (arr == nullptr) {
87         AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
88         return 0;
89     }
90     return sizeof(arr) / sizeof(arr[0]);
91 }
92 
HasPropFuzzTest()93 void HasPropFuzzTest()
94 {
95     shared_ptr<AudioXmlNode> ptr = AudioXmlNode::Create();
96     if (ptr == nullptr) {
97         return;
98     }
99     ptr->HasProp("abc");
100 }
101 
GetContentFuzzTest()102 void GetContentFuzzTest()
103 {
104     shared_ptr<AudioXmlNode> ptr = AudioXmlNode::Create();
105     if (ptr == nullptr) {
106         return;
107     }
108     std::string result = "abc";
109     ptr->GetContent(result);
110 }
111 
OperatorFuzzTest()112 void OperatorFuzzTest()
113 {
114     shared_ptr<AudioXmlNode> ptr1 = AudioXmlNode::Create();
115     shared_ptr<AudioXmlNode> ptr2 = ptr1;
116 }
117 
118 TestFuncs g_testFuncs[TESTSIZE] = {
119     HasPropFuzzTest,
120     GetContentFuzzTest,
121     OperatorFuzzTest,
122 };
123 
FuzzTest(const uint8_t * rawData,size_t size)124 void FuzzTest(const uint8_t* rawData, size_t size)
125 {
126     if (rawData == nullptr) {
127         return;
128     }
129 
130     // initialize data
131     RAW_DATA = rawData;
132     g_dataSize = size;
133     g_pos = 0;
134 
135     uint32_t code = GetData<uint32_t>();
136     uint32_t len = GetArrLength(g_testFuncs);
137     if (len > 0) {
138         g_testFuncs[code % len]();
139     } else {
140         AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
141     }
142 
143     return;
144 }
145 } // namespace AudioStandard
146 } // namesapce OHOS
147 
148 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)149 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
150 {
151     if (size < OHOS::AudioStandard::THRESHOLD) {
152         return 0;
153     }
154 
155     OHOS::AudioStandard::FuzzTest(data, size);
156     return 0;
157 }
158