• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MAX_CODE_LEN  = 512;
26 static constexpr int32_t MIN_SIZE_NUM = 4;
27 static constexpr int32_t NUM_1 = 1;
28 static const uint8_t* RAW_DATA = nullptr;
29 const size_t THRESHOLD = 10;
30 static size_t g_dataSize = 0;
31 static size_t g_pos;
32 std::shared_ptr<AudioVideoMuxer> AudioVideoMuxerFuzzer::fuzz_{nullptr};
33 
34 /*
35 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
36 * tips: only support basic type
37 */
38 template<class T>
GetData()39 T GetData()
40 {
41     T object {};
42     size_t objectSize = sizeof(object);
43     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
44         return object;
45     }
46     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
47     if (ret != EOK) {
48         return {};
49     }
50     g_pos += objectSize;
51     return object;
52 }
53 
54 template<class T>
GetArrLength(T & arr)55 uint32_t GetArrLength(T& arr)
56 {
57     if (arr == nullptr) {
58         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
59         return 0;
60     }
61     return sizeof(arr) / sizeof(arr[0]);
62 }
63 
AudioVideoMuxerFuzzTest()64 void AudioVideoMuxerFuzzer::AudioVideoMuxerFuzzTest()
65 {
66     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
67         return;
68     }
69     fuzz_ = std::make_shared<AudioVideoMuxer>();
70     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
71     OH_AVOutputFormat outputFormat = AV_OUTPUT_FORMAT_MPEG_4;
72     fuzz_->Create(outputFormat, nullptr);
73     std::shared_ptr<OHOS::Media::AVBuffer> sample = std::make_shared<OHOS::Media::AVBuffer>();
74     constexpr int32_t executionModeCount = static_cast<int32_t>(TrackType::META_TRACK) + NUM_1;
75     TrackType type = static_cast<TrackType>(GetData<uint8_t>() % executionModeCount);
76     std::shared_ptr<AVMuxerImpl> impl = std::make_shared<AVMuxerImpl>();
77     fuzz_->muxer_ = impl;
78     int trackId = GetData<int>();
79     fuzz_->WriteSampleBuffer(sample, type);
80     std::shared_ptr<Format> format = std::make_shared<Format>();
81     fuzz_->AddTrack(trackId, format, type);
82 }
83 
Test()84 void Test()
85 {
86     auto audioVideoMuxer = std::make_unique<AudioVideoMuxerFuzzer>();
87     if (audioVideoMuxer == nullptr) {
88         MEDIA_INFO_LOG("audioVideoMuxer is null");
89         return;
90     }
91     audioVideoMuxer->AudioVideoMuxerFuzzTest();
92 }
93 
94 typedef void (*TestFuncs[1])();
95 
96 TestFuncs g_testFuncs = {
97     Test,
98 };
99 
FuzzTest(const uint8_t * rawData,size_t size)100 bool FuzzTest(const uint8_t* rawData, size_t size)
101 {
102     // initialize data
103     RAW_DATA = rawData;
104     g_dataSize = size;
105     g_pos = 0;
106 
107     uint32_t code = GetData<uint32_t>();
108     uint32_t len = GetArrLength(g_testFuncs);
109     if (len > 0) {
110         g_testFuncs[code % len]();
111     } else {
112         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
113     }
114 
115     return true;
116 }
117 } // namespace CameraStandard
118 } // namespace OHOS
119 
120 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)121 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
122 {
123     if (size < OHOS::CameraStandard::THRESHOLD) {
124         return 0;
125     }
126 
127     OHOS::CameraStandard::FuzzTest(data, size);
128     return 0;
129 }