Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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 | |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 17 | #include <InputDevice.h> |
Harry Cutts | e36d835 | 2023-07-10 13:28:45 +0000 | [diff] [blame] | 18 | #include <InputReaderBase.h> |
Harry Cutts | 656e0ad | 2023-06-16 16:39:55 +0000 | [diff] [blame] | 19 | #include <MapperHelpers.h> |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 20 | #include <MultiTouchInputMapper.h> |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | const int32_t kMaxKeycodes = 100; |
| 25 | |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 26 | static void addProperty(FuzzEventHub& eventHub, std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) { |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 27 | // Pick a random property to set for the mapper to have set. |
| 28 | fdp->PickValueInArray<std::function<void()>>( |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 29 | {[&]() -> void { eventHub.addProperty("touch.deviceType", "touchScreen"); }, |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 30 | [&]() -> void { |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 31 | eventHub.addProperty("touch.deviceType", fdp->ConsumeRandomLengthString(8).data()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 32 | }, |
| 33 | [&]() -> void { |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 34 | eventHub.addProperty("touch.size.scale", fdp->ConsumeRandomLengthString(8).data()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 35 | }, |
| 36 | [&]() -> void { |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 37 | eventHub.addProperty("touch.size.bias", fdp->ConsumeRandomLengthString(8).data()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 38 | }, |
| 39 | [&]() -> void { |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 40 | eventHub.addProperty("touch.size.isSummed", |
| 41 | fdp->ConsumeRandomLengthString(8).data()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 42 | }, |
| 43 | [&]() -> void { |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 44 | eventHub.addProperty("touch.size.calibration", |
| 45 | fdp->ConsumeRandomLengthString(8).data()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 46 | }, |
| 47 | [&]() -> void { |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 48 | eventHub.addProperty("touch.pressure.scale", |
| 49 | fdp->ConsumeRandomLengthString(8).data()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 50 | }, |
| 51 | [&]() -> void { |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 52 | eventHub.addProperty("touch.size.calibration", |
| 53 | fdp->ConsumeBool() ? "diameter" : "area"); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 54 | }, |
| 55 | [&]() -> void { |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 56 | eventHub.addProperty("touch.pressure.calibration", |
| 57 | fdp->ConsumeRandomLengthString(8).data()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 58 | }})(); |
| 59 | } |
| 60 | |
| 61 | extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) { |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 62 | std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp = |
| 63 | std::make_shared<ThreadSafeFuzzedDataProvider>(data, size); |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 64 | |
| 65 | // Create mocked objects to support the fuzzed input mapper. |
| 66 | std::shared_ptr<FuzzEventHub> eventHub = std::make_shared<FuzzEventHub>(fdp); |
| 67 | FuzzInputReaderContext context(eventHub, fdp); |
| 68 | InputDevice device = getFuzzedInputDevice(*fdp, &context); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 69 | |
Harry Cutts | e36d835 | 2023-07-10 13:28:45 +0000 | [diff] [blame] | 70 | InputReaderConfiguration policyConfig; |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 71 | MultiTouchInputMapper& mapper = |
| 72 | getMapperForDevice<ThreadSafeFuzzedDataProvider, MultiTouchInputMapper>(*fdp.get(), |
| 73 | device, |
| 74 | policyConfig); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 75 | |
| 76 | // Loop through mapper operations until randomness is exhausted. |
| 77 | while (fdp->remaining_bytes() > 0) { |
| 78 | fdp->PickValueInArray<std::function<void()>>({ |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 79 | [&]() -> void { |
| 80 | addProperty(*eventHub.get(), fdp); |
| 81 | configureAndResetDevice(*fdp, device); |
| 82 | }, |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 83 | [&]() -> void { |
| 84 | std::string dump; |
| 85 | mapper.dump(dump); |
| 86 | }, |
| 87 | [&]() -> void { |
| 88 | InputDeviceInfo info; |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 89 | mapper.populateDeviceInfo(info); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 90 | }, |
| 91 | [&]() -> void { mapper.getSources(); }, |
| 92 | [&]() -> void { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 93 | std::list<NotifyArgs> unused = |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 94 | mapper.reconfigure(fdp->ConsumeIntegral<nsecs_t>(), policyConfig, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 95 | InputReaderConfiguration::Change( |
| 96 | fdp->ConsumeIntegral<uint32_t>())); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 97 | }, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 98 | [&]() -> void { |
| 99 | std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>()); |
| 100 | }, |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 101 | [&]() -> void { |
Harry Cutts | 656e0ad | 2023-06-16 16:39:55 +0000 | [diff] [blame] | 102 | RawEvent rawEvent = getFuzzedRawEvent(*fdp); |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 103 | std::list<NotifyArgs> unused = mapper.process(rawEvent); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 104 | }, |
| 105 | [&]() -> void { |
| 106 | mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(), |
| 107 | fdp->ConsumeIntegral<int32_t>()); |
| 108 | }, |
| 109 | [&]() -> void { |
| 110 | mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(), |
| 111 | fdp->ConsumeIntegral<int32_t>()); |
| 112 | }, |
| 113 | [&]() -> void { |
| 114 | std::vector<int32_t> keyCodes; |
| 115 | int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes); |
| 116 | for (int32_t i = 0; i < numBytes; ++i) { |
| 117 | keyCodes.push_back(fdp->ConsumeIntegral<int32_t>()); |
| 118 | } |
| 119 | mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes, |
| 120 | nullptr); |
| 121 | }, |
| 122 | [&]() -> void { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 123 | std::list<NotifyArgs> unused = |
| 124 | mapper.cancelTouch(fdp->ConsumeIntegral<nsecs_t>(), |
| 125 | fdp->ConsumeIntegral<nsecs_t>()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 126 | }, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 127 | [&]() -> void { |
| 128 | std::list<NotifyArgs> unused = |
| 129 | mapper.timeoutExpired(fdp->ConsumeIntegral<nsecs_t>()); |
| 130 | }, |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 131 | [&]() -> void { |
| 132 | StylusState state{fdp->ConsumeIntegral<nsecs_t>(), |
| 133 | fdp->ConsumeFloatingPoint<float>(), |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 134 | fdp->ConsumeIntegral<uint32_t>(), getFuzzedToolType(*fdp)}; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 135 | std::list<NotifyArgs> unused = mapper.updateExternalStylusState(state); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 136 | }, |
| 137 | [&]() -> void { mapper.getAssociatedDisplayId(); }, |
| 138 | })(); |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | } // namespace android |