• 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 "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 
23 namespace OHOS {
24 namespace CameraStandard {
25 using namespace DeferredProcessing;
26 static constexpr int32_t MAX_CODE_LEN  = 512;
27 static constexpr int32_t MIN_SIZE_NUM = 4;
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 
33 std::shared_ptr<Reader> ReaderFuzzer::fuzz_{nullptr};
34 
35 /*
36 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
37 * tips: only support basic type
38 */
39 template<class T>
GetData()40 T GetData()
41 {
42     T object {};
43     size_t objectSize = sizeof(object);
44     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
45         return object;
46     }
47     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
48     if (ret != EOK) {
49         return {};
50     }
51     g_pos += objectSize;
52     return object;
53 }
54 
55 template<class T>
GetArrLength(T & arr)56 uint32_t GetArrLength(T& arr)
57 {
58     if (arr == nullptr) {
59         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
60         return 0;
61     }
62     return sizeof(arr) / sizeof(arr[0]);
63 }
64 
ReaderFuzzTest()65 void ReaderFuzzer::ReaderFuzzTest()
66 {
67     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
68         return;
69     }
70     fuzz_ = std::make_shared<Reader>();
71     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
72     int32_t inputFd = GetData<int32_t>();
73     fuzz_->Create(inputFd);
74     fuzz_->GetSourceFormat();
75     Format sourceFormat;
76     fuzz_->sourceFormat_ = std::make_shared<Format>(sourceFormat);
77     fuzz_->InitTracksAndDemuxer();
78     auto mediaInfo = std::make_shared<MediaInfo>();
79     fuzz_->GetMediaInfo(mediaInfo);
80     fuzz_->GetSourceMediaInfo(mediaInfo);
81 
82     int32_t trackType = GetData<int32_t>();
83     int32_t trackIndex = GetData<int32_t>();
84     TrackFormat formatOfIndex;
85     Format trackFormat;
86     trackFormat.GetIntValue(Media::Tag::MEDIA_TYPE, trackType);
87     formatOfIndex.format = std::make_shared<Format>(trackFormat);
88     formatOfIndex.trackId = trackIndex;
89     fuzz_->GetTrackMediaInfo(formatOfIndex, mediaInfo);
90 }
91 
Test()92 void Test()
93 {
94     auto reader = std::make_unique<ReaderFuzzer>();
95     if (reader == nullptr) {
96         MEDIA_INFO_LOG("mpegManager is null");
97         return;
98     }
99     reader->ReaderFuzzTest();
100 }
101 
102 typedef void (*TestFuncs[1])();
103 
104 TestFuncs g_testFuncs = {
105     Test,
106 };
107 
FuzzTest(const uint8_t * rawData,size_t size)108 bool FuzzTest(const uint8_t* rawData, size_t size)
109 {
110     // initialize data
111     RAW_DATA = rawData;
112     g_dataSize = size;
113     g_pos = 0;
114 
115     uint32_t code = GetData<uint32_t>();
116     uint32_t len = GetArrLength(g_testFuncs);
117     if (len > 0) {
118         g_testFuncs[code % len]();
119     } else {
120         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
121     }
122 
123     return true;
124 }
125 } // namespace CameraStandard
126 } // namespace OHOS
127 
128 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)129 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
130 {
131     if (size < OHOS::CameraStandard::THRESHOLD) {
132         return 0;
133     }
134 
135     OHOS::CameraStandard::FuzzTest(data, size);
136     return 0;
137 }