• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_3;
26 using namespace OHOS::HDI::Display::Composer::V1_0;
27 static std::shared_ptr<OHOS::HDI::Display::Buffer::V1_3::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 
TestIsSupportAllocPassthrough()124 void TestIsSupportAllocPassthrough()
125 {
126     AllocInfo info = { 0 };
127     int32_t ret = GetAllocInfo(info);
128     BufferHandle* handle = nullptr;
129     if (ret != DISPLAY_SUCCESS) {
130         HDF_LOGE("%{public}s: function GetAllocInfo failed", __func__);
131         return;
132     }
133 
134     (void)g_bufferInterface->AllocMem(info, handle);
135 }
136 
TestReAllocMem(const BufferHandle & handle)137 void TestReAllocMem(const BufferHandle& handle)
138 {
139     AllocInfo info = { 0 };
140     int32_t ret = GetAllocInfo(info);
141     if (ret != DISPLAY_SUCCESS) {
142         HDF_LOGE("%{public}s: function GetAllocInfo failed", __func__);
143         return;
144     }
145 
146     BufferHandle* outHandle = nullptr;
147     (void)g_bufferInterface->ReAllocMem(info, handle, outHandle);
148 }
149 
150 using TestFuncs = void (*)(const BufferHandle&);
151 
152 TestFuncs g_testFuncs[] = {
153     TestFreeMem,
154     TestMmapUnmap,
155     TestFlushCache,
156     TestInvalidateCache,
157     TestGetImageLayout,
158     TestIsSupportAllocPassthrough,
159     TestReAllocMem
160 };
161 
FuzzTest(const uint8_t * rawData,size_t size)162 bool FuzzTest(const uint8_t* rawData, size_t size)
163 {
164     if (rawData == nullptr) {
165         return false;
166     }
167 
168     if (!g_isInit) {
169         g_isInit = true;
170         g_bufferInterface.reset(OHOS::HDI::Display::Buffer::V1_1::IDisplayBuffer::Get());
171         if (g_bufferInterface == nullptr) {
172             HDF_LOGE("%{public}s: get IDisplayBuffer failed", __func__);
173             return false;
174         }
175     }
176 
177     // initialize data
178     RANDOM_DATA = rawData;
179     g_dataSize = size;
180     g_pos = 0;
181     BufferHandle* buffer = UsingAllocmem();
182     if (buffer == nullptr) {
183         HDF_LOGE("%{public}s: function UsingAllocmem failed", __func__);
184         return false;
185     }
186 
187     uint32_t code = GetData<uint32_t>();
188     uint32_t len = GetArrLength(g_testFuncs);
189     if (len == 0) {
190         HDF_LOGE("%{public}s: g_testFuncs length is equal to 0", __func__);
191         g_bufferInterface->FreeMem(*buffer);
192         return false;
193     }
194 
195     g_testFuncs[code % len](*buffer);
196     return true;
197 }
198 } // OHOS
199 
200 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)201 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
202 {
203     if (size < OHOS::THRESHOLD) {
204         return 0;
205     }
206 
207     OHOS::FuzzTest(data, size);
208     return 0;
209 }
210