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