blob: 2cb5cdf9fc907a3755c8612748610faadb72c71c [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
69class FuzzEventHub : public EventHubInterface {
70 InputDeviceIdentifier mIdentifier;
71 std::vector<TouchVideoFrame> mVideoFrames;
72 PropertyMap mFuzzConfig;
Aditya Wazireea5be02022-10-31 12:40:10 +053073 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -070074
75public:
Aditya Wazireea5be02022-10-31 12:40:10 +053076 FuzzEventHub(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(std::move(fdp)) {}
Michael Ensingb8d93262020-05-12 00:41:30 -070077 ~FuzzEventHub() {}
78 void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); }
Michael Ensingb8d93262020-05-12 00:41:30 -070079
80 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
81 return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>());
82 }
83 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
84 return mIdentifier;
85 }
86 int32_t getDeviceControllerNumber(int32_t deviceId) const override {
87 return mFdp->ConsumeIntegral<int32_t>();
88 }
Harry Cuttsc34f7582023-03-07 16:23:30 +000089 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override {
90 return mFuzzConfig;
Michael Ensingb8d93262020-05-12 00:41:30 -070091 }
92 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
93 RawAbsoluteAxisInfo* outAxisInfo) const override {
94 return mFdp->ConsumeIntegral<status_t>();
95 }
96 bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); }
97 bool hasInputProperty(int32_t deviceId, int property) const override {
98 return mFdp->ConsumeBool();
99 }
100 bool hasMscEvent(int32_t deviceId, int mscEvent) const override { return mFdp->ConsumeBool(); }
101 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
102 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
103 return mFdp->ConsumeIntegral<status_t>();
104 }
105 status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override {
106 return mFdp->ConsumeIntegral<status_t>();
107 }
108 void setExcludedDevices(const std::vector<std::string>& devices) override {}
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700109 std::vector<RawEvent> getEvents(int timeoutMillis) override {
110 std::vector<RawEvent> events;
111 const size_t count = mFdp->ConsumeIntegralInRange<size_t>(0, kMaxSize);
112 for (size_t i = 0; i < count; ++i) {
113 int32_t type = mFdp->ConsumeBool() ? mFdp->PickValueInArray(kValidTypes)
114 : mFdp->ConsumeIntegral<int32_t>();
115 int32_t code = mFdp->ConsumeBool() ? mFdp->PickValueInArray(kValidCodes)
116 : mFdp->ConsumeIntegral<int32_t>();
117 events.push_back({
118 .when = mFdp->ConsumeIntegral<nsecs_t>(),
119 .readTime = mFdp->ConsumeIntegral<nsecs_t>(),
120 .deviceId = mFdp->ConsumeIntegral<int32_t>(),
121 .type = type,
122 .code = code,
123 .value = mFdp->ConsumeIntegral<int32_t>(),
124 });
125 }
126 return events;
Michael Ensingb8d93262020-05-12 00:41:30 -0700127 }
128 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; }
129
130 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
131 int32_t deviceId, int32_t absCode) const override {
132 return base::ResultError("Fuzzer", UNKNOWN_ERROR);
133 };
134 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
135 // containing the raw info of the sysfs node structure.
136 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
137 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
138 int32_t BatteryId) const override {
139 return std::nullopt;
140 };
141
142 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { return {}; };
143 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
144 return std::nullopt;
145 };
146 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
147 return std::nullopt;
148 };
149 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override{};
150 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
151 int32_t deviceId, int32_t lightId) const override {
152 return std::nullopt;
153 };
154 void setLightIntensities(int32_t deviceId, int32_t lightId,
155 std::unordered_map<LightColor, int32_t> intensities) override{};
156
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000157 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override {
158 return std::nullopt;
Michael Ensingb8d93262020-05-12 00:41:30 -0700159 };
160
161 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
162 return mFdp->ConsumeIntegral<int32_t>();
163 }
164 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
165 return mFdp->ConsumeIntegral<int32_t>();
166 }
167 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
168 return mFdp->ConsumeIntegral<int32_t>();
169 }
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000170 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700171 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
172 return mFdp->ConsumeIntegral<int32_t>();
173 }
174 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
175 int32_t* outValue) const override {
176 return mFdp->ConsumeIntegral<status_t>();
177 }
178 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
179 uint8_t* outFlags) const override {
180 return mFdp->ConsumeBool();
181 }
182 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
183 return mFdp->ConsumeBool();
184 }
185 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
186 return mFdp->ConsumeBool();
187 }
188 bool hasLed(int32_t deviceId, int32_t led) const override { return mFdp->ConsumeBool(); }
189 void setLedState(int32_t deviceId, int32_t led, bool on) override {}
190 void getVirtualKeyDefinitions(
191 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {}
192 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override {
193 return nullptr;
194 }
195 bool setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) override {
196 return mFdp->ConsumeBool();
197 }
198 void vibrate(int32_t deviceId, const VibrationElement& effect) override {}
199 void cancelVibrate(int32_t deviceId) override {}
200
201 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return {}; };
202
203 /* Query battery level. */
204 std::optional<int32_t> getBatteryCapacity(int32_t deviceId, int32_t batteryId) const override {
205 return std::nullopt;
206 };
207
208 /* Query battery status. */
209 std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const override {
210 return std::nullopt;
211 };
212
213 void requestReopenDevices() override {}
214 void wake() override {}
215 void dump(std::string& dump) const override {}
216 void monitor() const override {}
217 bool isDeviceEnabled(int32_t deviceId) const override { return mFdp->ConsumeBool(); }
218 status_t enableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
219 status_t disableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
220};
221
222class FuzzPointerController : public PointerControllerInterface {
Aditya Wazireea5be02022-10-31 12:40:10 +0530223 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700224
225public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530226 FuzzPointerController(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700227 ~FuzzPointerController() {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000228 std::optional<FloatRect> getBounds() const override {
229 if (mFdp->ConsumeBool()) {
230 return {};
231 } else {
232 return FloatRect{mFdp->ConsumeFloatingPoint<float>(),
233 mFdp->ConsumeFloatingPoint<float>(),
234 mFdp->ConsumeFloatingPoint<float>(),
235 mFdp->ConsumeFloatingPoint<float>()};
236 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700237 }
238 void move(float deltaX, float deltaY) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700239 void setPosition(float x, float y) override {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000240 FloatPoint getPosition() const override {
241 return {mFdp->ConsumeFloatingPoint<float>(), mFdp->ConsumeFloatingPoint<float>()};
242 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700243 void fade(Transition transition) override {}
244 void unfade(Transition transition) override {}
245 void setPresentation(Presentation presentation) override {}
246 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
247 BitSet32 spotIdBits, int32_t displayId) override {}
248 void clearSpots() override {}
249 int32_t getDisplayId() const override { return mFdp->ConsumeIntegral<int32_t>(); }
250 void setDisplayViewport(const DisplayViewport& displayViewport) override {}
251};
252
253class FuzzInputReaderPolicy : public InputReaderPolicyInterface {
254 TouchAffineTransformation mTransform;
255 std::shared_ptr<FuzzPointerController> mPointerController;
Aditya Wazireea5be02022-10-31 12:40:10 +0530256 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700257
258protected:
259 ~FuzzInputReaderPolicy() {}
260
261public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530262 FuzzInputReaderPolicy(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -0700263 mPointerController = std::make_shared<FuzzPointerController>(mFdp);
264 }
265 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {}
266 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
267 return mPointerController;
268 }
269 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {}
270 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
271 const InputDeviceIdentifier& identifier) override {
272 return nullptr;
273 }
274 std::string getDeviceAlias(const InputDeviceIdentifier& identifier) {
275 return mFdp->ConsumeRandomLengthString(32);
276 }
277 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000278 ui::Rotation surfaceRotation) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700279 return mTransform;
280 }
281 void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000282 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700283};
284
285class FuzzInputListener : public virtual InputListenerInterface {
286public:
287 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override {}
288 void notifyKey(const NotifyKeyArgs* args) override {}
289 void notifyMotion(const NotifyMotionArgs* args) override {}
290 void notifySwitch(const NotifySwitchArgs* args) override {}
291 void notifySensor(const NotifySensorArgs* args) override{};
292 void notifyVibratorState(const NotifyVibratorStateArgs* args) override{};
293 void notifyDeviceReset(const NotifyDeviceResetArgs* args) override {}
294 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override{};
295};
296
297class FuzzInputReaderContext : public InputReaderContext {
298 std::shared_ptr<EventHubInterface> mEventHub;
299 sp<InputReaderPolicyInterface> mPolicy;
Aditya Wazireea5be02022-10-31 12:40:10 +0530300 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700301
302public:
303 FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
304 const sp<InputReaderPolicyInterface>& policy,
305 InputListenerInterface& listener,
Aditya Wazireea5be02022-10-31 12:40:10 +0530306 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp)
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700307 : mEventHub(eventHub), mPolicy(policy), mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700308 ~FuzzInputReaderContext() {}
309 void updateGlobalMetaState() override {}
310 int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); }
311 void disableVirtualKeysUntil(nsecs_t time) override {}
312 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override {
313 return mFdp->ConsumeBool();
314 }
315 void fadePointer() override {}
316 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override {
317 return mPolicy->obtainPointerController(0);
318 }
319 void requestTimeoutAtTime(nsecs_t when) override {}
320 int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); }
321 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700322 std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override {
323 return {};
324 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700325 InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700326 EventHubInterface* getEventHub() override { return mEventHub.get(); }
327 int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); }
328
329 void updateLedMetaState(int32_t metaState) override{};
330 int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); };
Prabir Pradhanda20b172022-09-26 17:01:18 +0000331 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700332};
333
334} // namespace android