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> |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 19 | #include <KeyboardInputMapper.h> |
Harry Cutts | 656e0ad | 2023-06-16 16:39:55 +0000 | [diff] [blame] | 20 | #include <MapperHelpers.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("keyboard.orientationAware", "1"); }, |
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("keyboard.orientationAware", |
| 32 | fdp->ConsumeRandomLengthString(100).data()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 33 | }, |
| 34 | [&]() -> void { |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 35 | eventHub.addProperty("keyboard.doNotWakeByDefault", |
| 36 | fdp->ConsumeRandomLengthString(100).data()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 37 | }, |
| 38 | [&]() -> void { |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 39 | eventHub.addProperty("keyboard.handlesKeyRepeat", |
| 40 | fdp->ConsumeRandomLengthString(100).data()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 41 | }})(); |
| 42 | } |
| 43 | |
| 44 | extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) { |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 45 | std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp = |
| 46 | std::make_shared<ThreadSafeFuzzedDataProvider>(data, size); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 47 | |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 48 | // Create mocked objects to support the fuzzed input mapper. |
| 49 | std::shared_ptr<FuzzEventHub> eventHub = std::make_shared<FuzzEventHub>(fdp); |
| 50 | FuzzInputReaderContext context(eventHub, fdp); |
| 51 | InputDevice device = getFuzzedInputDevice(*fdp, &context); |
| 52 | |
| 53 | KeyboardInputMapper& mapper = getMapperForDevice< |
| 54 | ThreadSafeFuzzedDataProvider, |
| 55 | KeyboardInputMapper>(*fdp.get(), device, InputReaderConfiguration{}, |
| 56 | /*source=*/fdp->ConsumeIntegral<uint32_t>(), |
| 57 | /*keyboardType=*/fdp->ConsumeIntegral<int32_t>()); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 58 | |
| 59 | // Loop through mapper operations until randomness is exhausted. |
| 60 | while (fdp->remaining_bytes() > 0) { |
| 61 | fdp->PickValueInArray<std::function<void()>>({ |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 62 | [&]() -> void { addProperty(*eventHub.get(), fdp); }, |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 63 | [&]() -> void { |
| 64 | std::string dump; |
| 65 | mapper.dump(dump); |
| 66 | }, |
| 67 | [&]() -> void { |
| 68 | InputDeviceInfo info; |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 69 | mapper.populateDeviceInfo(info); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 70 | }, |
| 71 | [&]() -> void { mapper.getSources(); }, |
| 72 | [&]() -> void { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 73 | std::list<NotifyArgs> unused = |
Harry Cutts | e36d835 | 2023-07-10 13:28:45 +0000 | [diff] [blame] | 74 | mapper.reconfigure(fdp->ConsumeIntegral<nsecs_t>(), /*readerConfig=*/{}, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 75 | InputReaderConfiguration::Change( |
| 76 | fdp->ConsumeIntegral<uint32_t>())); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 77 | }, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 78 | [&]() -> void { |
| 79 | std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>()); |
| 80 | }, |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 81 | [&]() -> void { |
Harry Cutts | 656e0ad | 2023-06-16 16:39:55 +0000 | [diff] [blame] | 82 | RawEvent rawEvent = getFuzzedRawEvent(*fdp); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 83 | std::list<NotifyArgs> unused = mapper.process(&rawEvent); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 84 | }, |
| 85 | [&]() -> void { |
| 86 | mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(), |
| 87 | fdp->ConsumeIntegral<int32_t>()); |
| 88 | }, |
| 89 | [&]() -> void { |
| 90 | mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(), |
| 91 | fdp->ConsumeIntegral<int32_t>()); |
| 92 | }, |
| 93 | [&]() -> void { |
| 94 | std::vector<int32_t> keyCodes; |
| 95 | int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes); |
| 96 | for (int32_t i = 0; i < numBytes; ++i) { |
| 97 | keyCodes.push_back(fdp->ConsumeIntegral<int32_t>()); |
| 98 | } |
| 99 | mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes, |
| 100 | nullptr); |
| 101 | }, |
| 102 | [&]() -> void { mapper.getMetaState(); }, |
| 103 | [&]() -> void { mapper.updateMetaState(fdp->ConsumeIntegral<int32_t>()); }, |
| 104 | [&]() -> void { mapper.getAssociatedDisplayId(); }, |
| 105 | })(); |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | } // namespace android |