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_repository_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 std::shared_ptr<DeferredProcessing::VideoJobRepository> VideoJobRepositoryFuzzer::fuzz_{nullptr};
27 static constexpr int32_t MAX_CODE_LEN = 512;
28 static constexpr int32_t MIN_SIZE_NUM = 4;
29 static const uint8_t* RAW_DATA = nullptr;
30 const size_t THRESHOLD = 10;
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
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
VideoJobRepositoryFuzzTest()64 void VideoJobRepositoryFuzzer::VideoJobRepositoryFuzzTest()
65 {
66 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
67 return;
68 }
69
70 int32_t userId = GetData<int32_t>();
71 fuzz_ = std::make_shared<DeferredProcessing::VideoJobRepository>(userId);
72 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
73 uint8_t randomNum = GetData<uint8_t>();
74 std::vector<std::string> testStrings = {"test1", "test2"};
75 std::string videoId(testStrings[randomNum % testStrings.size()]);
76 fuzz_->SetJobPending(videoId);
77 fuzz_->SetJobRunning(videoId);
78 fuzz_->SetJobCompleted(videoId);
79 fuzz_->SetJobFailed(videoId);
80 fuzz_->SetJobPause(videoId);
81 fuzz_->SetJobError(videoId);
82 fuzz_->GetJob();
83 fuzz_->GetRunningJobCounts();
84 std::vector<std::string> list = {"fuzz1", "fuzz2"};
85 fuzz_->GetRunningJobList(list);
86 auto statusChanged = GetData<bool>();
87 std::string videoId_(testStrings[randomNum % testStrings.size()]);
88 sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>());
89 sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>());
90 DeferredVideoJobPtr jobPtr = std::make_shared<DeferredVideoJob>(videoId_, srcFd, dstFd);
91 fuzz_->NotifyJobChangedUnLocked(statusChanged, jobPtr);
92 }
93
Test()94 void Test()
95 {
96 auto videoJobRepository = std::make_unique<VideoJobRepositoryFuzzer>();
97 if (videoJobRepository == nullptr) {
98 MEDIA_INFO_LOG("videoJobRepository is null");
99 return;
100 }
101 videoJobRepository->VideoJobRepositoryFuzzTest();
102 }
103
104 typedef void (*TestFuncs[1])();
105
106 TestFuncs g_testFuncs = {
107 Test,
108 };
109
FuzzTest(const uint8_t * rawData,size_t size)110 bool FuzzTest(const uint8_t* rawData, size_t size)
111 {
112 // initialize data
113 RAW_DATA = rawData;
114 g_dataSize = size;
115 g_pos = 0;
116
117 uint32_t code = GetData<uint32_t>();
118 uint32_t len = GetArrLength(g_testFuncs);
119 if (len > 0) {
120 g_testFuncs[code % len]();
121 } else {
122 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
123 }
124
125 return true;
126 }
127 } // namespace CameraStandard
128 } // namespace OHOS
129
130 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)131 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
132 {
133 if (size < OHOS::CameraStandard::THRESHOLD) {
134 return 0;
135 }
136
137 OHOS::CameraStandard::FuzzTest(data, size);
138 return 0;
139 }