blob: bdedfdfa0c7ffa3fc518d7f5def8d3c086873f7d [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
Harry Cuttsccb75e82023-06-23 14:08:06 +000018#include <map>
Harry Cuttsf61c0472023-07-10 14:45:15 +000019#include <memory>
Harry Cuttsccb75e82023-06-23 14:08:06 +000020
21#include <EventHub.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070022#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Aditya Wazireea5be02022-10-31 12:40:10 +053025#include <ThreadSafeFuzzedDataProvider.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070026
27constexpr size_t kValidTypes[] = {EV_SW,
28 EV_SYN,
Michael Ensingb8d93262020-05-12 00:41:30 -070029 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,
Michael Ensingb8d93262020-05-12 00:41:30 -070052 MSC_SCAN,
53 REL_X,
54 REL_Y,
55 REL_WHEEL,
56 REL_HWHEEL,
57 BTN_LEFT,
58 BTN_RIGHT,
59 BTN_MIDDLE,
60 BTN_BACK,
61 BTN_SIDE,
62 BTN_FORWARD,
63 BTN_EXTRA,
64 BTN_TASK,
65};
66
Michael Ensingb8d93262020-05-12 00:41:30 -070067constexpr size_t kMaxSize = 256;
68
69namespace android {
70
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070071template<class Fdp>
72ToolType getFuzzedToolType(Fdp& fdp) {
73 const int32_t toolType = fdp.template ConsumeIntegralInRange<int32_t>(
74 static_cast<int32_t>(ToolType::ftl_first),
75 static_cast<int32_t>(ToolType::ftl_last));
76 return static_cast<ToolType>(toolType);
77}
78
Harry Cutts656e0ad2023-06-16 16:39:55 +000079template <class Fdp>
80RawEvent getFuzzedRawEvent(Fdp& fdp) {
81 const int32_t type = fdp.ConsumeBool() ? fdp.PickValueInArray(kValidTypes)
82 : fdp.template ConsumeIntegral<int32_t>();
83 const int32_t code = fdp.ConsumeBool() ? fdp.PickValueInArray(kValidCodes)
84 : fdp.template ConsumeIntegral<int32_t>();
85 return RawEvent{
86 .when = fdp.template ConsumeIntegral<nsecs_t>(),
87 .readTime = fdp.template ConsumeIntegral<nsecs_t>(),
88 .deviceId = fdp.template ConsumeIntegral<int32_t>(),
89 .type = type,
90 .code = code,
91 .value = fdp.template ConsumeIntegral<int32_t>(),
92 };
93}
94
Michael Ensingb8d93262020-05-12 00:41:30 -070095class FuzzEventHub : public EventHubInterface {
96 InputDeviceIdentifier mIdentifier;
97 std::vector<TouchVideoFrame> mVideoFrames;
98 PropertyMap mFuzzConfig;
Harry Cuttsccb75e82023-06-23 14:08:06 +000099 std::map<int32_t /* deviceId */, std::map<int /* axis */, RawAbsoluteAxisInfo>> mAxes;
Aditya Wazireea5be02022-10-31 12:40:10 +0530100 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700101
102public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530103 FuzzEventHub(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(std::move(fdp)) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700104 ~FuzzEventHub() {}
105 void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700106
107 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
108 return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>());
109 }
110 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
111 return mIdentifier;
112 }
113 int32_t getDeviceControllerNumber(int32_t deviceId) const override {
114 return mFdp->ConsumeIntegral<int32_t>();
115 }
Harry Cuttsc34f7582023-03-07 16:23:30 +0000116 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override {
117 return mFuzzConfig;
Michael Ensingb8d93262020-05-12 00:41:30 -0700118 }
Harry Cuttsccb75e82023-06-23 14:08:06 +0000119 void setAbsoluteAxisInfo(int32_t deviceId, int axis, const RawAbsoluteAxisInfo& axisInfo) {
120 mAxes[deviceId][axis] = axisInfo;
121 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700122 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
123 RawAbsoluteAxisInfo* outAxisInfo) const override {
Harry Cuttsccb75e82023-06-23 14:08:06 +0000124 if (auto deviceAxesIt = mAxes.find(deviceId); deviceAxesIt != mAxes.end()) {
125 const std::map<int, RawAbsoluteAxisInfo>& deviceAxes = deviceAxesIt->second;
126 if (auto axisInfoIt = deviceAxes.find(axis); axisInfoIt != deviceAxes.end()) {
127 *outAxisInfo = axisInfoIt->second;
128 return OK;
129 }
130 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700131 return mFdp->ConsumeIntegral<status_t>();
132 }
133 bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); }
134 bool hasInputProperty(int32_t deviceId, int property) const override {
135 return mFdp->ConsumeBool();
136 }
137 bool hasMscEvent(int32_t deviceId, int mscEvent) const override { return mFdp->ConsumeBool(); }
138 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
139 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
140 return mFdp->ConsumeIntegral<status_t>();
141 }
142 status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override {
143 return mFdp->ConsumeIntegral<status_t>();
144 }
145 void setExcludedDevices(const std::vector<std::string>& devices) override {}
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700146 std::vector<RawEvent> getEvents(int timeoutMillis) override {
147 std::vector<RawEvent> events;
148 const size_t count = mFdp->ConsumeIntegralInRange<size_t>(0, kMaxSize);
149 for (size_t i = 0; i < count; ++i) {
Harry Cutts656e0ad2023-06-16 16:39:55 +0000150 events.push_back(getFuzzedRawEvent(*mFdp));
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700151 }
152 return events;
Michael Ensingb8d93262020-05-12 00:41:30 -0700153 }
154 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; }
155
156 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
157 int32_t deviceId, int32_t absCode) const override {
158 return base::ResultError("Fuzzer", UNKNOWN_ERROR);
159 };
160 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
161 // containing the raw info of the sysfs node structure.
162 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
163 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
164 int32_t BatteryId) const override {
165 return std::nullopt;
166 };
167
168 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { return {}; };
169 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
170 return std::nullopt;
171 };
172 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
173 return std::nullopt;
174 };
175 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override{};
176 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
177 int32_t deviceId, int32_t lightId) const override {
178 return std::nullopt;
179 };
180 void setLightIntensities(int32_t deviceId, int32_t lightId,
181 std::unordered_map<LightColor, int32_t> intensities) override{};
182
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000183 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override {
184 return std::nullopt;
Michael Ensingb8d93262020-05-12 00:41:30 -0700185 };
186
187 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
188 return mFdp->ConsumeIntegral<int32_t>();
189 }
190 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
191 return mFdp->ConsumeIntegral<int32_t>();
192 }
193 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
194 return mFdp->ConsumeIntegral<int32_t>();
195 }
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000196 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700197 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
198 return mFdp->ConsumeIntegral<int32_t>();
199 }
200 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
201 int32_t* outValue) const override {
202 return mFdp->ConsumeIntegral<status_t>();
203 }
204 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
205 uint8_t* outFlags) const override {
206 return mFdp->ConsumeBool();
207 }
208 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
209 return mFdp->ConsumeBool();
210 }
211 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
212 return mFdp->ConsumeBool();
213 }
214 bool hasLed(int32_t deviceId, int32_t led) const override { return mFdp->ConsumeBool(); }
215 void setLedState(int32_t deviceId, int32_t led, bool on) override {}
216 void getVirtualKeyDefinitions(
217 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {}
218 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override {
219 return nullptr;
220 }
221 bool setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) override {
222 return mFdp->ConsumeBool();
223 }
224 void vibrate(int32_t deviceId, const VibrationElement& effect) override {}
225 void cancelVibrate(int32_t deviceId) override {}
226
227 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return {}; };
228
229 /* Query battery level. */
230 std::optional<int32_t> getBatteryCapacity(int32_t deviceId, int32_t batteryId) const override {
231 return std::nullopt;
232 };
233
234 /* Query battery status. */
235 std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const override {
236 return std::nullopt;
237 };
238
239 void requestReopenDevices() override {}
240 void wake() override {}
241 void dump(std::string& dump) const override {}
242 void monitor() const override {}
243 bool isDeviceEnabled(int32_t deviceId) const override { return mFdp->ConsumeBool(); }
244 status_t enableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
245 status_t disableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000246 void sysfsNodeChanged(const std::string& sysfsNodePath) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700247};
248
249class FuzzPointerController : public PointerControllerInterface {
Aditya Wazireea5be02022-10-31 12:40:10 +0530250 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700251
252public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530253 FuzzPointerController(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700254 ~FuzzPointerController() {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000255 std::optional<FloatRect> getBounds() const override {
256 if (mFdp->ConsumeBool()) {
257 return {};
258 } else {
259 return FloatRect{mFdp->ConsumeFloatingPoint<float>(),
260 mFdp->ConsumeFloatingPoint<float>(),
261 mFdp->ConsumeFloatingPoint<float>(),
262 mFdp->ConsumeFloatingPoint<float>()};
263 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700264 }
265 void move(float deltaX, float deltaY) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700266 void setPosition(float x, float y) override {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000267 FloatPoint getPosition() const override {
268 return {mFdp->ConsumeFloatingPoint<float>(), mFdp->ConsumeFloatingPoint<float>()};
269 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700270 void fade(Transition transition) override {}
271 void unfade(Transition transition) override {}
272 void setPresentation(Presentation presentation) override {}
273 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
274 BitSet32 spotIdBits, int32_t displayId) override {}
275 void clearSpots() override {}
276 int32_t getDisplayId() const override { return mFdp->ConsumeIntegral<int32_t>(); }
277 void setDisplayViewport(const DisplayViewport& displayViewport) override {}
278};
279
280class FuzzInputReaderPolicy : public InputReaderPolicyInterface {
281 TouchAffineTransformation mTransform;
282 std::shared_ptr<FuzzPointerController> mPointerController;
Aditya Wazireea5be02022-10-31 12:40:10 +0530283 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700284
285protected:
286 ~FuzzInputReaderPolicy() {}
287
288public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530289 FuzzInputReaderPolicy(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -0700290 mPointerController = std::make_shared<FuzzPointerController>(mFdp);
291 }
292 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {}
293 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
294 return mPointerController;
295 }
296 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {}
297 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000298 const InputDeviceIdentifier& identifier,
299 const std::optional<KeyboardLayoutInfo> layoutInfo) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700300 return nullptr;
301 }
302 std::string getDeviceAlias(const InputDeviceIdentifier& identifier) {
303 return mFdp->ConsumeRandomLengthString(32);
304 }
305 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000306 ui::Rotation surfaceRotation) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700307 return mTransform;
308 }
309 void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000310 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Arpit Singhb3b3f732023-07-04 14:30:05 +0000311 bool isInputMethodConnectionActive() override { return mFdp->ConsumeBool(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700312};
313
314class FuzzInputListener : public virtual InputListenerInterface {
315public:
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000316 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override {}
Prabir Pradhanc392d8f2023-04-13 19:32:51 +0000317 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override {}
318 void notifyKey(const NotifyKeyArgs& args) override {}
319 void notifyMotion(const NotifyMotionArgs& args) override {}
320 void notifySwitch(const NotifySwitchArgs& args) override {}
321 void notifySensor(const NotifySensorArgs& args) override{};
322 void notifyVibratorState(const NotifyVibratorStateArgs& args) override{};
323 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override {}
324 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override{};
Michael Ensingb8d93262020-05-12 00:41:30 -0700325};
326
327class FuzzInputReaderContext : public InputReaderContext {
328 std::shared_ptr<EventHubInterface> mEventHub;
329 sp<InputReaderPolicyInterface> mPolicy;
Aditya Wazireea5be02022-10-31 12:40:10 +0530330 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700331
332public:
333 FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
Harry Cuttsf61c0472023-07-10 14:45:15 +0000334 std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp)
335 : mEventHub(eventHub), mPolicy(sp<FuzzInputReaderPolicy>::make(fdp)), mFdp(fdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700336 ~FuzzInputReaderContext() {}
337 void updateGlobalMetaState() override {}
338 int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); }
339 void disableVirtualKeysUntil(nsecs_t time) override {}
340 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override {
341 return mFdp->ConsumeBool();
342 }
343 void fadePointer() override {}
344 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override {
345 return mPolicy->obtainPointerController(0);
346 }
347 void requestTimeoutAtTime(nsecs_t when) override {}
348 int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); }
349 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700350 std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override {
351 return {};
352 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700353 InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700354 EventHubInterface* getEventHub() override { return mEventHub.get(); }
355 int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); }
356
357 void updateLedMetaState(int32_t metaState) override{};
358 int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); };
Prabir Pradhanda20b172022-09-26 17:01:18 +0000359 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Arpit Singha5ea7c12023-07-05 15:39:25 +0000360
361 void setPreventingTouchpadTaps(bool prevent) {}
362 bool isPreventingTouchpadTaps() { return mFdp->ConsumeBool(); };
Michael Ensingb8d93262020-05-12 00:41:30 -0700363};
364
Harry Cuttsf61c0472023-07-10 14:45:15 +0000365template <class Fdp>
366InputDevice getFuzzedInputDevice(Fdp& fdp, FuzzInputReaderContext* context) {
367 InputDeviceIdentifier identifier;
368 identifier.name = fdp.ConsumeRandomLengthString(16);
369 identifier.location = fdp.ConsumeRandomLengthString(12);
370 int32_t deviceID = fdp.ConsumeIntegralInRange(0, 5);
371 int32_t deviceGeneration = fdp.ConsumeIntegralInRange(0, 5);
372 return InputDevice(context, deviceID, deviceGeneration, identifier);
373}
374
375template <class Fdp>
376void configureAndResetDevice(Fdp& fdp, InputDevice& device) {
377 nsecs_t arbitraryTime = fdp.template ConsumeIntegral<nsecs_t>();
378 std::list<NotifyArgs> out;
379 out += device.configure(arbitraryTime, /*readerConfig=*/{}, /*changes=*/{});
380 out += device.reset(arbitraryTime);
381}
382
383template <class Fdp, class T, typename... Args>
384T& getMapperForDevice(Fdp& fdp, InputDevice& device, Args... args) {
385 int32_t eventhubId = fdp.template ConsumeIntegral<int32_t>();
386 // ensure a device entry exists for this eventHubId
387 device.addEmptyEventHubDevice(eventhubId);
388 configureAndResetDevice(fdp, device);
389
390 return device.template constructAndAddMapper<T>(eventhubId, args...);
391}
392
Michael Ensingb8d93262020-05-12 00:41:30 -0700393} // namespace android