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
16 #include "av_codec_proxy_fuzzer.h"
17 #include "camera_log.h"
18
19 using namespace OHOS::Media::Plugins;
20
21 namespace OHOS {
22 namespace CameraStandard {
23 static constexpr int32_t MIN_SIZE_NUM = 64;
24 static constexpr int32_t NUM_1 = 1;
25 static constexpr size_t MAX_LENGTH_STRING = 64;
26
27 std::shared_ptr<AVCodecProxy> AvCodecProxyFuzzer::avCodecProxyFuzz_{nullptr};
28
AvCodecProxyFuzzerTest1(FuzzedDataProvider & fdp)29 void AvCodecProxyFuzzer::AvCodecProxyFuzzerTest1(FuzzedDataProvider& fdp)
30 {
31 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
32 return;
33 }
34 avCodecProxyFuzz_ = AVCodecProxy::CreateAVCodecProxy();
35 CHECK_RETURN_ELOG(!avCodecProxyFuzz_, "CreateAVCodecProxy Error");
36 int32_t fd = fdp.ConsumeIntegral<int32_t>();
37 constexpr int32_t executionModeCount = static_cast<int32_t>(OutputFormat::FLAC) + NUM_1;
38 OutputFormat format = static_cast<OutputFormat>(fdp.ConsumeIntegral<uint8_t>() % executionModeCount);
39 avCodecProxyFuzz_->CreateAVMuxer(fd, format);
40 std::shared_ptr<Meta> param = std::make_shared<Meta>();
41 avCodecProxyFuzz_->AVMuxerSetParameter(param);
42 std::shared_ptr<Meta> userMeta = std::make_shared<Meta>();
43 avCodecProxyFuzz_->AVMuxerSetUserMeta(userMeta);
44 int32_t trackIndex = fdp.ConsumeIntegral<int32_t>();
45 std::shared_ptr<Meta> trackDesc = std::make_shared<Meta>();
46 avCodecProxyFuzz_->AVMuxerAddTrack(trackIndex, trackDesc);
47 avCodecProxyFuzz_->AVMuxerStart();
48 std::shared_ptr<AVBuffer> sample = std::make_shared<AVBuffer>();
49 avCodecProxyFuzz_->AVMuxerWriteSample(trackIndex, sample);
50 avCodecProxyFuzz_->AVMuxerStop();
51 }
52
AvCodecProxyFuzzerTest2(FuzzedDataProvider & fdp)53 void AvCodecProxyFuzzer::AvCodecProxyFuzzerTest2(FuzzedDataProvider& fdp)
54 {
55 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
56 return;
57 }
58 avCodecProxyFuzz_ = AVCodecProxy::CreateAVCodecProxy();
59 CHECK_RETURN_ELOG(!avCodecProxyFuzz_, "CreateAVCodecProxy Error");
60 std::string codecMime(fdp.ConsumeRandomLengthString(MAX_LENGTH_STRING));
61 avCodecProxyFuzz_->CreateAVCodecVideoEncoder(codecMime);
62 avCodecProxyFuzz_->AVCodecVideoEncoderPrepare();
63 avCodecProxyFuzz_->AVCodecVideoEncoderStart();
64 avCodecProxyFuzz_->AVCodecVideoEncoderStop();
65 avCodecProxyFuzz_->AVCodecVideoEncoderRelease();
66 avCodecProxyFuzz_->IsVideoEncoderExisted();
67 avCodecProxyFuzz_->CreateInputSurface();
68 uint32_t index = fdp.ConsumeIntegral<uint32_t>();
69 avCodecProxyFuzz_->QueueInputBuffer(index);
70 avCodecProxyFuzz_->AVCodecVideoEncoderNotifyEos();
71 avCodecProxyFuzz_->ReleaseOutputBuffer(index);
72 Format format;
73 avCodecProxyFuzz_->AVCodecVideoEncoderSetParameter(format);
74 std::shared_ptr<MediaCodecCallback> callback = std::make_shared<CoderCallback>();
75 avCodecProxyFuzz_->AVCodecVideoEncoderSetCallback(callback);
76 avCodecProxyFuzz_->AVCodecVideoEncoderConfigure(format);
77 }
78
AvCodecProxyFuzzerTest3(FuzzedDataProvider & fdp)79 void AvCodecProxyFuzzer::AvCodecProxyFuzzerTest3(FuzzedDataProvider& fdp)
80 {
81 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
82 return;
83 }
84 avCodecProxyFuzz_ = AVCodecProxy::CreateAVCodecProxy();
85 CHECK_RETURN_ELOG(!avCodecProxyFuzz_, "CreateAVCodecProxy Error");
86 int32_t fd = fdp.ConsumeIntegral<int32_t>();
87 int64_t offset = fdp.ConsumeIntegral<int64_t>();
88 int64_t size = fdp.ConsumeIntegral<int64_t>();
89 avCodecProxyFuzz_->CreateAVSource(fd, offset, size);
90 Format format;
91 avCodecProxyFuzz_->AVSourceGetSourceFormat(format);
92 avCodecProxyFuzz_->AVSourceGetUserMeta(format);
93 uint32_t trackIndex = fdp.ConsumeIntegral<uint32_t>();
94 avCodecProxyFuzz_->AVSourceGetTrackFormat(format, trackIndex);
95 }
96
AvCodecProxyFuzzerTest4(FuzzedDataProvider & fdp)97 void AvCodecProxyFuzzer::AvCodecProxyFuzzerTest4(FuzzedDataProvider& fdp)
98 {
99 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
100 return;
101 }
102 avCodecProxyFuzz_ = AVCodecProxy::CreateAVCodecProxy();
103 CHECK_RETURN_ELOG(!avCodecProxyFuzz_, "CreateAVCodecProxy Error");
104 std::shared_ptr<AVSource> source = std::make_shared<AVSourceTest>();
105 avCodecProxyFuzz_->CreateAVDemuxer(source);
106 uint32_t trackIndex = fdp.ConsumeIntegral<uint32_t>();
107 std::shared_ptr<AVBuffer> sample = std::make_shared<AVBuffer>();
108 avCodecProxyFuzz_->ReadSampleBuffer(trackIndex, sample);
109 int64_t millisecond = fdp.ConsumeIntegral<int64_t>();
110 constexpr int32_t executionModeCount = static_cast<int32_t>(SeekMode::SEEK_CLOSEST_INNER) + NUM_1;
111 SeekMode mode = static_cast<SeekMode>(fdp.ConsumeIntegral<uint8_t>() % executionModeCount);
112 avCodecProxyFuzz_->AVDemuxerSeekToTime(millisecond, mode);
113 avCodecProxyFuzz_->AVDemuxerSelectTrackByID(trackIndex);
114 }
115
Test(uint8_t * data,size_t size)116 void Test(uint8_t* data, size_t size)
117 {
118 FuzzedDataProvider fdp(data, size);
119 auto avCodecProxyFuzzer = std::make_unique<AvCodecProxyFuzzer>();
120 if (avCodecProxyFuzzer == nullptr) {
121 MEDIA_INFO_LOG("avCodecProxyFuzzer is null");
122 return;
123 }
124 avCodecProxyFuzzer->AvCodecProxyFuzzerTest1(fdp);
125 avCodecProxyFuzzer->AvCodecProxyFuzzerTest2(fdp);
126 avCodecProxyFuzzer->AvCodecProxyFuzzerTest3(fdp);
127 avCodecProxyFuzzer->AvCodecProxyFuzzerTest4(fdp);
128 }
129
130 } // namespace CameraStandard
131 } // namespace OHOS
132
133 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)134 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
135 {
136 OHOS::CameraStandard::Test(data, size);
137 return 0;
138 }