• 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 "deferred_video_controller_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 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<DeferredVideoController> DeferredVideoControllerFuzzer::fuzz_{nullptr};
33 std::shared_ptr<VideoStrategyCenter> DeferredVideoControllerFuzzer::center_{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 
DeferredVideoControllerFuzzTest()65 void DeferredVideoControllerFuzzer::DeferredVideoControllerFuzzTest()
66 {
67     int32_t userId = GetData<int32_t>();
68     std::shared_ptr<VideoJobRepository> repository;
69     uint8_t randomNum = GetData<uint8_t>();
70     std::vector<std::string> testStrings = {"test1", "test2"};
71     std::string videoId(testStrings[randomNum % testStrings.size()]);
72     repository = std::make_shared<VideoJobRepository>(userId);
73     CHECK_ERROR_RETURN_LOG(!repository, "Create repository Error");
74     repository->SetJobPending(videoId);
75     repository->SetJobRunning(videoId);
76     repository->SetJobCompleted(videoId);
77     repository->SetJobFailed(videoId);
78     repository->SetJobPause(videoId);
79     repository->SetJobError(videoId);
80     center_ = std::make_shared<DeferredProcessing::VideoStrategyCenter>(userId, repository);
81     CHECK_ERROR_RETURN_LOG(!center_, "Create center_ Error");
82     const std::shared_ptr<VideoPostProcessor> postProcessor =
83         std::make_shared<VideoPostProcessor>(userId);
84     const std::shared_ptr<IVideoProcessCallbacksFuzz> callback =
85         std::make_shared<IVideoProcessCallbacksFuzz>();
86     std::shared_ptr<DeferredVideoProcessor> processor =
87         std::make_shared<DeferredVideoProcessor>(repository, postProcessor, callback);
88     fuzz_ = std::make_shared<DeferredVideoController>(userId, repository, processor);
89     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
90     sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>());
91     sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(GetData<int>());
92     std::shared_ptr<DeferredVideoJob> jobPtr =
93         std::make_shared<DeferredVideoJob>(videoId, srcFd, dstFd);
94     fuzz_->Initialize();
95     fuzz_->OnVideoJobChanged(jobPtr);
96     constexpr int32_t executionModeCount1 = static_cast<int32_t>(ExecutionMode::DUMMY) + 1;
97     ExecutionMode selectedExecutionMode = static_cast<ExecutionMode>(GetData<uint8_t>() % executionModeCount1);
98     constexpr int32_t executionModeCount2 = static_cast<int32_t>(DpsError::DPS_ERROR_VIDEO_PROC_INTERRUPTED) + 1;
99     DpsError selectedDpsError = static_cast<DpsError>(GetData<uint8_t>() % executionModeCount2);
100     std::shared_ptr<DeferredVideoWork> work =
101         std::make_shared<DeferredVideoWork>(jobPtr, selectedExecutionMode, dstFd);
102     fuzz_->HandleSuccess(work);
103     fuzz_->HandleError(work, selectedDpsError);
104     fuzz_->HandleServiceDied();
105     fuzz_->TryDoSchedule();
106     fuzz_->PostProcess(work);
107     fuzz_->SetDefaultExecutionMode();
108     fuzz_->StartSuspendLock();
109     fuzz_->StopSuspendLock();
110     fuzz_->HandleNormalSchedule(work);
111     fuzz_->OnTimerOut();
112 }
113 
Test()114 void Test()
115 {
116     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
117         return;
118     }
119     auto deferredVideoController = std::make_unique<DeferredVideoControllerFuzzer>();
120     if (deferredVideoController == nullptr) {
121         MEDIA_INFO_LOG("deferredVideoController is null");
122         return;
123     }
124     deferredVideoController->DeferredVideoControllerFuzzTest();
125 }
126 
127 typedef void (*TestFuncs[1])();
128 
129 TestFuncs g_testFuncs = {
130     Test,
131 };
132 
FuzzTest(const uint8_t * rawData,size_t size)133 bool FuzzTest(const uint8_t* rawData, size_t size)
134 {
135     // initialize data
136     RAW_DATA = rawData;
137     g_dataSize = size;
138     g_pos = 0;
139 
140     uint32_t code = GetData<uint32_t>();
141     uint32_t len = GetArrLength(g_testFuncs);
142     if (len > 0) {
143         g_testFuncs[code % len]();
144     } else {
145         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
146     }
147 
148     return true;
149 }
150 } // namespace CameraStandard
151 } // namespace OHOS
152 
153 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)154 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
155 {
156     if (size < OHOS::CameraStandard::THRESHOLD) {
157         return 0;
158     }
159 
160     OHOS::CameraStandard::FuzzTest(data, size);
161     return 0;
162 }