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 "marshalling_helper.h"
17 #include <parcel.h>
18
19 #include <securec.h>
20
21 #include <want.h>
22 #include "window.h"
23 #include "window_agent.h"
24 #include "window_impl.h"
25 #include "window_manager.h"
26 #include "window_manager_hilog.h"
27
28 using namespace OHOS::Rosen;
29
30 namespace OHOS {
31 namespace {
32 constexpr size_t DATA_MIN_SIZE = 2;
33 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowAgentFuzzTest"};
34 }
35
36 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)37 size_t GetObject(T& object, const uint8_t* data, size_t size)
38 {
39 size_t objectSize = sizeof(object);
40 if (objectSize > size) {
41 return 0;
42 }
43 return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
44 }
45
CheckWindowAgentFunctionsPart1(sptr<WindowAgent> agent,const uint8_t * data,size_t size)46 void CheckWindowAgentFunctionsPart1(sptr<WindowAgent> agent, const uint8_t* data, size_t size)
47 {
48 if (agent == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
49 return;
50 }
51 size_t startPos = 0;
52 OHOS::Rosen::Rect rect;
53 bool boolVal;
54 OHOS::Rosen::WindowSizeChangeReason reason;
55 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
56 startPos += GetObject<bool>(boolVal, data + startPos, size - startPos);
57 startPos += GetObject<OHOS::Rosen::WindowSizeChangeReason>(reason, data + startPos, size - startPos);
58 agent->UpdateWindowRect(rect, boolVal, reason);
59
60 WindowMode mode;
61 startPos += GetObject<WindowMode>(mode, data + startPos, size - startPos);
62 agent->UpdateWindowMode(mode);
63
64 uint32_t windowModeSupportType;
65 startPos += GetObject<uint32_t>(windowModeSupportType, data + startPos, size - startPos);
66 agent->UpdateWindowModeSupportType(windowModeSupportType);
67 agent->UpdateFocusStatus(boolVal);
68 agent->NotifyForegroundInteractiveStatus(boolVal);
69
70 sptr<AvoidArea> avoidArea = new AvoidArea();
71 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
72 avoidArea->topRect_ = rect;
73 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
74 avoidArea->leftRect_ = rect;
75 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
76 avoidArea->rightRect_ = rect;
77 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
78 avoidArea->bottomRect_ = rect;
79 AvoidAreaType type;
80 startPos += GetObject<AvoidAreaType>(type, data + startPos, size - startPos);
81 agent->UpdateAvoidArea(avoidArea, type);
82
83 WindowState state;
84 GetObject<WindowState>(state, data + startPos, size - startPos);
85 agent->UpdateWindowState(state);
86 }
87
CheckWindowAgentFunctionsPart2(sptr<WindowAgent> agent,const uint8_t * data,size_t size)88 void CheckWindowAgentFunctionsPart2(sptr<WindowAgent> agent, const uint8_t* data, size_t size)
89 {
90 if (agent == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
91 return;
92 }
93 size_t startPos = 0;
94
95 PointInfo point;
96 DragEvent dragEvent;
97 startPos += GetObject<PointInfo>(point, data + startPos, size - startPos);
98 startPos += GetObject<DragEvent>(dragEvent, data + startPos, size - startPos);
99 agent->UpdateWindowDragInfo(point, dragEvent);
100
101 DisplayId from;
102 DisplayId to;
103 startPos += GetObject<DisplayId>(from, data + startPos, size - startPos);
104 startPos += GetObject<DisplayId>(to, data + startPos, size - startPos);
105 agent->UpdateDisplayId(from, to);
106
107 OHOS::Rosen::Rect rect;
108 OccupiedAreaType type;
109 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
110 startPos += GetObject<OccupiedAreaType>(type, data + startPos, size - startPos);
111 sptr<OccupiedAreaChangeInfo> info = new OccupiedAreaChangeInfo(type, rect);
112 if (info != nullptr) {
113 agent->UpdateOccupiedAreaChangeInfo(info);
114 }
115
116 bool boolVal;
117 startPos += GetObject<bool>(boolVal, data + startPos, size - startPos);
118 agent->UpdateActiveStatus(boolVal);
119 agent->GetWindowProperty();
120 agent->NotifyTouchOutside();
121 agent->NotifyScreenshot();
122
123 Transform trans;
124 startPos += GetObject<Transform>(trans, data + startPos, size - startPos);
125 agent->UpdateZoomTransform(trans, boolVal);
126
127 uint32_t mode;
128 GetObject<uint32_t>(mode, data + startPos, size - startPos);
129 agent->RestoreSplitWindowMode(mode);
130 }
131
WindowAgentFuzzTest(const uint8_t * data,size_t size)132 void WindowAgentFuzzTest(const uint8_t* data, size_t size)
133 {
134 sptr<OHOS::Rosen::WindowOption> option = new OHOS::Rosen::WindowOption();
135 if (option == nullptr) {
136 WLOGFE("Failed to malloc option.");
137 return;
138 }
139 sptr<OHOS::Rosen::WindowImpl> window = new(std::nothrow) OHOS::Rosen::WindowImpl(option);
140 if (window == nullptr) {
141 WLOGFE("Failed to malloc window impl.");
142 return;
143 }
144 sptr<WindowAgent> windowAgent = new WindowAgent(window);
145 OHOS::CheckWindowAgentFunctionsPart1(windowAgent, data, size);
146 OHOS::CheckWindowAgentFunctionsPart2(windowAgent, data, size);
147 }
148 } // namespace.OHOS
149
150 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)151 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
152 {
153 /* Run your code on data */
154 OHOS::WindowAgentFuzzTest(data, size);
155 return 0;
156 }