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 "windowscene_fuzzer.h"
17
18 #include <securec.h>
19
20 #include "display_manager.h"
21 #include "window.h"
22 #include "window_manager.h"
23 #include "window_scene.h"
24
25 using namespace OHOS::Rosen;
26
27 namespace OHOS {
28 namespace {
29 constexpr size_t DATA_MIN_SIZE = 2;
30 constexpr char END_CHAR = '\0';
31 constexpr size_t LEN = 10;
32 }
33 class WindowLifeCycle : public IWindowLifeCycle {
34 public:
AfterForeground()35 virtual void AfterForeground() override
36 {
37 }
AfterBackground()38 virtual void AfterBackground() override
39 {
40 }
AfterFocused()41 virtual void AfterFocused() override
42 {
43 }
AfterUnfocused()44 virtual void AfterUnfocused() override
45 {
46 }
AfterActive()47 virtual void AfterActive() override
48 {
49 }
AfterInactive()50 virtual void AfterInactive() override
51 {
52 }
53 };
54
55 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)56 size_t GetObject(T &object, const uint8_t *data, size_t size)
57 {
58 size_t objectSize = sizeof(object);
59 if (objectSize > size) {
60 return 0;
61 }
62 return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
63 }
64
InitWindowOption1(WindowOption & windowOption,const uint8_t * data,size_t size)65 size_t InitWindowOption1(WindowOption &windowOption, const uint8_t *data, size_t size)
66 {
67 size_t startPos = 0;
68 Rect windowRect;
69 startPos += GetObject<Rect>(windowRect, data + startPos, size - startPos);
70 windowOption.SetWindowRect(windowRect);
71 uint32_t type;
72 startPos += GetObject<uint32_t>(type, data + startPos, size - startPos);
73 windowOption.SetWindowType(static_cast<WindowType>(type));
74 uint32_t mode;
75 startPos += GetObject<uint32_t>(mode, data + startPos, size - startPos);
76 windowOption.SetWindowMode(static_cast<WindowMode>(mode));
77 bool focusable;
78 startPos += GetObject<bool>(focusable, data + startPos, size - startPos);
79 windowOption.SetFocusable(focusable);
80 bool touchable;
81 startPos += GetObject<bool>(touchable, data + startPos, size - startPos);
82 windowOption.SetTouchable(touchable);
83 DisplayId displayId;
84 startPos += GetObject<DisplayId>(displayId, data + startPos, size - startPos);
85 windowOption.SetDisplayId(displayId);
86 uint32_t parentId;
87 startPos += GetObject<uint32_t>(parentId, data + startPos, size - startPos);
88 windowOption.SetParentId(parentId);
89 char name[LEN + 1];
90 name[LEN] = END_CHAR;
91 for (int i = 0; i < LEN; i++) {
92 startPos += GetObject<char>(name[i], data + startPos, size - startPos);
93 }
94 std::string windowName(name);
95 windowOption.SetWindowName(windowName);
96 return startPos;
97 }
98
InitWindowOption2(WindowOption & windowOption,const uint8_t * data,size_t size)99 size_t InitWindowOption2(WindowOption &windowOption, const uint8_t *data, size_t size)
100 {
101 size_t startPos = 0;
102 uint32_t flags;
103 startPos += GetObject<uint32_t>(flags, data + startPos, size - startPos);
104 windowOption.SetWindowFlags(flags);
105 PointInfo hitOffset;
106 startPos += GetObject<PointInfo>(hitOffset, data + startPos, size - startPos);
107 windowOption.SetHitOffset(hitOffset.x, hitOffset.y);
108 WindowTag windowTag;
109 startPos += GetObject<WindowTag>(windowTag, data + startPos, size - startPos);
110 windowOption.SetWindowTag(windowTag);
111 bool keepScreenOn;
112 startPos += GetObject<bool>(keepScreenOn, data + startPos, size - startPos);
113 windowOption.SetKeepScreenOn(keepScreenOn);
114 bool turnScreenOn;
115 startPos += GetObject<bool>(turnScreenOn, data + startPos, size - startPos);
116 windowOption.SetTurnScreenOn(turnScreenOn);
117 float brightness;
118 startPos += GetObject<float>(brightness, data + startPos, size - startPos);
119 windowOption.SetBrightness(brightness);
120 uint32_t callingWindow;
121 startPos += GetObject<uint32_t>(callingWindow, data + startPos, size - startPos);
122 windowOption.SetCallingWindow(callingWindow);
123 SystemBarProperty statusBarProperty;
124 SystemBarProperty navigationBarProperty;
125 startPos += GetObject<SystemBarProperty>(statusBarProperty, data + startPos, size - startPos);
126 startPos += GetObject<SystemBarProperty>(navigationBarProperty, data + startPos, size - startPos);
127 windowOption.SetSystemBarProperty(WindowType::WINDOW_TYPE_STATUS_BAR, statusBarProperty);
128 windowOption.SetSystemBarProperty(WindowType::WINDOW_TYPE_NAVIGATION_BAR, navigationBarProperty);
129 Orientation requestedOrientation;
130 startPos += GetObject<Orientation>(requestedOrientation, data + startPos, size - startPos);
131 windowOption.SetRequestedOrientation(requestedOrientation);
132 bool isMainHandleAvailable;
133 startPos += GetObject<bool>(isMainHandleAvailable, data + startPos, size - startPos);
134 windowOption.SetMainHandlerAvailable(isMainHandleAvailable);
135 return startPos;
136 }
137
InitWindowOption(WindowOption & windowOption,const uint8_t * data,size_t size)138 size_t InitWindowOption(WindowOption &windowOption, const uint8_t *data, size_t size)
139 {
140 size_t startPos = 0;
141 startPos += InitWindowOption1(windowOption, data + startPos, size - startPos);
142 startPos += InitWindowOption2(windowOption, data + startPos, size - startPos);
143 return startPos;
144 }
145
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)146 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
147 {
148 if (data == nullptr || size < DATA_MIN_SIZE) {
149 return false;
150 }
151 DisplayId displayId = DisplayManager::GetInstance().GetDefaultDisplayId();
152 sptr<WindowScene> windowScene = new WindowScene();
153 sptr<IWindowLifeCycle> listener = new WindowLifeCycle();
154 sptr<WindowOption> option = new WindowOption();
155 size_t startPos = 0;
156 startPos += InitWindowOption(*option, data, size);
157 windowScene->Init(displayId, nullptr, listener, option);
158 char name[LEN + 1];
159 name[LEN] = END_CHAR;
160 for (int i = 0; i < LEN; i++) {
161 startPos += GetObject<char>(name[i], data + startPos, size - startPos);
162 }
163 std::string windowName(name);
164 sptr<WindowOption> windowOption = new WindowOption();
165 startPos += InitWindowOption(*windowOption, data + startPos, size - startPos);
166 sptr<Window> window = windowScene->CreateWindow(windowName, windowOption);
167 uint32_t reason;
168 startPos += GetObject<uint32_t>(reason, data + startPos, size - startPos);
169 windowScene->GoForeground(reason);
170 SystemBarProperty systemBarProperty;
171 WindowType type;
172 startPos += GetObject<SystemBarProperty>(systemBarProperty, data + startPos, size - startPos);
173 startPos += GetObject<WindowType>(type, data + startPos, size - startPos);
174 windowScene->SetSystemBarProperty(type, systemBarProperty);
175 GetObject<uint32_t>(reason, data + startPos, size - startPos);
176 windowScene->GoBackground(reason);
177 if (window != nullptr) {
178 window->Destroy();
179 }
180 windowScene->GoDestroy();
181 return true;
182 }
183 } // namespace.OHOS
184
185 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)186 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
187 {
188 /* Run your code on data */
189 OHOS::DoSomethingInterestingWithMyAPI(data, size);
190 return 0;
191 }
192
193