blob: 7335fb75009e5fa102f09df87134cfd6bc890448 [file] [log] [blame]
Siarhei Vishniakou2defec02023-06-08 17:24:44 -07001/*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <android-base/stringprintf.h>
18#include <fuzzer/FuzzedDataProvider.h>
19#include "../FakeApplicationHandle.h"
20#include "../FakeInputDispatcherPolicy.h"
Prabir Pradhanc5340732024-03-20 22:53:52 +000021#include "../FakeWindows.h"
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070022#include "FuzzedInputStream.h"
23#include "dispatcher/InputDispatcher.h"
24#include "input/InputVerifier.h"
25
26namespace android {
27
28using android::base::Result;
29using android::gui::WindowInfo;
30
31namespace inputdispatcher {
32
33namespace {
34
35static constexpr int32_t MAX_RANDOM_DISPLAYS = 4;
36static constexpr int32_t MAX_RANDOM_WINDOWS = 4;
37
38/**
39 * Provide a valid motion stream, to make the fuzzer more effective.
40 */
41class NotifyStreamProvider {
42public:
43 NotifyStreamProvider(FuzzedDataProvider& fdp)
Siarhei Vishniakou119604e2023-12-06 14:22:35 -080044 : mFdp(fdp), mIdGenerator(IdGenerator::Source::OTHER) {}
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070045
46 std::optional<NotifyMotionArgs> nextMotion() {
47 NotifyMotionArgs args = generateFuzzedMotionArgs(mIdGenerator, mFdp, MAX_RANDOM_DISPLAYS);
Siarhei Vishniakou119604e2023-12-06 14:22:35 -080048 auto [it, _] = mVerifiers.emplace(args.displayId, "Fuzz Verifier");
49 InputVerifier& verifier = it->second;
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070050 const Result<void> result =
Siarhei Vishniakou119604e2023-12-06 14:22:35 -080051 verifier.processMovement(args.deviceId, args.source, args.action,
52 args.getPointerCount(), args.pointerProperties.data(),
53 args.pointerCoords.data(), args.flags);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070054 if (result.ok()) {
55 return args;
56 }
57 return {};
58 }
59
60private:
61 FuzzedDataProvider& mFdp;
62
63 IdGenerator mIdGenerator;
64
Siarhei Vishniakou119604e2023-12-06 14:22:35 -080065 std::map<int32_t /*displayId*/, InputVerifier> mVerifiers;
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070066};
67
Siarhei Vishniakouf1930a12023-11-16 10:28:05 -080068void scrambleWindow(FuzzedDataProvider& fdp, FakeWindowHandle& window) {
69 const int32_t left = fdp.ConsumeIntegralInRange<int32_t>(0, 100);
70 const int32_t top = fdp.ConsumeIntegralInRange<int32_t>(0, 100);
71 const int32_t width = fdp.ConsumeIntegralInRange<int32_t>(0, 100);
72 const int32_t height = fdp.ConsumeIntegralInRange<int32_t>(0, 100);
73
74 window.setFrame(Rect(left, top, left + width, top + height));
75 window.setSlippery(fdp.ConsumeBool());
76 window.setDupTouchToWallpaper(fdp.ConsumeBool());
77 window.setIsWallpaper(fdp.ConsumeBool());
78 window.setVisible(fdp.ConsumeBool());
79 window.setPreventSplitting(fdp.ConsumeBool());
80 const bool isTrustedOverlay = fdp.ConsumeBool();
81 window.setTrustedOverlay(isTrustedOverlay);
82 if (isTrustedOverlay) {
83 window.setSpy(fdp.ConsumeBool());
84 } else {
85 window.setSpy(false);
86 }
87}
88
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070089} // namespace
90
Prabir Pradhanc5340732024-03-20 22:53:52 +000091sp<FakeWindowHandle> generateFuzzedWindow(FuzzedDataProvider& fdp,
92 std::unique_ptr<InputDispatcher>& dispatcher,
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070093 int32_t displayId) {
94 static size_t windowNumber = 0;
95 std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
96 std::string windowName = android::base::StringPrintf("Win") + std::to_string(windowNumber++);
97 sp<FakeWindowHandle> window =
Siarhei Vishniakouf1930a12023-11-16 10:28:05 -080098 sp<FakeWindowHandle>::make(application, dispatcher, windowName, displayId);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070099
Siarhei Vishniakouf1930a12023-11-16 10:28:05 -0800100 scrambleWindow(fdp, *window);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700101 return window;
102}
103
104void randomizeWindows(
105 std::unordered_map<int32_t, std::vector<sp<FakeWindowHandle>>>& windowsPerDisplay,
Prabir Pradhanc5340732024-03-20 22:53:52 +0000106 FuzzedDataProvider& fdp, std::unique_ptr<InputDispatcher>& dispatcher) {
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700107 const int32_t displayId = fdp.ConsumeIntegralInRange<int32_t>(0, MAX_RANDOM_DISPLAYS - 1);
108 std::vector<sp<FakeWindowHandle>>& windows = windowsPerDisplay[displayId];
109
110 fdp.PickValueInArray<std::function<void()>>({
111 // Add a new window
112 [&]() -> void {
113 if (windows.size() < MAX_RANDOM_WINDOWS) {
114 windows.push_back(generateFuzzedWindow(fdp, dispatcher, displayId));
115 }
116 },
117 // Remove a window
118 [&]() -> void {
119 if (windows.empty()) {
120 return;
121 }
122 const int32_t erasedPosition =
123 fdp.ConsumeIntegralInRange<int32_t>(0, windows.size() - 1);
124
125 windows.erase(windows.begin() + erasedPosition);
126 if (windows.empty()) {
127 windowsPerDisplay.erase(displayId);
128 }
129 },
Siarhei Vishniakouf1930a12023-11-16 10:28:05 -0800130 // Change flags or move some of the existing windows
131 [&]() -> void {
132 for (auto& window : windows) {
133 if (fdp.ConsumeBool()) {
134 scrambleWindow(fdp, *window);
135 }
136 }
137 },
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700138 })();
139}
140
141extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
142 FuzzedDataProvider fdp(data, size);
143 NotifyStreamProvider streamProvider(fdp);
144
145 FakeInputDispatcherPolicy fakePolicy;
Prabir Pradhanc5340732024-03-20 22:53:52 +0000146 auto dispatcher = std::make_unique<InputDispatcher>(fakePolicy);
147 dispatcher->setInputDispatchMode(/*enabled=*/true, /*frozen=*/false);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700148 // Start InputDispatcher thread
Prabir Pradhanc5340732024-03-20 22:53:52 +0000149 dispatcher->start();
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700150
151 std::unordered_map<int32_t, std::vector<sp<FakeWindowHandle>>> windowsPerDisplay;
152
153 // Randomly invoke InputDispatcher api's until randomness is exhausted.
154 while (fdp.remaining_bytes() > 0) {
155 fdp.PickValueInArray<std::function<void()>>({
156 [&]() -> void {
157 std::optional<NotifyMotionArgs> motion = streamProvider.nextMotion();
158 if (motion) {
Prabir Pradhanc5340732024-03-20 22:53:52 +0000159 dispatcher->notifyMotion(*motion);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700160 }
161 },
162 [&]() -> void {
163 // Scramble the windows we currently have
164 randomizeWindows(/*byref*/ windowsPerDisplay, fdp, dispatcher);
165
166 std::vector<WindowInfo> windowInfos;
167 for (const auto& [displayId, windows] : windowsPerDisplay) {
168 for (const sp<FakeWindowHandle>& window : windows) {
169 windowInfos.emplace_back(*window->getInfo());
170 }
171 }
172
Prabir Pradhanc5340732024-03-20 22:53:52 +0000173 dispatcher->onWindowInfosChanged(
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700174 {windowInfos, {}, /*vsyncId=*/0, /*timestamp=*/0});
175 },
176 // Consume on all the windows
177 [&]() -> void {
178 for (const auto& [_, windows] : windowsPerDisplay) {
179 for (const sp<FakeWindowHandle>& window : windows) {
180 // To speed up the fuzzing, don't wait for consumption. If there's an
181 // event pending, this can be consumed on the next call instead.
182 // We also don't care about whether consumption succeeds here, or what
183 // kind of event is returned.
184 window->consume(0ms);
185 }
186 }
187 },
188 })();
189 }
190
Prabir Pradhanc5340732024-03-20 22:53:52 +0000191 dispatcher->stop();
Siarhei Vishniakou2defec02023-06-08 17:24:44 -0700192
193 return 0;
194}
195
196} // namespace inputdispatcher
197
198} // namespace android