1 /*
2 * Copyright (c) 2024 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 <cstddef>
17 #include <cstdint>
18 #include <parcel.h>
19 #include <securec.h>
20
21 #include <iremote_stub.h>
22 #include "message_option.h"
23 #include "message_parcel.h"
24 #include "marshalling_helper.h"
25 #include "window.h"
26 #include "window_agent.h"
27 #include "window_impl.h"
28 #include "window_manager.h"
29 #include "windowstubnotify_fuzzer.h"
30
31 using namespace OHOS::Rosen;
32
33 namespace OHOS {
34 namespace {
35 constexpr size_t DATA_MIN_SIZE = 2;
36 }
37
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)38 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
39 {
40 if (data == nullptr || size < DATA_MIN_SIZE) {
41 return false;
42 }
43
44 MessageParcel parcel;
45 MessageParcel reply;
46 MessageOption option;
47
48 parcel.WriteInterfaceToken(WindowStub::GetDescriptor());
49 parcel.WriteBuffer(data, size);
50
51 sptr<WindowOption> windowOption = new(std::nothrow)WindowOption();
52 if (windowOption == nullptr) {
53 return false;
54 }
55 sptr<WindowImpl> window = new(std::nothrow)WindowImpl(windowOption);
56 if (window == nullptr) {
57 return false;
58 }
59 sptr<WindowAgent> windowStub = new(std::nothrow)WindowAgent(window);
60 if (windowStub == nullptr) {
61 return false;
62 }
63
64 parcel.RewindRead(0);
65 windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_NOTIFY_OUTSIDE_PRESSED),
66 parcel, reply, option);
67 parcel.RewindRead(0);
68 windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_NOTIFY_SCREEN_SHOT),
69 parcel, reply, option);
70 parcel.RewindRead(0);
71 windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_NOTIFY_DESTROY),
72 parcel, reply, option);
73 parcel.RewindRead(0);
74 windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_NOTIFY_FOREGROUND),
75 parcel, reply, option);
76 parcel.RewindRead(0);
77 windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_NOTIFY_BACKGROUND),
78 parcel, reply, option);
79 parcel.RewindRead(0);
80 windowStub->OnRemoteRequest(static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_NOTIFY_CLIENT_POINT_UP),
81 parcel, reply, option);
82 parcel.RewindRead(0);
83 windowStub->OnRemoteRequest(
84 static_cast<uint32_t>(IWindow::WindowMessage::TRANS_ID_NOTIFY_FOREGROUND_INTERACTIVE_STATUS),
85 parcel, reply, option);
86 return true;
87 }
88 } // namespace.OHOS
89
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93 /* Run your code on data */
94 OHOS::DoSomethingInterestingWithMyAPI(data, size);
95 return 0;
96 }