| 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 |  | 
|  | 64 | MultiTouchInputMapper& mapper = fuzzer.getMapper<MultiTouchInputMapper>(); | 
|  | 65 | auto policyConfig = fuzzer.getPolicyConfig(); | 
|  | 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; | 
|  | 77 | mapper.populateDeviceInfo(&info); | 
|  | 78 | }, | 
|  | 79 | [&]() -> void { mapper.getSources(); }, | 
|  | 80 | [&]() -> void { | 
| Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 81 | std::list<NotifyArgs> unused = | 
|  | 82 | mapper.configure(fdp->ConsumeIntegral<nsecs_t>(), &policyConfig, | 
|  | 83 | fdp->ConsumeIntegral<uint32_t>()); | 
| Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 84 | }, | 
| Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 85 | [&]() -> void { | 
|  | 86 | std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>()); | 
|  | 87 | }, | 
| Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 88 | [&]() -> void { | 
|  | 89 | int32_t type = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidTypes) | 
|  | 90 | : fdp->ConsumeIntegral<int32_t>(); | 
|  | 91 | int32_t code = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidCodes) | 
|  | 92 | : fdp->ConsumeIntegral<int32_t>(); | 
|  | 93 | RawEvent rawEvent{fdp->ConsumeIntegral<nsecs_t>(), | 
|  | 94 | fdp->ConsumeIntegral<nsecs_t>(), | 
|  | 95 | fdp->ConsumeIntegral<int32_t>(), | 
|  | 96 | type, | 
|  | 97 | code, | 
|  | 98 | fdp->ConsumeIntegral<int32_t>()}; | 
| Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 99 | std::list<NotifyArgs> unused = mapper.process(&rawEvent); | 
| Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 100 | }, | 
|  | 101 | [&]() -> void { | 
|  | 102 | mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(), | 
|  | 103 | fdp->ConsumeIntegral<int32_t>()); | 
|  | 104 | }, | 
|  | 105 | [&]() -> void { | 
|  | 106 | mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(), | 
|  | 107 | fdp->ConsumeIntegral<int32_t>()); | 
|  | 108 | }, | 
|  | 109 | [&]() -> void { | 
|  | 110 | std::vector<int32_t> keyCodes; | 
|  | 111 | int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes); | 
|  | 112 | for (int32_t i = 0; i < numBytes; ++i) { | 
|  | 113 | keyCodes.push_back(fdp->ConsumeIntegral<int32_t>()); | 
|  | 114 | } | 
|  | 115 | mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes, | 
|  | 116 | nullptr); | 
|  | 117 | }, | 
|  | 118 | [&]() -> void { | 
| Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 119 | std::list<NotifyArgs> unused = | 
|  | 120 | mapper.cancelTouch(fdp->ConsumeIntegral<nsecs_t>(), | 
|  | 121 | fdp->ConsumeIntegral<nsecs_t>()); | 
| Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 122 | }, | 
| Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 123 | [&]() -> void { | 
|  | 124 | std::list<NotifyArgs> unused = | 
|  | 125 | mapper.timeoutExpired(fdp->ConsumeIntegral<nsecs_t>()); | 
|  | 126 | }, | 
| Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 127 | [&]() -> void { | 
|  | 128 | StylusState state{fdp->ConsumeIntegral<nsecs_t>(), | 
|  | 129 | fdp->ConsumeFloatingPoint<float>(), | 
|  | 130 | fdp->ConsumeIntegral<uint32_t>(), | 
|  | 131 | fdp->ConsumeIntegral<int32_t>()}; | 
| 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 |