Michael Ensing | 910968d | 2020-07-19 17:19:31 -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 <MapperHelpers.h> |
| 18 | #include <fuzzer/FuzzedDataProvider.h> |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 19 | #include "FuzzedInputStream.h" |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 20 | #include "InputCommonConverter.h" |
| 21 | #include "InputProcessor.h" |
| 22 | |
| 23 | namespace android { |
| 24 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 25 | namespace { |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 26 | |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 27 | constexpr int32_t MAX_RANDOM_DISPLAYS = 4; |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 28 | |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { |
| 32 | FuzzedDataProvider fdp(data, size); |
| 33 | |
| 34 | std::unique_ptr<FuzzInputListener> mFuzzListener = std::make_unique<FuzzInputListener>(); |
| 35 | std::unique_ptr<InputProcessorInterface> mClassifier = |
| 36 | std::make_unique<InputProcessor>(*mFuzzListener); |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 37 | IdGenerator idGenerator(IdGenerator::Source::OTHER); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 38 | |
| 39 | while (fdp.remaining_bytes() > 0) { |
| 40 | fdp.PickValueInArray<std::function<void()>>({ |
| 41 | [&]() -> void { |
| 42 | // SendToNextStage_NotifyConfigurationChangedArgs |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 43 | mClassifier->notifyConfigurationChanged( |
| 44 | {/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(), |
| 45 | /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>()}); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 46 | }, |
| 47 | [&]() -> void { |
| 48 | // SendToNextStage_NotifyKeyArgs |
Atharva_Deshpande | 2389f16 | 2023-08-01 16:19:09 +0530 | [diff] [blame] | 49 | const nsecs_t eventTime = |
| 50 | fdp.ConsumeIntegralInRange<nsecs_t>(0, |
| 51 | systemTime(SYSTEM_TIME_MONOTONIC)); |
| 52 | const nsecs_t readTime = fdp.ConsumeIntegralInRange< |
| 53 | nsecs_t>(eventTime, std::numeric_limits<nsecs_t>::max()); |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 54 | mClassifier->notifyKey({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(), |
| 55 | eventTime, readTime, |
| 56 | /*deviceId=*/fdp.ConsumeIntegral<int32_t>(), |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame^] | 57 | AINPUT_SOURCE_KEYBOARD, ui::LogicalDisplayId::DEFAULT, |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 58 | /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(), |
| 59 | AKEY_EVENT_ACTION_DOWN, |
| 60 | /*flags=*/fdp.ConsumeIntegral<int32_t>(), AKEYCODE_HOME, |
| 61 | /*scanCode=*/fdp.ConsumeIntegral<int32_t>(), AMETA_NONE, |
| 62 | /*downTime=*/fdp.ConsumeIntegral<nsecs_t>()}); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 63 | }, |
| 64 | [&]() -> void { |
| 65 | // SendToNextStage_NotifyMotionArgs |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 66 | mClassifier->notifyMotion( |
| 67 | generateFuzzedMotionArgs(idGenerator, fdp, MAX_RANDOM_DISPLAYS)); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 68 | }, |
| 69 | [&]() -> void { |
| 70 | // SendToNextStage_NotifySwitchArgs |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 71 | mClassifier->notifySwitch({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(), |
| 72 | /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(), |
| 73 | /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(), |
| 74 | /*switchValues=*/fdp.ConsumeIntegral<uint32_t>(), |
| 75 | /*switchMask=*/fdp.ConsumeIntegral<uint32_t>()}); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 76 | }, |
| 77 | [&]() -> void { |
| 78 | // SendToNextStage_NotifyDeviceResetArgs |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 79 | mClassifier->notifyDeviceReset({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(), |
| 80 | /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(), |
| 81 | /*deviceId=*/fdp.ConsumeIntegral<int32_t>()}); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 82 | }, |
| 83 | [&]() -> void { |
| 84 | // InputClassifierConverterTest |
Siarhei Vishniakou | 2defec0 | 2023-06-08 17:24:44 -0700 | [diff] [blame] | 85 | const NotifyMotionArgs motionArgs = |
| 86 | generateFuzzedMotionArgs(idGenerator, fdp, MAX_RANDOM_DISPLAYS); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 87 | aidl::android::hardware::input::common::MotionEvent motionEvent = |
| 88 | notifyMotionArgsToHalMotionEvent(motionArgs); |
| 89 | }, |
| 90 | })(); |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | } // namespace android |