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