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