• 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 "marshalling_helper.h"
17 
18 #include <securec.h>
19 
20 #include <want.h>
21 #include "window.h"
22 #include "window_impl.h"
23 #include "window_manager.h"
24 
25 using namespace OHOS::Rosen;
26 
27 namespace OHOS {
28 namespace {
29     constexpr size_t DATA_MIN_SIZE = 2;
30 }
31 class FocusChangedListener : public IFocusChangedListener {
32 public:
OnFocused(const sptr<FocusChangeInfo> & focusChangeInfo)33     virtual void OnFocused(const sptr<FocusChangeInfo>& focusChangeInfo) override
34     {
35     }
36 
OnUnfocused(const sptr<FocusChangeInfo> & focusChangeInfo)37     virtual void OnUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo) override
38     {
39     }
40 };
41 
42 class SystemBarChangedListener : public ISystemBarChangedListener {
43 public:
OnSystemBarPropertyChange(DisplayId displayId,const SystemBarRegionTints & tints)44     virtual void OnSystemBarPropertyChange(DisplayId displayId, const SystemBarRegionTints& tints) override
45     {
46     }
47 };
48 
49 class VisibilityChangedListener : public IVisibilityChangedListener {
50 public:
OnWindowVisibilityChanged(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfo)51     virtual void OnWindowVisibilityChanged(const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfo) override
52     {
53     }
54 };
55 
56 class WindowUpdateListener : public IWindowUpdateListener {
57 public:
OnWindowUpdate(const std::vector<sptr<AccessibilityWindowInfo>> & infos,WindowUpdateType type)58     virtual void OnWindowUpdate(const std::vector<sptr<AccessibilityWindowInfo>>& infos, WindowUpdateType type) override
59     {
60     }
61 };
62 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)63 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
64 {
65     if (data == nullptr || size < DATA_MIN_SIZE) {
66         return false;
67     }
68     std::string name = "WindowFuzzTest";
69     sptr<WindowOption> option = nullptr;
70     sptr<Window> window = Window::Create(name, option);
71     if (window == nullptr) {
72         return false;
73     }
74     window->Show(0);
75     Orientation orientation = static_cast<Orientation>(data[0]);
76     window->SetRequestedOrientation(static_cast<Orientation>(data[0]));
77     if (window->GetRequestedOrientation() != orientation) {
78         return false;
79     }
80     window->Hide(0);
81     window->Destroy();
82     return true;
83 }
84 
85 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)86 size_t GetObject(T &object, const uint8_t *data, size_t size)
87 {
88     size_t objectSize = sizeof(object);
89     if (objectSize > size) {
90         return 0;
91     }
92     return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
93 }
94 
CheckWindowImplFunctionsPart1(sptr<Window> window,const uint8_t * data,size_t size)95 void CheckWindowImplFunctionsPart1(sptr<Window> window, const uint8_t* data, size_t size)
96 {
97     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
98         return;
99     }
100     size_t startPos = 0;
101 
102     bool boolVal = false;
103     startPos += GetObject(boolVal, data + startPos, size - startPos);
104     window->SetFocusable(boolVal);
105     window->IsFocused();
106     startPos += GetObject(boolVal, data + startPos, size - startPos);
107     window->SetTouchable(boolVal);
108 
109     WindowType windowType;
110     WindowMode windowMode;
111     startPos += GetObject(windowType, data + startPos, size - startPos);
112     startPos += GetObject(windowMode, data + startPos, size - startPos);
113     window->SetWindowType(windowType);
114     window->SetWindowMode(windowMode);
115 
116     float alpha;
117     startPos += GetObject(alpha, data + startPos, size - startPos);
118     window->SetAlpha(alpha);
119 
120     Transform transForm;
121     startPos += GetObject(transForm, data + startPos, size - startPos);
122     window->SetTransform(transForm);
123 
124     WindowFlag windowFlag;
125     startPos += GetObject(windowFlag, data + startPos, size - startPos);
126     window->AddWindowFlag(windowFlag);
127     startPos += GetObject(windowFlag, data + startPos, size - startPos);
128     window->RemoveWindowFlag(windowFlag);
129 
130     SystemBarProperty systemBarProperty;
131     startPos += GetObject(windowType, data + startPos, size - startPos);
132     startPos += GetObject(systemBarProperty, data + startPos, size - startPos);
133     window->SetSystemBarProperty(windowType, systemBarProperty);
134 
135     startPos += GetObject(boolVal, data + startPos, size - startPos);
136     window->SetLayoutFullScreen(boolVal);
137 
138     startPos += GetObject(boolVal, data + startPos, size - startPos);
139     window->SetFullScreen(boolVal);
140 
141     startPos += GetObject(boolVal, data + startPos, size - startPos);
142     window->UpdateSurfaceNodeAfterCustomAnimation(boolVal);
143 }
144 
CheckWindowImplFunctionsPart2(sptr<WindowImpl> window,const uint8_t * data,size_t size)145 void CheckWindowImplFunctionsPart2(sptr<WindowImpl> window, const uint8_t* data, size_t size)
146 {
147     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
148         return;
149     }
150     size_t startPos = 0;
151 
152     int32_t posX;
153     int32_t posY;
154     startPos += GetObject(posX, data + startPos, size - startPos);
155     startPos += GetObject(posY, data + startPos, size - startPos);
156     window->MoveTo(posX, posY);
157 
158     uint32_t width;
159     uint32_t hight;
160     startPos += GetObject(width, data + startPos, size - startPos);
161     startPos += GetObject(hight, data + startPos, size - startPos);
162     window->Resize(width, hight);
163 
164     bool boolVal = false;
165     startPos += GetObject(boolVal, data + startPos, size - startPos);
166     window->SetKeepScreenOn(boolVal);
167 
168     startPos += GetObject(boolVal, data + startPos, size - startPos);
169     window->SetTurnScreenOn(boolVal);
170 
171     startPos += GetObject(boolVal, data + startPos, size - startPos);
172     window->SetTransparent(boolVal);
173 
174     startPos += GetObject(boolVal, data + startPos, size - startPos);
175     window->SetBrightness(boolVal);
176 
177     int32_t windowId;
178     startPos += GetObject(windowId, data + startPos, size - startPos);
179     window->SetCallingWindow(windowId);
180 
181     startPos += GetObject(boolVal, data + startPos, size - startPos);
182     window->SetPrivacyMode(boolVal);
183 
184     startPos += GetObject(boolVal, data + startPos, size - startPos);
185     window->SetSystemPrivacyMode(boolVal);
186 
187     startPos += GetObject(boolVal, data + startPos, size - startPos);
188     window->SetSnapshotSkip(boolVal);
189 }
190 
CheckWindowImplFunctionsPart3(sptr<WindowImpl> window,const uint8_t * data,size_t size)191 void CheckWindowImplFunctionsPart3(sptr<WindowImpl> window, const uint8_t* data, size_t size)
192 {
193     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
194         return;
195     }
196     size_t startPos = 0;
197     float floatVal;
198     startPos += GetObject(floatVal, data + startPos, size - startPos);
199     window->SetCornerRadius(floatVal);
200 
201     startPos += GetObject(floatVal, data + startPos, size - startPos);
202     window->SetShadowRadius(floatVal);
203 
204     startPos += GetObject(floatVal, data + startPos, size - startPos);
205     window->SetShadowOffsetX(floatVal);
206     startPos += GetObject(floatVal, data + startPos, size - startPos);
207     window->SetShadowOffsetY(floatVal);
208     startPos += GetObject(floatVal, data + startPos, size - startPos);
209     window->SetBlur(floatVal);
210     startPos += GetObject(floatVal, data + startPos, size - startPos);
211     window->SetBackdropBlur(floatVal);
212 
213     WindowBlurStyle blurStyle;
214     startPos += GetObject(blurStyle, data + startPos, size - startPos);
215     window->SetBackdropBlurStyle(blurStyle);
216 
217     bool boolVal;
218     OHOS::Rosen::Rect rect;
219     WindowSizeChangeReason reason;
220     startPos += GetObject(boolVal, data + startPos, size - startPos);
221     startPos += GetObject(rect, data + startPos, size - startPos);
222     startPos += GetObject(reason, data + startPos, size - startPos);
223     window->UpdateRect(rect, boolVal, reason);
224 
225     WindowMode mode;
226     startPos += GetObject(mode, data + startPos, size - startPos);
227     window->UpdateMode(mode);
228 
229     uint32_t modeSupportInfo;
230     startPos += GetObject(modeSupportInfo, data + startPos, size - startPos);
231     window->UpdateModeSupportInfo(modeSupportInfo);
232 
233     WindowState windowState;
234     startPos += GetObject(windowState, data + startPos, size - startPos);
235     window->UpdateWindowState(windowState);
236 
237     PointInfo point;
238     DragEvent event;
239     startPos += GetObject(point, data + startPos, size - startPos);
240     startPos += GetObject(event, data + startPos, size - startPos);
241     window->UpdateDragEvent(point, event);
242 
243     DisplayId displayId[2];
244     startPos += GetObject(displayId[0], data + startPos, size - startPos);
245     startPos += GetObject(displayId[1], data + startPos, size - startPos);
246     window->UpdateDisplayId(displayId[0], displayId[1]);
247 }
248 
CheckWindowImplFunctionsPart4(sptr<WindowImpl> window,const uint8_t * data,size_t size)249 void CheckWindowImplFunctionsPart4(sptr<WindowImpl> window, const uint8_t* data, size_t size)
250 {
251     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
252         return;
253     }
254     size_t startPos = 0;
255 
256     bool boolVal;
257     startPos += GetObject(boolVal, data + startPos, size - startPos);
258     window->UpdateActiveStatus(boolVal);
259 
260     Transform trans;
261     startPos += GetObject(trans, data + startPos, size - startPos);
262     startPos += GetObject(boolVal, data + startPos, size - startPos);
263     window->UpdateZoomTransform(trans, boolVal);
264 
265     startPos += GetObject(boolVal, data + startPos, size - startPos);
266     window->SetNeedRemoveWindowInputChannel(boolVal);
267 
268     std::vector<OHOS::Rosen::Rect> rectVector;
269     OHOS::Rosen::Rect rect;
270     startPos += GetObject(rect, data + startPos, size - startPos);
271     rectVector.emplace_back(rect);
272     window->SetTouchHotAreas(rectVector);
273     window->GetRequestedTouchHotAreas(rectVector);
274     rectVector.clear();
275 
276     ColorSpace colorSpace;
277     startPos += GetObject(colorSpace, data + startPos, size - startPos);
278     window->SetColorSpace(colorSpace);
279 
280     int32_t level;
281     startPos += GetObject(level, data + startPos, size - startPos);
282     window->NotifyMemoryLevel(level);
283 
284     uint32_t mode;
285     startPos += GetObject(mode, data + startPos, size - startPos);
286     window->RestoreSplitWindowMode(mode);
287 }
288 
CheckWindowImplFunctionsPart5(sptr<WindowImpl> window,const uint8_t * data,size_t size)289 void CheckWindowImplFunctionsPart5(sptr<WindowImpl> window, const uint8_t* data, size_t size)
290 {
291     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
292         return;
293     }
294     size_t startPos = 0;
295 
296     OHOS::Rosen::Rect rect;
297     WindowSizeChangeReason reason;
298     startPos += GetObject(rect, data + startPos, size - startPos);
299     startPos += GetObject(reason, data + startPos, size - startPos);
300     window->NotifySizeChange(rect, reason);
301 
302     DisplayId displayId[2];
303     startPos += GetObject(displayId[0], data + startPos, size - startPos);
304     startPos += GetObject(displayId[1], data + startPos, size - startPos);
305     window->NotifyDisplayMoveChange(displayId[0], displayId[1]);
306 
307     WindowMode mode;
308     startPos += GetObject(mode, data + startPos, size - startPos);
309     window->NotifyModeChange(mode);
310 
311     PointInfo point;
312     DragEvent dragEvent;
313     startPos += GetObject(point, data + startPos, size - startPos);
314     startPos += GetObject(dragEvent, data + startPos, size - startPos);
315     window->NotifyDragEvent(point, dragEvent);
316 
317     int32_t posX;
318     int32_t posY;
319     int32_t pointId;
320     int32_t sourceType;
321     startPos += GetObject(posX, data + startPos, size - startPos);
322     startPos += GetObject(posY, data + startPos, size - startPos);
323     startPos += GetObject(pointId, data + startPos, size - startPos);
324     startPos += GetObject(sourceType, data + startPos, size - startPos);
325     window->EndMoveOrDragWindow(posX, posY, pointId, sourceType);
326     window->IsPointInDragHotZone(posX, posY);
327 
328     bool boolVal;
329     startPos += GetObject(boolVal, data + startPos, size - startPos);
330     window->AdjustWindowAnimationFlag(boolVal);
331 
332     PropertyChangeAction action;
333     startPos += GetObject(action, data + startPos, size - startPos);
334     window->UpdateProperty(action);
335 
336     uint32_t uinte32Val;
337     startPos += GetObject(uinte32Val, data + startPos, size - startPos);
338     window->SetBackgroundColor(uinte32Val);
339 
340     LifeCycleEvent lifeCycleEvent;
341     OHOS::Rosen::WMError errCode;
342     startPos += GetObject(lifeCycleEvent, data + startPos, size - startPos);
343     startPos += GetObject(errCode, data + startPos, size - startPos);
344     window->RecordLifeCycleExceptionEvent(lifeCycleEvent, errCode);
345     window->TransferLifeCycleEventToString(lifeCycleEvent);
346 
347     startPos += GetObject(rect, data + startPos, size - startPos);
348     window->GetSystemAlarmWindowDefaultSize(rect);
349     window->HandleModeChangeHotZones(posX, posY);
350 }
351 
352 
CheckWindowImplFunctionsPart6(sptr<WindowImpl> window,const uint8_t * data,size_t size)353 void CheckWindowImplFunctionsPart6(sptr<WindowImpl> window, const uint8_t* data, size_t size)
354 {
355     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
356         return;
357     }
358     size_t startPos = 0;
359 
360     TransitionReason reason;
361     startPos += GetObject(reason, data + startPos, size - startPos);
362     window->NotifyWindowTransition(reason);
363 
364     WindowType type;
365     startPos += GetObject(type, data + startPos, size - startPos);
366     window->CheckCameraFloatingWindowMultiCreated(type);
367 
368     uint32_t uint32Val[2];
369     startPos += GetObject(uint32Val[0], data + startPos, size - startPos);
370     startPos += GetObject(uint32Val[1], data + startPos, size - startPos);
371     window->SetModeSupportInfo(uint32Val[0]);
372 
373     float floatVal;
374     startPos += GetObject(floatVal, data + startPos, size - startPos);
375     window->CalculateStartRectExceptHotZone(floatVal);
376 
377     Transform transform;
378     startPos += GetObject(transform, data + startPos, size - startPos);
379     window->TransformSurfaceNode(transform);
380 
381     window->WindowCreateCheck(uint32Val[0]);
382     window->CalculatePointerDirection(uint32Val[0], uint32Val[1]);
383 }
384 
WindowImplFuzzTest(const uint8_t * data,size_t size)385 void WindowImplFuzzTest(const uint8_t* data, size_t size)
386 {
387     std::string name = "WindowFuzzTest";
388     sptr<OHOS::Rosen::WindowOption> option = new OHOS::Rosen::WindowOption();
389     sptr<OHOS::Rosen::WindowImpl> window = new(std::nothrow) OHOS::Rosen::WindowImpl(option);
390     if (window == nullptr) {
391         return;
392     }
393     OHOS::Rosen::WMError error = window->Create(option->GetParentId(), nullptr);
394     if (error != OHOS::Rosen::WMError::WM_OK) {
395         return;
396     }
397 
398     size_t startPos = 0;
399     uint32_t reason = 0;
400     bool withAnimation = false;
401     startPos += GetObject(reason, data + startPos, size - startPos);
402     startPos += GetObject(withAnimation, data + startPos, size - startPos);
403     window->Show(reason, withAnimation);
404 
405     OHOS::CheckWindowImplFunctionsPart1(window, data, size);
406     OHOS::CheckWindowImplFunctionsPart2(window, data, size);
407     OHOS::CheckWindowImplFunctionsPart3(window, data, size);
408     OHOS::CheckWindowImplFunctionsPart4(window, data, size);
409     OHOS::CheckWindowImplFunctionsPart5(window, data, size);
410     OHOS::CheckWindowImplFunctionsPart6(window, data, size);
411 
412     window->Hide(reason, withAnimation);
413     window->Destroy();
414 }
415 } // namespace.OHOS
416 
417 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)418 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
419 {
420     /* Run your code on data */
421     OHOS::DoSomethingInterestingWithMyAPI(data, size);
422     OHOS::WindowImplFuzzTest(data, size);
423     return 0;
424 }