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> |
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 | #include <SwitchInputMapper.h> |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) { |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 25 | std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp = |
| 26 | std::make_shared<ThreadSafeFuzzedDataProvider>(data, size); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 27 | |
Harry Cutts | f61c047 | 2023-07-10 14:45:15 +0000 | [diff] [blame] | 28 | // Create mocked objects to support the fuzzed input mapper. |
| 29 | std::shared_ptr<FuzzEventHub> eventHub = std::make_shared<FuzzEventHub>(fdp); |
| 30 | FuzzInputReaderContext context(eventHub, fdp); |
| 31 | InputDevice device = getFuzzedInputDevice(*fdp, &context); |
| 32 | |
| 33 | SwitchInputMapper& mapper = |
| 34 | getMapperForDevice<ThreadSafeFuzzedDataProvider, |
| 35 | SwitchInputMapper>(*fdp.get(), device, InputReaderConfiguration{}); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 36 | |
| 37 | // Loop through mapper operations until randomness is exhausted. |
| 38 | while (fdp->remaining_bytes() > 0) { |
| 39 | fdp->PickValueInArray<std::function<void()>>({ |
| 40 | [&]() -> void { |
| 41 | std::string dump; |
| 42 | mapper.dump(dump); |
| 43 | }, |
| 44 | [&]() -> void { mapper.getSources(); }, |
| 45 | [&]() -> void { |
Harry Cutts | 656e0ad | 2023-06-16 16:39:55 +0000 | [diff] [blame] | 46 | RawEvent rawEvent = getFuzzedRawEvent(*fdp); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 47 | std::list<NotifyArgs> unused = mapper.process(&rawEvent); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 48 | }, |
| 49 | [&]() -> void { |
| 50 | mapper.getSwitchState(fdp->ConsumeIntegral<uint32_t>(), |
| 51 | fdp->ConsumeIntegral<int32_t>()); |
| 52 | }, |
| 53 | })(); |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | } // namespace android |