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