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 #include <cstddef>
16 #include <cstdint>
17 #include "native_avcodec_videodecoder.h"
18 #include "native_averrors.h"
19 #include "native_avcodec_base.h"
20 #include "videodec_api11_sample.h"
21 #include <fuzzer/FuzzedDataProvider.h>
22 #include <fstream>
23 using namespace std;
24 using namespace OHOS;
25 using namespace OHOS::Media;
26 #define FUZZ_PROJECT_NAME "hwdecoderapi11_fuzzer"
27
28 static VDecApi11FuzzSample *g_vDecSample = nullptr;
29 constexpr uint32_t DEFAULT_WIDTH = 1920;
30 constexpr uint32_t DEFAULT_HEIGHT = 1080;
31 constexpr double DEFAULT_FRAME_RATE = 30.0;
32 constexpr uint32_t SPS_SIZE = 0x19;
33 constexpr uint32_t PPS_SIZE = 0x05;
34 constexpr uint32_t START_CODE_SIZE = 4;
35 constexpr uint8_t SPS[SPS_SIZE + START_CODE_SIZE] = {0x00, 0x00, 0x00, 0x01, 0x67, 0x64, 0x00, 0x28, 0xAC,
36 0xB4, 0x03, 0xC0, 0x11, 0x3F, 0x2E, 0x02, 0x20, 0x00,
37 0x00, 0x03, 0x00, 0x20, 0x00, 0x00, 0x07, 0x81, 0xE3,
38 0x06, 0x54};
39 constexpr uint8_t PPS[PPS_SIZE + START_CODE_SIZE] = {0x00, 0x00, 0x00, 0x01, 0x68, 0xEF, 0x0F, 0x2C, 0x8B};
40 bool g_isSurfMode = true;
41
42 namespace OHOS {
SaveCorpus(const uint8_t * data,size_t size,const std::string & filename)43 void SaveCorpus(const uint8_t *data, size_t size, const std::string& filename)
44 {
45 std::ofstream file(filename, std::ios::out | std::ios::binary);
46 if (file.is_open()) {
47 file.write(reinterpret_cast<const char*>(data), size);
48 file.close();
49 }
50 }
RunNormalDecoder()51 void RunNormalDecoder()
52 {
53 VDecApi11FuzzSample *vDecSample = new VDecApi11FuzzSample();
54 vDecSample->isSurfMode = true;
55 vDecSample->defaultWidth = DEFAULT_WIDTH;
56 vDecSample->defaultHeight = DEFAULT_HEIGHT;
57 vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
58 int ret = vDecSample->CreateVideoDecoder();
59 if (ret != 0) {
60 delete vDecSample;
61 vDecSample = nullptr;
62 return;
63 }
64 vDecSample->ConfigureVideoDecoder();
65 vDecSample->SetVideoDecoderCallback();
66 vDecSample->StartVideoDecoder();
67 vDecSample->WaitForEOS();
68 delete vDecSample;
69 }
70
71 bool g_needRunNormalDecoder = true;
HwdecoderApi11FuzzTest(const uint8_t * data,size_t size)72 bool HwdecoderApi11FuzzTest(const uint8_t *data, size_t size)
73 {
74 if (size < sizeof(int32_t)) {
75 return false;
76 }
77 std::string filename = "/data/test/corpus-HwdecoderAPI11Hdr2SdrFuzzTest";
78 SaveCorpus(data, size, filename);
79 if (g_needRunNormalDecoder) {
80 g_needRunNormalDecoder = false;
81 RunNormalDecoder();
82 }
83 FuzzedDataProvider fdp(data, size);
84 int data0 = fdp.ConsumeIntegral<int32_t>();
85 if (!g_vDecSample) {
86 g_vDecSample = new VDecApi11FuzzSample();
87 g_vDecSample->isSurfMode = true;
88 g_vDecSample->defaultWidth = DEFAULT_WIDTH;
89 g_vDecSample->defaultHeight = DEFAULT_HEIGHT;
90 g_vDecSample->defaultFrameRate = DEFAULT_FRAME_RATE;
91 int32_t ret = g_vDecSample->CreateVideoDecoder();
92 if (ret != 0) {
93 delete g_vDecSample;
94 g_vDecSample = nullptr;
95 return true;
96 }
97 g_vDecSample->ConfigureVideoDecoder();
98 g_vDecSample->SetVideoDecoderCallback();
99 g_vDecSample->Start();
100 g_vDecSample->InputFuncFUZZ(SPS, SPS_SIZE + START_CODE_SIZE);
101 g_vDecSample->InputFuncFUZZ(PPS, PPS_SIZE + START_CODE_SIZE);
102 }
103 g_vDecSample->InputFuncFUZZ(data, size);
104 g_vDecSample->SetParameter(data0);
105 g_vDecSample->Flush();
106 g_vDecSample->Stop();
107 g_vDecSample->Reset();
108 delete g_vDecSample;
109 g_vDecSample = nullptr;
110 return true;
111 }
112 } // namespace OHOS
113
114 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)115 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
116 {
117 /* Run your code on data */
118 OHOS::HwdecoderApi11FuzzTest(data, size);
119 return 0;
120 }
121