• 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 "camera_demuxer_fuzzer.h"
17 #include "message_parcel.h"
18 #include "camera_log.h"
19 #include "securec.h"
20 
21 namespace OHOS {
22 namespace CameraStandard {
23 using namespace DeferredProcessing;
24 static constexpr int32_t MAX_CODE_LEN = 512;
25 static constexpr int32_t MIN_SIZE_NUM = 4;
26 static const uint8_t* RAW_DATA = nullptr;
27 const size_t THRESHOLD = 10;
28 static size_t g_dataSize = 0;
29 static size_t g_pos;
30 
31 std::shared_ptr<Demuxer> CameraDemuxerFuzzer::fuzz_{nullptr};
32 
33 /*
34 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
35 * tips: only support basic type
36 */
37 template<class T>
GetData()38 T GetData()
39 {
40     T object {};
41     size_t objectSize = sizeof(object);
42     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
43         return object;
44     }
45     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
46     if (ret != EOK) {
47         return {};
48     }
49     g_pos += objectSize;
50     return object;
51 }
52 
53 template<class T>
GetArrLength(T & arr)54 uint32_t GetArrLength(T& arr)
55 {
56     if (arr == nullptr) {
57         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
58         return 0;
59     }
60     return sizeof(arr) / sizeof(arr[0]);
61 }
62 
CameraDemuxerFuzzTest()63 void CameraDemuxerFuzzer::CameraDemuxerFuzzTest()
64 {
65     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
66         return;
67     }
68 
69     fuzz_ = std::make_shared<Demuxer>();
70     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
71     std::shared_ptr<AVSourceTest> source = std::make_shared<AVSourceTest>();
72     std::map<Media::Plugins::MediaType, std::shared_ptr<Track>> tracks;
73     fuzz_->Create(source, tracks);
74 
75     std::vector<uint8_t> memoryFlags = {
76         static_cast<uint8_t>(MemoryFlag::MEMORY_READ_ONLY),
77         static_cast<uint8_t>(MemoryFlag::MEMORY_WRITE_ONLY),
78         static_cast<uint8_t>(MemoryFlag::MEMORY_READ_WRITE)
79     };
80 
81     uint8_t randomIndex = GetData<uint8_t>() % memoryFlags.size();
82     MemoryFlag selectedFlag = static_cast<MemoryFlag>(memoryFlags[randomIndex]);
83     std::shared_ptr<AVAllocator> avAllocator =
84         AVAllocatorFactory::CreateSharedAllocator(selectedFlag);
85     int32_t capacity = GetData<int32_t>();
86     if (capacity <= 0) {
87         MEDIA_INFO_LOG("Invalid capacity: %d", capacity);
88         return;
89     }
90     std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer(avAllocator, capacity);
91 
92     std::vector<Media::Plugins::MediaType> mediaTypes = {
93         Media::Plugins::MediaType::UNKNOWN,
94         Media::Plugins::MediaType::AUDIO,
95         Media::Plugins::MediaType::VIDEO,
96         Media::Plugins::MediaType::SUBTITLE,
97         Media::Plugins::MediaType::ATTACHMENT,
98         Media::Plugins::MediaType::DATA,
99         Media::Plugins::MediaType::TIMEDMETA
100     };
101 
102     uint8_t mediaTypeIndex = GetData<uint8_t>() % mediaTypes.size();
103     Media::Plugins::MediaType selectedMediaType = mediaTypes[mediaTypeIndex];
104     fuzz_->ReadStream(selectedMediaType, buffer);
105 }
106 
Test()107 void Test()
108 {
109     auto cameraDemuxer = std::make_unique<CameraDemuxerFuzzer>();
110     if (cameraDemuxer == nullptr) {
111         MEDIA_INFO_LOG("cameraDemuxer is null");
112         return;
113     }
114     cameraDemuxer->CameraDemuxerFuzzTest();
115 }
116 
117 typedef void (*TestFuncs[1])();
118 
119 TestFuncs g_testFuncs = {
120     Test,
121 };
122 
FuzzTest(const uint8_t * rawData,size_t size)123 bool FuzzTest(const uint8_t* rawData, size_t size)
124 {
125     // initialize data
126     RAW_DATA = rawData;
127     g_dataSize = size;
128     g_pos = 0;
129 
130     uint32_t code = GetData<uint32_t>();
131     uint32_t len = GetArrLength(g_testFuncs);
132     if (len > 0) {
133         g_testFuncs[code % len]();
134     } else {
135         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
136     }
137 
138     return true;
139 }
140 } // namespace CameraStandard
141 } // namespace OHOS
142 
143 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)144 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
145 {
146     if (size < OHOS::CameraStandard::THRESHOLD) {
147         return 0;
148     }
149 
150     OHOS::CameraStandard::FuzzTest(data, size);
151     return 0;
152 }