blob: 7c9be5c8d49ef667df455e99e12bf14577dccafe [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 }
89 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
90 *outConfiguration = mFuzzConfig;
91 }
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() {}
228 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
229 return mFdp->ConsumeBool();
230 }
231 void move(float deltaX, float deltaY) override {}
232 void setButtonState(int32_t buttonState) override {}
233 int32_t getButtonState() const override { return mFdp->ConsumeIntegral<int32_t>(); }
234 void setPosition(float x, float y) override {}
235 void getPosition(float* outX, float* outY) const override {}
236 void fade(Transition transition) override {}
237 void unfade(Transition transition) override {}
238 void setPresentation(Presentation presentation) override {}
239 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
240 BitSet32 spotIdBits, int32_t displayId) override {}
241 void clearSpots() override {}
242 int32_t getDisplayId() const override { return mFdp->ConsumeIntegral<int32_t>(); }
243 void setDisplayViewport(const DisplayViewport& displayViewport) override {}
244};
245
246class FuzzInputReaderPolicy : public InputReaderPolicyInterface {
247 TouchAffineTransformation mTransform;
248 std::shared_ptr<FuzzPointerController> mPointerController;
Aditya Wazireea5be02022-10-31 12:40:10 +0530249 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700250
251protected:
252 ~FuzzInputReaderPolicy() {}
253
254public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530255 FuzzInputReaderPolicy(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -0700256 mPointerController = std::make_shared<FuzzPointerController>(mFdp);
257 }
258 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {}
259 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
260 return mPointerController;
261 }
262 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {}
263 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
264 const InputDeviceIdentifier& identifier) override {
265 return nullptr;
266 }
267 std::string getDeviceAlias(const InputDeviceIdentifier& identifier) {
268 return mFdp->ConsumeRandomLengthString(32);
269 }
270 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000271 ui::Rotation surfaceRotation) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700272 return mTransform;
273 }
274 void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000275 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700276};
277
278class FuzzInputListener : public virtual InputListenerInterface {
279public:
280 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override {}
281 void notifyKey(const NotifyKeyArgs* args) override {}
282 void notifyMotion(const NotifyMotionArgs* args) override {}
283 void notifySwitch(const NotifySwitchArgs* args) override {}
284 void notifySensor(const NotifySensorArgs* args) override{};
285 void notifyVibratorState(const NotifyVibratorStateArgs* args) override{};
286 void notifyDeviceReset(const NotifyDeviceResetArgs* args) override {}
287 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override{};
288};
289
290class FuzzInputReaderContext : public InputReaderContext {
291 std::shared_ptr<EventHubInterface> mEventHub;
292 sp<InputReaderPolicyInterface> mPolicy;
Aditya Wazireea5be02022-10-31 12:40:10 +0530293 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700294
295public:
296 FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
297 const sp<InputReaderPolicyInterface>& policy,
298 InputListenerInterface& listener,
Aditya Wazireea5be02022-10-31 12:40:10 +0530299 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp)
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700300 : mEventHub(eventHub), mPolicy(policy), mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700301 ~FuzzInputReaderContext() {}
302 void updateGlobalMetaState() override {}
303 int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); }
304 void disableVirtualKeysUntil(nsecs_t time) override {}
305 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override {
306 return mFdp->ConsumeBool();
307 }
308 void fadePointer() override {}
309 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override {
310 return mPolicy->obtainPointerController(0);
311 }
312 void requestTimeoutAtTime(nsecs_t when) override {}
313 int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); }
314 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700315 std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override {
316 return {};
317 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700318 InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700319 EventHubInterface* getEventHub() override { return mEventHub.get(); }
320 int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); }
321
322 void updateLedMetaState(int32_t metaState) override{};
323 int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); };
Prabir Pradhanda20b172022-09-26 17:01:18 +0000324 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700325};
326
327} // namespace android