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
18 #include "native_avcodec_videoencoder.h"
19 #include "native_averrors.h"
20 #include "native_avcodec_base.h"
21 #include "native_avcapability.h"
22 #include "videoenc_sample.h"
23 #include <fuzzer/FuzzedDataProvider.h>
24 #include <fstream>
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::Media;
28 #define FUZZ_PROJECT_NAME "encoderapi10_fuzzer"
29
SaveCorpus(const uint8_t * data,size_t size,const std::string & filename)30 void SaveCorpus(const uint8_t *data, size_t size, const std::string& filename)
31 {
32 std::ofstream file(filename, std::ios::out | std::ios::binary);
33 if (file.is_open()) {
34 file.write(reinterpret_cast<const char*>(data), size);
35 file.close();
36 }
37 }
RunNormalEncoder()38 void RunNormalEncoder()
39 {
40 auto vEncSample = make_unique<VEncNdkFuzzSample>();
41 int32_t ret = vEncSample->CreateVideoEncoder();
42 if (ret != 0) {
43 return;
44 }
45 if (vEncSample->SetVideoEncoderCallback() != AV_ERR_OK) {
46 return;
47 }
48 if (vEncSample->ConfigureVideoEncoder() != AV_ERR_OK) {
49 return;
50 }
51 if (vEncSample->StartVideoEncoder() != AV_ERR_OK) {
52 return;
53 }
54 vEncSample->WaitForEOS();
55
56 auto vEncSampleSurf = make_unique<VEncNdkFuzzSample>();
57 vEncSampleSurf->surfInput = true;
58 ret = vEncSampleSurf->CreateVideoEncoder();
59 if (ret != 0) {
60 return;
61 }
62 if (vEncSampleSurf->SetVideoEncoderCallback() != AV_ERR_OK) {
63 return;
64 }
65 if (vEncSampleSurf->ConfigureVideoEncoder() != AV_ERR_OK) {
66 return;
67 }
68 if (vEncSampleSurf->StartVideoEncoder() != AV_ERR_OK) {
69 return;
70 }
71 vEncSampleSurf->WaitForEOS();
72 }
73
74 bool g_needRunNormalEncoder = true;
75 namespace OHOS {
EncoderAPI10FuzzTest(const uint8_t * data,size_t size)76 bool EncoderAPI10FuzzTest(const uint8_t *data, size_t size)
77 {
78 if (size < sizeof(int32_t)) {
79 return false;
80 }
81 std::string filename = "/data/test/corpus-EncoderAPI10H264FuzzTest";
82 SaveCorpus(data, size, filename);
83 if (g_needRunNormalEncoder) {
84 g_needRunNormalEncoder = false;
85 RunNormalEncoder();
86 }
87 FuzzedDataProvider fdp(data, size);
88 int data1 = fdp.ConsumeIntegral<int32_t>();
89 bool data2 = fdp.ConsumeBool();
90 VEncNdkFuzzSample *vEncSample = new VEncNdkFuzzSample();
91 vEncSample->fuzzMode = true;
92 vEncSample->fuzzData = data;
93 vEncSample->fuzzSize = size;
94 vEncSample->surfInput = data2;
95 int32_t ret = vEncSample->CreateVideoEncoder();
96 if (ret != 0) {
97 delete vEncSample;
98 vEncSample = nullptr;
99 return false;
100 }
101 if (vEncSample->SetVideoEncoderCallback() != AV_ERR_OK) {
102 delete vEncSample;
103 vEncSample = nullptr;
104 return false;
105 }
106 if (vEncSample->ConfigureVideoEncoder() != AV_ERR_OK) {
107 delete vEncSample;
108 vEncSample = nullptr;
109 return false;
110 }
111 if (vEncSample->StartVideoEncoder() != AV_ERR_OK) {
112 delete vEncSample;
113 vEncSample = nullptr;
114 return false;
115 }
116 vEncSample->SetParameterFuzz(data1);
117 vEncSample->WaitForEOS();
118 delete vEncSample;
119 vEncSample = nullptr;
120 return true;
121 }
122 } // namespace OHOS
123
124 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)125 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
126 {
127 /* Run your code on data */
128 OHOS::EncoderAPI10FuzzTest(data, size);
129 return 0;
130 }
131