• 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 "video_job_queue_fuzzer.h"
17 #include "camera_log.h"
18 #include "message_parcel.h"
19 #include "ipc_file_descriptor.h"
20 #include "securec.h"
21 
22 namespace OHOS {
23 namespace CameraStandard {
24 using namespace DeferredProcessing;
25 using DeferredVideoJobPtr = std::shared_ptr<DeferredVideoJob>;
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 std::shared_ptr<VideoJobQueue> VideoJobQueueFuzzer::fuzz_{nullptr};
33 std::shared_ptr<DeferredVideoWork> DeferredVideoWorkFuzzer::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 
Initialization()65 void DeferredVideoWorkFuzzer::Initialization()
66 {
67     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
68         return;
69     }
70     uint8_t randomNum = GetData<uint8_t>();
71     std::vector<std::string> testStrings = {"test1", "test2"};
72     std::string videoId_(testStrings[randomNum % testStrings.size()]);
73 
74     sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>());
75     sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>());
76 
77     DeferredVideoJobPtr jobPtr = std::make_shared<DeferredVideoJob>(videoId_, srcFd, dstFd);
78     auto isAutoSuspend = GetData<bool>();
79     fuzz_ = std::make_shared<DeferredProcessing::
80         DeferredVideoWork>(jobPtr, ExecutionMode::HIGH_PERFORMANCE, isAutoSuspend);
81     CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
82 }
83 
VideoJobQueueFuzzTest()84 void VideoJobQueueFuzzer::VideoJobQueueFuzzTest()
85 {
86     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
87         return;
88     }
89 
90     uint8_t randomNum = GetData<uint8_t>();
91     std::vector<std::string> testStrings = {"test1", "test2"};
92     std::string videoId(testStrings[randomNum % testStrings.size()]);
93 
94     sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>());
95     sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>());
96 
97     DeferredVideoJobPtr jobPtr = std::make_shared<DeferredVideoJob>(videoId, srcFd, dstFd);
98     if (fuzz_ == nullptr) {
99         DeferredProcessing::VideoJobQueue::Comparator comp =
100             [](DeferredVideoJobPtr a, DeferredVideoJobPtr b) {
101                 return a->GetVideoId() < b->GetVideoId();
102             };
103         fuzz_ = std::make_shared<DeferredProcessing::VideoJobQueue>(comp);
104     }
105     fuzz_->Contains(jobPtr);
106     fuzz_->Peek();
107     fuzz_->Push(jobPtr);
108     fuzz_->GetAllElements();
109     fuzz_->Pop();
110     fuzz_->Remove(jobPtr);
111     auto x = (GetData<uint32_t>());
112     auto y = (GetData<uint32_t>());
113     fuzz_->Swap(x, y);
114 }
115 
Test()116 void Test()
117 {
118     auto deferredVideoWork = std::make_unique<DeferredVideoWorkFuzzer>();
119     if (deferredVideoWork == nullptr) {
120         MEDIA_INFO_LOG("deferredVideoWork is null");
121         return;
122     }
123     deferredVideoWork->Initialization();
124     auto videoJobQueue = std::make_unique<VideoJobQueueFuzzer>();
125     if (videoJobQueue == nullptr) {
126         MEDIA_INFO_LOG("videoJobQueue is null");
127         return;
128     }
129     videoJobQueue->VideoJobQueueFuzzTest();
130 }
131 
132 typedef void (*TestFuncs[1])();
133 
134 TestFuncs g_testFuncs = {
135     Test,
136 };
137 
FuzzTest(const uint8_t * rawData,size_t size)138 bool FuzzTest(const uint8_t* rawData, size_t size)
139 {
140     // initialize data
141     RAW_DATA = rawData;
142     g_dataSize = size;
143     g_pos = 0;
144 
145     uint32_t code = GetData<uint32_t>();
146     uint32_t len = GetArrLength(g_testFuncs);
147     if (len > 0) {
148         g_testFuncs[code % len]();
149     } else {
150         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
151     }
152 
153     return true;
154 }
155 } // namespace CameraStandard
156 } // namespace OHOS
157 
158 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)159 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
160 {
161     if (size < OHOS::CameraStandard::THRESHOLD) {
162         return 0;
163     }
164 
165     OHOS::CameraStandard::FuzzTest(data, size);
166     return 0;
167 }