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