blob: 9f4aa5cd5e7a5f974b350bedf14e960e9174450e [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
Michael Ensingb8d93262020-05-12 00:41:30 -070077class FuzzEventHub : public EventHubInterface {
78 InputDeviceIdentifier mIdentifier;
79 std::vector<TouchVideoFrame> mVideoFrames;
80 PropertyMap mFuzzConfig;
Aditya Wazireea5be02022-10-31 12:40:10 +053081 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -070082
83public:
Aditya Wazireea5be02022-10-31 12:40:10 +053084 FuzzEventHub(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(std::move(fdp)) {}
Michael Ensingb8d93262020-05-12 00:41:30 -070085 ~FuzzEventHub() {}
86 void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); }
Michael Ensingb8d93262020-05-12 00:41:30 -070087
88 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
89 return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>());
90 }
91 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
92 return mIdentifier;
93 }
94 int32_t getDeviceControllerNumber(int32_t deviceId) const override {
95 return mFdp->ConsumeIntegral<int32_t>();
96 }
Harry Cuttsc34f7582023-03-07 16:23:30 +000097 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override {
98 return mFuzzConfig;
Michael Ensingb8d93262020-05-12 00:41:30 -070099 }
100 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
101 RawAbsoluteAxisInfo* outAxisInfo) const override {
102 return mFdp->ConsumeIntegral<status_t>();
103 }
104 bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); }
105 bool hasInputProperty(int32_t deviceId, int property) const override {
106 return mFdp->ConsumeBool();
107 }
108 bool hasMscEvent(int32_t deviceId, int mscEvent) const override { return mFdp->ConsumeBool(); }
109 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
110 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
111 return mFdp->ConsumeIntegral<status_t>();
112 }
113 status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override {
114 return mFdp->ConsumeIntegral<status_t>();
115 }
116 void setExcludedDevices(const std::vector<std::string>& devices) override {}
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700117 std::vector<RawEvent> getEvents(int timeoutMillis) override {
118 std::vector<RawEvent> events;
119 const size_t count = mFdp->ConsumeIntegralInRange<size_t>(0, kMaxSize);
120 for (size_t i = 0; i < count; ++i) {
121 int32_t type = mFdp->ConsumeBool() ? mFdp->PickValueInArray(kValidTypes)
122 : mFdp->ConsumeIntegral<int32_t>();
123 int32_t code = mFdp->ConsumeBool() ? mFdp->PickValueInArray(kValidCodes)
124 : mFdp->ConsumeIntegral<int32_t>();
125 events.push_back({
126 .when = mFdp->ConsumeIntegral<nsecs_t>(),
127 .readTime = mFdp->ConsumeIntegral<nsecs_t>(),
128 .deviceId = mFdp->ConsumeIntegral<int32_t>(),
129 .type = type,
130 .code = code,
131 .value = mFdp->ConsumeIntegral<int32_t>(),
132 });
133 }
134 return events;
Michael Ensingb8d93262020-05-12 00:41:30 -0700135 }
136 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; }
137
138 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
139 int32_t deviceId, int32_t absCode) const override {
140 return base::ResultError("Fuzzer", UNKNOWN_ERROR);
141 };
142 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
143 // containing the raw info of the sysfs node structure.
144 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
145 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
146 int32_t BatteryId) const override {
147 return std::nullopt;
148 };
149
150 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { return {}; };
151 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
152 return std::nullopt;
153 };
154 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
155 return std::nullopt;
156 };
157 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override{};
158 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
159 int32_t deviceId, int32_t lightId) const override {
160 return std::nullopt;
161 };
162 void setLightIntensities(int32_t deviceId, int32_t lightId,
163 std::unordered_map<LightColor, int32_t> intensities) override{};
164
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000165 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override {
166 return std::nullopt;
Michael Ensingb8d93262020-05-12 00:41:30 -0700167 };
168
169 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
170 return mFdp->ConsumeIntegral<int32_t>();
171 }
172 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
173 return mFdp->ConsumeIntegral<int32_t>();
174 }
175 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
176 return mFdp->ConsumeIntegral<int32_t>();
177 }
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000178 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700179 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
180 return mFdp->ConsumeIntegral<int32_t>();
181 }
182 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
183 int32_t* outValue) const override {
184 return mFdp->ConsumeIntegral<status_t>();
185 }
186 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
187 uint8_t* outFlags) const override {
188 return mFdp->ConsumeBool();
189 }
190 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
191 return mFdp->ConsumeBool();
192 }
193 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
194 return mFdp->ConsumeBool();
195 }
196 bool hasLed(int32_t deviceId, int32_t led) const override { return mFdp->ConsumeBool(); }
197 void setLedState(int32_t deviceId, int32_t led, bool on) override {}
198 void getVirtualKeyDefinitions(
199 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {}
200 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override {
201 return nullptr;
202 }
203 bool setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) override {
204 return mFdp->ConsumeBool();
205 }
206 void vibrate(int32_t deviceId, const VibrationElement& effect) override {}
207 void cancelVibrate(int32_t deviceId) override {}
208
209 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return {}; };
210
211 /* Query battery level. */
212 std::optional<int32_t> getBatteryCapacity(int32_t deviceId, int32_t batteryId) const override {
213 return std::nullopt;
214 };
215
216 /* Query battery status. */
217 std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const override {
218 return std::nullopt;
219 };
220
221 void requestReopenDevices() override {}
222 void wake() override {}
223 void dump(std::string& dump) const override {}
224 void monitor() const override {}
225 bool isDeviceEnabled(int32_t deviceId) const override { return mFdp->ConsumeBool(); }
226 status_t enableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
227 status_t disableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000228 void sysfsNodeChanged(const std::string& sysfsNodePath) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700229};
230
231class FuzzPointerController : public PointerControllerInterface {
Aditya Wazireea5be02022-10-31 12:40:10 +0530232 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700233
234public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530235 FuzzPointerController(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700236 ~FuzzPointerController() {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000237 std::optional<FloatRect> getBounds() const override {
238 if (mFdp->ConsumeBool()) {
239 return {};
240 } else {
241 return FloatRect{mFdp->ConsumeFloatingPoint<float>(),
242 mFdp->ConsumeFloatingPoint<float>(),
243 mFdp->ConsumeFloatingPoint<float>(),
244 mFdp->ConsumeFloatingPoint<float>()};
245 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700246 }
247 void move(float deltaX, float deltaY) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700248 void setPosition(float x, float y) override {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000249 FloatPoint getPosition() const override {
250 return {mFdp->ConsumeFloatingPoint<float>(), mFdp->ConsumeFloatingPoint<float>()};
251 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700252 void fade(Transition transition) override {}
253 void unfade(Transition transition) override {}
254 void setPresentation(Presentation presentation) override {}
255 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
256 BitSet32 spotIdBits, int32_t displayId) override {}
257 void clearSpots() override {}
258 int32_t getDisplayId() const override { return mFdp->ConsumeIntegral<int32_t>(); }
259 void setDisplayViewport(const DisplayViewport& displayViewport) override {}
260};
261
262class FuzzInputReaderPolicy : public InputReaderPolicyInterface {
263 TouchAffineTransformation mTransform;
264 std::shared_ptr<FuzzPointerController> mPointerController;
Aditya Wazireea5be02022-10-31 12:40:10 +0530265 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700266
267protected:
268 ~FuzzInputReaderPolicy() {}
269
270public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530271 FuzzInputReaderPolicy(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -0700272 mPointerController = std::make_shared<FuzzPointerController>(mFdp);
273 }
274 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {}
275 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
276 return mPointerController;
277 }
278 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {}
279 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
280 const InputDeviceIdentifier& identifier) override {
281 return nullptr;
282 }
283 std::string getDeviceAlias(const InputDeviceIdentifier& identifier) {
284 return mFdp->ConsumeRandomLengthString(32);
285 }
286 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000287 ui::Rotation surfaceRotation) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700288 return mTransform;
289 }
290 void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000291 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700292};
293
294class FuzzInputListener : public virtual InputListenerInterface {
295public:
296 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) override {}
297 void notifyKey(const NotifyKeyArgs* args) override {}
298 void notifyMotion(const NotifyMotionArgs* args) override {}
299 void notifySwitch(const NotifySwitchArgs* args) override {}
300 void notifySensor(const NotifySensorArgs* args) override{};
301 void notifyVibratorState(const NotifyVibratorStateArgs* args) override{};
302 void notifyDeviceReset(const NotifyDeviceResetArgs* args) override {}
303 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) override{};
304};
305
306class FuzzInputReaderContext : public InputReaderContext {
307 std::shared_ptr<EventHubInterface> mEventHub;
308 sp<InputReaderPolicyInterface> mPolicy;
Aditya Wazireea5be02022-10-31 12:40:10 +0530309 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700310
311public:
312 FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
313 const sp<InputReaderPolicyInterface>& policy,
314 InputListenerInterface& listener,
Aditya Wazireea5be02022-10-31 12:40:10 +0530315 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp)
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700316 : mEventHub(eventHub), mPolicy(policy), mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700317 ~FuzzInputReaderContext() {}
318 void updateGlobalMetaState() override {}
319 int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); }
320 void disableVirtualKeysUntil(nsecs_t time) override {}
321 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override {
322 return mFdp->ConsumeBool();
323 }
324 void fadePointer() override {}
325 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override {
326 return mPolicy->obtainPointerController(0);
327 }
328 void requestTimeoutAtTime(nsecs_t when) override {}
329 int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); }
330 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700331 std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override {
332 return {};
333 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700334 InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700335 EventHubInterface* getEventHub() override { return mEventHub.get(); }
336 int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); }
337
338 void updateLedMetaState(int32_t metaState) override{};
339 int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); };
Prabir Pradhanda20b172022-09-26 17:01:18 +0000340 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700341};
342
343} // namespace android