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 "videodec_inner_sample.h"
18 #include <fuzzer/FuzzedDataProvider.h>
19 #include "native_avcapability.h"
20 using namespace std;
21 using namespace OHOS;
22 using namespace OHOS::Media;
23 using namespace OHOS::MediaAVCodec;
24 #define FUZZ_PROJECT_NAME "hwdecoderinner_fuzzer"
25 VDecNdkInnerFuzzSample *g_vDecSample = nullptr;
26
27 namespace OHOS {
SaveCorpus(const uint8_t * data,size_t size,const std::string & filename)28 void SaveCorpus(const uint8_t *data, size_t size, const std::string& filename)
29 {
30 std::ofstream file(filename, std::ios::out | std::ios::binary);
31 if (file.is_open()) {
32 file.write(reinterpret_cast<const char*>(data), size);
33 file.close();
34 }
35 }
36
ReleaseSample()37 bool ReleaseSample()
38 {
39 delete g_vDecSample;
40 g_vDecSample = nullptr;
41 return true;
42 }
43
HwdecoderInnerFuzzTest(const uint8_t * data,size_t size)44 bool HwdecoderInnerFuzzTest(const uint8_t *data, size_t size)
45 {
46 if (size < sizeof(int32_t)) {
47 return false;
48 }
49 std::string filename = "/data/test/corpus-HwdecoderInnerFuzzTest";
50 SaveCorpus(data, size, filename);
51 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, false, HARDWARE);
52 if (cap == nullptr) {
53 return false;
54 }
55 string codecName = OH_AVCapability_GetName(cap);
56 FuzzedDataProvider fdp(data, size);
57 g_vDecSample = new VDecNdkInnerFuzzSample();
58 g_vDecSample->isP3Full = fdp.ConsumeBool();
59 if (g_vDecSample->isP3Full) {
60 g_vDecSample->defaultColorspace = static_cast<int32_t>(OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT709_FULL);
61 }
62 g_vDecSample->sfOutput = fdp.ConsumeBool();
63 int32_t ret = g_vDecSample->CreateByName(codecName);
64 if (ret != AV_ERR_OK) {
65 return ReleaseSample();
66 }
67 ret = g_vDecSample->Configure();
68 if (ret != AV_ERR_OK) {
69 return ReleaseSample();
70 }
71 if (g_vDecSample->SetCallback() != AV_ERR_OK) {
72 return ReleaseSample();
73 }
74 if (g_vDecSample->sfOutput) {
75 g_vDecSample->SetOutputSurface();
76 }
77 g_vDecSample->Prepare();
78 ret = g_vDecSample->Start();
79 if (ret != AV_ERR_OK) {
80 return ReleaseSample();
81 }
82 g_vDecSample->InputFuncFUZZ(data, size);
83 g_vDecSample->Flush();
84 g_vDecSample->Stop();
85 g_vDecSample->Reset();
86 return ReleaseSample();
87 }
88 } // namespace OHOS
89
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
92 {
93 /* Run your code on data */
94 OHOS::HwdecoderInnerFuzzTest(data, size);
95 return 0;
96 }
97