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 "deferred_video_processor_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 static constexpr int32_t MAX_CODE_LEN = 512;
26 static constexpr int32_t MIN_SIZE_NUM = 4;
27 static constexpr int NUM_TWO = 2;
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<DeferredVideoProcessor> DeferredVideoProcessorFuzzer::fuzz_{nullptr};
34 std::shared_ptr<VideoStrategyCenter> DeferredVideoProcessorFuzzer::center_{nullptr};
35
36 /*
37 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
38 * tips: only support basic type
39 */
40 template<class T>
GetData()41 T GetData()
42 {
43 T object {};
44 size_t objectSize = sizeof(object);
45 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
46 return object;
47 }
48 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
49 if (ret != EOK) {
50 return {};
51 }
52 g_pos += objectSize;
53 return object;
54 }
55
56 template<class T>
GetArrLength(T & arr)57 uint32_t GetArrLength(T& arr)
58 {
59 if (arr == nullptr) {
60 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
61 return 0;
62 }
63 return sizeof(arr) / sizeof(arr[0]);
64 }
65
DeferredVideoProcessorFuzzTest()66 void DeferredVideoProcessorFuzzer::DeferredVideoProcessorFuzzTest()
67 {
68 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
69 return;
70 }
71 int32_t userId = GetData<int32_t>();
72 uint8_t randomNum = GetData<uint8_t>();
73 std::vector<std::string> testStrings = {"test1", "test2"};
74 std::string videoId(testStrings[randomNum % testStrings.size()]);
75 std::shared_ptr<VideoJobRepository> repository = std::make_shared<VideoJobRepository>(userId);
76 CHECK_ERROR_RETURN_LOG(!repository, "Create repository Error");
77 repository->SetJobPending(videoId);
78 repository->SetJobRunning(videoId);
79 repository->SetJobCompleted(videoId);
80 repository->SetJobFailed(videoId);
81 repository->SetJobPause(videoId);
82 repository->SetJobError(videoId);
83 center_ = std::make_shared<DeferredProcessing::VideoStrategyCenter>(userId, repository);
84 CHECK_ERROR_RETURN_LOG(!center_, "Create center_ Error");
85 const std::shared_ptr<VideoPostProcessor> postProcessor = std::make_shared<VideoPostProcessor>(userId);
86 const std::shared_ptr<IVideoProcessCallbacksFuzz> callback = std::make_shared<IVideoProcessCallbacksFuzz>();
87 fuzz_ = std::make_shared<DeferredVideoProcessor>(repository, postProcessor, callback);
88 sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>());
89 sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>());
90 std::shared_ptr<DeferredVideoJob> jobPtr = std::make_shared<DeferredVideoJob>(videoId, srcFd, dstFd);
91 constexpr int32_t executionModeCount1 = static_cast<int32_t>(ExecutionMode::DUMMY) + 1;
92 ExecutionMode selectedExecutionMode = static_cast<ExecutionMode>(GetData<uint8_t>() % executionModeCount1);
93 std::shared_ptr<DeferredVideoWork> work =
94 std::make_shared<DeferredVideoWork>(jobPtr, selectedExecutionMode, dstFd);
95 fuzz_->Initialize();
96 fuzz_->PostProcess(work);
97 constexpr int32_t executionModeCount2 =
98 static_cast<int32_t>(ScheduleType::NORMAL_TIME_STATE) + NUM_TWO;
99 ScheduleType selectedScheduleType = static_cast<ScheduleType>(GetData<uint8_t>() % executionModeCount2);
100 constexpr int32_t executionModeCount3 =
101 static_cast<int32_t>(DpsError::DPS_ERROR_VIDEO_PROC_INTERRUPTED) + NUM_TWO;
102 DpsError selectedDpsError = static_cast<DpsError>(GetData<uint8_t>() % executionModeCount3);
103 constexpr int32_t executionModeCount4 =
104 static_cast<int32_t>(DpsStatus::DPS_SESSION_STATE_SUSPENDED) + NUM_TWO;
105 DpsStatus selectedDpsStatus = static_cast<DpsStatus>(GetData<uint8_t>() % executionModeCount4);
106 fuzz_->PauseRequest(selectedScheduleType);
107 fuzz_->SetDefaultExecutionMode();
108 fuzz_->IsFatalError(selectedDpsError);
109 fuzz_->OnStateChanged(userId, selectedDpsStatus);
110 fuzz_->OnError(userId, videoId, selectedDpsError);
111 sptr<IPCFileDescriptor> ipcFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>()*NUM_TWO);
112 fuzz_->OnProcessDone(userId, videoId, ipcFd);
113 }
114
Test()115 void Test()
116 {
117 auto deferredVideoProcessor = std::make_unique<DeferredVideoProcessorFuzzer>();
118 if (deferredVideoProcessor == nullptr) {
119 MEDIA_INFO_LOG("deferredVideoProcessor is null");
120 return;
121 }
122 deferredVideoProcessor->DeferredVideoProcessorFuzzTest();
123 }
124
125 typedef void (*TestFuncs[1])();
126
127 TestFuncs g_testFuncs = {
128 Test,
129 };
130
FuzzTest(const uint8_t * rawData,size_t size)131 bool FuzzTest(const uint8_t* rawData, size_t size)
132 {
133 // initialize data
134 RAW_DATA = rawData;
135 g_dataSize = size;
136 g_pos = 0;
137
138 uint32_t code = GetData<uint32_t>();
139 uint32_t len = GetArrLength(g_testFuncs);
140 if (len > 0) {
141 g_testFuncs[code % len]();
142 } else {
143 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
144 }
145
146 return true;
147 }
148 } // namespace CameraStandard
149 } // namespace OHOS
150
151 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)152 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
153 {
154 if (size < OHOS::CameraStandard::THRESHOLD) {
155 return 0;
156 }
157
158 OHOS::CameraStandard::FuzzTest(data, size);
159 return 0;
160 }