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 <fuzzer/FuzzedDataProvider.h>
18 #include "native_avcapability.h"
19 #include "vdec_sync_sample.h"
20
21 using namespace std;
22 using namespace OHOS;
23 using namespace OHOS::MediaAVCodec;
24
25 #define FUZZ_PROJECT_NAME "syncresourcecapidec_fuzzer"
26
27 std::string sourcePath = "/data/test/media/720_1280_25_avcc.h264";
28 std::string outPath = "/data/test/media/outputTest";
29 uint32_t DEFAULT_WIDTH = 720;
30 uint32_t DEFAULT_HEIGHT = 1280;
31 std::shared_ptr<VideoDecSyncSample> videoDec = nullptr;
32 std::shared_ptr<FormatMock> format = nullptr;
33
SetSync()34 void SetSync()
35 {
36 format->PutIntValue(MediaDescriptionKey::MD_KEY_WIDTH, DEFAULT_WIDTH);
37 format->PutIntValue(MediaDescriptionKey::MD_KEY_HEIGHT, DEFAULT_HEIGHT);
38 format->PutIntValue(MediaDescriptionKey::MD_KEY_PIXEL_FORMAT, static_cast<int32_t>(VideoPixelFormat::NV12));
39 format->PutIntValue(Media::Tag::AV_CODEC_ENABLE_SYNC_MODE, 1);
40 }
41
VideoDecoderFuzzTest(const uint8_t * data,size_t size)42 bool VideoDecoderFuzzTest(const uint8_t *data, size_t size)
43 {
44 std::shared_ptr<OHOS::MediaAVCodec::VDecSignal> vdecSignal = std::make_shared<OHOS::MediaAVCodec::VDecSignal>();
45 videoDec = std::make_shared<OHOS::MediaAVCodec::VideoDecSyncSample>(vdecSignal);
46 if (videoDec == nullptr) {
47 return false;
48 }
49
50 format = FormatMockFactory::CreateFormat();
51 if (!videoDec->CreateVideoDecMockByMime(CodecMimeType::VIDEO_AVC.data())) {
52 return false;
53 }
54 SetSync();
55 videoDec->SetSource(sourcePath);
56 videoDec->SetOutPath(outPath);
57 FuzzedDataProvider fdp(data, size);
58 uint32_t rangeFlag = fdp.ConsumeIntegral<uint32_t>();
59 format->PutIntValue(MediaDescriptionKey::MD_KEY_RANGE_FLAG, rangeFlag);
60 videoDec->Configure(format);
61 videoDec->Prepare();
62 videoDec->Start();
63
64 format = nullptr;
65 videoDec = nullptr;
66 return true;
67 }
68
VideoDecoderResourceFuzzTest(const uint8_t * data,size_t size)69 bool VideoDecoderResourceFuzzTest(const uint8_t *data, size_t size)
70 {
71 std::shared_ptr<OHOS::MediaAVCodec::VDecSignal> vdecSignal = std::make_shared<OHOS::MediaAVCodec::VDecSignal>();
72 videoDec = std::make_shared<OHOS::MediaAVCodec::VideoDecSyncSample>(vdecSignal);
73 if (videoDec == nullptr) {
74 return false;
75 }
76
77 format = FormatMockFactory::CreateFormat();
78 if (!videoDec->CreateVideoDecMockByMime(CodecMimeType::VIDEO_AVC.data())) {
79 return false;
80 }
81 SetSync();
82 videoDec->SetSource(sourcePath);
83 videoDec->SetOutPath(outPath);
84 videoDec->Configure(format);
85 videoDec->Prepare();
86 videoDec->FuzzStart();
87 int ret = videoDec->InputFuncFUZZ(data, size);
88 if (ret != 0) {
89 return false;
90 }
91
92 videoDec->Stop();
93 format = nullptr;
94 videoDec = nullptr;
95 return true;
96 }
97
98 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)99 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
100 {
101 /* Run your code on data */
102 VideoDecoderFuzzTest(data, size);
103 VideoDecoderResourceFuzzTest(data, size);
104 return 0;
105 }
106