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 | */ |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 16 | #pragma once |
| 17 | |
| 18 | #include <InputDevice.h> |
| 19 | #include <InputMapper.h> |
| 20 | #include <InputReader.h> |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 21 | #include <ThreadSafeFuzzedDataProvider.h> |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 22 | |
| 23 | constexpr size_t kValidTypes[] = {EV_SW, |
| 24 | EV_SYN, |
| 25 | SYN_REPORT, |
| 26 | EV_ABS, |
| 27 | EV_KEY, |
| 28 | EV_MSC, |
| 29 | EV_REL, |
| 30 | android::EventHubInterface::DEVICE_ADDED, |
| 31 | android::EventHubInterface::DEVICE_REMOVED, |
| 32 | android::EventHubInterface::FINISHED_DEVICE_SCAN}; |
| 33 | |
| 34 | constexpr size_t kValidCodes[] = { |
| 35 | SYN_REPORT, |
| 36 | ABS_MT_SLOT, |
| 37 | SYN_MT_REPORT, |
| 38 | ABS_MT_POSITION_X, |
| 39 | ABS_MT_POSITION_Y, |
| 40 | ABS_MT_TOUCH_MAJOR, |
| 41 | ABS_MT_TOUCH_MINOR, |
| 42 | ABS_MT_WIDTH_MAJOR, |
| 43 | ABS_MT_WIDTH_MINOR, |
| 44 | ABS_MT_ORIENTATION, |
| 45 | ABS_MT_TRACKING_ID, |
| 46 | ABS_MT_PRESSURE, |
| 47 | ABS_MT_DISTANCE, |
| 48 | ABS_MT_TOOL_TYPE, |
| 49 | SYN_MT_REPORT, |
| 50 | MSC_SCAN, |
| 51 | REL_X, |
| 52 | REL_Y, |
| 53 | REL_WHEEL, |
| 54 | REL_HWHEEL, |
| 55 | BTN_LEFT, |
| 56 | BTN_RIGHT, |
| 57 | BTN_MIDDLE, |
| 58 | BTN_BACK, |
| 59 | BTN_SIDE, |
| 60 | BTN_FORWARD, |
| 61 | BTN_EXTRA, |
| 62 | BTN_TASK, |
| 63 | }; |
| 64 | |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 65 | constexpr size_t kMaxSize = 256; |
| 66 | |
| 67 | namespace android { |
| 68 | |
| 69 | class FuzzEventHub : public EventHubInterface { |
| 70 | InputDeviceIdentifier mIdentifier; |
| 71 | std::vector<TouchVideoFrame> mVideoFrames; |
| 72 | PropertyMap mFuzzConfig; |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 73 | std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 74 | |
| 75 | public: |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 76 | FuzzEventHub(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(std::move(fdp)) {} |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 77 | ~FuzzEventHub() {} |
| 78 | void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); } |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 79 | |
| 80 | ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override { |
| 81 | return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>()); |
| 82 | } |
| 83 | InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override { |
| 84 | return mIdentifier; |
| 85 | } |
| 86 | int32_t getDeviceControllerNumber(int32_t deviceId) const override { |
| 87 | return mFdp->ConsumeIntegral<int32_t>(); |
| 88 | } |
| 89 | void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override { |
| 90 | *outConfiguration = mFuzzConfig; |
| 91 | } |
| 92 | status_t getAbsoluteAxisInfo(int32_t deviceId, int axis, |
| 93 | RawAbsoluteAxisInfo* outAxisInfo) const override { |
| 94 | return mFdp->ConsumeIntegral<status_t>(); |
| 95 | } |
| 96 | bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); } |
| 97 | bool hasInputProperty(int32_t deviceId, int property) const override { |
| 98 | return mFdp->ConsumeBool(); |
| 99 | } |
| 100 | bool hasMscEvent(int32_t deviceId, int mscEvent) const override { return mFdp->ConsumeBool(); } |
| 101 | status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState, |
| 102 | int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override { |
| 103 | return mFdp->ConsumeIntegral<status_t>(); |
| 104 | } |
| 105 | status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override { |
| 106 | return mFdp->ConsumeIntegral<status_t>(); |
| 107 | } |
| 108 | void setExcludedDevices(const std::vector<std::string>& devices) override {} |
Siarhei Vishniakou | 7b3ea0b | 2022-09-16 14:23:20 -0700 | [diff] [blame] | 109 | std::vector<RawEvent> getEvents(int timeoutMillis) override { |
| 110 | std::vector<RawEvent> events; |
| 111 | const size_t count = mFdp->ConsumeIntegralInRange<size_t>(0, kMaxSize); |
| 112 | for (size_t i = 0; i < count; ++i) { |
| 113 | int32_t type = mFdp->ConsumeBool() ? mFdp->PickValueInArray(kValidTypes) |
| 114 | : mFdp->ConsumeIntegral<int32_t>(); |
| 115 | int32_t code = mFdp->ConsumeBool() ? mFdp->PickValueInArray(kValidCodes) |
| 116 | : mFdp->ConsumeIntegral<int32_t>(); |
| 117 | events.push_back({ |
| 118 | .when = mFdp->ConsumeIntegral<nsecs_t>(), |
| 119 | .readTime = mFdp->ConsumeIntegral<nsecs_t>(), |
| 120 | .deviceId = mFdp->ConsumeIntegral<int32_t>(), |
| 121 | .type = type, |
| 122 | .code = code, |
| 123 | .value = mFdp->ConsumeIntegral<int32_t>(), |
| 124 | }); |
| 125 | } |
| 126 | return events; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 127 | } |
| 128 | std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; } |
| 129 | |
| 130 | base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor( |
| 131 | int32_t deviceId, int32_t absCode) const override { |
| 132 | return base::ResultError("Fuzzer", UNKNOWN_ERROR); |
| 133 | }; |
| 134 | // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node, |
| 135 | // containing the raw info of the sysfs node structure. |
| 136 | std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; } |
| 137 | std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, |
| 138 | int32_t BatteryId) const override { |
| 139 | return std::nullopt; |
| 140 | }; |
| 141 | |
| 142 | std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { return {}; }; |
| 143 | std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override { |
| 144 | return std::nullopt; |
| 145 | }; |
| 146 | std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override { |
| 147 | return std::nullopt; |
| 148 | }; |
| 149 | void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override{}; |
| 150 | std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities( |
| 151 | int32_t deviceId, int32_t lightId) const override { |
| 152 | return std::nullopt; |
| 153 | }; |
| 154 | void setLightIntensities(int32_t deviceId, int32_t lightId, |
| 155 | std::unordered_map<LightColor, int32_t> intensities) override{}; |
| 156 | |
Vaibhav Devmurari | 7fb4113 | 2023-01-02 13:30:26 +0000 | [diff] [blame] | 157 | std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override { |
| 158 | return std::nullopt; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override { |
| 162 | return mFdp->ConsumeIntegral<int32_t>(); |
| 163 | } |
| 164 | int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override { |
| 165 | return mFdp->ConsumeIntegral<int32_t>(); |
| 166 | } |
| 167 | int32_t getSwitchState(int32_t deviceId, int32_t sw) const override { |
| 168 | return mFdp->ConsumeIntegral<int32_t>(); |
| 169 | } |
Vaibhav Devmurari | cbba14c | 2022-10-10 16:54:49 +0000 | [diff] [blame] | 170 | void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const override {} |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 171 | int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override { |
| 172 | return mFdp->ConsumeIntegral<int32_t>(); |
| 173 | } |
| 174 | status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis, |
| 175 | int32_t* outValue) const override { |
| 176 | return mFdp->ConsumeIntegral<status_t>(); |
| 177 | } |
| 178 | bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes, |
| 179 | uint8_t* outFlags) const override { |
| 180 | return mFdp->ConsumeBool(); |
| 181 | } |
| 182 | bool hasScanCode(int32_t deviceId, int32_t scanCode) const override { |
| 183 | return mFdp->ConsumeBool(); |
| 184 | } |
| 185 | bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override { |
| 186 | return mFdp->ConsumeBool(); |
| 187 | } |
| 188 | bool hasLed(int32_t deviceId, int32_t led) const override { return mFdp->ConsumeBool(); } |
| 189 | void setLedState(int32_t deviceId, int32_t led, bool on) override {} |
| 190 | void getVirtualKeyDefinitions( |
| 191 | int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {} |
| 192 | const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override { |
| 193 | return nullptr; |
| 194 | } |
| 195 | bool setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) override { |
| 196 | return mFdp->ConsumeBool(); |
| 197 | } |
| 198 | void vibrate(int32_t deviceId, const VibrationElement& effect) override {} |
| 199 | void cancelVibrate(int32_t deviceId) override {} |
| 200 | |
| 201 | std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return {}; }; |
| 202 | |
| 203 | /* Query battery level. */ |
| 204 | std::optional<int32_t> getBatteryCapacity(int32_t deviceId, int32_t batteryId) const override { |
| 205 | return std::nullopt; |
| 206 | }; |
| 207 | |
| 208 | /* Query battery status. */ |
| 209 | std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const override { |
| 210 | return std::nullopt; |
| 211 | }; |
| 212 | |
| 213 | void requestReopenDevices() override {} |
| 214 | void wake() override {} |
| 215 | void dump(std::string& dump) const override {} |
| 216 | void monitor() const override {} |
| 217 | bool isDeviceEnabled(int32_t deviceId) const override { return mFdp->ConsumeBool(); } |
| 218 | status_t enableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); } |
| 219 | status_t disableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); } |
| 220 | }; |
| 221 | |
| 222 | class FuzzPointerController : public PointerControllerInterface { |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 223 | std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 224 | |
| 225 | public: |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 226 | FuzzPointerController(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {} |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 227 | ~FuzzPointerController() {} |
| 228 | bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override { |
| 229 | return mFdp->ConsumeBool(); |
| 230 | } |
| 231 | void move(float deltaX, float deltaY) override {} |
| 232 | void setButtonState(int32_t buttonState) override {} |
| 233 | int32_t getButtonState() const override { return mFdp->ConsumeIntegral<int32_t>(); } |
| 234 | void setPosition(float x, float y) override {} |
| 235 | void getPosition(float* outX, float* outY) const override {} |
| 236 | void fade(Transition transition) override {} |
| 237 | void unfade(Transition transition) override {} |
| 238 | void setPresentation(Presentation presentation) override {} |
| 239 | void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, |
| 240 | BitSet32 spotIdBits, int32_t displayId) override {} |
| 241 | void clearSpots() override {} |
| 242 | int32_t getDisplayId() const override { return mFdp->ConsumeIntegral<int32_t>(); } |
| 243 | void setDisplayViewport(const DisplayViewport& displayViewport) override {} |
| 244 | }; |
| 245 | |
| 246 | class FuzzInputReaderPolicy : public InputReaderPolicyInterface { |
| 247 | TouchAffineTransformation mTransform; |
| 248 | std::shared_ptr<FuzzPointerController> mPointerController; |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 249 | std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 250 | |
| 251 | protected: |
| 252 | ~FuzzInputReaderPolicy() {} |
| 253 | |
| 254 | public: |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 255 | FuzzInputReaderPolicy(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) { |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 256 | mPointerController = std::make_shared<FuzzPointerController>(mFdp); |
| 257 | } |
| 258 | void getReaderConfiguration(InputReaderConfiguration* outConfig) override {} |
| 259 | std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override { |
| 260 | return mPointerController; |
| 261 | } |
| 262 | void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {} |
| 263 | std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay( |
| 264 | const InputDeviceIdentifier& identifier) override { |
| 265 | return nullptr; |
| 266 | } |
| 267 | std::string getDeviceAlias(const InputDeviceIdentifier& identifier) { |
| 268 | return mFdp->ConsumeRandomLengthString(32); |
| 269 | } |
| 270 | TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor, |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 271 | ui::Rotation surfaceRotation) override { |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 272 | return mTransform; |
| 273 | } |
| 274 | void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; } |
Prabir Pradhan | da20b17 | 2022-09-26 17:01:18 +0000 | [diff] [blame] | 275 | void notifyStylusGestureStarted(int32_t, nsecs_t) {} |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | class FuzzInputListener : public virtual InputListenerInterface { |
| 279 | public: |
| 280 | void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override {} |
| 281 | void notifyKey(const NotifyKeyArgs* args) override {} |
| 282 | void notifyMotion(const NotifyMotionArgs* args) override {} |
| 283 | void notifySwitch(const NotifySwitchArgs* args) override {} |
| 284 | void notifySensor(const NotifySensorArgs* args) override{}; |
| 285 | void notifyVibratorState(const NotifyVibratorStateArgs* args) override{}; |
| 286 | void notifyDeviceReset(const NotifyDeviceResetArgs* args) override {} |
| 287 | void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override{}; |
| 288 | }; |
| 289 | |
| 290 | class FuzzInputReaderContext : public InputReaderContext { |
| 291 | std::shared_ptr<EventHubInterface> mEventHub; |
| 292 | sp<InputReaderPolicyInterface> mPolicy; |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 293 | std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 294 | |
| 295 | public: |
| 296 | FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub, |
| 297 | const sp<InputReaderPolicyInterface>& policy, |
| 298 | InputListenerInterface& listener, |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 299 | std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 300 | : mEventHub(eventHub), mPolicy(policy), mFdp(mFdp) {} |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 301 | ~FuzzInputReaderContext() {} |
| 302 | void updateGlobalMetaState() override {} |
| 303 | int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); } |
| 304 | void disableVirtualKeysUntil(nsecs_t time) override {} |
| 305 | bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override { |
| 306 | return mFdp->ConsumeBool(); |
| 307 | } |
| 308 | void fadePointer() override {} |
| 309 | std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override { |
| 310 | return mPolicy->obtainPointerController(0); |
| 311 | } |
| 312 | void requestTimeoutAtTime(nsecs_t when) override {} |
| 313 | int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); } |
| 314 | void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {} |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 315 | std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override { |
| 316 | return {}; |
| 317 | } |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 318 | InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); } |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 319 | EventHubInterface* getEventHub() override { return mEventHub.get(); } |
| 320 | int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); } |
| 321 | |
| 322 | void updateLedMetaState(int32_t metaState) override{}; |
| 323 | int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); }; |
Prabir Pradhan | da20b17 | 2022-09-26 17:01:18 +0000 | [diff] [blame] | 324 | void notifyStylusGestureStarted(int32_t, nsecs_t) {} |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | } // namespace android |