Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 21 | #include "../FakeWindowHandle.h" |
| 22 | #include "FuzzedInputStream.h" |
| 23 | #include "dispatcher/InputDispatcher.h" |
| 24 | #include "input/InputVerifier.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | using android::base::Result; |
| 29 | using android::gui::WindowInfo; |
| 30 | |
| 31 | namespace inputdispatcher { |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | static constexpr int32_t MAX_RANDOM_DISPLAYS = 4; |
| 36 | static constexpr int32_t MAX_RANDOM_WINDOWS = 4; |
| 37 | |
| 38 | /** |
| 39 | * Provide a valid motion stream, to make the fuzzer more effective. |
| 40 | */ |
| 41 | class NotifyStreamProvider { |
| 42 | public: |
| 43 | NotifyStreamProvider(FuzzedDataProvider& fdp) |
Siarhei Vishniakou | 119604e | 2023-12-06 14:22:35 -0800 | [diff] [blame] | 44 | : mFdp(fdp), mIdGenerator(IdGenerator::Source::OTHER) {} |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 45 | |
| 46 | std::optional<NotifyMotionArgs> nextMotion() { |
| 47 | NotifyMotionArgs args = generateFuzzedMotionArgs(mIdGenerator, mFdp, MAX_RANDOM_DISPLAYS); |
Siarhei Vishniakou | 119604e | 2023-12-06 14:22:35 -0800 | [diff] [blame] | 48 | auto [it, _] = mVerifiers.emplace(args.displayId, "Fuzz Verifier"); |
| 49 | InputVerifier& verifier = it->second; |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 50 | const Result<void> result = |
Siarhei Vishniakou | 119604e | 2023-12-06 14:22:35 -0800 | [diff] [blame] | 51 | verifier.processMovement(args.deviceId, args.source, args.action, |
| 52 | args.getPointerCount(), args.pointerProperties.data(), |
| 53 | args.pointerCoords.data(), args.flags); |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 54 | if (result.ok()) { |
| 55 | return args; |
| 56 | } |
| 57 | return {}; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | FuzzedDataProvider& mFdp; |
| 62 | |
| 63 | IdGenerator mIdGenerator; |
| 64 | |
Siarhei Vishniakou | 119604e | 2023-12-06 14:22:35 -0800 | [diff] [blame] | 65 | std::map<int32_t /*displayId*/, InputVerifier> mVerifiers; |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | } // namespace |
| 69 | |
| 70 | sp<FakeWindowHandle> generateFuzzedWindow(FuzzedDataProvider& fdp, InputDispatcher& dispatcher, |
| 71 | int32_t displayId) { |
| 72 | static size_t windowNumber = 0; |
| 73 | std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); |
| 74 | std::string windowName = android::base::StringPrintf("Win") + std::to_string(windowNumber++); |
| 75 | sp<FakeWindowHandle> window = |
| 76 | sp<FakeWindowHandle>::make(application, dispatcher, "Fake", displayId); |
| 77 | |
| 78 | const int32_t left = fdp.ConsumeIntegralInRange<int32_t>(0, 100); |
| 79 | const int32_t top = fdp.ConsumeIntegralInRange<int32_t>(0, 100); |
| 80 | const int32_t width = fdp.ConsumeIntegralInRange<int32_t>(0, 100); |
| 81 | const int32_t height = fdp.ConsumeIntegralInRange<int32_t>(0, 100); |
| 82 | |
| 83 | window->setFrame(Rect(left, top, left + width, top + height)); |
| 84 | window->setSlippery(fdp.ConsumeBool()); |
| 85 | window->setDupTouchToWallpaper(fdp.ConsumeBool()); |
| 86 | window->setTrustedOverlay(fdp.ConsumeBool()); |
| 87 | return window; |
| 88 | } |
| 89 | |
| 90 | void randomizeWindows( |
| 91 | std::unordered_map<int32_t, std::vector<sp<FakeWindowHandle>>>& windowsPerDisplay, |
| 92 | FuzzedDataProvider& fdp, InputDispatcher& dispatcher) { |
| 93 | const int32_t displayId = fdp.ConsumeIntegralInRange<int32_t>(0, MAX_RANDOM_DISPLAYS - 1); |
| 94 | std::vector<sp<FakeWindowHandle>>& windows = windowsPerDisplay[displayId]; |
| 95 | |
| 96 | fdp.PickValueInArray<std::function<void()>>({ |
| 97 | // Add a new window |
| 98 | [&]() -> void { |
| 99 | if (windows.size() < MAX_RANDOM_WINDOWS) { |
| 100 | windows.push_back(generateFuzzedWindow(fdp, dispatcher, displayId)); |
| 101 | } |
| 102 | }, |
| 103 | // Remove a window |
| 104 | [&]() -> void { |
| 105 | if (windows.empty()) { |
| 106 | return; |
| 107 | } |
| 108 | const int32_t erasedPosition = |
| 109 | fdp.ConsumeIntegralInRange<int32_t>(0, windows.size() - 1); |
| 110 | |
| 111 | windows.erase(windows.begin() + erasedPosition); |
| 112 | if (windows.empty()) { |
| 113 | windowsPerDisplay.erase(displayId); |
| 114 | } |
| 115 | }, |
| 116 | // Could also clone a window, change flags, reposition, etc... |
| 117 | })(); |
| 118 | } |
| 119 | |
| 120 | extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) { |
| 121 | FuzzedDataProvider fdp(data, size); |
| 122 | NotifyStreamProvider streamProvider(fdp); |
| 123 | |
| 124 | FakeInputDispatcherPolicy fakePolicy; |
| 125 | InputDispatcher dispatcher(fakePolicy); |
| 126 | dispatcher.setInputDispatchMode(/*enabled=*/true, /*frozen=*/false); |
| 127 | // Start InputDispatcher thread |
| 128 | dispatcher.start(); |
| 129 | |
| 130 | std::unordered_map<int32_t, std::vector<sp<FakeWindowHandle>>> windowsPerDisplay; |
| 131 | |
| 132 | // Randomly invoke InputDispatcher api's until randomness is exhausted. |
| 133 | while (fdp.remaining_bytes() > 0) { |
| 134 | fdp.PickValueInArray<std::function<void()>>({ |
| 135 | [&]() -> void { |
| 136 | std::optional<NotifyMotionArgs> motion = streamProvider.nextMotion(); |
| 137 | if (motion) { |
| 138 | dispatcher.notifyMotion(*motion); |
| 139 | } |
| 140 | }, |
| 141 | [&]() -> void { |
| 142 | // Scramble the windows we currently have |
| 143 | randomizeWindows(/*byref*/ windowsPerDisplay, fdp, dispatcher); |
| 144 | |
| 145 | std::vector<WindowInfo> windowInfos; |
| 146 | for (const auto& [displayId, windows] : windowsPerDisplay) { |
| 147 | for (const sp<FakeWindowHandle>& window : windows) { |
| 148 | windowInfos.emplace_back(*window->getInfo()); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | dispatcher.onWindowInfosChanged( |
| 153 | {windowInfos, {}, /*vsyncId=*/0, /*timestamp=*/0}); |
| 154 | }, |
| 155 | // Consume on all the windows |
| 156 | [&]() -> void { |
| 157 | for (const auto& [_, windows] : windowsPerDisplay) { |
| 158 | for (const sp<FakeWindowHandle>& window : windows) { |
| 159 | // To speed up the fuzzing, don't wait for consumption. If there's an |
| 160 | // event pending, this can be consumed on the next call instead. |
| 161 | // We also don't care about whether consumption succeeds here, or what |
| 162 | // kind of event is returned. |
| 163 | window->consume(0ms); |
| 164 | } |
| 165 | } |
| 166 | }, |
| 167 | })(); |
| 168 | } |
| 169 | |
| 170 | dispatcher.stop(); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | } // namespace inputdispatcher |
| 176 | |
| 177 | } // namespace android |