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