1 /*
2 * Copyright (c) 2023 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 "device_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <securec.h>
21 #include <string>
22
23 #include "display_common_fuzzer.h"
24
25 namespace OHOS {
26 using namespace OHOS::HDI::Display::Buffer::V1_0;
27 using namespace OHOS::HDI::Display::Composer::V1_2;
28
29 static sptr<Composer::V1_2::IDisplayComposerInterface> g_composerInterface = nullptr;
30 static std::shared_ptr<IDisplayBuffer> g_bufferInterface = nullptr;
31
32 static bool g_isInit = false;
33 static const uint8_t* g_data = nullptr;
34 static size_t g_dataSize = 0;
35 static size_t g_pos;
36
37 /*
38 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
39 * tips: only support basic type
40 */
41 template<class T>
GetData()42 T GetData()
43 {
44 T object {};
45 size_t objectSize = sizeof(object);
46 if (g_data == nullptr || objectSize > g_dataSize - g_pos) {
47 return object;
48 }
49 errno_t ret = memcpy_s(&object, objectSize, g_data + g_pos, objectSize);
50 if (ret != EOK) {
51 return {};
52 }
53 g_pos += objectSize;
54 return object;
55 }
56
GetAllocInfo(AllocInfo & info)57 static int32_t GetAllocInfo(AllocInfo& info)
58 {
59 uint32_t lenUsage = GetArrLength(CONVERT_TABLE_USAGE);
60 if (lenUsage == 0) {
61 HDF_LOGE("%{public}s: CONVERT_TABLE_USAGE length is equal to 0", __func__);
62 return DISPLAY_FAILURE;
63 }
64 uint32_t lenFormat = GetArrLength(CONVERT_TABLE_FORMAT);
65 if (lenFormat == 0) {
66 HDF_LOGE("%{public}s: CONVERT_TABLE_FORMAT length is equal to 0", __func__);
67 return DISPLAY_FAILURE;
68 }
69
70 info.width = GetData<uint32_t>() % WIDTH;
71 info.height = GetData<uint32_t>() % HEIGHT;
72 info.usage = CONVERT_TABLE_USAGE[GetData<uint32_t>() % lenUsage];
73 info.format = CONVERT_TABLE_FORMAT[GetData<uint32_t>() % lenFormat];
74 info.expectedSize = info.width * info.height;
75
76 return DISPLAY_SUCCESS;
77 }
78
GetIRect(IRect & rect)79 static int32_t GetIRect(IRect& rect)
80 {
81 rect.x = GetData<int32_t>();
82 rect.y = GetData<int32_t>();
83 rect.w = GetData<int32_t>();
84 rect.h = GetData<int32_t>();
85 return DISPLAY_SUCCESS;
86 }
87
UsingAllocmem()88 BufferHandle* UsingAllocmem()
89 {
90 AllocInfo info = { 0 };
91 int32_t ret = GetAllocInfo(info);
92 if (ret != DISPLAY_SUCCESS) {
93 HDF_LOGE("%{public}s: function GetAllocInfo failed", __func__);
94 return nullptr;
95 }
96
97 BufferHandle* handle = nullptr;
98 ret = g_bufferInterface->AllocMem(info, handle);
99 if (ret != DISPLAY_SUCCESS) {
100 HDF_LOGE("%{public}s: function AllocMem failed", __func__);
101 return nullptr;
102 }
103 return handle;
104 }
105
TestSetClientBufferCacheCount(uint32_t devId)106 int32_t TestSetClientBufferCacheCount(uint32_t devId)
107 {
108 uint32_t cacheCount = GetData<uint32_t>();
109 int32_t ret = g_composerInterface->SetClientBufferCacheCount(devId, cacheCount);
110 if (ret != DISPLAY_SUCCESS) {
111 HDF_LOGE("%{public}s: function SetClientBufferCacheCount failed", __func__);
112 return DISPLAY_FAILURE;
113 }
114 return ret;
115 }
116
TestGetDisplaySupportedModes(uint32_t devId)117 int32_t TestGetDisplaySupportedModes(uint32_t devId)
118 {
119 DisplayModeInfo info = { 0 };
120 info.width = GetData<int32_t>() % WIDTH;
121 info.height = GetData<int32_t>() % HEIGHT;
122 info.freshRate = GetData<uint32_t>();
123 info.id = GetData<int32_t>();
124
125 std::vector<DisplayModeInfo> infos;
126 infos.push_back(info);
127 int32_t ret = g_composerInterface->GetDisplaySupportedModes(devId, infos);
128 if (ret != DISPLAY_SUCCESS) {
129 HDF_LOGE("%{public}s: function GetDisplaySupportedModes failed", __func__);
130 return DISPLAY_FAILURE;
131 }
132 return ret;
133 }
134
TestSetGetDisplayMode(uint32_t devId)135 int32_t TestSetGetDisplayMode(uint32_t devId)
136 {
137 uint32_t modeId = GetData<uint32_t>();
138 int32_t ret = g_composerInterface->SetDisplayMode(devId, modeId);
139 if (ret != DISPLAY_SUCCESS) {
140 HDF_LOGE("%{public}s: function SetDisplayMode failed", __func__);
141 return DISPLAY_FAILURE;
142 }
143 ret = g_composerInterface->GetDisplayMode(devId, modeId);
144 if (ret != DISPLAY_SUCCESS) {
145 HDF_LOGE("%{public}s: function GetDisplayMode failed", __func__);
146 return DISPLAY_FAILURE;
147 }
148 return ret;
149 }
150
TestSetGetDisplayPowerStatus(uint32_t devId)151 int32_t TestSetGetDisplayPowerStatus(uint32_t devId)
152 {
153 uint32_t len = GetArrLength(CONVERT_TABLE_POWER_STATUS);
154 if (len == 0) {
155 HDF_LOGE("%{public}s: CONVERT_TABLE_POWER_STATUS length is equal to 0", __func__);
156 return DISPLAY_FAILURE;
157 }
158 Composer::V1_0::DispPowerStatus status = CONVERT_TABLE_POWER_STATUS[GetData<uint32_t>() % len];
159 int32_t ret = g_composerInterface->SetDisplayPowerStatus(devId, status);
160 if (ret != DISPLAY_SUCCESS) {
161 HDF_LOGE("%{public}s: function SetDisplayPowerStatus failed", __func__);
162 return DISPLAY_FAILURE;
163 }
164 ret = g_composerInterface->GetDisplayPowerStatus(devId, status);
165 if (ret != DISPLAY_SUCCESS) {
166 HDF_LOGE("%{public}s: function GetDisplayPowerStatus failed", __func__);
167 return DISPLAY_FAILURE;
168 }
169 return ret;
170 }
171
TestPrepareDisplayLayers(uint32_t devId)172 int32_t TestPrepareDisplayLayers(uint32_t devId)
173 {
174 bool needFlushFb = GetRandBoolValue(GetData<uint32_t>());
175 int32_t ret = g_composerInterface->PrepareDisplayLayers(devId, needFlushFb);
176 if (ret != DISPLAY_SUCCESS) {
177 HDF_LOGE("%{public}s: function PrepareDisplayLayers failed", __func__);
178 return DISPLAY_FAILURE;
179 }
180 return ret;
181 }
182
TestSetGetDisplayBacklight(uint32_t devId)183 int32_t TestSetGetDisplayBacklight(uint32_t devId)
184 {
185 uint32_t level = GetData<uint32_t>();
186 int32_t ret = g_composerInterface->SetDisplayBacklight(devId, level);
187 if (ret != DISPLAY_SUCCESS) {
188 HDF_LOGE("%{public}s: function SetDisplayBacklight failed", __func__);
189 return DISPLAY_FAILURE;
190 }
191 ret = g_composerInterface->GetDisplayBacklight(devId, level);
192 if (ret != DISPLAY_SUCCESS) {
193 HDF_LOGE("%{public}s: function GetDisplayBacklight failed", __func__);
194 return DISPLAY_FAILURE;
195 }
196 return ret;
197 }
198
TestGetDisplayProperty(uint32_t devId)199 int32_t TestGetDisplayProperty(uint32_t devId)
200 {
201 uint32_t id = GetData<uint32_t>();
202 uint64_t value = GetData<uint32_t>();
203 int32_t ret = g_composerInterface->GetDisplayProperty(devId, id, value);
204 if (ret != DISPLAY_SUCCESS) {
205 HDF_LOGE("%{public}s: function GetDisplayProperty failed", __func__);
206 return DISPLAY_FAILURE;
207 }
208 return ret;
209 }
210
TestUpdateHardwareCursor(uint32_t devId)211 int32_t TestUpdateHardwareCursor(uint32_t devId)
212 {
213 BufferHandle* buffer = UsingAllocmem();
214 if (buffer == nullptr) {
215 HDF_LOGE("%{public}s: Failed to UsingAllocmem", __func__);
216 return DISPLAY_FAILURE;
217 }
218
219 int32_t x = GetData<uint32_t>();
220 int32_t y = GetData<uint32_t>();
221 int32_t ret = g_composerInterface->UpdateHardwareCursor(devId, x, y, buffer);
222 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
223 HDF_LOGE("%{public}s: function UpdateHardwareCursor failed, %{public}d", __func__, ret);
224 return DISPLAY_FAILURE;
225 }
226 return DISPLAY_SUCCESS;
227 }
228
TestEnableHardwareCursorStats(uint32_t devId)229 int32_t TestEnableHardwareCursorStats(uint32_t devId)
230 {
231 bool enable = GetRandBoolValue(GetData<uint32_t>());
232 int32_t ret = g_composerInterface->EnableHardwareCursorStats(devId, enable);
233 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
234 HDF_LOGE("%{public}s: function EnableHardwareCursorStats failed, %{public}d", __func__, ret);
235 return DISPLAY_FAILURE;
236 }
237 return DISPLAY_SUCCESS;
238 }
239
TestGetHardwareCursorStats(uint32_t devId)240 int32_t TestGetHardwareCursorStats(uint32_t devId)
241 {
242 uint32_t frameCount = GetData<uint32_t>();
243 uint32_t vsyncCount = GetData<uint32_t>();
244 int32_t ret = g_composerInterface->GetHardwareCursorStats(devId, frameCount, vsyncCount);
245 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
246 HDF_LOGE("%{public}s: function GetHardwareCursorStats failed, %{public}d", __func__, ret);
247 return DISPLAY_FAILURE;
248 }
249 return DISPLAY_SUCCESS;
250 }
251
TestGetDisplayCompChange(uint32_t devId)252 int32_t TestGetDisplayCompChange(uint32_t devId)
253 {
254 std::vector<uint32_t> layers;
255 layers.push_back(GetData<uint32_t>());
256 std::vector<int32_t> types;
257 types.push_back(GetData<int32_t>());
258
259 int32_t ret = g_composerInterface->GetDisplayCompChange(devId, layers, types);
260 if (ret != DISPLAY_SUCCESS) {
261 HDF_LOGE("%{public}s: function GetDisplayCompChange failed", __func__);
262 return DISPLAY_FAILURE;
263 }
264 return ret;
265 }
266
TestSetDisplayClientCrop(uint32_t devId)267 int32_t TestSetDisplayClientCrop(uint32_t devId)
268 {
269 IRect rect;
270 int32_t ret = GetIRect(rect);
271 if (ret != DISPLAY_SUCCESS) {
272 HDF_LOGE("%{public}s: function GetIRect failed", __func__);
273 return DISPLAY_FAILURE;
274 }
275 ret = g_composerInterface->SetDisplayClientCrop(devId, rect);
276 if (ret != DISPLAY_SUCCESS) {
277 HDF_LOGE("%{public}s: function SetDisplayClientCrop failed", __func__);
278 return DISPLAY_FAILURE;
279 }
280 return ret;
281 }
282
TestSetDisplayClientDamage(uint32_t devId)283 int32_t TestSetDisplayClientDamage(uint32_t devId)
284 {
285 IRect rect;
286 int32_t ret = GetIRect(rect);
287 if (ret != DISPLAY_SUCCESS) {
288 HDF_LOGE("%{public}s: function GetIRect failed", __func__);
289 return DISPLAY_FAILURE;
290 }
291 std::vector<IRect> rects;
292 rects.push_back(rect);
293 ret = g_composerInterface->SetDisplayClientDamage(devId, rects);
294 if (ret != DISPLAY_SUCCESS) {
295 HDF_LOGE("%{public}s: function SetDisplayClientDamage failed", __func__);
296 }
297 return ret;
298 }
299
TestSetDisplayVsyncEnabled(uint32_t devId)300 int32_t TestSetDisplayVsyncEnabled(uint32_t devId)
301 {
302 bool enabled = GetRandBoolValue(GetData<uint32_t>());
303 int32_t ret = g_composerInterface->SetDisplayVsyncEnabled(devId, enabled);
304 if (ret != DISPLAY_SUCCESS) {
305 HDF_LOGE("%{public}s: function SetDisplayVsyncEnabled failed", __func__);
306 }
307 return ret;
308 }
309
TestRegDisplayVBlankCallback(uint32_t devId)310 int32_t TestRegDisplayVBlankCallback(uint32_t devId)
311 {
312 uint32_t param1 = GetData<uint32_t>();
313 VBlankCallback param2 = GetData<VBlankCallback>();
314 void* param3 = malloc(PARAM_VOIDPTR_LEN);
315 if (param3 == nullptr) {
316 HDF_LOGE("%{public}s: void* param3 malloc failed", __func__);
317 return DISPLAY_FAILURE;
318 }
319 int32_t ret = g_composerInterface->RegDisplayVBlankCallback(param1, param2, param3);
320 if (ret != DISPLAY_SUCCESS) {
321 HDF_LOGE("%{public}s: function RegDisplayVBlankCallback failed", __func__);
322 }
323 free(param3);
324 param3 = nullptr;
325 return ret;
326 }
327
TestGetDisplayReleaseFence(uint32_t devId)328 int32_t TestGetDisplayReleaseFence(uint32_t devId)
329 {
330 std::vector<uint32_t> layers;
331 layers.push_back(GetData<uint32_t>());
332 std::vector<int32_t> fences;
333 fences.push_back(GetData<int32_t>());
334
335 int32_t ret = g_composerInterface->GetDisplayReleaseFence(devId, layers, fences);
336 if (ret != DISPLAY_SUCCESS) {
337 HDF_LOGE("%{public}s: function GetDisplayReleaseFence failed", __func__);
338 }
339 return ret;
340 }
341
TestDestroyVirtualDisplay(uint32_t devId)342 int32_t TestDestroyVirtualDisplay(uint32_t devId)
343 {
344 int32_t ret = g_composerInterface->DestroyVirtualDisplay(devId);
345 if (ret != DISPLAY_SUCCESS) {
346 HDF_LOGE("%{public}s: function DestroyVirtualDisplay failed", __func__);
347 }
348 return ret;
349 }
350
TestSetVirtualDisplayBuffer(uint32_t devId)351 int32_t TestSetVirtualDisplayBuffer(uint32_t devId)
352 {
353 int32_t fence = GetData<int32_t>();
354 BufferHandle* buffer = UsingAllocmem();
355 if (buffer == nullptr) {
356 HDF_LOGE("%{public}s: Failed to UsingAllocmem", __func__);
357 return DISPLAY_FAILURE;
358 }
359 int32_t ret = g_composerInterface->SetVirtualDisplayBuffer(devId, *buffer, fence);
360 if (ret != DISPLAY_SUCCESS) {
361 HDF_LOGE("%{public}s: function SetVirtualDisplayBuffer failed", __func__);
362 }
363 g_bufferInterface->FreeMem(*buffer);
364 return ret;
365 }
366
TestSetDisplayProperty(uint32_t devId)367 int32_t TestSetDisplayProperty(uint32_t devId)
368 {
369 uint32_t id = GetData<uint32_t>();
370 uint64_t value = GetData<uint64_t>();
371 int32_t ret = g_composerInterface->SetDisplayProperty(devId, id, value);
372 if (ret != DISPLAY_SUCCESS) {
373 HDF_LOGE("%{public}s: SetDisplayProperty failed", __func__);
374 }
375 return ret;
376 }
377
TestCommit(uint32_t devId)378 int32_t TestCommit(uint32_t devId)
379 {
380 int32_t fence = GetData<int32_t>();
381 int32_t ret = g_composerInterface->Commit(devId, fence);
382 if (ret != DISPLAY_SUCCESS) {
383 HDF_LOGE("%{public}s: function Commit failed", __func__);
384 }
385 return ret;
386 }
387
TestGetDisplaySupportedModesExt(uint32_t devId)388 int TestGetDisplaySupportedModesExt(uint32_t devId)
389 {
390 std::vector<DisplayModeInfoExt> modes;
391 modes.push_back(GetData<DisplayModeInfoExt>());
392 int32_t ret = g_composerInterface->GetDisplaySupportedModesExt(devId, modes);
393 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
394 HDF_LOGE("%{public}s: GetDisplaySupportedModesExt failed", __func__);
395 }
396 return ret;
397 }
398
TestModeCallback(uint32_t modeId,uint64_t vBlankPeriod,void * data)399 void TestModeCallback(uint32_t modeId, uint64_t vBlankPeriod, void* data)
400 {
401 }
402
TestSetDisplayModeAsync(uint32_t devId)403 int TestSetDisplayModeAsync(uint32_t devId)
404 {
405 uint32_t modeid = GetData<uint32_t>();
406 int32_t ret = g_composerInterface->SetDisplayModeAsync(devId, modeid, TestModeCallback);
407 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
408 HDF_LOGE("%{public}s: SetDisplayModeAsync failed", __func__);
409 }
410 return ret;
411 }
412
TestGetDisplayVBlankPeriod(uint32_t devId)413 int TestGetDisplayVBlankPeriod(uint32_t devId)
414 {
415 uint64_t period = GetData<uint64_t>();
416 int32_t ret = g_composerInterface->GetDisplayVBlankPeriod(devId, period);
417 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
418 HDF_LOGE("%{public}s: GetDisplayVBlankPeriod failed", __func__);
419 }
420 return ret;
421 }
422
TestSeamlessChangeCallback(uint32_t devId,void * data)423 void TestSeamlessChangeCallback(uint32_t devId, void* data)
424 {
425 }
426
TestRegSeamlessChangeCallback(uint32_t devId)427 int TestRegSeamlessChangeCallback(uint32_t devId)
428 {
429 int32_t ret = g_composerInterface->RegSeamlessChangeCallback(TestSeamlessChangeCallback, nullptr);
430 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
431 HDF_LOGE("%{public}s: SetDisplayModeAsync failed", __func__);
432 }
433 return ret;
434 }
435
TestGetSupportedLayerPerFrameParameterKey(uint32_t devId)436 int TestGetSupportedLayerPerFrameParameterKey(uint32_t devId)
437 {
438 std::vector<std::string> keys;
439 int32_t ret = g_composerInterface->GetSupportedLayerPerFrameParameterKey(keys);
440 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
441 HDF_LOGE("%{public}s: failed with ret=%{public}d", __func__, ret);
442 }
443 return ret;
444 }
445
TestSetDisplayOverlayResolution(uint32_t devId)446 int TestSetDisplayOverlayResolution(uint32_t devId)
447 {
448 uint32_t width = GetData<uint32_t>() % WIDTH;
449 uint32_t height = GetData<uint32_t>() % HEIGHT;
450 int32_t ret = g_composerInterface->SetDisplayOverlayResolution(devId, width, height);
451 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
452 HDF_LOGE("%{public}s: failed with ret=%{public}d", __func__, ret);
453 }
454 return ret;
455 }
456
TestRefreshCallback(uint32_t devId,void * data)457 static void TestRefreshCallback(uint32_t devId, void* data)
458 {
459 }
460
TestRegRefreshCallback(uint32_t devId)461 int TestRegRefreshCallback(uint32_t devId)
462 {
463 int32_t ret = g_composerInterface->RegRefreshCallback(TestRefreshCallback, nullptr);
464 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
465 HDF_LOGE("%{public}s: failed with ret=%{public}d", __func__, ret);
466 }
467 return ret;
468 }
469
TestGetDisplaySupportedColorGamuts(uint32_t devId)470 int TestGetDisplaySupportedColorGamuts(uint32_t devId)
471 {
472 std::vector<ColorGamut> gamuts;
473 int32_t ret = g_composerInterface->GetDisplaySupportedColorGamuts(devId, gamuts);
474 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
475 HDF_LOGE("%{public}s: failed with ret=%{public}d", __func__, ret);
476 }
477 return ret;
478 }
479
TestGetHDRCapabilityInfos(uint32_t devId)480 int TestGetHDRCapabilityInfos(uint32_t devId)
481 {
482 HDRCapability info = { 0 };
483 int32_t ret = g_composerInterface->GetHDRCapabilityInfos(devId, info);
484 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
485 HDF_LOGE("%{public}s: failed with ret=%{public}d", __func__, ret);
486 }
487 return ret;
488 }
489
TestGetDisplayIdentificationData(uint32_t devId)490 int TestGetDisplayIdentificationData(uint32_t devId)
491 {
492 uint8_t portId = GetData<uint8_t>();
493 std::vector<uint8_t> edidData = {};
494 edidData.push_back(GetData<uint8_t>());
495 int32_t ret = g_composerInterface->GetDisplayIdentificationData(devId, portId, edidData);
496 if ((ret != DISPLAY_SUCCESS) && (ret != DISPLAY_NOT_SUPPORT)) {
497 HDF_LOGE("%{public}s: failed with ret=%{public}d", __func__, ret);
498 }
499 HDF_LOGD("displayId[%u], portId[%u], edidDataLength[%u]", devId, portId, edidData.size());
500 return ret;
501 }
502
503 typedef int32_t (*TestFuncs[])(uint32_t);
504
505 TestFuncs g_testFuncs = {
506 TestSetClientBufferCacheCount,
507 TestGetDisplaySupportedModes,
508 TestSetGetDisplayMode,
509 TestSetGetDisplayPowerStatus,
510 TestPrepareDisplayLayers,
511 TestSetGetDisplayBacklight,
512 TestGetDisplayProperty,
513 TestUpdateHardwareCursor,
514 TestEnableHardwareCursorStats,
515 TestGetHardwareCursorStats,
516 TestGetDisplayCompChange,
517 TestSetDisplayClientCrop,
518 TestSetDisplayClientDamage,
519 TestSetDisplayVsyncEnabled,
520 TestGetDisplayReleaseFence,
521 TestDestroyVirtualDisplay,
522 TestSetVirtualDisplayBuffer,
523 TestSetDisplayProperty,
524 TestGetDisplaySupportedModesExt,
525 TestSetDisplayModeAsync,
526 TestGetDisplayVBlankPeriod,
527 TestRegSeamlessChangeCallback,
528 TestGetSupportedLayerPerFrameParameterKey,
529 TestSetDisplayOverlayResolution,
530 TestRegRefreshCallback,
531 TestGetDisplaySupportedColorGamuts,
532 TestGetHDRCapabilityInfos,
533 TestCommit,
534 TestGetDisplayIdentificationData,
535 };
536
FuzzTest(const uint8_t * rawData,size_t size)537 bool FuzzTest(const uint8_t* rawData, size_t size)
538 {
539 if (rawData == nullptr) {
540 return false;
541 }
542
543 // initialize service
544 if (!g_isInit) {
545 g_isInit = true;
546 g_composerInterface = Composer::V1_2::IDisplayComposerInterface::Get();
547 if (g_composerInterface == nullptr) {
548 HDF_LOGE("%{public}s: get IDisplayComposerInterface failed", __func__);
549 return false;
550 }
551 g_bufferInterface.reset(IDisplayBuffer::Get());
552 if (g_bufferInterface == nullptr) {
553 HDF_LOGE("%{public}s: get IDisplayBuffer failed", __func__);
554 return false;
555 }
556 }
557
558 // initialize data
559 g_data = rawData;
560 g_dataSize = size;
561 g_pos = 0;
562
563 uint32_t code = GetData<uint32_t>();
564 uint32_t devId = GetData<uint32_t>();
565 uint32_t len = GetArrLength(g_testFuncs);
566 if (len == 0) {
567 HDF_LOGE("%{public}s: g_testFuncs length is equal to 0", __func__);
568 return false;
569 }
570
571 int32_t ret = g_testFuncs[code % len](devId);
572 if (ret != DISPLAY_SUCCESS) {
573 HDF_LOGE("function %{public}u failed", code % len);
574 return false;
575 }
576
577 return true;
578 }
579 } // OHOS
580
581 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)582 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
583 {
584 if (size < OHOS::THRESHOLD) {
585 return 0;
586 }
587
588 OHOS::FuzzTest(data, size);
589 return 0;
590 }
591