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 modeSupportInfo;
65 startPos += GetObject<uint32_t>(modeSupportInfo, data + startPos, size - startPos);
66 agent->UpdateWindowModeSupportInfo(modeSupportInfo);
67 agent->UpdateFocusStatus(boolVal);
68
69 sptr<AvoidArea> avoidArea = new AvoidArea();
70 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
71 avoidArea->topRect_ = rect;
72 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
73 avoidArea->leftRect_ = rect;
74 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
75 avoidArea->rightRect_ = rect;
76 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
77 avoidArea->bottomRect_ = rect;
78 AvoidAreaType type;
79 startPos += GetObject<AvoidAreaType>(type, data + startPos, size - startPos);
80 agent->UpdateAvoidArea(avoidArea, type);
81
82 WindowState state;
83 GetObject<WindowState>(state, data + startPos, size - startPos);
84 agent->UpdateWindowState(state);
85 }
86
CheckWindowAgentFunctionsPart2(sptr<WindowAgent> agent,const uint8_t * data,size_t size)87 void CheckWindowAgentFunctionsPart2(sptr<WindowAgent> agent, const uint8_t* data, size_t size)
88 {
89 if (agent == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
90 return;
91 }
92 size_t startPos = 0;
93
94 PointInfo point;
95 DragEvent dragEvent;
96 startPos += GetObject<PointInfo>(point, data + startPos, size - startPos);
97 startPos += GetObject<DragEvent>(dragEvent, data + startPos, size - startPos);
98 agent->UpdateWindowDragInfo(point, dragEvent);
99
100 DisplayId from;
101 DisplayId to;
102 startPos += GetObject<DisplayId>(from, data + startPos, size - startPos);
103 startPos += GetObject<DisplayId>(to, data + startPos, size - startPos);
104 agent->UpdateDisplayId(from, to);
105
106 OHOS::Rosen::Rect rect;
107 OccupiedAreaType type;
108 startPos += GetObject<OHOS::Rosen::Rect>(rect, data + startPos, size - startPos);
109 startPos += GetObject<OccupiedAreaType>(type, data + startPos, size - startPos);
110 sptr<OccupiedAreaChangeInfo> info = new OccupiedAreaChangeInfo(type, rect);
111 if (info != nullptr) {
112 agent->UpdateOccupiedAreaChangeInfo(info);
113 }
114
115 bool boolVal;
116 startPos += GetObject<bool>(boolVal, data + startPos, size - startPos);
117 agent->UpdateActiveStatus(boolVal);
118 agent->GetWindowProperty();
119 agent->NotifyTouchOutside();
120 agent->NotifyScreenshot();
121
122 Transform trans;
123 startPos += GetObject<Transform>(trans, data + startPos, size - startPos);
124 agent->UpdateZoomTransform(trans, boolVal);
125
126 uint32_t mode;
127 GetObject<uint32_t>(mode, data + startPos, size - startPos);
128 agent->RestoreSplitWindowMode(mode);
129 }
130
WindowAgentFuzzTest(const uint8_t * data,size_t size)131 void WindowAgentFuzzTest(const uint8_t* data, size_t size)
132 {
133 sptr<OHOS::Rosen::WindowOption> option = new OHOS::Rosen::WindowOption();
134 if (option == nullptr) {
135 WLOGFE("Failed to malloc option.");
136 return;
137 }
138 sptr<OHOS::Rosen::WindowImpl> window = new(std::nothrow) OHOS::Rosen::WindowImpl(option);
139 if (window == nullptr) {
140 WLOGFE("Failed to malloc window impl.");
141 return;
142 }
143 sptr<WindowAgent> windowAgent = new WindowAgent(window);
144 OHOS::CheckWindowAgentFunctionsPart1(windowAgent, data, size);
145 OHOS::CheckWindowAgentFunctionsPart2(windowAgent, data, size);
146 }
147 } // namespace.OHOS
148
149 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)150 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
151 {
152 /* Run your code on data */
153 OHOS::WindowAgentFuzzTest(data, size);
154 return 0;
155 }