• 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 "mpeg_manager_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 
22 namespace OHOS {
23 namespace CameraStandard {
24 using namespace DeferredProcessing;
25 static constexpr int32_t MAX_CODE_LEN  = 512;
26 static constexpr int32_t MIN_SIZE_NUM = 4;
27 static const uint8_t* RAW_DATA = nullptr;
28 const size_t THRESHOLD = 10;
29 static size_t g_dataSize = 0;
30 static size_t g_pos;
31 
32 std::shared_ptr<MpegManager> MpegManagerFuzzer::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 
MpegManagerFuzzTest()64 void MpegManagerFuzzer::MpegManagerFuzzTest()
65 {
66     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
67         return;
68     }
69     fuzz_ = std::make_shared<MpegManager>();
70     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
71     MediaResult result1 = MediaResult::FAIL;
72     MediaResult result2 = MediaResult::PAUSE;
73     fuzz_->UnInit(result1);
74     fuzz_->UnInit(result2);
75     fuzz_->GetSurface();
76     fuzz_->GetMakerSurface();
77     fuzz_->GetProcessTimeStamp();
78     fuzz_->GetResultFd();
79     fuzz_->InitVideoCodec();
80     fuzz_->UnInitVideoCodec();
81     fuzz_->InitVideoMakerSurface();
82     fuzz_->UnInitVideoMaker();
83     std::vector<uint8_t> memoryFlags = {
84         static_cast<uint8_t>(MemoryFlag::MEMORY_READ_ONLY),
85         static_cast<uint8_t>(MemoryFlag::MEMORY_WRITE_ONLY),
86         static_cast<uint8_t>(MemoryFlag::MEMORY_READ_WRITE)
87     };
88     uint8_t randomIndex = GetData<uint8_t>() % memoryFlags.size();
89     MemoryFlag selectedFlag = static_cast<MemoryFlag>(memoryFlags[randomIndex]);
90     std::shared_ptr<AVAllocator> avAllocator = AVAllocatorFactory::CreateSharedAllocator(selectedFlag);
91     fuzz_->OnMakerBufferAvailable();
92     int64_t timestamp = GetData<int64_t>();
93     fuzz_->AcquireMakerBuffer(timestamp);
94     int flags = 1;
95     uint8_t randomNum = GetData<uint8_t>();
96     std::vector<std::string> testStrings = {"test1", "test2"};
97     std::string requestId(testStrings[randomNum % testStrings.size()]);
98     fuzz_->GetFileFd(requestId, flags, "_vid_temp");
99     fuzz_->GetFileFd(requestId, flags, "_vid");
100 }
101 
Test()102 void Test()
103 {
104     auto mpegManager = std::make_unique<MpegManagerFuzzer>();
105     if (mpegManager == nullptr) {
106         MEDIA_INFO_LOG("mpegManager is null");
107         return;
108     }
109     mpegManager->MpegManagerFuzzTest();
110 }
111 
112 typedef void (*TestFuncs[1])();
113 
114 TestFuncs g_testFuncs = {
115     Test,
116 };
117 
FuzzTest(const uint8_t * rawData,size_t size)118 bool FuzzTest(const uint8_t* rawData, size_t size)
119 {
120     // initialize data
121     RAW_DATA = rawData;
122     g_dataSize = size;
123     g_pos = 0;
124 
125     uint32_t code = GetData<uint32_t>();
126     uint32_t len = GetArrLength(g_testFuncs);
127     if (len > 0) {
128         g_testFuncs[code % len]();
129     } else {
130         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
131     }
132 
133     return true;
134 }
135 } // namespace CameraStandard
136 } // namespace OHOS
137 
138 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)139 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
140 {
141     if (size < OHOS::CameraStandard::THRESHOLD) {
142         return 0;
143     }
144 
145     OHOS::CameraStandard::FuzzTest(data, size);
146     return 0;
147 }