blob: 4a2c98ccaeeebfe48fed730f86323909f52afc8c [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,
Michael Ensingb8d93262020-05-12 00:41:30 -070025 EV_ABS,
26 EV_KEY,
27 EV_MSC,
28 EV_REL,
29 android::EventHubInterface::DEVICE_ADDED,
30 android::EventHubInterface::DEVICE_REMOVED,
31 android::EventHubInterface::FINISHED_DEVICE_SCAN};
32
33constexpr size_t kValidCodes[] = {
34 SYN_REPORT,
35 ABS_MT_SLOT,
36 SYN_MT_REPORT,
37 ABS_MT_POSITION_X,
38 ABS_MT_POSITION_Y,
39 ABS_MT_TOUCH_MAJOR,
40 ABS_MT_TOUCH_MINOR,
41 ABS_MT_WIDTH_MAJOR,
42 ABS_MT_WIDTH_MINOR,
43 ABS_MT_ORIENTATION,
44 ABS_MT_TRACKING_ID,
45 ABS_MT_PRESSURE,
46 ABS_MT_DISTANCE,
47 ABS_MT_TOOL_TYPE,
Michael Ensingb8d93262020-05-12 00:41:30 -070048 MSC_SCAN,
49 REL_X,
50 REL_Y,
51 REL_WHEEL,
52 REL_HWHEEL,
53 BTN_LEFT,
54 BTN_RIGHT,
55 BTN_MIDDLE,
56 BTN_BACK,
57 BTN_SIDE,
58 BTN_FORWARD,
59 BTN_EXTRA,
60 BTN_TASK,
61};
62
Michael Ensingb8d93262020-05-12 00:41:30 -070063constexpr size_t kMaxSize = 256;
64
65namespace android {
66
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070067template<class Fdp>
68ToolType getFuzzedToolType(Fdp& fdp) {
69 const int32_t toolType = fdp.template ConsumeIntegralInRange<int32_t>(
70 static_cast<int32_t>(ToolType::ftl_first),
71 static_cast<int32_t>(ToolType::ftl_last));
72 return static_cast<ToolType>(toolType);
73}
74
Harry Cutts656e0ad2023-06-16 16:39:55 +000075template <class Fdp>
76RawEvent getFuzzedRawEvent(Fdp& fdp) {
77 const int32_t type = fdp.ConsumeBool() ? fdp.PickValueInArray(kValidTypes)
78 : fdp.template ConsumeIntegral<int32_t>();
79 const int32_t code = fdp.ConsumeBool() ? fdp.PickValueInArray(kValidCodes)
80 : fdp.template ConsumeIntegral<int32_t>();
81 return RawEvent{
82 .when = fdp.template ConsumeIntegral<nsecs_t>(),
83 .readTime = fdp.template ConsumeIntegral<nsecs_t>(),
84 .deviceId = fdp.template ConsumeIntegral<int32_t>(),
85 .type = type,
86 .code = code,
87 .value = fdp.template ConsumeIntegral<int32_t>(),
88 };
89}
90
Michael Ensingb8d93262020-05-12 00:41:30 -070091class FuzzEventHub : public EventHubInterface {
92 InputDeviceIdentifier mIdentifier;
93 std::vector<TouchVideoFrame> mVideoFrames;
94 PropertyMap mFuzzConfig;
Aditya Wazireea5be02022-10-31 12:40:10 +053095 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -070096
97public:
Aditya Wazireea5be02022-10-31 12:40:10 +053098 FuzzEventHub(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(std::move(fdp)) {}
Michael Ensingb8d93262020-05-12 00:41:30 -070099 ~FuzzEventHub() {}
100 void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700101
102 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
103 return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>());
104 }
105 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
106 return mIdentifier;
107 }
108 int32_t getDeviceControllerNumber(int32_t deviceId) const override {
109 return mFdp->ConsumeIntegral<int32_t>();
110 }
Harry Cuttsc34f7582023-03-07 16:23:30 +0000111 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override {
112 return mFuzzConfig;
Michael Ensingb8d93262020-05-12 00:41:30 -0700113 }
114 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
115 RawAbsoluteAxisInfo* outAxisInfo) const override {
116 return mFdp->ConsumeIntegral<status_t>();
117 }
118 bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); }
119 bool hasInputProperty(int32_t deviceId, int property) const override {
120 return mFdp->ConsumeBool();
121 }
122 bool hasMscEvent(int32_t deviceId, int mscEvent) const override { return mFdp->ConsumeBool(); }
123 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
124 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
125 return mFdp->ConsumeIntegral<status_t>();
126 }
127 status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override {
128 return mFdp->ConsumeIntegral<status_t>();
129 }
130 void setExcludedDevices(const std::vector<std::string>& devices) override {}
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700131 std::vector<RawEvent> getEvents(int timeoutMillis) override {
132 std::vector<RawEvent> events;
133 const size_t count = mFdp->ConsumeIntegralInRange<size_t>(0, kMaxSize);
134 for (size_t i = 0; i < count; ++i) {
Harry Cutts656e0ad2023-06-16 16:39:55 +0000135 events.push_back(getFuzzedRawEvent(*mFdp));
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700136 }
137 return events;
Michael Ensingb8d93262020-05-12 00:41:30 -0700138 }
139 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; }
140
141 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
142 int32_t deviceId, int32_t absCode) const override {
143 return base::ResultError("Fuzzer", UNKNOWN_ERROR);
144 };
145 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
146 // containing the raw info of the sysfs node structure.
147 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
148 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
149 int32_t BatteryId) const override {
150 return std::nullopt;
151 };
152
153 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { return {}; };
154 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
155 return std::nullopt;
156 };
157 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
158 return std::nullopt;
159 };
160 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override{};
161 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
162 int32_t deviceId, int32_t lightId) const override {
163 return std::nullopt;
164 };
165 void setLightIntensities(int32_t deviceId, int32_t lightId,
166 std::unordered_map<LightColor, int32_t> intensities) override{};
167
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000168 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override {
169 return std::nullopt;
Michael Ensingb8d93262020-05-12 00:41:30 -0700170 };
171
172 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
173 return mFdp->ConsumeIntegral<int32_t>();
174 }
175 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
176 return mFdp->ConsumeIntegral<int32_t>();
177 }
178 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
179 return mFdp->ConsumeIntegral<int32_t>();
180 }
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000181 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700182 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
183 return mFdp->ConsumeIntegral<int32_t>();
184 }
185 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
186 int32_t* outValue) const override {
187 return mFdp->ConsumeIntegral<status_t>();
188 }
189 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
190 uint8_t* outFlags) const override {
191 return mFdp->ConsumeBool();
192 }
193 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
194 return mFdp->ConsumeBool();
195 }
196 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
197 return mFdp->ConsumeBool();
198 }
199 bool hasLed(int32_t deviceId, int32_t led) const override { return mFdp->ConsumeBool(); }
200 void setLedState(int32_t deviceId, int32_t led, bool on) override {}
201 void getVirtualKeyDefinitions(
202 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {}
203 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override {
204 return nullptr;
205 }
206 bool setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) override {
207 return mFdp->ConsumeBool();
208 }
209 void vibrate(int32_t deviceId, const VibrationElement& effect) override {}
210 void cancelVibrate(int32_t deviceId) override {}
211
212 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return {}; };
213
214 /* Query battery level. */
215 std::optional<int32_t> getBatteryCapacity(int32_t deviceId, int32_t batteryId) const override {
216 return std::nullopt;
217 };
218
219 /* Query battery status. */
220 std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const override {
221 return std::nullopt;
222 };
223
224 void requestReopenDevices() override {}
225 void wake() override {}
226 void dump(std::string& dump) const override {}
227 void monitor() const override {}
228 bool isDeviceEnabled(int32_t deviceId) const override { return mFdp->ConsumeBool(); }
229 status_t enableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
230 status_t disableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000231 void sysfsNodeChanged(const std::string& sysfsNodePath) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700232};
233
234class FuzzPointerController : public PointerControllerInterface {
Aditya Wazireea5be02022-10-31 12:40:10 +0530235 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700236
237public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530238 FuzzPointerController(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700239 ~FuzzPointerController() {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000240 std::optional<FloatRect> getBounds() const override {
241 if (mFdp->ConsumeBool()) {
242 return {};
243 } else {
244 return FloatRect{mFdp->ConsumeFloatingPoint<float>(),
245 mFdp->ConsumeFloatingPoint<float>(),
246 mFdp->ConsumeFloatingPoint<float>(),
247 mFdp->ConsumeFloatingPoint<float>()};
248 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700249 }
250 void move(float deltaX, float deltaY) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700251 void setPosition(float x, float y) override {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000252 FloatPoint getPosition() const override {
253 return {mFdp->ConsumeFloatingPoint<float>(), mFdp->ConsumeFloatingPoint<float>()};
254 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700255 void fade(Transition transition) override {}
256 void unfade(Transition transition) override {}
257 void setPresentation(Presentation presentation) override {}
258 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
259 BitSet32 spotIdBits, int32_t displayId) override {}
260 void clearSpots() override {}
261 int32_t getDisplayId() const override { return mFdp->ConsumeIntegral<int32_t>(); }
262 void setDisplayViewport(const DisplayViewport& displayViewport) override {}
263};
264
265class FuzzInputReaderPolicy : public InputReaderPolicyInterface {
266 TouchAffineTransformation mTransform;
267 std::shared_ptr<FuzzPointerController> mPointerController;
Aditya Wazireea5be02022-10-31 12:40:10 +0530268 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700269
270protected:
271 ~FuzzInputReaderPolicy() {}
272
273public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530274 FuzzInputReaderPolicy(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -0700275 mPointerController = std::make_shared<FuzzPointerController>(mFdp);
276 }
277 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {}
278 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
279 return mPointerController;
280 }
281 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {}
282 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
283 const InputDeviceIdentifier& identifier) override {
284 return nullptr;
285 }
286 std::string getDeviceAlias(const InputDeviceIdentifier& identifier) {
287 return mFdp->ConsumeRandomLengthString(32);
288 }
289 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000290 ui::Rotation surfaceRotation) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700291 return mTransform;
292 }
293 void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000294 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700295};
296
297class FuzzInputListener : public virtual InputListenerInterface {
298public:
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000299 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override {}
Prabir Pradhanc392d8f2023-04-13 19:32:51 +0000300 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override {}
301 void notifyKey(const NotifyKeyArgs& args) override {}
302 void notifyMotion(const NotifyMotionArgs& args) override {}
303 void notifySwitch(const NotifySwitchArgs& args) override {}
304 void notifySensor(const NotifySensorArgs& args) override{};
305 void notifyVibratorState(const NotifyVibratorStateArgs& args) override{};
306 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override {}
307 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override{};
Michael Ensingb8d93262020-05-12 00:41:30 -0700308};
309
310class FuzzInputReaderContext : public InputReaderContext {
311 std::shared_ptr<EventHubInterface> mEventHub;
312 sp<InputReaderPolicyInterface> mPolicy;
Aditya Wazireea5be02022-10-31 12:40:10 +0530313 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700314
315public:
316 FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
317 const sp<InputReaderPolicyInterface>& policy,
318 InputListenerInterface& listener,
Aditya Wazireea5be02022-10-31 12:40:10 +0530319 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp)
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700320 : mEventHub(eventHub), mPolicy(policy), mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700321 ~FuzzInputReaderContext() {}
322 void updateGlobalMetaState() override {}
323 int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); }
324 void disableVirtualKeysUntil(nsecs_t time) override {}
325 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override {
326 return mFdp->ConsumeBool();
327 }
328 void fadePointer() override {}
329 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override {
330 return mPolicy->obtainPointerController(0);
331 }
332 void requestTimeoutAtTime(nsecs_t when) override {}
333 int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); }
334 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700335 std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override {
336 return {};
337 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700338 InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700339 EventHubInterface* getEventHub() override { return mEventHub.get(); }
340 int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); }
341
342 void updateLedMetaState(int32_t metaState) override{};
343 int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); };
Prabir Pradhanda20b172022-09-26 17:01:18 +0000344 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700345};
346
347} // namespace android