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