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> |
| 19 | #include "InputCommonConverter.h" |
| 20 | #include "InputProcessor.h" |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | static constexpr int32_t MAX_AXES = 64; |
| 25 | |
| 26 | // Used by two fuzz operations and a bit lengthy, so pulled out into a function. |
| 27 | NotifyMotionArgs generateFuzzedMotionArgs(FuzzedDataProvider &fdp) { |
| 28 | // Create a basic motion event for testing |
| 29 | PointerProperties properties; |
| 30 | properties.id = 0; |
Siarhei Vishniakou | 09a8fe4 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 31 | properties.toolType = getFuzzedToolType(fdp); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 32 | PointerCoords coords; |
| 33 | coords.clear(); |
| 34 | for (int32_t i = 0; i < fdp.ConsumeIntegralInRange<int32_t>(0, MAX_AXES); i++) { |
| 35 | coords.setAxisValue(fdp.ConsumeIntegral<int32_t>(), fdp.ConsumeFloatingPoint<float>()); |
| 36 | } |
| 37 | |
| 38 | const nsecs_t downTime = 2; |
| 39 | const nsecs_t readTime = downTime + fdp.ConsumeIntegralInRange<nsecs_t>(0, 1E8); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 40 | NotifyMotionArgs motionArgs(/*sequenceNum=*/fdp.ConsumeIntegral<uint32_t>(), |
| 41 | /*eventTime=*/downTime, readTime, |
| 42 | /*deviceId=*/fdp.ConsumeIntegral<int32_t>(), AINPUT_SOURCE_ANY, |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 43 | ADISPLAY_ID_DEFAULT, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 44 | /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(), |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 45 | AMOTION_EVENT_ACTION_DOWN, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 46 | /*actionButton=*/fdp.ConsumeIntegral<int32_t>(), |
| 47 | /*flags=*/fdp.ConsumeIntegral<int32_t>(), AMETA_NONE, |
| 48 | /*buttonState=*/fdp.ConsumeIntegral<int32_t>(), |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 49 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 50 | /*pointerCount=*/1, &properties, &coords, |
| 51 | /*xPrecision=*/fdp.ConsumeFloatingPoint<float>(), |
| 52 | /*yPrecision=*/fdp.ConsumeFloatingPoint<float>(), |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 53 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 54 | AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime, |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 55 | /*videoFrames=*/{}); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 56 | return motionArgs; |
| 57 | } |
| 58 | |
| 59 | extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { |
| 60 | FuzzedDataProvider fdp(data, size); |
| 61 | |
| 62 | std::unique_ptr<FuzzInputListener> mFuzzListener = std::make_unique<FuzzInputListener>(); |
| 63 | std::unique_ptr<InputProcessorInterface> mClassifier = |
| 64 | std::make_unique<InputProcessor>(*mFuzzListener); |
| 65 | |
| 66 | while (fdp.remaining_bytes() > 0) { |
| 67 | fdp.PickValueInArray<std::function<void()>>({ |
| 68 | [&]() -> void { |
| 69 | // SendToNextStage_NotifyConfigurationChangedArgs |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 70 | mClassifier->notifyConfigurationChanged( |
| 71 | {/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(), |
| 72 | /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>()}); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 73 | }, |
| 74 | [&]() -> void { |
| 75 | // SendToNextStage_NotifyKeyArgs |
Atharva_Deshpande | 2389f16 | 2023-08-01 16:19:09 +0530 | [diff] [blame^] | 76 | const nsecs_t eventTime = |
| 77 | fdp.ConsumeIntegralInRange<nsecs_t>(0, |
| 78 | systemTime(SYSTEM_TIME_MONOTONIC)); |
| 79 | const nsecs_t readTime = fdp.ConsumeIntegralInRange< |
| 80 | nsecs_t>(eventTime, std::numeric_limits<nsecs_t>::max()); |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 81 | mClassifier->notifyKey({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(), |
| 82 | eventTime, readTime, |
| 83 | /*deviceId=*/fdp.ConsumeIntegral<int32_t>(), |
| 84 | AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_DEFAULT, |
| 85 | /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(), |
| 86 | AKEY_EVENT_ACTION_DOWN, |
| 87 | /*flags=*/fdp.ConsumeIntegral<int32_t>(), AKEYCODE_HOME, |
| 88 | /*scanCode=*/fdp.ConsumeIntegral<int32_t>(), AMETA_NONE, |
| 89 | /*downTime=*/fdp.ConsumeIntegral<nsecs_t>()}); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 90 | }, |
| 91 | [&]() -> void { |
| 92 | // SendToNextStage_NotifyMotionArgs |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 93 | mClassifier->notifyMotion(generateFuzzedMotionArgs(fdp)); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 94 | }, |
| 95 | [&]() -> void { |
| 96 | // SendToNextStage_NotifySwitchArgs |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 97 | mClassifier->notifySwitch({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(), |
| 98 | /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(), |
| 99 | /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(), |
| 100 | /*switchValues=*/fdp.ConsumeIntegral<uint32_t>(), |
| 101 | /*switchMask=*/fdp.ConsumeIntegral<uint32_t>()}); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 102 | }, |
| 103 | [&]() -> void { |
| 104 | // SendToNextStage_NotifyDeviceResetArgs |
Prabir Pradhan | c392d8f | 2023-04-13 19:32:51 +0000 | [diff] [blame] | 105 | mClassifier->notifyDeviceReset({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(), |
| 106 | /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(), |
| 107 | /*deviceId=*/fdp.ConsumeIntegral<int32_t>()}); |
Michael Ensing | 910968d | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 108 | }, |
| 109 | [&]() -> void { |
| 110 | // InputClassifierConverterTest |
| 111 | const NotifyMotionArgs motionArgs = generateFuzzedMotionArgs(fdp); |
| 112 | aidl::android::hardware::input::common::MotionEvent motionEvent = |
| 113 | notifyMotionArgsToHalMotionEvent(motionArgs); |
| 114 | }, |
| 115 | })(); |
| 116 | } |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | } // namespace android |