• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "encoderapi11_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-EncoderAPI10H26510BitFuzzTest";
82     SaveCorpus(data, size, filename);
83     if (g_needRunNormalEncoder) {
84         g_needRunNormalEncoder = false;
85         RunNormalEncoder();
86     }
87     bool result = false;
88     FuzzedDataProvider fdp(data, size);
89     int data1 = fdp.ConsumeIntegral<int32_t>();
90     bool data2 = fdp.ConsumeBool();
91     VEncNdkFuzzSample *vEncSample = new VEncNdkFuzzSample();
92     vEncSample->fuzzMode = true;
93     vEncSample->fuzzData = data;
94     vEncSample->fuzzSize = size;
95     vEncSample->surfInput = data2;
96     int32_t ret = vEncSample->CreateVideoEncoder();
97     if (ret != 0) {
98         delete vEncSample;
99         vEncSample = nullptr;
100         return true;
101     }
102     if (vEncSample->SetVideoEncoderCallback() != AV_ERR_OK) {
103         delete vEncSample;
104         vEncSample = nullptr;
105         return true;
106     }
107     if (vEncSample->ConfigureVideoEncoder() != AV_ERR_OK) {
108         delete vEncSample;
109         vEncSample = nullptr;
110         return true;
111     }
112     if (vEncSample->StartVideoEncoder() != AV_ERR_OK) {
113         delete vEncSample;
114         vEncSample = nullptr;
115         return true;
116     }
117     vEncSample->SetParameterFuzz(data1);
118     vEncSample->WaitForEOS();
119     delete vEncSample;
120     vEncSample = nullptr;
121     return result;
122 }
123 } // namespace OHOS
124 
125 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)126 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
127 {
128     /* Run your code on data */
129     OHOS::EncoderAPI10FuzzTest(data, size);
130     return 0;
131 }
132