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