• 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 "common_method.h"
23 #include "define_multimodal.h"
24 #include "input_manager.h"
25 #include "mmi_log.h"
26 
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "UpdateDisplayInfoFuzzTest" };
31 } // namespace
32 
GetString(size_t objectSize,const uint8_t * data,size_t size,std::string & object)33 size_t GetString(size_t objectSize, const uint8_t *data, size_t size, std::string &object)
34 {
35     if (objectSize > size) {
36         return 0;
37     }
38     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
39     if (ret != EOK) {
40         return 0;
41     }
42     return objectSize;
43 }
44 
UpdateDisplayInfoFuzzTest(const uint8_t * data,size_t size)45 void UpdateDisplayInfoFuzzTest(const uint8_t* data, size_t size)
46 {
47     DisplayGroupInfo displayGroupInfo;
48     size_t startPos = 0;
49     startPos += GetObject<int32_t>(displayGroupInfo.width, data + startPos, size - startPos);
50     startPos += GetObject<int32_t>(displayGroupInfo.height, data + startPos, size - startPos);
51     startPos += GetObject<int32_t>(displayGroupInfo.focusWindowId, data + startPos, size - startPos);
52     std::vector<WindowInfo> windowsInfos;
53     std::vector<DisplayInfo> displaysInfos;
54     for (size_t i = 0; i < WindowInfo::MAX_HOTAREA_COUNT + 1; ++i) {
55         WindowInfo windowInfo;
56         startPos += GetObject<int32_t>(windowInfo.id, data + startPos, size - startPos);
57         startPos += GetObject<int32_t>(windowInfo.pid, data + startPos, size - startPos);
58         startPos += GetObject<int32_t>(windowInfo.uid, data + startPos, size - startPos);
59         startPos += GetObject<int32_t>(windowInfo.area.x, data + startPos, size - startPos);
60         startPos += GetObject<int32_t>(windowInfo.area.y, data + startPos, size - startPos);
61         startPos += GetObject<int32_t>(windowInfo.area.width, data + startPos, size - startPos);
62         startPos += GetObject<int32_t>(windowInfo.area.height, data + startPos, size - startPos);
63         windowsInfos.push_back(windowInfo);
64 
65         DisplayInfo displayInfo;
66         startPos += GetObject<int32_t>(displayInfo.id, data + startPos, size - startPos);
67         startPos += GetObject<int32_t>(displayInfo.x, data + startPos, size - startPos);
68         startPos += GetObject<int32_t>(displayInfo.y, data + startPos, size - startPos);
69         startPos += GetObject<int32_t>(displayInfo.width, data + startPos, size - startPos);
70         startPos += GetObject<int32_t>(displayInfo.height, data + startPos, size - startPos);
71 
72         size_t objectSize = 0;
73         std::string name = "";
74         size_t ret = 0;
75         ret = GetString(objectSize, data, size, name);
76         if (ret == 0) {
77             MMI_HILOGD("%{public}s:%{public}d The return value is 0", __func__, __LINE__);
78             return;
79         }
80         displayInfo.name = name;
81         std::string uniq = "";
82         ret = GetString(objectSize, data, size, uniq);
83         if (ret == 0) {
84             MMI_HILOGD("%{public}s:%{public}d The return value is 0", __func__, __LINE__);
85             return;
86         }
87         displayInfo.uniq = uniq;
88         displaysInfos.push_back(displayInfo);
89     }
90     displayGroupInfo.windowsInfo = windowsInfos;
91     displayGroupInfo.displaysInfo = displaysInfos;
92     InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo);
93     MMI_HILOGD("Update display info success");
94 }
95 } // namespace MMI
96 } // namespace OHOS
97 
98 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)99 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
100 {
101     /* Run your code on data */
102     OHOS::MMI::UpdateDisplayInfoFuzzTest(data, size);
103     return 0;
104 }
105