1 /*
2 * Copyright (c) 2024 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 "mediacodecdecoderadapterimpl_fuzzer.h"
17
18 #include <cstring>
19 #include <fuzzer/FuzzedDataProvider.h>
20
21 #include "avsharedmemory.h"
22 #include "avsharedmemorybase.h"
23 #include "media_codec_decoder_adapter_impl.h"
24 #include "native_avformat.h"
25 #include "native_avbuffer.h"
26
27 using namespace OHOS::NWeb;
28
29 namespace OHOS {
30 constexpr uint8_t MAX_STRING_LENGTH = 255;
31
32 class DecoderCallbackAdapterMock : public DecoderCallbackAdapter {
33 public:
34 DecoderCallbackAdapterMock() = default;
35
36 ~DecoderCallbackAdapterMock() override = default;
37
OnError(ErrorType errorType,int32_t errorCode)38 void OnError(ErrorType errorType, int32_t errorCode) override {}
39
OnStreamChanged(int32_t width,int32_t height,double frameRate)40 void OnStreamChanged(int32_t width, int32_t height, double frameRate) override {}
41
OnNeedInputData(uint32_t index,std::shared_ptr<OhosBufferAdapter> buffer)42 void OnNeedInputData(uint32_t index, std::shared_ptr<OhosBufferAdapter> buffer) override {}
43
OnNeedOutputData(uint32_t index,std::shared_ptr<BufferInfoAdapter> info,BufferFlag flag)44 void OnNeedOutputData(uint32_t index, std::shared_ptr<BufferInfoAdapter> info, BufferFlag flag) override {}
45 };
46
47 class DecoderFormatAdapterMock : public DecoderFormatAdapter {
48 public:
49 DecoderFormatAdapterMock() = default;
50
GetWidth()51 int32_t GetWidth() override
52 {
53 return width;
54 }
55
GetHeight()56 int32_t GetHeight() override
57 {
58 return height;
59 }
60
GetFrameRate()61 double GetFrameRate() override
62 {
63 return frameRate;
64 }
65
SetWidth(int32_t w)66 void SetWidth(int32_t w) override
67 {
68 width = w;
69 }
70
SetHeight(int32_t h)71 void SetHeight(int32_t h) override
72 {
73 height = h;
74 }
75
SetFrameRate(double fr)76 void SetFrameRate(double fr) override
77 {
78 frameRate = fr;
79 }
80
81 int32_t width;
82 int32_t height;
83 double frameRate;
84 };
85
MediaCodecDecoderAdapterImplFuzzTest(const uint8_t * data,size_t size)86 bool MediaCodecDecoderAdapterImplFuzzTest(const uint8_t* data, size_t size)
87 {
88 NWeb::MediaCodecDecoderAdapterImpl mediaCodecDecoderAdapterImpl;
89 NWeb::DecoderAdapterCode code = mediaCodecDecoderAdapterImpl.CreateVideoDecoderByMime("testmimeType");
90 std::shared_ptr<NWeb::DecoderFormatAdapter> format = std::make_unique<DecoderFormatAdapterMock>();
91 FuzzedDataProvider dataProvider(data, size);
92 std::string stringParam = dataProvider.ConsumeRandomLengthString(MAX_STRING_LENGTH);
93 int32_t intParam = dataProvider.ConsumeIntegralInRange<int32_t>(0, 10000);
94 uint32_t uintParam = dataProvider.ConsumeIntegralInRange<uint32_t>(0, 10000);
95 code = mediaCodecDecoderAdapterImpl.CreateVideoDecoderByName(stringParam);
96 code = mediaCodecDecoderAdapterImpl.ConfigureDecoder(format);
97 code = mediaCodecDecoderAdapterImpl.SetParameterDecoder(format);
98
99 void* window = nullptr;
100 code = mediaCodecDecoderAdapterImpl.SetOutputSurface(window);
101 code = mediaCodecDecoderAdapterImpl.PrepareDecoder();
102 code = mediaCodecDecoderAdapterImpl.StartDecoder();
103 code = mediaCodecDecoderAdapterImpl.StopDecoder();
104 code = mediaCodecDecoderAdapterImpl.FlushDecoder();
105 code = mediaCodecDecoderAdapterImpl.ResetDecoder();
106 code = mediaCodecDecoderAdapterImpl.ReleaseDecoder();
107 code = mediaCodecDecoderAdapterImpl.SetAVCencInfo(uintParam, nullptr);
108 code = mediaCodecDecoderAdapterImpl.SetDecryptionConfig(nullptr, true);
109 code = mediaCodecDecoderAdapterImpl.SetDecryptionConfig(nullptr, false);
110
111 code = mediaCodecDecoderAdapterImpl.QueueInputBufferDec(
112 uintParam, 0, intParam, intParam, BufferFlag::CODEC_BUFFER_FLAG_NONE);
113 code = mediaCodecDecoderAdapterImpl.GetOutputFormatDec(format);
114 code = mediaCodecDecoderAdapterImpl.ReleaseOutputBufferDec(uintParam, true);
115
116 constexpr int32_t MEMSIZE = 1024 * 1024;
117 OH_AVFormat* codecFormat = OH_AVFormat_Create();
118 OH_AVBuffer* buffer = OH_AVBuffer_Create(MEMSIZE);
119 mediaCodecDecoderAdapterImpl.OnError(uintParam);
120 mediaCodecDecoderAdapterImpl.OnOutputFormatChanged(nullptr);
121 mediaCodecDecoderAdapterImpl.OnInputBufferAvailable(uintParam, nullptr);
122 mediaCodecDecoderAdapterImpl.OnOutputBufferAvailable(uintParam, nullptr);
123
124 mediaCodecDecoderAdapterImpl.OnOutputFormatChanged(codecFormat);
125 mediaCodecDecoderAdapterImpl.OnInputBufferAvailable(uintParam, buffer);
126 mediaCodecDecoderAdapterImpl.OnOutputBufferAvailable(uintParam, buffer);
127
128 OH_AVFormat_Destroy(codecFormat);
129 OH_AVBuffer_Destroy(buffer);
130 codecFormat = nullptr;
131 buffer = nullptr;
132 return true;
133 }
134 } // namespace OHOS
135
136 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)137 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
138 {
139 /* Run your code on data */
140 OHOS::MediaCodecDecoderAdapterImplFuzzTest(data, size);
141 return 0;
142 }