blob: cd852d668cf1dea655c59a2375979e456448c2db [file] [log] [blame]
Michael Ensingb8d93262020-05-12 00:41:30 -07001/*
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
25using android::hardware::input::InputDeviceCountryCode;
26
27constexpr 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
38constexpr 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
69constexpr 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
109constexpr size_t kMaxSize = 256;
110
111namespace android {
112
113class FuzzEventHub : public EventHubInterface {
114 InputDeviceIdentifier mIdentifier;
115 std::vector<TouchVideoFrame> mVideoFrames;
116 PropertyMap mFuzzConfig;
Michael Ensingb8d93262020-05-12 00:41:30 -0700117 std::shared_ptr<FuzzedDataProvider> mFdp;
118
119public:
120 FuzzEventHub(std::shared_ptr<FuzzedDataProvider> fdp) : mFdp(std::move(fdp)) {}
121 ~FuzzEventHub() {}
122 void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700123
124 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
125 return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>());
126 }
127 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
128 return mIdentifier;
129 }
130 int32_t getDeviceControllerNumber(int32_t deviceId) const override {
131 return mFdp->ConsumeIntegral<int32_t>();
132 }
133 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
134 *outConfiguration = mFuzzConfig;
135 }
136 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
137 RawAbsoluteAxisInfo* outAxisInfo) const override {
138 return mFdp->ConsumeIntegral<status_t>();
139 }
140 bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); }
141 bool hasInputProperty(int32_t deviceId, int property) const override {
142 return mFdp->ConsumeBool();
143 }
144 bool hasMscEvent(int32_t deviceId, int mscEvent) const override { return mFdp->ConsumeBool(); }
145 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
146 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
147 return mFdp->ConsumeIntegral<status_t>();
148 }
149 status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override {
150 return mFdp->ConsumeIntegral<status_t>();
151 }
152 void setExcludedDevices(const std::vector<std::string>& devices) override {}
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700153 std::vector<RawEvent> getEvents(int timeoutMillis) override {
154 std::vector<RawEvent> events;
155 const size_t count = mFdp->ConsumeIntegralInRange<size_t>(0, kMaxSize);
156 for (size_t i = 0; i < count; ++i) {
157 int32_t type = mFdp->ConsumeBool() ? mFdp->PickValueInArray(kValidTypes)
158 : mFdp->ConsumeIntegral<int32_t>();
159 int32_t code = mFdp->ConsumeBool() ? mFdp->PickValueInArray(kValidCodes)
160 : mFdp->ConsumeIntegral<int32_t>();
161 events.push_back({
162 .when = mFdp->ConsumeIntegral<nsecs_t>(),
163 .readTime = mFdp->ConsumeIntegral<nsecs_t>(),
164 .deviceId = mFdp->ConsumeIntegral<int32_t>(),
165 .type = type,
166 .code = code,
167 .value = mFdp->ConsumeIntegral<int32_t>(),
168 });
169 }
170 return events;
Michael Ensingb8d93262020-05-12 00:41:30 -0700171 }
172 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; }
173
174 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
175 int32_t deviceId, int32_t absCode) const override {
176 return base::ResultError("Fuzzer", UNKNOWN_ERROR);
177 };
178 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
179 // containing the raw info of the sysfs node structure.
180 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
181 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
182 int32_t BatteryId) const override {
183 return std::nullopt;
184 };
185
186 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { return {}; };
187 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
188 return std::nullopt;
189 };
190 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
191 return std::nullopt;
192 };
193 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override{};
194 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
195 int32_t deviceId, int32_t lightId) const override {
196 return std::nullopt;
197 };
198 void setLightIntensities(int32_t deviceId, int32_t lightId,
199 std::unordered_map<LightColor, int32_t> intensities) override{};
200
201 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
202 return mFdp->PickValueInArray<InputDeviceCountryCode>(kCountryCodes);
203 };
204
205 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
206 return mFdp->ConsumeIntegral<int32_t>();
207 }
208 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
209 return mFdp->ConsumeIntegral<int32_t>();
210 }
211 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
212 return mFdp->ConsumeIntegral<int32_t>();
213 }
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000214 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700215 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
216 return mFdp->ConsumeIntegral<int32_t>();
217 }
218 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
219 int32_t* outValue) const override {
220 return mFdp->ConsumeIntegral<status_t>();
221 }
222 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
223 uint8_t* outFlags) const override {
224 return mFdp->ConsumeBool();
225 }
226 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
227 return mFdp->ConsumeBool();
228 }
229 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
230 return mFdp->ConsumeBool();
231 }
232 bool hasLed(int32_t deviceId, int32_t led) const override { return mFdp->ConsumeBool(); }
233 void setLedState(int32_t deviceId, int32_t led, bool on) override {}
234 void getVirtualKeyDefinitions(
235 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {}
236 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override {
237 return nullptr;
238 }
239 bool setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) override {
240 return mFdp->ConsumeBool();
241 }
242 void vibrate(int32_t deviceId, const VibrationElement& effect) override {}
243 void cancelVibrate(int32_t deviceId) override {}
244
245 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return {}; };
246
247 /* Query battery level. */
248 std::optional<int32_t> getBatteryCapacity(int32_t deviceId, int32_t batteryId) const override {
249 return std::nullopt;
250 };
251
252 /* Query battery status. */
253 std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const override {
254 return std::nullopt;
255 };
256
257 void requestReopenDevices() override {}
258 void wake() override {}
259 void dump(std::string& dump) const override {}
260 void monitor() const override {}
261 bool isDeviceEnabled(int32_t deviceId) const override { return mFdp->ConsumeBool(); }
262 status_t enableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
263 status_t disableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
264};
265
266class FuzzPointerController : public PointerControllerInterface {
267 std::shared_ptr<FuzzedDataProvider> mFdp;
268
269public:
270 FuzzPointerController(std::shared_ptr<FuzzedDataProvider> mFdp) : mFdp(mFdp) {}
271 ~FuzzPointerController() {}
272 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
273 return mFdp->ConsumeBool();
274 }
275 void move(float deltaX, float deltaY) override {}
276 void setButtonState(int32_t buttonState) override {}
277 int32_t getButtonState() const override { return mFdp->ConsumeIntegral<int32_t>(); }
278 void setPosition(float x, float y) override {}
279 void getPosition(float* outX, float* outY) const override {}
280 void fade(Transition transition) override {}
281 void unfade(Transition transition) override {}
282 void setPresentation(Presentation presentation) override {}
283 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
284 BitSet32 spotIdBits, int32_t displayId) override {}
285 void clearSpots() override {}
286 int32_t getDisplayId() const override { return mFdp->ConsumeIntegral<int32_t>(); }
287 void setDisplayViewport(const DisplayViewport& displayViewport) override {}
288};
289
290class FuzzInputReaderPolicy : public InputReaderPolicyInterface {
291 TouchAffineTransformation mTransform;
292 std::shared_ptr<FuzzPointerController> mPointerController;
293 std::shared_ptr<FuzzedDataProvider> mFdp;
294
295protected:
296 ~FuzzInputReaderPolicy() {}
297
298public:
299 FuzzInputReaderPolicy(std::shared_ptr<FuzzedDataProvider> mFdp) : mFdp(mFdp) {
300 mPointerController = std::make_shared<FuzzPointerController>(mFdp);
301 }
302 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {}
303 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
304 return mPointerController;
305 }
306 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {}
307 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
308 const InputDeviceIdentifier& identifier) override {
309 return nullptr;
310 }
311 std::string getDeviceAlias(const InputDeviceIdentifier& identifier) {
312 return mFdp->ConsumeRandomLengthString(32);
313 }
314 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000315 ui::Rotation surfaceRotation) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700316 return mTransform;
317 }
318 void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000319 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700320};
321
322class FuzzInputListener : public virtual InputListenerInterface {
323public:
324 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override {}
325 void notifyKey(const NotifyKeyArgs* args) override {}
326 void notifyMotion(const NotifyMotionArgs* args) override {}
327 void notifySwitch(const NotifySwitchArgs* args) override {}
328 void notifySensor(const NotifySensorArgs* args) override{};
329 void notifyVibratorState(const NotifyVibratorStateArgs* args) override{};
330 void notifyDeviceReset(const NotifyDeviceResetArgs* args) override {}
331 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override{};
332};
333
334class FuzzInputReaderContext : public InputReaderContext {
335 std::shared_ptr<EventHubInterface> mEventHub;
336 sp<InputReaderPolicyInterface> mPolicy;
Michael Ensingb8d93262020-05-12 00:41:30 -0700337 std::shared_ptr<FuzzedDataProvider> mFdp;
338
339public:
340 FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
341 const sp<InputReaderPolicyInterface>& policy,
342 InputListenerInterface& listener,
343 std::shared_ptr<FuzzedDataProvider> mFdp)
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700344 : mEventHub(eventHub), mPolicy(policy), mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700345 ~FuzzInputReaderContext() {}
346 void updateGlobalMetaState() override {}
347 int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); }
348 void disableVirtualKeysUntil(nsecs_t time) override {}
349 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override {
350 return mFdp->ConsumeBool();
351 }
352 void fadePointer() override {}
353 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override {
354 return mPolicy->obtainPointerController(0);
355 }
356 void requestTimeoutAtTime(nsecs_t when) override {}
357 int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); }
358 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700359 std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override {
360 return {};
361 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700362 InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700363 EventHubInterface* getEventHub() override { return mEventHub.get(); }
364 int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); }
365
366 void updateLedMetaState(int32_t metaState) override{};
367 int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); };
Prabir Pradhanda20b172022-09-26 17:01:18 +0000368 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700369};
370
371} // namespace android