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 "reader_fuzzer.h"
17 #include "message_parcel.h"
18 #include "ipc_file_descriptor.h"
19 #include "foundation/multimedia/camera_framework/common/utils/camera_log.h"
20 #include "securec.h"
21 #include <memory>
22 #include <fuzzer/FuzzedDataProvider.h>
23
24 namespace OHOS {
25 namespace CameraStandard {
26 using namespace DeferredProcessing;
27 static constexpr int32_t MIN_SIZE_NUM = 10;
28
29 std::shared_ptr<Reader> ReaderFuzzer::fuzz_{nullptr};
30 static const uint8_t* RAW_DATA = nullptr;
31 static size_t g_dataSize = 0;
32 static size_t g_pos;
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
ReaderFuzzTest(FuzzedDataProvider & fdp)54 void ReaderFuzzer::ReaderFuzzTest(FuzzedDataProvider& fdp)
55 {
56 fuzz_ = std::make_shared<Reader>();
57 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
58 fuzz_->GetSourceFormat();
59 Format sourceFormat;
60 fuzz_->sourceFormat_ = std::make_shared<Format>(sourceFormat);
61 fuzz_->InitTracksAndDemuxer();
62 auto mediaInfo = std::make_shared<MediaInfo>();
63 fuzz_->GetMediaInfo(mediaInfo);
64 fuzz_->GetSourceMediaInfo(mediaInfo);
65
66 int32_t trackType = fdp.ConsumeIntegralInRange<int32_t>(0, 300);
67 int32_t trackIndex = fdp.ConsumeIntegralInRange<int32_t>(0, 300);
68 TrackFormat formatOfIndex;
69 Format trackFormat;
70 trackFormat.GetIntValue(Media::Tag::MEDIA_TYPE, trackType);
71 formatOfIndex.format = std::make_shared<Format>(trackFormat);
72 formatOfIndex.trackId = trackIndex;
73 fuzz_->GetTrackMediaInfo(formatOfIndex, mediaInfo);
74 }
75
Test(uint8_t * data,size_t size)76 void Test(uint8_t* data, size_t size)
77 {
78 FuzzedDataProvider fdp(data, size);
79 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
80 return;
81 }
82 auto reader = std::make_unique<ReaderFuzzer>();
83 if (reader == nullptr) {
84 MEDIA_INFO_LOG("mpegManager is null");
85 return;
86 }
87 reader->ReaderFuzzTest(fdp);
88 }
89 } // namespace CameraStandard
90 } // namespace OHOS
91
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
94 {
95 OHOS::CameraStandard::Test(data, size);
96 return 0;
97 }