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
18 #include <fcntl.h>
19
20 #include "camera_log.h"
21 #include "dp_log.h"
22 #include "message_parcel.h"
23 #include "ipc_file_descriptor.h"
24 #include "securec.h"
25
26 namespace OHOS {
27 namespace CameraStandard {
28 using namespace DeferredProcessing;
29 static constexpr int32_t MIN_SIZE_NUM = 128;
30 static constexpr int NUM_TWO = 2;
31 const size_t MAX_LENGTH_STRING = 64;
32 const char* TEST_FILE_PATH_1 = "/data/test/DeferredVideoProcessorFuzzTest_test_file1.mp4";
33 const char* TEST_FILE_PATH_2 = "/data/test/DeferredVideoProcessorFuzzTest_test_file2.mp4";
34 std::shared_ptr<DeferredVideoProcessor> DeferredVideoProcessorFuzzer::fuzz_{nullptr};
35 std::shared_ptr<VideoStrategyCenter> DeferredVideoProcessorFuzzer::center_{nullptr};
36
DeferredVideoProcessorFuzzTest(FuzzedDataProvider & fdp)37 void DeferredVideoProcessorFuzzer::DeferredVideoProcessorFuzzTest(FuzzedDataProvider& fdp)
38 {
39 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
40 return;
41 }
42 int32_t userId = fdp.ConsumeIntegral<int32_t>();
43 std::string videoId(fdp.ConsumeRandomLengthString(MAX_LENGTH_STRING));
44 std::shared_ptr<VideoJobRepository> repository = std::make_shared<VideoJobRepository>(userId);
45 DP_CHECK_ERROR_RETURN_LOG(!repository, "Create repository Error");
46 repository->SetJobPending(videoId);
47 repository->SetJobRunning(videoId);
48 repository->SetJobCompleted(videoId);
49 repository->SetJobFailed(videoId);
50 repository->SetJobPause(videoId);
51 repository->SetJobError(videoId);
52 center_ = std::make_shared<DeferredProcessing::VideoStrategyCenter>(repository);
53 DP_CHECK_ERROR_RETURN_LOG(!center_, "Create center_ Error");
54 const std::shared_ptr<VideoPostProcessor> postProcessor = std::make_shared<VideoPostProcessor>(userId);
55 const std::shared_ptr<IVideoProcessCallbacksFuzz> callback = std::make_shared<IVideoProcessCallbacksFuzz>();
56 fuzz_ = std::make_shared<DeferredVideoProcessor>(repository, postProcessor, callback);
57 int sfd = open(TEST_FILE_PATH_1, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
58 int dfd = open(TEST_FILE_PATH_2, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
59 fdsan_exchange_owner_tag(sfd, 0, LOG_DOMAIN);
60 fdsan_exchange_owner_tag(dfd, 0, LOG_DOMAIN);
61 sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(sfd);
62 sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(dfd);
63 std::shared_ptr<DeferredVideoJob> jobPtr = std::make_shared<DeferredVideoJob>(videoId, srcFd, dstFd);
64 constexpr int32_t executionModeCount1 = static_cast<int32_t>(ExecutionMode::DUMMY) + 1;
65 ExecutionMode selectedExecutionMode =
66 static_cast<ExecutionMode>(fdp.ConsumeIntegral<uint8_t>() % executionModeCount1);
67 std::shared_ptr<DeferredVideoWork> work =
68 std::make_shared<DeferredVideoWork>(jobPtr, selectedExecutionMode, fdp.ConsumeBool());
69 fuzz_->Initialize();
70 fuzz_->PostProcess(work);
71 constexpr int32_t executionModeCount2 =
72 static_cast<int32_t>(SchedulerType::NORMAL_TIME_STATE) + NUM_TWO;
73 SchedulerType selectedScheduleType =
74 static_cast<SchedulerType>(fdp.ConsumeIntegral<uint8_t>() % executionModeCount2);
75 constexpr int32_t executionModeCount3 =
76 static_cast<int32_t>(DpsError::DPS_ERROR_VIDEO_PROC_INTERRUPTED) + NUM_TWO;
77 DpsError selectedDpsError = static_cast<DpsError>(fdp.ConsumeIntegral<uint8_t>() % executionModeCount3);
78 constexpr int32_t executionModeCount4 =
79 static_cast<int32_t>(DpsStatus::DPS_SESSION_STATE_SUSPENDED) + NUM_TWO;
80 DpsStatus selectedDpsStatus = static_cast<DpsStatus>(fdp.ConsumeIntegral<uint8_t>() % executionModeCount4);
81 fuzz_->PauseRequest(selectedScheduleType);
82 fuzz_->SetDefaultExecutionMode();
83 fuzz_->IsFatalError(selectedDpsError);
84 fuzz_->OnStateChanged(userId, selectedDpsStatus);
85 fuzz_->OnError(userId, videoId, selectedDpsError);
86 sptr<IPCFileDescriptor> ipcFd = sptr<IPCFileDescriptor>::MakeSptr(fdp.ConsumeIntegral<int>());
87 fuzz_->OnProcessDone(userId, videoId, ipcFd);
88
89 remove(TEST_FILE_PATH_1);
90 remove(TEST_FILE_PATH_2);
91 }
92
Test(uint8_t * data,size_t size)93 void Test(uint8_t* data, size_t size)
94 {
95 FuzzedDataProvider fdp(data, size);
96 auto deferredVideoProcessor = std::make_unique<DeferredVideoProcessorFuzzer>();
97 if (deferredVideoProcessor == nullptr) {
98 DP_INFO_LOG("deferredVideoProcessor is null");
99 return;
100 }
101 deferredVideoProcessor->DeferredVideoProcessorFuzzTest(fdp);
102 }
103
104 } // namespace CameraStandard
105 } // namespace OHOS
106
107 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)108 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
109 {
110 OHOS::CameraStandard::Test(data, size);
111 return 0;
112 }