1 /*
2 * Copyright (c) 2023 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 "videoenc_sample.h"
22 #include "native_avcapability.h"
23 #include <fuzzer/FuzzedDataProvider.h>
24 using namespace std;
25 using namespace OHOS;
26 using namespace OHOS::Media;
27 #define FUZZ_PROJECT_NAME "encoderconfigure_fuzzer"
28
RunNormalEncoder()29 void RunNormalEncoder()
30 {
31 VEncFuzzSample *vEncSample = new VEncFuzzSample();
32 vEncSample->inpDir = "/data/test/media/1280_720_nv.yuv";
33 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory("video/avc", true, HARDWARE);
34 string tmpCodecName = OH_AVCapability_GetName(cap);
35 int32_t ret = vEncSample->CreateVideoEncoder(tmpCodecName.c_str());
36 if (ret != 0) {
37 delete vEncSample;
38 vEncSample = nullptr;
39 return;
40 }
41 if (vEncSample->SetVideoEncoderCallback() != AV_ERR_OK) {
42 delete vEncSample;
43 vEncSample = nullptr;
44 return;
45 }
46 if (vEncSample->ConfigureVideoEncoder() != AV_ERR_OK) {
47 delete vEncSample;
48 vEncSample = nullptr;
49 return;
50 }
51 if (vEncSample->StartVideoEncoder() != AV_ERR_OK) {
52 delete vEncSample;
53 vEncSample = nullptr;
54 return;
55 }
56 vEncSample->WaitForEOS();
57 delete vEncSample;
58 }
59 bool g_needRunNormalEncoder = true;
60 namespace OHOS {
EncoderConfigureFuzzTest(const uint8_t * data,size_t size)61 bool EncoderConfigureFuzzTest(const uint8_t *data, size_t size)
62 {
63 if (size < sizeof(int32_t)) {
64 return false;
65 }
66 if (g_needRunNormalEncoder) {
67 g_needRunNormalEncoder = false;
68 RunNormalEncoder();
69 }
70 FuzzedDataProvider fdp(data, size);
71 bool result = false;
72 int32_t intval = fdp.ConsumeIntegral<uint32_t>();
73 VEncFuzzSample *vEncSample = new VEncFuzzSample();
74 vEncSample->inpDir = "/data/test/media/1280_720_nv.yuv";
75 OH_AVCapability *cap = OH_AVCodec_GetCapabilityByCategory("video/avc", true, HARDWARE);
76 string tmpCodecName = OH_AVCapability_GetName(cap);
77 int32_t ret = vEncSample->CreateVideoEncoder(tmpCodecName.c_str());
78 if (ret != 0) {
79 delete vEncSample;
80 vEncSample = nullptr;
81 return true;
82 }
83 if (vEncSample->SetVideoEncoderCallback() != AV_ERR_OK) {
84 delete vEncSample;
85 vEncSample = nullptr;
86 return true;
87 }
88 vEncSample->fuzzMode = true;
89 if (vEncSample->ConfigureVideoEncoderFuzz(intval) != AV_ERR_OK) {
90 delete vEncSample;
91 vEncSample = nullptr;
92 return true;
93 }
94 if (vEncSample->StartVideoEncoder() != AV_ERR_OK) {
95 delete vEncSample;
96 vEncSample = nullptr;
97 return true;
98 }
99 vEncSample->WaitForEOS();
100 delete vEncSample;
101 vEncSample = nullptr;
102 return result;
103 }
104 } // namespace OHOS
105
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
108 {
109 /* Run your code on data */
110 OHOS::EncoderConfigureFuzzTest(data, size);
111 return 0;
112 }
113