1 /*
2 * Copyright (c) 2024 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 "audio_video_muxer_fuzzer.h"
17 #include "message_parcel.h"
18 #include "securec.h"
19 #include "camera_log.h"
20 #include "avmuxer.h"
21 #include "avmuxer_impl.h"
22
23 namespace OHOS {
24 namespace CameraStandard {
25 static constexpr int32_t MIN_SIZE_NUM = 6;
26 static constexpr int32_t NUM_1 = 1;
27 std::shared_ptr<AudioVideoMuxer> AudioVideoMuxerFuzzer::fuzz_{nullptr};
28
29 /*
30 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
31 * tips: only support basic type
32 */
33
AudioVideoMuxerFuzzTest(FuzzedDataProvider & fdp)34 void AudioVideoMuxerFuzzer::AudioVideoMuxerFuzzTest(FuzzedDataProvider& fdp)
35 {
36 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
37 return;
38 }
39 fuzz_ = std::make_shared<AudioVideoMuxer>();
40 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
41 uint8_t randomNum = fdp.ConsumeIntegral<uint8_t>();
42 fuzz_->Create(static_cast<OH_AVOutputFormat>(randomNum), nullptr);
43 std::shared_ptr<OHOS::Media::AVBuffer> sample = std::make_shared<OHOS::Media::AVBuffer>();
44
45 constexpr int32_t executionModeCount = static_cast<int32_t>(TrackType::META_TRACK) + NUM_1;
46 TrackType type = static_cast<TrackType>(fdp.ConsumeIntegral<uint8_t>() % executionModeCount);
47 int32_t trackId = fdp.ConsumeIntegral<int32_t>();
48 fuzz_->WriteSampleBuffer(sample, type);
49 std::shared_ptr<Format> format = std::make_shared<Format>();
50 fuzz_->AddTrack(trackId, format, type);
51 }
52
Test(uint8_t * data,size_t size)53 void Test(uint8_t* data, size_t size)
54 {
55 auto audioVideoMuxer = std::make_unique<AudioVideoMuxerFuzzer>();
56 if (audioVideoMuxer == nullptr) {
57 MEDIA_INFO_LOG("audioVideoMuxer is null");
58 return;
59 }
60 FuzzedDataProvider fdp(data, size);
61 audioVideoMuxer->AudioVideoMuxerFuzzTest(fdp);
62 }
63
64 } // namespace CameraStandard
65 } // namespace OHOS
66
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
69 {
70 OHOS::CameraStandard::Test(data, size);
71 return 0;
72 }