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