| Michael Ensing | 39b87e7 | 2020-07-19 17:19:31 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2020 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 "InputClassifier.h" | 
 | 20 | #include "InputClassifierConverter.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; | 
 | 31 |     properties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; | 
 | 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 |     nsecs_t downTime = 2; | 
 | 39 |     NotifyMotionArgs motionArgs(fdp.ConsumeIntegral<uint32_t>() /*sequenceNum*/, | 
 | 40 |                                 downTime /*eventTime*/, fdp.ConsumeIntegral<int32_t>() /*deviceId*/, | 
 | 41 |                                 AINPUT_SOURCE_ANY, ADISPLAY_ID_DEFAULT, | 
 | 42 |                                 fdp.ConsumeIntegral<uint32_t>() /*policyFlags*/, | 
 | 43 |                                 AMOTION_EVENT_ACTION_DOWN, | 
 | 44 |                                 fdp.ConsumeIntegral<int32_t>() /*actionButton*/, | 
 | 45 |                                 fdp.ConsumeIntegral<int32_t>() /*flags*/, AMETA_NONE, | 
 | 46 |                                 fdp.ConsumeIntegral<int32_t>() /*buttonState*/, | 
 | 47 |                                 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, | 
 | 48 |                                 1 /*pointerCount*/, &properties, &coords, | 
 | 49 |                                 fdp.ConsumeFloatingPoint<float>() /*xPrecision*/, | 
 | 50 |                                 fdp.ConsumeFloatingPoint<float>() /*yPrecision*/, | 
 | 51 |                                 AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
 | 52 |                                 AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime, | 
 | 53 |                                 {} /*videoFrames*/); | 
 | 54 |     return motionArgs; | 
 | 55 | } | 
 | 56 |  | 
 | 57 | extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { | 
 | 58 |     FuzzedDataProvider fdp(data, size); | 
 | 59 |  | 
 | 60 |     sp<FuzzInputListener> mFuzzListener = new FuzzInputListener(); | 
 | 61 |     sp<InputClassifierInterface> mClassifier = new InputClassifier(mFuzzListener); | 
 | 62 |  | 
 | 63 |     while (fdp.remaining_bytes() > 0) { | 
 | 64 |         fdp.PickValueInArray<std::function<void()>>({ | 
 | 65 |                 [&]() -> void { | 
 | 66 |                     // SendToNextStage_NotifyConfigurationChangedArgs | 
 | 67 |                     NotifyConfigurationChangedArgs | 
 | 68 |                             args(fdp.ConsumeIntegral<uint32_t>() /*sequenceNum*/, | 
 | 69 |                                  fdp.ConsumeIntegral<nsecs_t>() /*eventTime*/); | 
 | 70 |                     mClassifier->notifyConfigurationChanged(&args); | 
 | 71 |                 }, | 
 | 72 |                 [&]() -> void { | 
 | 73 |                     // SendToNextStage_NotifyKeyArgs | 
 | 74 |                     NotifyKeyArgs keyArgs(fdp.ConsumeIntegral<uint32_t>() /*sequenceNum*/, | 
 | 75 |                                           fdp.ConsumeIntegral<nsecs_t>() /*eventTime*/, | 
 | 76 |                                           fdp.ConsumeIntegral<int32_t>() /*deviceId*/, | 
 | 77 |                                           AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_DEFAULT, | 
 | 78 |                                           fdp.ConsumeIntegral<uint32_t>() /*policyFlags*/, | 
 | 79 |                                           AKEY_EVENT_ACTION_DOWN, | 
 | 80 |                                           fdp.ConsumeIntegral<int32_t>() /*flags*/, AKEYCODE_HOME, | 
 | 81 |                                           fdp.ConsumeIntegral<int32_t>() /*scanCode*/, AMETA_NONE, | 
 | 82 |                                           fdp.ConsumeIntegral<nsecs_t>() /*downTime*/); | 
 | 83 |  | 
 | 84 |                     mClassifier->notifyKey(&keyArgs); | 
 | 85 |                 }, | 
 | 86 |                 [&]() -> void { | 
 | 87 |                     // SendToNextStage_NotifyMotionArgs | 
 | 88 |                     NotifyMotionArgs motionArgs = generateFuzzedMotionArgs(fdp); | 
 | 89 |                     mClassifier->notifyMotion(&motionArgs); | 
 | 90 |                 }, | 
 | 91 |                 [&]() -> void { | 
 | 92 |                     // SendToNextStage_NotifySwitchArgs | 
 | 93 |                     NotifySwitchArgs switchArgs(fdp.ConsumeIntegral<uint32_t>() /*sequenceNum*/, | 
 | 94 |                                                 fdp.ConsumeIntegral<nsecs_t>() /*eventTime*/, | 
 | 95 |                                                 fdp.ConsumeIntegral<uint32_t>() /*policyFlags*/, | 
 | 96 |                                                 fdp.ConsumeIntegral<uint32_t>() /*switchValues*/, | 
 | 97 |                                                 fdp.ConsumeIntegral<uint32_t>() /*switchMask*/); | 
 | 98 |  | 
 | 99 |                     mClassifier->notifySwitch(&switchArgs); | 
 | 100 |                 }, | 
 | 101 |                 [&]() -> void { | 
 | 102 |                     // SendToNextStage_NotifyDeviceResetArgs | 
 | 103 |                     NotifyDeviceResetArgs resetArgs(fdp.ConsumeIntegral<uint32_t>() /*sequenceNum*/, | 
 | 104 |                                                     fdp.ConsumeIntegral<nsecs_t>() /*eventTime*/, | 
 | 105 |                                                     fdp.ConsumeIntegral<int32_t>() /*deviceId*/); | 
 | 106 |  | 
 | 107 |                     mClassifier->notifyDeviceReset(&resetArgs); | 
 | 108 |                 }, | 
 | 109 |                 [&]() -> void { | 
 | 110 |                     // InputClassifierConverterTest | 
 | 111 |                     const NotifyMotionArgs motionArgs = generateFuzzedMotionArgs(fdp); | 
 | 112 |                     hardware::input::common::V1_0::MotionEvent motionEvent = | 
 | 113 |                             notifyMotionArgsToHalMotionEvent(motionArgs); | 
 | 114 |                 }, | 
 | 115 |         })(); | 
 | 116 |     } | 
 | 117 |     return 0; | 
 | 118 | } | 
 | 119 |  | 
 | 120 | } // namespace android |