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 "acquireaudiobuffer_fuzzer.h"
17
18 #include <securec.h>
19 #include <sys/mman.h>
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include "ohos_adapter_helper.h"
22 #include "screen_capture_adapter_impl.h"
23
24 using namespace OHOS::NWeb;
25
26 class AudioBufferAdapterMock : public AudioBufferAdapter {
27 public:
AudioBufferAdapterMock()28 AudioBufferAdapterMock() {}
29
30 uint8_t* GetBuffer() override;
31
32 int32_t GetLength() override;
33
34 int64_t GetTimestamp() override;
35
36 OHOS::NWeb::AudioCaptureSourceTypeAdapter GetSourcetype() override;
37
38 void SetBuffer(uint8_t* buffer) override;
39
40 void SetLength(int32_t length) override;
41
42 void SetTimestamp(int64_t timestamp) override;
43
44 void SetSourcetype(OHOS::NWeb::AudioCaptureSourceTypeAdapter sourcetype) override;
45
46 private:
47 uint8_t* buffer_;
48 int32_t len_;
49 int64_t timestamp_;
50 OHOS::NWeb::AudioCaptureSourceTypeAdapter type_;
51 };
52
GetBuffer()53 uint8_t* AudioBufferAdapterMock::GetBuffer()
54 {
55 return buffer_;
56 }
57
GetLength()58 int32_t AudioBufferAdapterMock::GetLength()
59 {
60 return len_;
61 }
62
GetTimestamp()63 int64_t AudioBufferAdapterMock::GetTimestamp()
64 {
65 return timestamp_;
66 }
67
GetSourcetype()68 OHOS::NWeb::AudioCaptureSourceTypeAdapter AudioBufferAdapterMock::GetSourcetype()
69 {
70 return type_;
71 }
72
SetBuffer(uint8_t * buffer)73 void AudioBufferAdapterMock::SetBuffer(uint8_t* buffer)
74 {
75 buffer_ = buffer;
76 }
77
SetLength(int32_t length)78 void AudioBufferAdapterMock::SetLength(int32_t length)
79 {
80 len_ = length;
81 }
82
SetTimestamp(int64_t timestamp)83 void AudioBufferAdapterMock::SetTimestamp(int64_t timestamp)
84 {
85 timestamp_ = timestamp;
86 }
87
SetSourcetype(OHOS::NWeb::AudioCaptureSourceTypeAdapter sourcetype)88 void AudioBufferAdapterMock::SetSourcetype(OHOS::NWeb::AudioCaptureSourceTypeAdapter sourcetype)
89 {
90 type_ = sourcetype;
91 }
92
93 namespace OHOS {
94
ApplyAcquireAudioBufferFuzzTest(const uint8_t * data,size_t size)95 bool ApplyAcquireAudioBufferFuzzTest(const uint8_t* data, size_t size)
96 {
97 if ((data == nullptr) || (size == 0)) {
98 return true;
99 }
100 ScreenCaptureAdapterImpl impl;
101 FuzzedDataProvider dataProvider(data, size);
102 std::shared_ptr<AudioBufferAdapter> buffer = std::make_shared<AudioBufferAdapterMock>();
103 int32_t typeID = dataProvider.ConsumeIntegralInRange<int32_t>(-1, 3);
104 AudioCaptureSourceTypeAdapter type = static_cast<AudioCaptureSourceTypeAdapter>(typeID);
105 impl.AcquireAudioBuffer(buffer, type);
106 return true;
107 }
108
109 } // namespace OHOS
110
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114 OHOS::ApplyAcquireAudioBufferFuzzTest(data, size);
115 return 0;
116 }