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 "syncvideoenc_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 "syncencoder_fuzzer"
29
30
31 OH_AVCapability *cap = nullptr;
32 constexpr int32_t ONE = 1;
33 constexpr int32_t TWO = 2;
34 string g_codeName = "";
35 VEncSyncSample *vEncSample = nullptr;
36
SaveCorpus(const uint8_t * data,size_t size,const std::string & filename)37 void SaveCorpus(const uint8_t *data, size_t size, const std::string& filename)
38 {
39 std::ofstream file(filename, std::ios::out | std::ios::binary);
40 if (file.is_open()) {
41 file.write(reinterpret_cast<const char*>(data), size);
42 file.close();
43 }
44 }
45
GetCodeName(const char * mimeName,OH_AVCodecCategory category)46 string GetCodeName(const char* mimeName, OH_AVCodecCategory category)
47 {
48 cap = OH_AVCodec_GetCapabilityByCategory(mimeName, true, category);
49 if (cap == nullptr) {
50 return "";
51 }
52 return OH_AVCapability_GetName(cap);
53 }
54
ReleaseSample()55 bool ReleaseSample()
56 {
57 delete vEncSample;
58 vEncSample = nullptr;
59 return true;
60 }
61
CodecType()62 void CodecType()
63 {
64 if (vEncSample->codecType == ONE) {
65 g_codeName = GetCodeName(OH_AVCODEC_MIMETYPE_VIDEO_AVC, HARDWARE);
66 } else if (vEncSample->codecType == TWO) {
67 g_codeName = GetCodeName(OH_AVCODEC_MIMETYPE_VIDEO_HEVC, HARDWARE);
68 }
69 }
70
71 namespace OHOS {
EncoderSyncFuzzTest(const uint8_t * data,size_t size)72 bool EncoderSyncFuzzTest(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-EncoderSyncFuzzTest";
78 SaveCorpus(data, size, filename);
79 FuzzedDataProvider fdp(data, size);
80 int data1 = fdp.ConsumeIntegral<int32_t>();
81 vEncSample = new VEncSyncSample();
82 vEncSample->codecType = fdp.ConsumeIntegralInRange<int32_t>(ONE, TWO);
83 CodecType();
84 if (g_codeName == "") {
85 return false;
86 }
87 vEncSample->fuzzData = data;
88 vEncSample->fuzzSize = size;
89 vEncSample->surfInput = fdp.ConsumeBool();
90 vEncSample->fuzzMode = true;
91 vEncSample->enbleSyncMode = fdp.ConsumeIntegral<int32_t>();
92 vEncSample->syncInputWaitTime = fdp.ConsumeIntegral<int64_t>();
93 vEncSample->syncOutputWaitTime = 1;
94 int32_t intval = fdp.ConsumeIntegral<uint32_t>();
95 int32_t ret = vEncSample->CreateVideoEncoder(g_codeName.c_str());
96 if (ret != AV_ERR_OK) {
97 return ReleaseSample();
98 }
99 if (vEncSample->ConfigureVideoEncoderFuzz(intval) != AV_ERR_OK) {
100 return ReleaseSample();
101 }
102 if (vEncSample->surfInput) {
103 vEncSample->CreateSurface();
104 }
105 if (vEncSample->enbleSyncMode == 0) {
106 return ReleaseSample();
107 }
108 if (vEncSample->Start() != AV_ERR_OK) {
109 return ReleaseSample();
110 }
111 if (vEncSample->surfInput) {
112 vEncSample->InputFuncSurfaceFuzz();
113 } else {
114 vEncSample->SyncInputFuncFuzz();
115 }
116 vEncSample->SyncOutputFuncFuzz();
117 vEncSample->SetParameter(data1);
118 return ReleaseSample();
119 }
120 } // namespace OHOS
121
122 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)123 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
124 {
125 /* Run your code on data */
126 OHOS::EncoderSyncFuzzTest(data, size);
127 return 0;
128 }
129