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