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 | |
| 17 | #include <FuzzContainer.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 | |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 26 | static void addProperty(FuzzContainer& fuzzer, 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()>>( |
| 29 | {[&]() -> void { fuzzer.addProperty("touch.deviceType", "touchScreen"); }, |
| 30 | [&]() -> void { |
| 31 | fuzzer.addProperty("touch.deviceType", fdp->ConsumeRandomLengthString(8).data()); |
| 32 | }, |
| 33 | [&]() -> void { |
| 34 | fuzzer.addProperty("touch.size.scale", fdp->ConsumeRandomLengthString(8).data()); |
| 35 | }, |
| 36 | [&]() -> void { |
| 37 | fuzzer.addProperty("touch.size.bias", fdp->ConsumeRandomLengthString(8).data()); |
| 38 | }, |
| 39 | [&]() -> void { |
| 40 | fuzzer.addProperty("touch.size.isSummed", |
| 41 | fdp->ConsumeRandomLengthString(8).data()); |
| 42 | }, |
| 43 | [&]() -> void { |
| 44 | fuzzer.addProperty("touch.size.calibration", |
| 45 | fdp->ConsumeRandomLengthString(8).data()); |
| 46 | }, |
| 47 | [&]() -> void { |
| 48 | fuzzer.addProperty("touch.pressure.scale", |
| 49 | fdp->ConsumeRandomLengthString(8).data()); |
| 50 | }, |
| 51 | [&]() -> void { |
| 52 | fuzzer.addProperty("touch.size.calibration", |
| 53 | fdp->ConsumeBool() ? "diameter" : "area"); |
| 54 | }, |
| 55 | [&]() -> void { |
| 56 | fuzzer.addProperty("touch.pressure.calibration", |
| 57 | fdp->ConsumeRandomLengthString(8).data()); |
| 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); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 64 | FuzzContainer fuzzer(fdp); |
| 65 | |
Harry Cutts | e36d835 | 2023-07-10 13:28:45 +0000 | [diff] [blame] | 66 | InputReaderConfiguration policyConfig; |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 67 | MultiTouchInputMapper& mapper = fuzzer.getMapper<MultiTouchInputMapper>(policyConfig); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 68 | |
| 69 | // Loop through mapper operations until randomness is exhausted. |
| 70 | while (fdp->remaining_bytes() > 0) { |
| 71 | fdp->PickValueInArray<std::function<void()>>({ |
| 72 | [&]() -> void { addProperty(fuzzer, fdp); }, |
| 73 | [&]() -> void { |
| 74 | std::string dump; |
| 75 | mapper.dump(dump); |
| 76 | }, |
| 77 | [&]() -> void { |
| 78 | InputDeviceInfo info; |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 79 | mapper.populateDeviceInfo(info); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 80 | }, |
| 81 | [&]() -> void { mapper.getSources(); }, |
| 82 | [&]() -> void { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 83 | std::list<NotifyArgs> unused = |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 84 | mapper.reconfigure(fdp->ConsumeIntegral<nsecs_t>(), policyConfig, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 85 | InputReaderConfiguration::Change( |
| 86 | fdp->ConsumeIntegral<uint32_t>())); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 87 | }, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 88 | [&]() -> void { |
| 89 | std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>()); |
| 90 | }, |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 91 | [&]() -> void { |
Harry Cutts | 656e0ad | 2023-06-16 16:39:55 +0000 | [diff] [blame] | 92 | RawEvent rawEvent = getFuzzedRawEvent(*fdp); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 93 | std::list<NotifyArgs> unused = mapper.process(&rawEvent); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 94 | }, |
| 95 | [&]() -> void { |
| 96 | mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(), |
| 97 | fdp->ConsumeIntegral<int32_t>()); |
| 98 | }, |
| 99 | [&]() -> void { |
| 100 | mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(), |
| 101 | fdp->ConsumeIntegral<int32_t>()); |
| 102 | }, |
| 103 | [&]() -> void { |
| 104 | std::vector<int32_t> keyCodes; |
| 105 | int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes); |
| 106 | for (int32_t i = 0; i < numBytes; ++i) { |
| 107 | keyCodes.push_back(fdp->ConsumeIntegral<int32_t>()); |
| 108 | } |
| 109 | mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes, |
| 110 | nullptr); |
| 111 | }, |
| 112 | [&]() -> void { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 113 | std::list<NotifyArgs> unused = |
| 114 | mapper.cancelTouch(fdp->ConsumeIntegral<nsecs_t>(), |
| 115 | fdp->ConsumeIntegral<nsecs_t>()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 116 | }, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 117 | [&]() -> void { |
| 118 | std::list<NotifyArgs> unused = |
| 119 | mapper.timeoutExpired(fdp->ConsumeIntegral<nsecs_t>()); |
| 120 | }, |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 121 | [&]() -> void { |
| 122 | StylusState state{fdp->ConsumeIntegral<nsecs_t>(), |
| 123 | fdp->ConsumeFloatingPoint<float>(), |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 124 | fdp->ConsumeIntegral<uint32_t>(), getFuzzedToolType(*fdp)}; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 125 | std::list<NotifyArgs> unused = mapper.updateExternalStylusState(state); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 126 | }, |
| 127 | [&]() -> void { mapper.getAssociatedDisplayId(); }, |
| 128 | })(); |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | } // namespace android |