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