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