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