blob: 698daf555a01d97c2789d8749782bf3348a63888 [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
23constexpr 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
34constexpr 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 Ensingb8d93262020-05-12 00:41:30 -070065constexpr size_t kMaxSize = 256;
66
67namespace android {
68
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070069template<class Fdp>
70ToolType getFuzzedToolType(Fdp& fdp) {
71 const int32_t toolType = fdp.template ConsumeIntegralInRange<int32_t>(
72 static_cast<int32_t>(ToolType::ftl_first),
73 static_cast<int32_t>(ToolType::ftl_last));
74 return static_cast<ToolType>(toolType);
75}
76
Harry Cutts656e0ad2023-06-16 16:39:55 +000077template <class Fdp>
78RawEvent getFuzzedRawEvent(Fdp& fdp) {
79 const int32_t type = fdp.ConsumeBool() ? fdp.PickValueInArray(kValidTypes)
80 : fdp.template ConsumeIntegral<int32_t>();
81 const int32_t code = fdp.ConsumeBool() ? fdp.PickValueInArray(kValidCodes)
82 : fdp.template ConsumeIntegral<int32_t>();
83 return RawEvent{
84 .when = fdp.template ConsumeIntegral<nsecs_t>(),
85 .readTime = fdp.template ConsumeIntegral<nsecs_t>(),
86 .deviceId = fdp.template ConsumeIntegral<int32_t>(),
87 .type = type,
88 .code = code,
89 .value = fdp.template ConsumeIntegral<int32_t>(),
90 };
91}
92
Michael Ensingb8d93262020-05-12 00:41:30 -070093class FuzzEventHub : public EventHubInterface {
94 InputDeviceIdentifier mIdentifier;
95 std::vector<TouchVideoFrame> mVideoFrames;
96 PropertyMap mFuzzConfig;
Aditya Wazireea5be02022-10-31 12:40:10 +053097 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -070098
99public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530100 FuzzEventHub(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(std::move(fdp)) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700101 ~FuzzEventHub() {}
102 void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700103
104 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
105 return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>());
106 }
107 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
108 return mIdentifier;
109 }
110 int32_t getDeviceControllerNumber(int32_t deviceId) const override {
111 return mFdp->ConsumeIntegral<int32_t>();
112 }
Harry Cuttsc34f7582023-03-07 16:23:30 +0000113 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override {
114 return mFuzzConfig;
Michael Ensingb8d93262020-05-12 00:41:30 -0700115 }
116 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
117 RawAbsoluteAxisInfo* outAxisInfo) const override {
118 return mFdp->ConsumeIntegral<status_t>();
119 }
120 bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); }
121 bool hasInputProperty(int32_t deviceId, int property) const override {
122 return mFdp->ConsumeBool();
123 }
124 bool hasMscEvent(int32_t deviceId, int mscEvent) const override { return mFdp->ConsumeBool(); }
125 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
126 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
127 return mFdp->ConsumeIntegral<status_t>();
128 }
129 status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override {
130 return mFdp->ConsumeIntegral<status_t>();
131 }
132 void setExcludedDevices(const std::vector<std::string>& devices) override {}
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700133 std::vector<RawEvent> getEvents(int timeoutMillis) override {
134 std::vector<RawEvent> events;
135 const size_t count = mFdp->ConsumeIntegralInRange<size_t>(0, kMaxSize);
136 for (size_t i = 0; i < count; ++i) {
Harry Cutts656e0ad2023-06-16 16:39:55 +0000137 events.push_back(getFuzzedRawEvent(*mFdp));
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700138 }
139 return events;
Michael Ensingb8d93262020-05-12 00:41:30 -0700140 }
141 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; }
142
143 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
144 int32_t deviceId, int32_t absCode) const override {
145 return base::ResultError("Fuzzer", UNKNOWN_ERROR);
146 };
147 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
148 // containing the raw info of the sysfs node structure.
149 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
150 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
151 int32_t BatteryId) const override {
152 return std::nullopt;
153 };
154
155 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { return {}; };
156 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
157 return std::nullopt;
158 };
159 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
160 return std::nullopt;
161 };
162 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override{};
163 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
164 int32_t deviceId, int32_t lightId) const override {
165 return std::nullopt;
166 };
167 void setLightIntensities(int32_t deviceId, int32_t lightId,
168 std::unordered_map<LightColor, int32_t> intensities) override{};
169
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000170 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override {
171 return std::nullopt;
Michael Ensingb8d93262020-05-12 00:41:30 -0700172 };
173
174 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
175 return mFdp->ConsumeIntegral<int32_t>();
176 }
177 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
178 return mFdp->ConsumeIntegral<int32_t>();
179 }
180 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
181 return mFdp->ConsumeIntegral<int32_t>();
182 }
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000183 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700184 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
185 return mFdp->ConsumeIntegral<int32_t>();
186 }
187 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
188 int32_t* outValue) const override {
189 return mFdp->ConsumeIntegral<status_t>();
190 }
191 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
192 uint8_t* outFlags) const override {
193 return mFdp->ConsumeBool();
194 }
195 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
196 return mFdp->ConsumeBool();
197 }
198 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
199 return mFdp->ConsumeBool();
200 }
201 bool hasLed(int32_t deviceId, int32_t led) const override { return mFdp->ConsumeBool(); }
202 void setLedState(int32_t deviceId, int32_t led, bool on) override {}
203 void getVirtualKeyDefinitions(
204 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {}
205 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override {
206 return nullptr;
207 }
208 bool setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) override {
209 return mFdp->ConsumeBool();
210 }
211 void vibrate(int32_t deviceId, const VibrationElement& effect) override {}
212 void cancelVibrate(int32_t deviceId) override {}
213
214 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return {}; };
215
216 /* Query battery level. */
217 std::optional<int32_t> getBatteryCapacity(int32_t deviceId, int32_t batteryId) const override {
218 return std::nullopt;
219 };
220
221 /* Query battery status. */
222 std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const override {
223 return std::nullopt;
224 };
225
226 void requestReopenDevices() override {}
227 void wake() override {}
228 void dump(std::string& dump) const override {}
229 void monitor() const override {}
230 bool isDeviceEnabled(int32_t deviceId) const override { return mFdp->ConsumeBool(); }
231 status_t enableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
232 status_t disableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000233 void sysfsNodeChanged(const std::string& sysfsNodePath) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700234};
235
236class FuzzPointerController : public PointerControllerInterface {
Aditya Wazireea5be02022-10-31 12:40:10 +0530237 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700238
239public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530240 FuzzPointerController(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700241 ~FuzzPointerController() {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000242 std::optional<FloatRect> getBounds() const override {
243 if (mFdp->ConsumeBool()) {
244 return {};
245 } else {
246 return FloatRect{mFdp->ConsumeFloatingPoint<float>(),
247 mFdp->ConsumeFloatingPoint<float>(),
248 mFdp->ConsumeFloatingPoint<float>(),
249 mFdp->ConsumeFloatingPoint<float>()};
250 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700251 }
252 void move(float deltaX, float deltaY) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700253 void setPosition(float x, float y) override {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000254 FloatPoint getPosition() const override {
255 return {mFdp->ConsumeFloatingPoint<float>(), mFdp->ConsumeFloatingPoint<float>()};
256 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700257 void fade(Transition transition) override {}
258 void unfade(Transition transition) override {}
259 void setPresentation(Presentation presentation) override {}
260 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
261 BitSet32 spotIdBits, int32_t displayId) override {}
262 void clearSpots() override {}
263 int32_t getDisplayId() const override { return mFdp->ConsumeIntegral<int32_t>(); }
264 void setDisplayViewport(const DisplayViewport& displayViewport) override {}
265};
266
267class FuzzInputReaderPolicy : public InputReaderPolicyInterface {
268 TouchAffineTransformation mTransform;
269 std::shared_ptr<FuzzPointerController> mPointerController;
Aditya Wazireea5be02022-10-31 12:40:10 +0530270 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700271
272protected:
273 ~FuzzInputReaderPolicy() {}
274
275public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530276 FuzzInputReaderPolicy(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -0700277 mPointerController = std::make_shared<FuzzPointerController>(mFdp);
278 }
279 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {}
280 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
281 return mPointerController;
282 }
283 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {}
284 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
285 const InputDeviceIdentifier& identifier) override {
286 return nullptr;
287 }
288 std::string getDeviceAlias(const InputDeviceIdentifier& identifier) {
289 return mFdp->ConsumeRandomLengthString(32);
290 }
291 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000292 ui::Rotation surfaceRotation) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700293 return mTransform;
294 }
295 void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000296 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700297};
298
299class FuzzInputListener : public virtual InputListenerInterface {
300public:
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000301 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override {}
Prabir Pradhanc392d8f2023-04-13 19:32:51 +0000302 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override {}
303 void notifyKey(const NotifyKeyArgs& args) override {}
304 void notifyMotion(const NotifyMotionArgs& args) override {}
305 void notifySwitch(const NotifySwitchArgs& args) override {}
306 void notifySensor(const NotifySensorArgs& args) override{};
307 void notifyVibratorState(const NotifyVibratorStateArgs& args) override{};
308 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override {}
309 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override{};
Michael Ensingb8d93262020-05-12 00:41:30 -0700310};
311
312class FuzzInputReaderContext : public InputReaderContext {
313 std::shared_ptr<EventHubInterface> mEventHub;
314 sp<InputReaderPolicyInterface> mPolicy;
Aditya Wazireea5be02022-10-31 12:40:10 +0530315 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700316
317public:
318 FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
319 const sp<InputReaderPolicyInterface>& policy,
320 InputListenerInterface& listener,
Aditya Wazireea5be02022-10-31 12:40:10 +0530321 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp)
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700322 : mEventHub(eventHub), mPolicy(policy), mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700323 ~FuzzInputReaderContext() {}
324 void updateGlobalMetaState() override {}
325 int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); }
326 void disableVirtualKeysUntil(nsecs_t time) override {}
327 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override {
328 return mFdp->ConsumeBool();
329 }
330 void fadePointer() override {}
331 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override {
332 return mPolicy->obtainPointerController(0);
333 }
334 void requestTimeoutAtTime(nsecs_t when) override {}
335 int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); }
336 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700337 std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override {
338 return {};
339 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700340 InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700341 EventHubInterface* getEventHub() override { return mEventHub.get(); }
342 int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); }
343
344 void updateLedMetaState(int32_t metaState) override{};
345 int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); };
Prabir Pradhanda20b172022-09-26 17:01:18 +0000346 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700347};
348
349} // namespace android