| Michael Ensing | 7c58e65 | 2020-05-12 00:41:30 -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 | #pragma once |
| 18 | |
| 19 | #include <InputDevice.h> |
| 20 | #include <InputMapper.h> |
| 21 | #include <InputReader.h> |
| 22 | #include <fuzzer/FuzzedDataProvider.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | class FuzzEventHub : public EventHubInterface { |
| 27 | InputDeviceIdentifier mIdentifier; |
| 28 | std::vector<TouchVideoFrame> mVideoFrames; |
| 29 | PropertyMap mFuzzConfig; |
| 30 | std::mutex mEventLock; |
| 31 | size_t mCount = 0; |
| 32 | RawEvent mBuf[256]; |
| 33 | std::shared_ptr<FuzzedDataProvider> fdp; |
| 34 | |
| 35 | public: |
| 36 | FuzzEventHub(std::shared_ptr<FuzzedDataProvider> fdp) : fdp(fdp) {} |
| 37 | ~FuzzEventHub() {} |
| 38 | void addProperty(const String8& key, const String8 value) { |
| 39 | mFuzzConfig.addProperty(key, value); |
| 40 | } |
| 41 | void addEvents(std::shared_ptr<FuzzedDataProvider> fdp) { |
| 42 | std::lock_guard<std::mutex> guard(mEventLock); |
| 43 | mCount = fdp->ConsumeIntegralInRange<size_t>(0, 256); |
| 44 | |
| 45 | for (size_t i = 0; i < mCount; i++) |
| 46 | mBuf[i] = {fdp->ConsumeIntegral<nsecs_t>(), fdp->ConsumeIntegral<int32_t>(), |
| 47 | fdp->ConsumeIntegral<int32_t>(), fdp->ConsumeIntegral<int32_t>(), |
| 48 | fdp->ConsumeIntegral<int32_t>()}; |
| 49 | } |
| 50 | uint32_t getDeviceClasses(int32_t deviceId) const override { |
| 51 | return fdp->ConsumeIntegral<uint32_t>(); |
| 52 | } |
| 53 | InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override { |
| 54 | return mIdentifier; |
| 55 | } |
| 56 | int32_t getDeviceControllerNumber(int32_t deviceId) const override { |
| 57 | return fdp->ConsumeIntegral<int32_t>(); |
| 58 | } |
| 59 | void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override { |
| 60 | *outConfiguration = mFuzzConfig; |
| 61 | } |
| 62 | status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 63 | RawAbsoluteAxisInfo* outAxisInfo) const override { |
| 64 | return fdp->ConsumeIntegral<status_t>(); |
| 65 | } |
| 66 | bool hasRelativeAxis(int32_t deviceId, int axis) const override { return fdp->ConsumeBool(); } |
| 67 | bool hasInputProperty(int32_t deviceId, int property) const override { |
| 68 | return fdp->ConsumeBool(); |
| 69 | } |
| 70 | status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState, |
| 71 | int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override { |
| 72 | return fdp->ConsumeIntegral<status_t>(); |
| 73 | } |
| 74 | status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override { |
| 75 | return fdp->ConsumeIntegral<status_t>(); |
| 76 | } |
| 77 | void setExcludedDevices(const std::vector<std::string>& devices) override {} |
| 78 | size_t getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) override { |
| 79 | std::lock_guard<std::mutex> guard(mEventLock); |
| 80 | for (size_t i = 0; i < mCount; i++) buffer[i] = mBuf[i]; |
| 81 | |
| 82 | return mCount; |
| 83 | } |
| 84 | std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; } |
| 85 | int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override { |
| 86 | return fdp->ConsumeIntegral<int32_t>(); |
| 87 | } |
| 88 | int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override { |
| 89 | return fdp->ConsumeIntegral<int32_t>(); |
| 90 | } |
| 91 | int32_t getSwitchState(int32_t deviceId, int32_t sw) const override { |
| 92 | return fdp->ConsumeIntegral<int32_t>(); |
| 93 | } |
| 94 | status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, |
| 95 | int32_t* outValue) const override { |
| 96 | return fdp->ConsumeIntegral<status_t>(); |
| 97 | } |
| 98 | bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes, |
| 99 | uint8_t* outFlags) const override { |
| 100 | return fdp->ConsumeBool(); |
| 101 | } |
| 102 | bool hasScanCode(int32_t deviceId, int32_t scanCode) const override { |
| 103 | return fdp->ConsumeBool(); |
| 104 | } |
| 105 | bool hasLed(int32_t deviceId, int32_t led) const override { return fdp->ConsumeBool(); } |
| 106 | void setLedState(int32_t deviceId, int32_t led, bool on) override {} |
| 107 | void getVirtualKeyDefinitions( |
| 108 | int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {} |
| 109 | sp<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override { return nullptr; } |
| 110 | bool setKeyboardLayoutOverlay(int32_t deviceId, const sp<KeyCharacterMap>& map) override { |
| 111 | return fdp->ConsumeBool(); |
| 112 | } |
| 113 | void vibrate(int32_t deviceId, nsecs_t duration) override {} |
| 114 | void cancelVibrate(int32_t deviceId) override {} |
| 115 | void requestReopenDevices() override {} |
| 116 | void wake() override {} |
| 117 | void dump(std::string& dump) override {} |
| 118 | void monitor() override {} |
| 119 | bool isDeviceEnabled(int32_t deviceId) override { return fdp->ConsumeBool(); } |
| 120 | status_t enableDevice(int32_t deviceId) override { return fdp->ConsumeIntegral<status_t>(); } |
| 121 | status_t disableDevice(int32_t deviceId) override { return fdp->ConsumeIntegral<status_t>(); } |
| 122 | }; |
| 123 | |
| 124 | class FuzzPointerController : public PointerControllerInterface { |
| 125 | std::shared_ptr<FuzzedDataProvider> fdp; |
| 126 | |
| 127 | public: |
| 128 | FuzzPointerController(std::shared_ptr<FuzzedDataProvider> fdp) : fdp(fdp) {} |
| 129 | ~FuzzPointerController() {} |
| 130 | bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override { |
| 131 | return fdp->ConsumeBool(); |
| 132 | } |
| 133 | void move(float deltaX, float deltaY) override {} |
| 134 | void setButtonState(int32_t buttonState) override {} |
| 135 | int32_t getButtonState() const override { return fdp->ConsumeIntegral<int32_t>(); } |
| 136 | void setPosition(float x, float y) override {} |
| 137 | void getPosition(float* outX, float* outY) const override {} |
| 138 | void fade(Transition transition) override {} |
| 139 | void unfade(Transition transition) override {} |
| 140 | void setPresentation(Presentation presentation) override {} |
| 141 | void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, |
| 142 | BitSet32 spotIdBits, int32_t displayId) override {} |
| 143 | void clearSpots() override {} |
| 144 | int32_t getDisplayId() const override { return fdp->ConsumeIntegral<int32_t>(); } |
| 145 | void setDisplayViewport(const DisplayViewport& displayViewport) override {} |
| 146 | }; |
| 147 | |
| 148 | class FuzzInputReaderPolicy : public InputReaderPolicyInterface { |
| 149 | TouchAffineTransformation mTransform; |
| 150 | std::shared_ptr<FuzzPointerController> mPointerController; |
| 151 | std::shared_ptr<FuzzedDataProvider> fdp; |
| 152 | |
| 153 | protected: |
| 154 | ~FuzzInputReaderPolicy() {} |
| 155 | |
| 156 | public: |
| 157 | FuzzInputReaderPolicy(std::shared_ptr<FuzzedDataProvider> fdp) : fdp(fdp) { |
| 158 | mPointerController = std::make_shared<FuzzPointerController>(fdp); |
| 159 | } |
| 160 | void getReaderConfiguration(InputReaderConfiguration* outConfig) override {} |
| 161 | std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override { |
| 162 | return mPointerController; |
| 163 | } |
| 164 | void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {} |
| 165 | sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier) override { |
| 166 | return nullptr; |
| 167 | } |
| 168 | std::string getDeviceAlias(const InputDeviceIdentifier& identifier) { |
| 169 | return fdp->ConsumeRandomLengthString(32); |
| 170 | } |
| 171 | TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor, |
| 172 | int32_t surfaceRotation) override { |
| 173 | return mTransform; |
| 174 | } |
| 175 | void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; } |
| 176 | }; |
| 177 | |
| 178 | class FuzzInputListener : public virtual InputListenerInterface { |
| 179 | protected: |
| 180 | ~FuzzInputListener() {} |
| 181 | |
| 182 | public: |
| 183 | FuzzInputListener() {} |
| 184 | void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override {} |
| 185 | void notifyKey(const NotifyKeyArgs* args) override {} |
| 186 | void notifyMotion(const NotifyMotionArgs* args) override {} |
| 187 | void notifySwitch(const NotifySwitchArgs* args) override {} |
| 188 | void notifyDeviceReset(const NotifyDeviceResetArgs* args) override {} |
| 189 | }; |
| 190 | |
| 191 | class FuzzInputReaderContext : public InputReaderContext { |
| 192 | std::shared_ptr<EventHubInterface> mEventHub; |
| 193 | sp<InputReaderPolicyInterface> mPolicy; |
| 194 | sp<InputListenerInterface> mListener; |
| 195 | std::shared_ptr<FuzzedDataProvider> fdp; |
| 196 | |
| 197 | public: |
| 198 | FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub, |
| 199 | const sp<InputReaderPolicyInterface>& policy, |
| 200 | const sp<InputListenerInterface>& listener, |
| 201 | std::shared_ptr<FuzzedDataProvider> fdp) |
| 202 | : mEventHub(eventHub), mPolicy(policy), mListener(listener), fdp(fdp) {} |
| 203 | ~FuzzInputReaderContext() {} |
| 204 | void updateGlobalMetaState() override {} |
| 205 | int32_t getGlobalMetaState() { return fdp->ConsumeIntegral<int32_t>(); } |
| 206 | void disableVirtualKeysUntil(nsecs_t time) override {} |
| 207 | bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override { |
| 208 | return fdp->ConsumeBool(); |
| 209 | } |
| 210 | void fadePointer() override {} |
| 211 | std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override { |
| 212 | return mPolicy->obtainPointerController(0); |
| 213 | } |
| 214 | void requestTimeoutAtTime(nsecs_t when) override {} |
| 215 | int32_t bumpGeneration() override { return fdp->ConsumeIntegral<int32_t>(); } |
| 216 | void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {} |
| 217 | void dispatchExternalStylusState(const StylusState& outState) override {} |
| 218 | InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); } |
| 219 | InputListenerInterface* getListener() override { return mListener.get(); } |
| 220 | EventHubInterface* getEventHub() override { return mEventHub.get(); } |
| 221 | int32_t getNextId() override { return fdp->ConsumeIntegral<int32_t>(); } |
| 222 | }; |
| 223 | |
| 224 | } // namespace android |