1 /*
2 * Copyright (c) 2022 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 "pointerstyle_fuzzer.h"
17
18 #include "ipc_skeleton.h"
19 #include "securec.h"
20
21 #include "input_manager.h"
22
23 namespace OHOS {
24 namespace MMI {
GetObject(const uint8_t * data,size_t size,T & object)25 template <class T> size_t GetObject(const uint8_t *data, size_t size, T &object)
26 {
27 size_t objectSize = sizeof(object);
28 if (objectSize > size) {
29 return 0;
30 }
31 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
32 if (ret != EOK) {
33 return 0;
34 }
35 return objectSize;
36 }
37
GetString(const uint8_t * data,size_t size,char * object,size_t objectSize)38 size_t GetString(const uint8_t *data, size_t size, char *object, size_t objectSize)
39 {
40 if (objectSize > size) {
41 return 0;
42 }
43 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
44 if (ret != EOK) {
45 return 0;
46 }
47 return objectSize;
48 }
49
UpdateHotAreas(const uint8_t * data,size_t size,WindowInfo & windowInfo)50 void UpdateHotAreas(const uint8_t *data, size_t size, WindowInfo &windowInfo)
51 {
52 size_t startPos = 0;
53 std::vector<Rect> defaultHotAreasInfo;
54 std::vector<Rect> pointerHotAreasInfo;
55 for (size_t j = 0; j < WindowInfo::MAX_HOTAREA_COUNT; ++j) {
56 Rect defaultRect;
57 startPos += GetObject<int32_t>(data + startPos, size - startPos, defaultRect.height);
58 startPos += GetObject<int32_t>(data + startPos, size - startPos, defaultRect.width);
59 startPos += GetObject<int32_t>(data + startPos, size - startPos, defaultRect.x);
60 startPos += GetObject<int32_t>(data + startPos, size - startPos, defaultRect.y);
61 defaultHotAreasInfo.push_back(defaultRect);
62 Rect pointerRect;
63 startPos += GetObject<int32_t>(data + startPos, size - startPos, pointerRect.height);
64 startPos += GetObject<int32_t>(data + startPos, size - startPos, pointerRect.width);
65 startPos += GetObject<int32_t>(data + startPos, size - startPos, pointerRect.x);
66 startPos += GetObject<int32_t>(data + startPos, size - startPos, pointerRect.y);
67 pointerHotAreasInfo.push_back(pointerRect);
68 }
69 windowInfo.defaultHotAreas = defaultHotAreasInfo;
70 windowInfo.pointerHotAreas = pointerHotAreasInfo;
71 }
72
UpdateDisplayInfo(const uint8_t * data,size_t size,int32_t windowId)73 void UpdateDisplayInfo(const uint8_t *data, size_t size, int32_t windowId)
74 {
75 DisplayGroupInfo displayGroupInfo;
76 size_t startPos = 0;
77 size_t stringSize = 4;
78 int32_t displayWidth = 0;
79 int32_t displayHeight = 0;
80 startPos += GetObject<int32_t>(data + startPos, size - startPos, displayWidth);
81 startPos += GetObject<int32_t>(data + startPos, size - startPos, displayHeight);
82 startPos += GetObject<int32_t>(data + startPos, size - startPos, displayGroupInfo.focusWindowId);
83 std::vector<WindowInfo> windowsInfo;
84 std::vector<DisplayInfo> displaysInfo;
85 WindowInfo windowInfo;
86 windowInfo.id = windowId;
87 windowInfo.pid = IPCSkeleton::GetCallingPid();
88 startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.uid);
89 startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.x);
90 startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.y);
91 startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.width);
92 startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.height);
93 UpdateHotAreas(data, size, windowInfo);
94 windowsInfo.push_back(windowInfo);
95
96 DisplayInfo displayInfo;
97 startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.id);
98 startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.x);
99 startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.y);
100 startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.width);
101 startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.height);
102 startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.dpi);
103 char name[] = "name";
104 startPos += GetString(data + startPos, size - startPos, name, stringSize);
105 displayInfo.name = name;
106 char uniq[] = "uniq";
107 GetString(data + startPos, size - startPos, uniq, stringSize);
108 displaysInfo.push_back(displayInfo);
109 displayGroupInfo.windowsInfo = windowsInfo;
110 displayGroupInfo.displaysInfo = displaysInfo;
111
112 UserScreenInfo userScreenInfo;
113 ScreenInfo screenInfo;
114 screenInfo.screenType =(ScreenType)windowInfo.windowType;
115 screenInfo.dpi = displayInfo.dpi;
116 screenInfo.height = windowInfo.area.height;
117 screenInfo.width = windowInfo.area.width;
118 screenInfo.physicalWidth = displayWidth;
119 screenInfo.physicalHeight = displayHeight;
120 screenInfo.id = displayInfo.id;
121 screenInfo.rotation = Rotation::ROTATION_0;
122 screenInfo.tpDirection = Direction::DIRECTION0;
123 screenInfo.uniqueId = uniq;
124 userScreenInfo.screens.push_back(screenInfo);
125
126 userScreenInfo.displayGroups.push_back(displayGroupInfo);
127 InputManager::GetInstance()->UpdateDisplayInfo(userScreenInfo);
128 }
129
PointerStyleFuzzTest(const uint8_t * data,size_t size)130 void PointerStyleFuzzTest(const uint8_t *data, size_t size)
131 {
132 int32_t windowId;
133 size_t startPos = 0;
134 startPos += GetObject<int32_t>(data + startPos, size - startPos, windowId);
135 UpdateDisplayInfo(data, size, windowId);
136 PointerStyle pointerStyle;
137 GetObject<int32_t>(data + startPos, size - startPos, pointerStyle.id);
138 InputManager::GetInstance()->SetPointerStyle(windowId, pointerStyle);
139 InputManager::GetInstance()->GetPointerStyle(windowId, pointerStyle);
140 }
141 } // MMI
142 } // OHOS
143
144 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)145 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
146 {
147 /* Run your code on data */
148 OHOS::MMI::PointerStyleFuzzTest(data, size);
149 return 0;
150 }