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