• 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 namespace OHOS {
27 namespace MMI {
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "UpdateDisplayInfoFuzzTest" };
30 } // namespace
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 }
79 
UpdateDisplayInfoFuzzTest(const uint8_t * data,size_t size)80 void UpdateDisplayInfoFuzzTest(const uint8_t* data, size_t size)
81 {
82     DisplayGroupInfo displayGroupInfo;
83     size_t startPos = 0;
84     size_t stringSize = 4;
85     startPos += GetObject<int32_t>(data + startPos, size - startPos, displayGroupInfo.width);
86     startPos += GetObject<int32_t>(data + startPos, size - startPos, displayGroupInfo.height);
87     startPos += GetObject<int32_t>(data + startPos, size - startPos, displayGroupInfo.focusWindowId);
88     std::vector<WindowInfo> windowsInfo;
89     std::vector<DisplayInfo> displaysInfo;
90     for (size_t i = 0; i < WindowInfo::MAX_HOTAREA_COUNT + 1; ++i) {
91         WindowInfo windowInfo;
92         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.x);
93         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.y);
94         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.pid);
95         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.uid);
96         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.width);
97         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.area.height);
98         startPos += GetObject<int32_t>(data + startPos, size - startPos, windowInfo.id);
99         UpdateHotAreas(data, size, windowInfo);
100         windowsInfo.push_back(windowInfo);
101 
102         DisplayInfo displayInfo;
103         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.dpi);
104         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.x);
105         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.y);
106         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.width);
107         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.height);
108         startPos += GetObject<int32_t>(data + startPos, size - startPos, displayInfo.id);
109         char name[] = "name";
110         startPos += GetString(data + startPos, size - startPos, name, stringSize);
111         displayInfo.name = name;
112         char uniq[] = "uniq";
113         startPos += GetString(data + startPos, size - startPos, uniq, stringSize);
114         displayInfo.uniq = uniq;
115         displaysInfo.push_back(displayInfo);
116     }
117     displayGroupInfo.windowsInfo = windowsInfo;
118     displayGroupInfo.displaysInfo = displaysInfo;
119     InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo);
120     MMI_HILOGD("Update display info success");
121 }
122 } // namespace MMI
123 } // namespace OHOS
124 
125 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)126 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
127 {
128     /* Run your code on data */
129     OHOS::MMI::UpdateDisplayInfoFuzzTest(data, size);
130     return 0;
131 }
132