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     startPos += GetObject<int32_t>(data + startPos, size - startPos, displayGroupInfo.width);
100     startPos += GetObject<int32_t>(data + startPos, size - startPos, displayGroupInfo.height);
101     startPos += GetObject<int32_t>(data + startPos, size - startPos, displayGroupInfo.focusWindowId);
102     std::vector<WindowInfo> windowsInfo;
103     std::vector<DisplayInfo> displaysInfo;
104     for (size_t i = 0; i < WindowInfo::MAX_HOTAREA_COUNT + 1; ++i) {
105         WindowInfo windowInfo;
106         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.x);
107         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.y);
108         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.pid);
109         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.uid);
110         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.width);
111         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.height);
112         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.id);
113         UpdateHotAreas(data, size, windowInfo);
114         windowsInfo.push_back(windowInfo);
115 
116         DisplayInfo displayInfo;
117         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.dpi);
118         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.x);
119         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.y);
120         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.width);
121         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.height);
122         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.id);
123         char name[] = "name";
124         startPos += GetString(data + startPos, size - startPos, name, stringSize);
125         displayInfo.name = name;
126         char uniq[] = "uniq";
127         startPos += GetString(data + startPos, size - startPos, uniq, stringSize);
128         displayInfo.uniq = uniq;
129         displaysInfo.push_back(displayInfo);
130     }
131     displayGroupInfo.windowsInfo = windowsInfo;
132     displayGroupInfo.displaysInfo = displaysInfo;
133     InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo);
134     MMI_HILOGD("Update display info success");
135 }
136 } // namespace MMI
137 } // namespace OHOS
138 
139 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)140 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
141 {
142     /* Run your code on data */
143     OHOS::MMI::UpdateDisplayInfoFuzzTest(data, size);
144     return 0;
145 }
146