• 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 #include <iremote_broker.h>
20 #include <iservice_registry.h>
21 
22 #include "ability_context.h"
23 #include "ability_context_impl.h"
24 #include "js_runtime.h"
25 #include <want.h>
26 #include "window.h"
27 #include "window_accessibility_controller.h"
28 #include "window_impl.h"
29 #include "window_manager.h"
30 #include "window_extension_connection.h"
31 #include "window_adapter.h"
32 #include "wm_common.h"
33 #include "window_option.h"
34 
35 using namespace OHOS::Rosen;
36 
37 namespace OHOS {
38 namespace {
39     constexpr size_t DATA_MIN_SIZE = 2;
40 }
41 class FocusChangedListener : public IFocusChangedListener {
42 public:
OnFocused(const sptr<FocusChangeInfo> & focusChangeInfo)43     virtual void OnFocused(const sptr<FocusChangeInfo>& focusChangeInfo) override
44     {
45     }
46 
OnUnfocused(const sptr<FocusChangeInfo> & focusChangeInfo)47     virtual void OnUnfocused(const sptr<FocusChangeInfo>& focusChangeInfo) override
48     {
49     }
50 };
51 
52 class SystemBarChangedListener : public ISystemBarChangedListener {
53 public:
OnSystemBarPropertyChange(DisplayId displayId,const SystemBarRegionTints & tints)54     virtual void OnSystemBarPropertyChange(DisplayId displayId, const SystemBarRegionTints& tints) override
55     {
56     }
57 };
58 
59 class VisibilityChangedListener : public IVisibilityChangedListener {
60 public:
OnWindowVisibilityChanged(const std::vector<sptr<WindowVisibilityInfo>> & windowVisibilityInfo)61     virtual void OnWindowVisibilityChanged(const std::vector<sptr<WindowVisibilityInfo>>& windowVisibilityInfo) override
62     {
63     }
64 };
65 
66 class WindowUpdateListener : public IWindowUpdateListener {
67 public:
OnWindowUpdate(const std::vector<sptr<AccessibilityWindowInfo>> & infos,WindowUpdateType type)68     virtual void OnWindowUpdate(const std::vector<sptr<AccessibilityWindowInfo>>& infos, WindowUpdateType type) override
69     {
70     }
71 };
72 
73 class ExtensionCallback : public IWindowExtensionCallback {
74 public:
OnWindowReady(const std::shared_ptr<Rosen::RSSurfaceNode> & rsSurfaceNode)75     void OnWindowReady(const std::shared_ptr<Rosen::RSSurfaceNode>& rsSurfaceNode) override
76     {
77     }
OnExtensionDisconnected()78     void OnExtensionDisconnected() override
79     {
80     }
OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> & event)81     void OnKeyEvent(const std::shared_ptr<MMI::KeyEvent>& event) override
82     {
83     }
OnPointerEvent(const std::shared_ptr<MMI::PointerEvent> & event)84     void OnPointerEvent(const std::shared_ptr<MMI::PointerEvent>& event) override
85     {
86     }
OnBackPress()87     void OnBackPress() override
88     {
89     }
90 };
91 
92 class OccupiedAreaChangeListener : public IOccupiedAreaChangeListener {
93 public:
OnSizeChange(const sptr<OccupiedAreaChangeInfo> & info,const std::shared_ptr<RSTransaction> & rsTransaction=nullptr)94     void OnSizeChange(const sptr<OccupiedAreaChangeInfo>& info,
95         const std::shared_ptr<RSTransaction>& rsTransaction = nullptr) override
96     {
97     }
98 };
99 
100 class TouchOutsideListener : public ITouchOutsideListener {
101 public:
OnTouchOutside() const102     void OnTouchOutside() const override
103     {
104     }
105 };
106 
107 class AnimationTransitionController : public IAnimationTransitionController {
108 public:
AnimationForShown()109     void AnimationForShown() override
110     {
111     }
112 
AnimationForHidden()113     void AnimationForHidden() override
114     {
115     }
116 };
117 
118 class ScreenshotListener : public IScreenshotListener {
119 public:
OnScreenshot()120     void OnScreenshot() override
121     {
122     }
123 };
124 
125 class DialogTargetTouchListener : public IDialogTargetTouchListener {
126 public:
OnDialogTargetTouch() const127     void OnDialogTargetTouch() const override
128     {
129     }
130 };
131 
132 class DialogDeathRecipientListener : public IDialogDeathRecipientListener {
133 public:
OnDialogDeathRecipient() const134     void OnDialogDeathRecipient() const override
135     {
136     }
137 };
138 
139 class WindowDragListener : public IWindowDragListener {
140 public:
OnDrag(int32_t x,int32_t y,DragEvent event)141     void OnDrag(int32_t x, int32_t y, DragEvent event) override
142     {
143     }
144 };
145 
146 class DisplayMoveListener : public IDisplayMoveListener {
147 public:
OnDisplayMove(DisplayId from,DisplayId to)148     void OnDisplayMove(DisplayId from, DisplayId to) override
149     {
150     }
151 };
152 
153 class AceAbilityHandler : public IAceAbilityHandler {
154 public:
SetBackgroundColor(uint32_t color)155     void SetBackgroundColor(uint32_t color) override
156     {
157     }
158 
GetBackgroundColor()159     virtual uint32_t GetBackgroundColor() override
160     {
161         return 0xffffffff;
162     }
163 };
164 
165 class WindowLifeCycle : public IWindowLifeCycle {
166 public:
AfterForeground()167     void AfterForeground() override
168     {
169     }
170 
AfterBackground()171     void AfterBackground() override
172     {
173     }
174 
AfterFocused()175     void AfterFocused() override
176     {
177     }
178 
AfterUnfocused()179     void AfterUnfocused() override
180     {
181     }
182 
ForegroundFailed(int32_t ret)183     void ForegroundFailed(int32_t ret) override
184     {
185     }
186 
BackgroundFailed(int32_t ret)187     void BackgroundFailed(int32_t ret) override
188     {
189     }
190 
AfterActive()191     void AfterActive() override
192     {
193     }
194 
AfterInactive()195     void AfterInactive()  override
196     {
197     }
198 };
199 
200 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)201 size_t GetObject(T &object, const uint8_t *data, size_t size)
202 {
203     size_t objectSize = sizeof(object);
204     if (objectSize > size) {
205         return 0;
206     }
207     return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
208 }
209 
DoSomethingInterestingWithMyAPI1(const uint8_t * data,size_t size)210 bool DoSomethingInterestingWithMyAPI1(const uint8_t* data, size_t size)
211 {
212     if (data == nullptr || size < DATA_MIN_SIZE) {
213         return false;
214     }
215     std::string name = "WindowFuzzTest1";
216     sptr<WindowOption> option = nullptr;
217     sptr<Window> window = new Window();
218     if (window == nullptr) {
219         return false;
220     }
221     size_t startPos = 0;
222     sptr<IOccupiedAreaChangeListener> iOccupiedAreaChangeListener = new IOccupiedAreaChangeListener();
223     OHOS::Rosen::Rect rect_ = {0, 0, 0, 0};
224     window->RegisterOccupiedAreaChangeListener(iOccupiedAreaChangeListener);
225     int32_t safeHeight = 80;
226     startPos += GetObject<int32_t>(safeHeight, data + startPos, size - startPos);
227     sptr<OHOS::Rosen::OccupiedAreaChangeInfo> info = new OccupiedAreaChangeInfo(
228         OccupiedAreaType::TYPE_INPUT, rect_, safeHeight);
229     iOccupiedAreaChangeListener->OnSizeChange(info, nullptr);
230     window->UnregisterOccupiedAreaChangeListener(iOccupiedAreaChangeListener);
231     sptr<IVisibilityChangedListener> visibilityChangedListener = new VisibilityChangedListener();
232     std::vector<sptr<WindowVisibilityInfo>> infos;
233     visibilityChangedListener->OnWindowVisibilityChanged(infos);
234     return true;
235 }
236 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)237 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
238 {
239     if (data == nullptr || size < DATA_MIN_SIZE) {
240         return false;
241     }
242     std::string name = "WindowFuzzTest";
243     sptr<WindowOption> option = nullptr;
244     sptr<Window> window = new Window();
245     if (window == nullptr) {
246         return false;
247     }
248     std::string windowName = "window test";
249     window->Find(windowName);
250     size_t startPos = 0;
251     std::shared_ptr<AbilityRuntime::Context> context = nullptr;
252     startPos += GetObject(context, data + startPos, size - startPos);
253     window->GetTopWindowWithContext(context);
254     uint32_t mainWinId = 1;
255     startPos += GetObject<uint32_t>(mainWinId, data + startPos, size - startPos);
256     window->GetTopWindowWithId(mainWinId);
257     uint32_t parentId = 1;
258     startPos += GetObject<uint32_t>(parentId, data + startPos, size - startPos);
259     window->GetSubWindow(parentId);
260     std::shared_ptr<AppExecFwk::Configuration> configuration = nullptr;
261     startPos += GetObject(configuration, data + startPos, size - startPos);
262     window->UpdateConfigurationForAll(configuration);
263     window->Show(0);
264     window->SetRequestedOrientation(static_cast<Orientation>(data[0]));
265     window->Hide(0);
266     window->Destroy();
267     sptr<IFocusChangedListener> focusChangedListener = new FocusChangedListener();
268     sptr<FocusChangeInfo> focusChangeInfo = new FocusChangeInfo();
269     focusChangedListener->OnFocused(focusChangeInfo);
270     focusChangedListener->OnUnfocused(focusChangeInfo);
271     sptr<IDispatchInputEventListener> iDispatchInputEventListener = new IDispatchInputEventListener();
272     std::shared_ptr<MMI::KeyEvent> keyEvent = nullptr;
273     std::shared_ptr<MMI::PointerEvent> pointerEvent = nullptr;
274     std::shared_ptr<MMI::AxisEvent> axisEvent = nullptr;
275     iDispatchInputEventListener->OnDispatchPointerEvent(pointerEvent);
276     iDispatchInputEventListener->OnDispatchKeyEvent(keyEvent);
277     std::shared_ptr<IInputEventConsumer> iInputEventConsumer = std::make_shared<IInputEventConsumer>();
278     iInputEventConsumer->OnInputEvent(keyEvent);
279     iInputEventConsumer->OnInputEvent(pointerEvent);
280     iInputEventConsumer->OnInputEvent(axisEvent);
281     DoSomethingInterestingWithMyAPI1(data, size);
282     return true;
283 }
284 
CheckWindowImplFunctionsPart1(sptr<Window> window,const uint8_t * data,size_t size)285 void CheckWindowImplFunctionsPart1(sptr<Window> window, const uint8_t* data, size_t size)
286 {
287     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
288         return;
289     }
290     size_t startPos = 0;
291 
292     bool boolVal = false;
293     startPos += GetObject(boolVal, data + startPos, size - startPos);
294     window->SetFocusable(boolVal);
295     window->IsFocused();
296     startPos += GetObject(boolVal, data + startPos, size - startPos);
297     window->SetTouchable(boolVal);
298     window->SetResizeByDragEnabled(boolVal);
299     window->SetRaiseByClickEnabled(boolVal);
300 
301     WindowType windowType;
302     WindowMode windowMode;
303     startPos += GetObject(windowType, data + startPos, size - startPos);
304     startPos += GetObject(windowMode, data + startPos, size - startPos);
305     window->SetWindowType(windowType);
306     window->SetWindowMode(windowMode);
307 
308     float alpha;
309     startPos += GetObject(alpha, data + startPos, size - startPos);
310     window->SetAlpha(alpha);
311 
312     Transform transForm;
313     startPos += GetObject(transForm, data + startPos, size - startPos);
314     window->SetTransform(transForm);
315 
316     WindowFlag windowFlag;
317     startPos += GetObject(windowFlag, data + startPos, size - startPos);
318     window->AddWindowFlag(windowFlag);
319     startPos += GetObject(windowFlag, data + startPos, size - startPos);
320     window->RemoveWindowFlag(windowFlag);
321 
322     SystemBarProperty systemBarProperty;
323     startPos += GetObject(windowType, data + startPos, size - startPos);
324     startPos += GetObject(systemBarProperty, data + startPos, size - startPos);
325     window->SetSystemBarProperty(windowType, systemBarProperty);
326 
327     startPos += GetObject(boolVal, data + startPos, size - startPos);
328     window->SetLayoutFullScreen(boolVal);
329 
330     startPos += GetObject(boolVal, data + startPos, size - startPos);
331     window->SetFullScreen(boolVal);
332 
333     startPos += GetObject(boolVal, data + startPos, size - startPos);
334     window->UpdateSurfaceNodeAfterCustomAnimation(boolVal);
335 }
336 
CheckWindowImplFunctionsPart2(sptr<WindowImpl> window,const uint8_t * data,size_t size)337 void CheckWindowImplFunctionsPart2(sptr<WindowImpl> window, const uint8_t* data, size_t size)
338 {
339     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
340         return;
341     }
342     size_t startPos = 0;
343 
344     int32_t posX;
345     int32_t posY;
346     startPos += GetObject(posX, data + startPos, size - startPos);
347     startPos += GetObject(posY, data + startPos, size - startPos);
348     window->MoveTo(posX, posY);
349 
350     uint32_t width;
351     uint32_t hight;
352     startPos += GetObject(width, data + startPos, size - startPos);
353     startPos += GetObject(hight, data + startPos, size - startPos);
354     window->Resize(width, hight);
355 
356     bool boolVal = false;
357     startPos += GetObject(boolVal, data + startPos, size - startPos);
358     window->SetKeepScreenOn(boolVal);
359 
360     startPos += GetObject(boolVal, data + startPos, size - startPos);
361     window->SetTurnScreenOn(boolVal);
362 
363     startPos += GetObject(boolVal, data + startPos, size - startPos);
364     window->SetTransparent(boolVal);
365 
366     startPos += GetObject(boolVal, data + startPos, size - startPos);
367     window->SetBrightness(boolVal);
368 
369     int32_t windowId;
370     startPos += GetObject(windowId, data + startPos, size - startPos);
371     window->SetCallingWindow(windowId);
372 
373     startPos += GetObject(boolVal, data + startPos, size - startPos);
374     window->SetPrivacyMode(boolVal);
375 
376     startPos += GetObject(boolVal, data + startPos, size - startPos);
377     window->SetSystemPrivacyMode(boolVal);
378 
379     startPos += GetObject(boolVal, data + startPos, size - startPos);
380     window->SetSnapshotSkip(boolVal);
381 }
382 
CheckWindowImplFunctionsPart3(sptr<WindowImpl> window,const uint8_t * data,size_t size)383 void CheckWindowImplFunctionsPart3(sptr<WindowImpl> window, const uint8_t* data, size_t size)
384 {
385     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
386         return;
387     }
388     size_t startPos = 0;
389     float floatVal;
390     startPos += GetObject(floatVal, data + startPos, size - startPos);
391     window->SetCornerRadius(floatVal);
392 
393     startPos += GetObject(floatVal, data + startPos, size - startPos);
394     window->SetShadowRadius(floatVal);
395 
396     startPos += GetObject(floatVal, data + startPos, size - startPos);
397     window->SetShadowOffsetX(floatVal);
398     startPos += GetObject(floatVal, data + startPos, size - startPos);
399     window->SetShadowOffsetY(floatVal);
400     startPos += GetObject(floatVal, data + startPos, size - startPos);
401     window->SetBlur(floatVal);
402     startPos += GetObject(floatVal, data + startPos, size - startPos);
403     window->SetBackdropBlur(floatVal);
404 
405     WindowBlurStyle blurStyle;
406     startPos += GetObject(blurStyle, data + startPos, size - startPos);
407     window->SetBackdropBlurStyle(blurStyle);
408 
409     bool boolVal;
410     OHOS::Rosen::Rect rect;
411     WindowSizeChangeReason reason;
412     startPos += GetObject(boolVal, data + startPos, size - startPos);
413     startPos += GetObject(rect, data + startPos, size - startPos);
414     startPos += GetObject(reason, data + startPos, size - startPos);
415     window->UpdateRect(rect, boolVal, reason);
416 
417     WindowMode mode;
418     startPos += GetObject(mode, data + startPos, size - startPos);
419     window->UpdateMode(mode);
420 
421     uint32_t modeSupportInfo;
422     startPos += GetObject(modeSupportInfo, data + startPos, size - startPos);
423     window->UpdateModeSupportInfo(modeSupportInfo);
424 
425     WindowState windowState;
426     startPos += GetObject(windowState, data + startPos, size - startPos);
427     window->UpdateWindowState(windowState);
428 
429     PointInfo point;
430     DragEvent event;
431     startPos += GetObject(point, data + startPos, size - startPos);
432     startPos += GetObject(event, data + startPos, size - startPos);
433     window->UpdateDragEvent(point, event);
434 
435     DisplayId displayId[2];
436     startPos += GetObject(displayId[0], data + startPos, size - startPos);
437     startPos += GetObject(displayId[1], data + startPos, size - startPos);
438     window->UpdateDisplayId(displayId[0], displayId[1]);
439 }
440 
CheckWindowImplFunctionsPart4(sptr<WindowImpl> window,const uint8_t * data,size_t size)441 void CheckWindowImplFunctionsPart4(sptr<WindowImpl> window, const uint8_t* data, size_t size)
442 {
443     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
444         return;
445     }
446     size_t startPos = 0;
447 
448     bool boolVal;
449     startPos += GetObject(boolVal, data + startPos, size - startPos);
450     window->UpdateActiveStatus(boolVal);
451 
452     Transform trans;
453     startPos += GetObject(trans, data + startPos, size - startPos);
454     startPos += GetObject(boolVal, data + startPos, size - startPos);
455     window->UpdateZoomTransform(trans, boolVal);
456 
457     startPos += GetObject(boolVal, data + startPos, size - startPos);
458     window->SetNeedRemoveWindowInputChannel(boolVal);
459 
460     std::vector<OHOS::Rosen::Rect> rectVector;
461     OHOS::Rosen::Rect rect;
462     startPos += GetObject(rect, data + startPos, size - startPos);
463     rectVector.emplace_back(rect);
464     window->SetTouchHotAreas(rectVector);
465     window->GetRequestedTouchHotAreas(rectVector);
466     rectVector.clear();
467 
468     ColorSpace colorSpace;
469     startPos += GetObject(colorSpace, data + startPos, size - startPos);
470     window->SetColorSpace(colorSpace);
471 
472     int32_t level;
473     startPos += GetObject(level, data + startPos, size - startPos);
474     window->NotifyMemoryLevel(level);
475 
476     uint32_t mode;
477     startPos += GetObject(mode, data + startPos, size - startPos);
478     window->RestoreSplitWindowMode(mode);
479 }
480 
CheckWindowImplFunctionsPart5(sptr<WindowImpl> window,const uint8_t * data,size_t size)481 void CheckWindowImplFunctionsPart5(sptr<WindowImpl> window, const uint8_t* data, size_t size)
482 {
483     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
484         return;
485     }
486     size_t startPos = 0;
487 
488     OHOS::Rosen::Rect rect;
489     WindowSizeChangeReason reason;
490     startPos += GetObject(rect, data + startPos, size - startPos);
491     startPos += GetObject(reason, data + startPos, size - startPos);
492     window->NotifySizeChange(rect, reason);
493 
494     DisplayId displayId[2];
495     startPos += GetObject(displayId[0], data + startPos, size - startPos);
496     startPos += GetObject(displayId[1], data + startPos, size - startPos);
497     window->NotifyDisplayMoveChange(displayId[0], displayId[1]);
498 
499     WindowMode mode;
500     startPos += GetObject(mode, data + startPos, size - startPos);
501     window->NotifyModeChange(mode);
502 
503     PointInfo point;
504     DragEvent dragEvent;
505     startPos += GetObject(point, data + startPos, size - startPos);
506     startPos += GetObject(dragEvent, data + startPos, size - startPos);
507     window->NotifyDragEvent(point, dragEvent);
508 
509     int32_t posX;
510     int32_t posY;
511     int32_t pointId;
512     int32_t sourceType;
513     startPos += GetObject(posX, data + startPos, size - startPos);
514     startPos += GetObject(posY, data + startPos, size - startPos);
515     startPos += GetObject(pointId, data + startPos, size - startPos);
516     startPos += GetObject(sourceType, data + startPos, size - startPos);
517     window->EndMoveOrDragWindow(posX, posY, pointId, sourceType);
518 
519     bool boolVal;
520     startPos += GetObject(boolVal, data + startPos, size - startPos);
521     window->AdjustWindowAnimationFlag(boolVal);
522 
523     PropertyChangeAction action;
524     startPos += GetObject(action, data + startPos, size - startPos);
525     window->UpdateProperty(action);
526 
527     uint32_t uinte32Val;
528     startPos += GetObject(uinte32Val, data + startPos, size - startPos);
529     window->SetBackgroundColor(uinte32Val);
530 
531     LifeCycleEvent lifeCycleEvent;
532     OHOS::Rosen::WMError errCode;
533     startPos += GetObject(lifeCycleEvent, data + startPos, size - startPos);
534     startPos += GetObject(errCode, data + startPos, size - startPos);
535     window->RecordLifeCycleExceptionEvent(lifeCycleEvent, errCode);
536     window->TransferLifeCycleEventToString(lifeCycleEvent);
537 
538     startPos += GetObject(rect, data + startPos, size - startPos);
539     window->GetSystemAlarmWindowDefaultSize(rect);
540     window->HandleModeChangeHotZones(posX, posY);
541 }
542 
CheckWindowImplFunctionsPart6(sptr<WindowImpl> window,const uint8_t * data,size_t size)543 void CheckWindowImplFunctionsPart6(sptr<WindowImpl> window, const uint8_t* data, size_t size)
544 {
545     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
546         return;
547     }
548     size_t startPos = 0;
549 
550     TransitionReason reason;
551     startPos += GetObject(reason, data + startPos, size - startPos);
552     window->NotifyWindowTransition(reason);
553 
554     WindowType type;
555     startPos += GetObject(type, data + startPos, size - startPos);
556     window->CheckCameraFloatingWindowMultiCreated(type);
557 
558     uint32_t uint32Val[2];
559     startPos += GetObject(uint32Val[0], data + startPos, size - startPos);
560     startPos += GetObject(uint32Val[1], data + startPos, size - startPos);
561     window->SetModeSupportInfo(uint32Val[0]);
562 
563     float floatVal;
564     startPos += GetObject(floatVal, data + startPos, size - startPos);
565     window->CalculateStartRectExceptHotZone(floatVal);
566 
567     Transform transform;
568     startPos += GetObject(transform, data + startPos, size - startPos);
569     window->TransformSurfaceNode(transform);
570 
571     window->WindowCreateCheck(uint32Val[0]);
572     window->CalculatePointerDirection(uint32Val[0], uint32Val[1]);
573     sptr<IWindowChangeListener> iWindowChangeListener = new IWindowChangeListener();
574     std::shared_ptr<RSTransaction> rstransaction;
575     OHOS::Rosen::Rect rect_ = {0, 0, 0, 0};
576     iWindowChangeListener->OnSizeChange(rect_, WindowSizeChangeReason::UNDEFINED, rstransaction);
577     sptr<IWindowExtensionCallback> iWindowExtensionCallback = new ExtensionCallback();
578     std::shared_ptr<Rosen::RSSurfaceNode> rsSurfaceNode = nullptr;
579     std::shared_ptr<MMI::KeyEvent> keyEvent = nullptr;
580     iWindowExtensionCallback->OnWindowReady(rsSurfaceNode);
581     iWindowExtensionCallback->OnExtensionDisconnected();
582     iWindowExtensionCallback->OnKeyEvent(keyEvent);
583     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
584     iWindowExtensionCallback->OnPointerEvent(pointerEvent);
585     iWindowExtensionCallback->OnBackPress();
586 }
587 
CheckWindowImplFunctionsPart7(sptr<WindowImpl> window,const uint8_t * data,size_t size)588 void CheckWindowImplFunctionsPart7(sptr<WindowImpl> window, const uint8_t* data, size_t size)
589 {
590     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
591         return;
592     }
593     size_t startPos = 0;
594     NotifyNativeWinDestroyFunc func = [](std::string) {};
595     window->RegisterWindowDestroyedListener(func);
596 
597     sptr<IOccupiedAreaChangeListener> occupiedAreaChangeListener = new OccupiedAreaChangeListener();
598     window->RegisterOccupiedAreaChangeListener(occupiedAreaChangeListener);
599     window->UnregisterOccupiedAreaChangeListener(occupiedAreaChangeListener);
600     sptr<ITouchOutsideListener> touchOutsideListener = new TouchOutsideListener();
601     window->RegisterTouchOutsideListener(touchOutsideListener);
602     window->UnregisterTouchOutsideListener(touchOutsideListener);
603     sptr<IAnimationTransitionController> animationTransitionController = new AnimationTransitionController();
604     window->RegisterAnimationTransitionController(animationTransitionController);
605     sptr<IScreenshotListener> screenshotListener = new IScreenshotListener();
606     window->RegisterScreenshotListener(screenshotListener);
607     window->UnregisterScreenshotListener(screenshotListener);
608     sptr<IDialogTargetTouchListener> dialogTargetTouchListener = new DialogTargetTouchListener();
609     window->RegisterDialogTargetTouchListener(dialogTargetTouchListener);
610     window->UnregisterDialogTargetTouchListener(dialogTargetTouchListener);
611     sptr<IDialogDeathRecipientListener> dialogDeathRecipientListener = new DialogDeathRecipientListener();
612     window->RegisterDialogDeathRecipientListener(dialogDeathRecipientListener);
613     window->UnregisterDialogDeathRecipientListener(dialogDeathRecipientListener);
614     sptr<IAceAbilityHandler> aceAbilityHandler = new AceAbilityHandler();
615     window->SetAceAbilityHandler(aceAbilityHandler);
616     uint32_t modeSupportInfo;
617     startPos += GetObject<uint32_t>(modeSupportInfo, data + startPos, size - startPos);
618     window->SetRequestModeSupportInfo(modeSupportInfo);
619     float ratio;
620     startPos += GetObject<float>(ratio, data + startPos, size - startPos);
621     window->SetAspectRatio(ratio);
622     AvoidAreaType avoidAreaType = AvoidAreaType::TYPE_SYSTEM;
623     AvoidArea avoidArea;
624     startPos += GetObject<AvoidAreaType>(avoidAreaType, data + startPos, size - startPos);
625     startPos += GetObject<AvoidArea>(avoidArea, data + startPos, size - startPos);
626     window->GetAvoidAreaByType(avoidAreaType, avoidArea);
627     WindowGravity gravity = WindowGravity::WINDOW_GRAVITY_FLOAT;
628     uint32_t invalidGravityPercent = 0;
629     startPos += GetObject<WindowGravity>(gravity, data + startPos, size - startPos);
630     startPos += GetObject<uint32_t>(invalidGravityPercent, data + startPos, size - startPos);
631     window->SetWindowGravity(gravity, invalidGravityPercent);
632     sptr<IRemoteObject> targetToken;
633     window->BindDialogTarget(targetToken);
634     std::string color = "ff22ee44";
635     startPos += GetObject<std::string>(color, data + startPos, size - startPos);
636     window->SetShadowColor(color);
637 }
638 
CheckWindowImplFunctionsPart8(sptr<WindowImpl> window,const uint8_t * data,size_t size)639 void CheckWindowImplFunctionsPart8(sptr<WindowImpl> window, const uint8_t* data, size_t size)
640 {
641     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
642         return;
643     }
644     size_t startPos = 0;
645     std::vector<std::string> params{"-h"};
646     std::vector<std::string> info{""};
647     window->DumpInfo(params, info);
648     params.push_back("");
649     window->DumpInfo(params, info);
650 
651     auto keyEvent = MMI::KeyEvent::Create();
652     window->ConsumeKeyEvent(keyEvent);
653     keyEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_BACK);
654     keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
655     window->ConsumeKeyEvent(keyEvent);
656 
657     auto pointerEvent = MMI::PointerEvent::Create();
658     MMI::PointerEvent::PointerItem item;
659     int32_t pointerId = 0;
660     startPos += GetObject<int32_t>(pointerId, data + startPos, size - startPos);
661     pointerEvent->SetPointerId(pointerId);
662     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
663     window->ConsumePointerEvent(pointerEvent);
664     item.SetPointerId(pointerId);
665     item.SetDisplayX(15); // 15 : position x
666     item.SetDisplayY(15); // 15 : position y
667     pointerEvent->AddPointerItem(item);
668     window->ConsumePointerEvent(pointerEvent);
669     int32_t x = 5;
670     int32_t y = 5;
671     startPos += GetObject<int32_t>(x, data + startPos, size - startPos);
672     startPos += GetObject<int32_t>(y, data + startPos, size - startPos);
673     item.SetDisplayX(x); // 5 : position x
674     item.SetDisplayY(y); // 5 : position y
675     pointerEvent->UpdatePointerItem(pointerId, item);
676     window->ConsumePointerEvent(pointerEvent);
677 }
678 
CheckWindowImplFunctionsPart9(sptr<WindowImpl> window,const uint8_t * data,size_t size)679 void CheckWindowImplFunctionsPart9(sptr<WindowImpl> window, const uint8_t* data, size_t size)
680 {
681     if (window == nullptr || data == nullptr || size < DATA_MIN_SIZE) {
682         return;
683     }
684     std::shared_ptr<IInputEventConsumer> iInputEventConsumer = std::make_shared<IInputEventConsumer>();
685     window->SetInputEventConsumer(iInputEventConsumer);
686     std::shared_ptr<VsyncCallback> callback;
687     window->RequestVsync(callback);
688     std::shared_ptr<AppExecFwk::Configuration> configuration;
689     window->UpdateConfiguration(configuration);
690     sptr<IWindowLifeCycle> windowLifeCycleListener = new IWindowLifeCycle();
691     window->RegisterLifeCycleListener(windowLifeCycleListener);
692     window->UnregisterLifeCycleListener(windowLifeCycleListener);
693     sptr<IWindowChangeListener> windowChangeListener = new IWindowChangeListener();
694     window->RegisterWindowChangeListener(sptr<IWindowChangeListener>(windowChangeListener));
695     window->UnregisterWindowChangeListener(sptr<IWindowChangeListener>(windowChangeListener));
696     sptr<IAvoidAreaChangedListener> avoidAreaChangedListener = new IAvoidAreaChangedListener();
697     window->RegisterAvoidAreaChangeListener(avoidAreaChangedListener);
698     window->UnregisterAvoidAreaChangeListener(avoidAreaChangedListener);
699     sptr<IWindowDragListener> windowDragListener = new WindowDragListener();
700     window->RegisterDragListener(windowDragListener);
701     window->UnregisterDragListener(windowDragListener);
702     sptr<IDisplayMoveListener> displayMoveListener = new DisplayMoveListener();
703     window->RegisterDisplayMoveListener(displayMoveListener);
704     window->UnregisterDisplayMoveListener(displayMoveListener);
705     AAFwk::Want want;
706     window->OnNewWant(want);
707 }
708 
WindowImplFuzzTest(const uint8_t * data,size_t size)709 void WindowImplFuzzTest(const uint8_t* data, size_t size)
710 {
711     std::string name = "WindowFuzzTest";
712     sptr<OHOS::Rosen::WindowOption> option = new OHOS::Rosen::WindowOption();
713     option->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
714     sptr<OHOS::Rosen::WindowImpl> window = new(std::nothrow) OHOS::Rosen::WindowImpl(option);
715     if (window == nullptr) {
716         return;
717     }
718     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext =
719         std::make_shared<AbilityRuntime::AbilityContextImpl>();
720     window->Create(option->GetParentId(), abilityContext);
721 
722     size_t startPos = 0;
723     uint32_t reason = 0;
724     bool withAnimation = false;
725     startPos += GetObject(reason, data + startPos, size - startPos);
726     startPos += GetObject(withAnimation, data + startPos, size - startPos);
727     window->Show(reason, withAnimation);
728     std::shared_ptr<AbilityRuntime::Context> context = nullptr;
729     startPos += GetObject(context, data + startPos, size - startPos);
730     window->GetTopWindowWithContext(context);
731 
732     OHOS::CheckWindowImplFunctionsPart1(window, data, size);
733     OHOS::CheckWindowImplFunctionsPart2(window, data, size);
734     OHOS::CheckWindowImplFunctionsPart3(window, data, size);
735     OHOS::CheckWindowImplFunctionsPart4(window, data, size);
736     OHOS::CheckWindowImplFunctionsPart5(window, data, size);
737     OHOS::CheckWindowImplFunctionsPart6(window, data, size);
738     OHOS::CheckWindowImplFunctionsPart7(window, data, size);
739     OHOS::CheckWindowImplFunctionsPart8(window, data, size);
740     OHOS::CheckWindowImplFunctionsPart9(window, data, size);
741 
742     window->Hide(reason, withAnimation);
743     window->Destroy();
744 }
745 
WindowImplFuzzTest01(const uint8_t * data,size_t size)746 void WindowImplFuzzTest01(const uint8_t* data, size_t size)
747 {
748     std::string name = "WindowFuzzTest01";
749     sptr<OHOS::Rosen::WindowOption> option = new OHOS::Rosen::WindowOption();
750     sptr<OHOS::Rosen::WindowImpl> window = new(std::nothrow) OHOS::Rosen::WindowImpl(option);
751     if (window == nullptr) {
752         return;
753     }
754     sptr<IWindowUpdateListener> windowUpdateListener = new WindowUpdateListener();
755     std::vector<sptr<AccessibilityWindowInfo>> accessibilityWindowInfo;
756     WindowUpdateType type = Rosen::WindowUpdateType::WINDOW_UPDATE_ADDED;
757     windowUpdateListener->OnWindowUpdate(accessibilityWindowInfo, type);
758     std::string windowName = "test1";
759     window->Find(windowName);
760     size_t startPos = 0;
761     uint32_t mainWinId = 1;
762     startPos += GetObject<uint32_t>(mainWinId, data + startPos, size - startPos);
763     window->GetTopWindowWithId(mainWinId);
764     uint32_t parentId = 1;
765     startPos += GetObject<uint32_t>(parentId, data + startPos, size - startPos);
766     window->GetSubWindow(parentId);
767     std::shared_ptr<AppExecFwk::Configuration> configuration;
768     window->UpdateConfigurationForAll(configuration);
769     int32_t x;
770     int32_t y;
771     float scale;
772     startPos += GetObject<int32_t>(x, data + startPos, size - startPos);
773     startPos += GetObject<int32_t>(y, data + startPos, size - startPos);
774     startPos += GetObject<float>(scale, data + startPos, size - startPos);
775     WindowAccessibilityController::GetInstance().SetAnchorAndScale(x, y, scale);
776     int32_t deltaX;
777     int32_t deltaY;
778     startPos += GetObject<int32_t>(deltaX, data + startPos, size - startPos);
779     startPos += GetObject<int32_t>(deltaY, data + startPos, size - startPos);
780     WindowAccessibilityController::GetInstance().SetAnchorOffset(deltaX, deltaY);
781     sptr<WindowExtensionConnection> connection = new(std::nothrow)WindowExtensionConnection();
782     if (connection == nullptr) {
783         return;
784     }
785     AppExecFwk::ElementName element;
786     element.SetBundleName("com.test.windowextension");
787     element.SetAbilityName("WindowExtAbility");
788     Rosen::Rect rect {100, 100, 60, 60};
789     uint32_t uid = 100;
790     uint32_t windowId = INVALID_WINDOW_ID;
791     startPos += GetObject<uint32_t>(uid, data + startPos, size - startPos);
792     startPos += GetObject<uint32_t>(windowId, data + startPos, size - startPos);
793     connection->ConnectExtension(element, rect, uid, windowId, nullptr);
794     connection->SetBounds(rect);
795 }
796 } // namespace.OHOS
797 
798 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)799 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
800 {
801     /* Run your code on data */
802     OHOS::DoSomethingInterestingWithMyAPI(data, size);
803     OHOS::WindowImplFuzzTest(data, size);
804     OHOS::WindowImplFuzzTest01(data, size);
805     return 0;
806 }