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_process_command_fuzzer.h"
17 #include "buffer_info.h"
18 #include "securec.h"
19 #include "foundation/multimedia/camera_framework/common/utils/camera_log.h"
20 using namespace std;
21
22 namespace OHOS {
23 namespace CameraStandard {
24 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 /*
33 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
34 * tips: only support basic type
35 */
36 template<class T>
GetData()37 T GetData()
38 {
39 T object {};
40 size_t objectSize = sizeof(object);
41 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
42 return object;
43 }
44 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
45 if (ret != EOK) {
46 return {};
47 }
48 g_pos += objectSize;
49 return object;
50 }
51
52 template<class T>
GetArrLength(T & arr)53 uint32_t GetArrLength(T& arr)
54 {
55 if (arr == nullptr) {
56 MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
57 return 0;
58 }
59 return sizeof(arr) / sizeof(arr[0]);
60 }
61
VideoProcessCommandFuzzTest()62 void VideoProcessCommandFuzzer::VideoProcessCommandFuzzTest()
63 {
64 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
65 return;
66 }
67 auto userId = GetData<int32_t>();
68 std::shared_ptr<VideoProcessCommandFuzz>command = std::make_shared<VideoProcessCommandFuzz>(userId);
69 if (command == nullptr) {
70 return;
71 }
72 command->Executing();
73 uint8_t randomNum = GetData<uint8_t>();
74 std::vector<std::string> testStrings = {"test1", "test2"};
75 std::string videoId(testStrings[randomNum % testStrings.size()]);
76 std::shared_ptr<VideoProcessSuccessFuzz>success = std::make_shared<VideoProcessSuccessFuzz>(userId, videoId);
77 if (success == nullptr) {
78 return;
79 }
80 success->Executing();
81 auto errorCode = GetData<int32_t>();
82 std::shared_ptr<VideoProcessFailedFuzz>failed = std::make_shared<VideoProcessFailedFuzz>(userId,
83 videoId, static_cast<DpsError>(errorCode));
84 if (failed == nullptr) {
85 return;
86 }
87 failed->Executing();
88 }
89
Test()90 void Test()
91 {
92 auto videoProcessCommand = std::make_unique<VideoProcessCommandFuzzer>();
93 if (videoProcessCommand == nullptr) {
94 MEDIA_INFO_LOG("videoProcessCommand is null");
95 return;
96 }
97 videoProcessCommand->VideoProcessCommandFuzzTest();
98 }
99
100 typedef void (*TestFuncs[1])();
101
102 TestFuncs g_testFuncs = {
103 Test,
104 };
105
FuzzTest(const uint8_t * rawData,size_t size)106 bool FuzzTest(const uint8_t* rawData, size_t size)
107 {
108 // initialize data
109 RAW_DATA = rawData;
110 g_dataSize = size;
111 g_pos = 0;
112
113 uint32_t code = GetData<uint32_t>();
114 uint32_t len = GetArrLength(g_testFuncs);
115 if (len > 0) {
116 g_testFuncs[code % len]();
117 } else {
118 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
119 }
120
121 return true;
122 }
123 }
124 }
125 }
126 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)127 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
128 {
129 if (size < OHOS::CameraStandard::DeferredProcessing::THRESHOLD) {
130 return 0;
131 }
132
133 OHOS::CameraStandard::DeferredProcessing::FuzzTest(data, size);
134 return 0;
135 }
136
137