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 "mapper_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <securec.h>
21 #include "hdf_base.h"
22 #include "display_common_fuzzer.h"
23 namespace OHOS {
24 using namespace OHOS::HDI::Display::Buffer;
25 using namespace OHOS::HDI::Display::Buffer::V1_2;
26 using namespace OHOS::HDI::Display::Composer::V1_0;
27 static std::shared_ptr<OHOS::HDI::Display::Buffer::V1_2::IDisplayBuffer> g_bufferInterface = nullptr;
28
29 static bool g_isInit = false;
30 static const uint8_t* RANDOM_DATA = nullptr;
31 static size_t g_dataSize = 0;
32 static size_t g_pos;
33 /*
34 * describe: get data from outside untrusted data(RANDOM_DATA) which size is according to sizeof(T)
35 * tips: only support basic type
36 */
37 template<class T>
GetData()38 T GetData()
39 {
40 T object {};
41 size_t objectSize = sizeof(object);
42 if (RANDOM_DATA == nullptr || objectSize > g_dataSize - g_pos) {
43 return object;
44 }
45 errno_t ret = memcpy_s(&object, objectSize, RANDOM_DATA + g_pos, objectSize);
46 if (ret != EOK) {
47 return {};
48 }
49 g_pos += objectSize;
50 return object;
51 }
52
GetAllocInfo(OHOS::HDI::Display::Buffer::V1_0::AllocInfo & info)53 static int32_t GetAllocInfo(OHOS::HDI::Display::Buffer::V1_0::AllocInfo& info)
54 {
55 uint32_t lenUsage = GetArrLength(CONVERT_TABLE_USAGE);
56 if (lenUsage == 0) {
57 HDF_LOGE("%{public}s: CONVERT_TABLE_USAGE length is equal to 0", __func__);
58 return DISPLAY_FAILURE;
59 }
60 uint32_t lenFormat = GetArrLength(CONVERT_TABLE_FORMAT);
61 if (lenFormat == 0) {
62 HDF_LOGE("%{public}s: CONVERT_TABLE_FORMAT length is equal to 0", __func__);
63 return DISPLAY_FAILURE;
64 }
65
66 info.width = GetData<uint32_t>() % WIDTH;
67 info.height = GetData<uint32_t>() % HEIGHT;
68 info.usage = CONVERT_TABLE_USAGE[GetData<uint32_t>() % lenUsage];
69 info.format = CONVERT_TABLE_FORMAT[GetData<uint32_t>() % lenFormat];
70 info.expectedSize = info.width * info.height;
71 return DISPLAY_SUCCESS;
72 }
73
UsingAllocmem()74 BufferHandle* UsingAllocmem()
75 {
76 AllocInfo info = { 0 };
77 int32_t ret = GetAllocInfo(info);
78 if (ret != DISPLAY_SUCCESS) {
79 HDF_LOGE("%{public}s: function GetAllocInfo failed", __func__);
80 return nullptr;
81 }
82
83 BufferHandle* handle = nullptr;
84 ret = g_bufferInterface->AllocMem(info, handle);
85 if (ret != DISPLAY_SUCCESS && handle != nullptr) {
86 HDF_LOGE("%{public}s: function AllocMem failed", __func__);
87 g_bufferInterface->FreeMem(*handle);
88 return nullptr;
89 }
90 return handle;
91 }
92
TestFreeMem(const BufferHandle & handle)93 void TestFreeMem(const BufferHandle& handle)
94 {
95 (void)g_bufferInterface->FreeMem(handle);
96 }
97
TestMmapUnmap(const BufferHandle & handle)98 void TestMmapUnmap(const BufferHandle& handle)
99 {
100 (void)g_bufferInterface->Mmap(handle);
101 (void)g_bufferInterface->Unmap(handle);
102 (void)g_bufferInterface->FreeMem(handle);
103 }
104
TestFlushCache(const BufferHandle & handle)105 void TestFlushCache(const BufferHandle& handle)
106 {
107 (void)g_bufferInterface->FlushCache(handle);
108 (void)g_bufferInterface->FreeMem(handle);
109 }
110
TestInvalidateCache(const BufferHandle & handle)111 void TestInvalidateCache(const BufferHandle& handle)
112 {
113 (void)g_bufferInterface->InvalidateCache(handle);
114 (void)g_bufferInterface->FreeMem(handle);
115 }
116
TestGetImageLayout(const BufferHandle & handle)117 void TestGetImageLayout(const BufferHandle& handle)
118 {
119 V1_2::ImageLayout layout = {0};
120 (void)g_bufferInterface->GetImageLayout(handle, layout);
121 (void)g_bufferInterface->FreeMem(handle);
122 }
123
124 using TestFuncs = void (*)(const BufferHandle&);
125
126 TestFuncs g_testFuncs[] = {
127 TestFreeMem,
128 TestMmapUnmap,
129 TestFlushCache,
130 TestInvalidateCache,
131 TestGetImageLayout
132 };
133
FuzzTest(const uint8_t * rawData,size_t size)134 bool FuzzTest(const uint8_t* rawData, size_t size)
135 {
136 if (rawData == nullptr) {
137 return false;
138 }
139
140 if (!g_isInit) {
141 g_isInit = true;
142 g_bufferInterface.reset(OHOS::HDI::Display::Buffer::V1_1::IDisplayBuffer::Get());
143 if (g_bufferInterface == nullptr) {
144 HDF_LOGE("%{public}s: get IDisplayBuffer failed", __func__);
145 return false;
146 }
147 }
148
149 // initialize data
150 RANDOM_DATA = rawData;
151 g_dataSize = size;
152 g_pos = 0;
153 BufferHandle* buffer = UsingAllocmem();
154 if (buffer == nullptr) {
155 HDF_LOGE("%{public}s: function UsingAllocmem failed", __func__);
156 return false;
157 }
158
159 uint32_t code = GetData<uint32_t>();
160 uint32_t len = GetArrLength(g_testFuncs);
161 if (len == 0) {
162 HDF_LOGE("%{public}s: g_testFuncs length is equal to 0", __func__);
163 g_bufferInterface->FreeMem(*buffer);
164 return false;
165 }
166
167 g_testFuncs[code % len](*buffer);
168 return true;
169 }
170 } // OHOS
171
172 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)173 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
174 {
175 if (size < OHOS::THRESHOLD) {
176 return 0;
177 }
178
179 OHOS::FuzzTest(data, size);
180 return 0;
181 }
182