blob: 5039d1a5f140e51e86d4dc7516b5cd086df57164 [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
Harry Cuttsccb75e82023-06-23 14:08:06 +000018#include <map>
19
20#include <EventHub.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070021#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Aditya Wazireea5be02022-10-31 12:40:10 +053024#include <ThreadSafeFuzzedDataProvider.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070025
26constexpr size_t kValidTypes[] = {EV_SW,
27 EV_SYN,
Michael Ensingb8d93262020-05-12 00:41:30 -070028 EV_ABS,
29 EV_KEY,
30 EV_MSC,
31 EV_REL,
32 android::EventHubInterface::DEVICE_ADDED,
33 android::EventHubInterface::DEVICE_REMOVED,
34 android::EventHubInterface::FINISHED_DEVICE_SCAN};
35
36constexpr size_t kValidCodes[] = {
37 SYN_REPORT,
38 ABS_MT_SLOT,
39 SYN_MT_REPORT,
40 ABS_MT_POSITION_X,
41 ABS_MT_POSITION_Y,
42 ABS_MT_TOUCH_MAJOR,
43 ABS_MT_TOUCH_MINOR,
44 ABS_MT_WIDTH_MAJOR,
45 ABS_MT_WIDTH_MINOR,
46 ABS_MT_ORIENTATION,
47 ABS_MT_TRACKING_ID,
48 ABS_MT_PRESSURE,
49 ABS_MT_DISTANCE,
50 ABS_MT_TOOL_TYPE,
Michael Ensingb8d93262020-05-12 00:41:30 -070051 MSC_SCAN,
52 REL_X,
53 REL_Y,
54 REL_WHEEL,
55 REL_HWHEEL,
56 BTN_LEFT,
57 BTN_RIGHT,
58 BTN_MIDDLE,
59 BTN_BACK,
60 BTN_SIDE,
61 BTN_FORWARD,
62 BTN_EXTRA,
63 BTN_TASK,
64};
65
Michael Ensingb8d93262020-05-12 00:41:30 -070066constexpr size_t kMaxSize = 256;
67
68namespace android {
69
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070070template<class Fdp>
71ToolType getFuzzedToolType(Fdp& fdp) {
72 const int32_t toolType = fdp.template ConsumeIntegralInRange<int32_t>(
73 static_cast<int32_t>(ToolType::ftl_first),
74 static_cast<int32_t>(ToolType::ftl_last));
75 return static_cast<ToolType>(toolType);
76}
77
Harry Cutts656e0ad2023-06-16 16:39:55 +000078template <class Fdp>
79RawEvent getFuzzedRawEvent(Fdp& fdp) {
80 const int32_t type = fdp.ConsumeBool() ? fdp.PickValueInArray(kValidTypes)
81 : fdp.template ConsumeIntegral<int32_t>();
82 const int32_t code = fdp.ConsumeBool() ? fdp.PickValueInArray(kValidCodes)
83 : fdp.template ConsumeIntegral<int32_t>();
84 return RawEvent{
85 .when = fdp.template ConsumeIntegral<nsecs_t>(),
86 .readTime = fdp.template ConsumeIntegral<nsecs_t>(),
87 .deviceId = fdp.template ConsumeIntegral<int32_t>(),
88 .type = type,
89 .code = code,
90 .value = fdp.template ConsumeIntegral<int32_t>(),
91 };
92}
93
Michael Ensingb8d93262020-05-12 00:41:30 -070094class FuzzEventHub : public EventHubInterface {
95 InputDeviceIdentifier mIdentifier;
96 std::vector<TouchVideoFrame> mVideoFrames;
97 PropertyMap mFuzzConfig;
Harry Cuttsccb75e82023-06-23 14:08:06 +000098 std::map<int32_t /* deviceId */, std::map<int /* axis */, RawAbsoluteAxisInfo>> mAxes;
Aditya Wazireea5be02022-10-31 12:40:10 +053099 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700100
101public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530102 FuzzEventHub(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(std::move(fdp)) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700103 ~FuzzEventHub() {}
104 void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700105
106 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
107 return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>());
108 }
109 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
110 return mIdentifier;
111 }
112 int32_t getDeviceControllerNumber(int32_t deviceId) const override {
113 return mFdp->ConsumeIntegral<int32_t>();
114 }
Harry Cuttsc34f7582023-03-07 16:23:30 +0000115 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override {
116 return mFuzzConfig;
Michael Ensingb8d93262020-05-12 00:41:30 -0700117 }
Harry Cuttsccb75e82023-06-23 14:08:06 +0000118 void setAbsoluteAxisInfo(int32_t deviceId, int axis, const RawAbsoluteAxisInfo& axisInfo) {
119 mAxes[deviceId][axis] = axisInfo;
120 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700121 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
122 RawAbsoluteAxisInfo* outAxisInfo) const override {
Harry Cuttsccb75e82023-06-23 14:08:06 +0000123 if (auto deviceAxesIt = mAxes.find(deviceId); deviceAxesIt != mAxes.end()) {
124 const std::map<int, RawAbsoluteAxisInfo>& deviceAxes = deviceAxesIt->second;
125 if (auto axisInfoIt = deviceAxes.find(axis); axisInfoIt != deviceAxes.end()) {
126 *outAxisInfo = axisInfoIt->second;
127 return OK;
128 }
129 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700130 return mFdp->ConsumeIntegral<status_t>();
131 }
132 bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); }
133 bool hasInputProperty(int32_t deviceId, int property) const override {
134 return mFdp->ConsumeBool();
135 }
136 bool hasMscEvent(int32_t deviceId, int mscEvent) const override { return mFdp->ConsumeBool(); }
137 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
138 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
139 return mFdp->ConsumeIntegral<status_t>();
140 }
141 status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override {
142 return mFdp->ConsumeIntegral<status_t>();
143 }
144 void setExcludedDevices(const std::vector<std::string>& devices) override {}
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700145 std::vector<RawEvent> getEvents(int timeoutMillis) override {
146 std::vector<RawEvent> events;
147 const size_t count = mFdp->ConsumeIntegralInRange<size_t>(0, kMaxSize);
148 for (size_t i = 0; i < count; ++i) {
Harry Cutts656e0ad2023-06-16 16:39:55 +0000149 events.push_back(getFuzzedRawEvent(*mFdp));
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700150 }
151 return events;
Michael Ensingb8d93262020-05-12 00:41:30 -0700152 }
153 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; }
154
155 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
156 int32_t deviceId, int32_t absCode) const override {
157 return base::ResultError("Fuzzer", UNKNOWN_ERROR);
158 };
159 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
160 // containing the raw info of the sysfs node structure.
161 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
162 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
163 int32_t BatteryId) const override {
164 return std::nullopt;
165 };
166
167 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { return {}; };
168 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
169 return std::nullopt;
170 };
171 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
172 return std::nullopt;
173 };
174 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override{};
175 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
176 int32_t deviceId, int32_t lightId) const override {
177 return std::nullopt;
178 };
179 void setLightIntensities(int32_t deviceId, int32_t lightId,
180 std::unordered_map<LightColor, int32_t> intensities) override{};
181
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000182 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override {
183 return std::nullopt;
Michael Ensingb8d93262020-05-12 00:41:30 -0700184 };
185
186 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
187 return mFdp->ConsumeIntegral<int32_t>();
188 }
189 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
190 return mFdp->ConsumeIntegral<int32_t>();
191 }
192 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
193 return mFdp->ConsumeIntegral<int32_t>();
194 }
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000195 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700196 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
197 return mFdp->ConsumeIntegral<int32_t>();
198 }
199 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
200 int32_t* outValue) const override {
201 return mFdp->ConsumeIntegral<status_t>();
202 }
203 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
204 uint8_t* outFlags) const override {
205 return mFdp->ConsumeBool();
206 }
207 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
208 return mFdp->ConsumeBool();
209 }
210 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
211 return mFdp->ConsumeBool();
212 }
213 bool hasLed(int32_t deviceId, int32_t led) const override { return mFdp->ConsumeBool(); }
214 void setLedState(int32_t deviceId, int32_t led, bool on) override {}
215 void getVirtualKeyDefinitions(
216 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {}
217 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override {
218 return nullptr;
219 }
220 bool setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) override {
221 return mFdp->ConsumeBool();
222 }
223 void vibrate(int32_t deviceId, const VibrationElement& effect) override {}
224 void cancelVibrate(int32_t deviceId) override {}
225
226 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return {}; };
227
228 /* Query battery level. */
229 std::optional<int32_t> getBatteryCapacity(int32_t deviceId, int32_t batteryId) const override {
230 return std::nullopt;
231 };
232
233 /* Query battery status. */
234 std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const override {
235 return std::nullopt;
236 };
237
238 void requestReopenDevices() override {}
239 void wake() override {}
240 void dump(std::string& dump) const override {}
241 void monitor() const override {}
242 bool isDeviceEnabled(int32_t deviceId) const override { return mFdp->ConsumeBool(); }
243 status_t enableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
244 status_t disableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000245 void sysfsNodeChanged(const std::string& sysfsNodePath) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700246};
247
248class FuzzPointerController : public PointerControllerInterface {
Aditya Wazireea5be02022-10-31 12:40:10 +0530249 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700250
251public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530252 FuzzPointerController(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700253 ~FuzzPointerController() {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000254 std::optional<FloatRect> getBounds() const override {
255 if (mFdp->ConsumeBool()) {
256 return {};
257 } else {
258 return FloatRect{mFdp->ConsumeFloatingPoint<float>(),
259 mFdp->ConsumeFloatingPoint<float>(),
260 mFdp->ConsumeFloatingPoint<float>(),
261 mFdp->ConsumeFloatingPoint<float>()};
262 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700263 }
264 void move(float deltaX, float deltaY) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700265 void setPosition(float x, float y) override {}
Prabir Pradhan2719e822023-02-28 17:39:36 +0000266 FloatPoint getPosition() const override {
267 return {mFdp->ConsumeFloatingPoint<float>(), mFdp->ConsumeFloatingPoint<float>()};
268 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700269 void fade(Transition transition) override {}
270 void unfade(Transition transition) override {}
271 void setPresentation(Presentation presentation) override {}
272 void setSpots(const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
273 BitSet32 spotIdBits, int32_t displayId) override {}
274 void clearSpots() override {}
275 int32_t getDisplayId() const override { return mFdp->ConsumeIntegral<int32_t>(); }
276 void setDisplayViewport(const DisplayViewport& displayViewport) override {}
277};
278
279class FuzzInputReaderPolicy : public InputReaderPolicyInterface {
280 TouchAffineTransformation mTransform;
281 std::shared_ptr<FuzzPointerController> mPointerController;
Aditya Wazireea5be02022-10-31 12:40:10 +0530282 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700283
284protected:
285 ~FuzzInputReaderPolicy() {}
286
287public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530288 FuzzInputReaderPolicy(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -0700289 mPointerController = std::make_shared<FuzzPointerController>(mFdp);
290 }
291 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {}
292 std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) override {
293 return mPointerController;
294 }
295 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {}
296 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
297 const InputDeviceIdentifier& identifier) override {
298 return nullptr;
299 }
300 std::string getDeviceAlias(const InputDeviceIdentifier& identifier) {
301 return mFdp->ConsumeRandomLengthString(32);
302 }
303 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000304 ui::Rotation surfaceRotation) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700305 return mTransform;
306 }
307 void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000308 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700309};
310
311class FuzzInputListener : public virtual InputListenerInterface {
312public:
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000313 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override {}
Prabir Pradhanc392d8f2023-04-13 19:32:51 +0000314 void notifyConfigurationChanged(const NotifyConfigurationChangedArgs& args) override {}
315 void notifyKey(const NotifyKeyArgs& args) override {}
316 void notifyMotion(const NotifyMotionArgs& args) override {}
317 void notifySwitch(const NotifySwitchArgs& args) override {}
318 void notifySensor(const NotifySensorArgs& args) override{};
319 void notifyVibratorState(const NotifyVibratorStateArgs& args) override{};
320 void notifyDeviceReset(const NotifyDeviceResetArgs& args) override {}
321 void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override{};
Michael Ensingb8d93262020-05-12 00:41:30 -0700322};
323
324class FuzzInputReaderContext : public InputReaderContext {
325 std::shared_ptr<EventHubInterface> mEventHub;
326 sp<InputReaderPolicyInterface> mPolicy;
Aditya Wazireea5be02022-10-31 12:40:10 +0530327 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700328
329public:
330 FuzzInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
331 const sp<InputReaderPolicyInterface>& policy,
332 InputListenerInterface& listener,
Aditya Wazireea5be02022-10-31 12:40:10 +0530333 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp)
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700334 : mEventHub(eventHub), mPolicy(policy), mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700335 ~FuzzInputReaderContext() {}
336 void updateGlobalMetaState() override {}
337 int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); }
338 void disableVirtualKeysUntil(nsecs_t time) override {}
339 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override {
340 return mFdp->ConsumeBool();
341 }
342 void fadePointer() override {}
343 std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) override {
344 return mPolicy->obtainPointerController(0);
345 }
346 void requestTimeoutAtTime(nsecs_t when) override {}
347 int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); }
348 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700349 std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override {
350 return {};
351 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700352 InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700353 EventHubInterface* getEventHub() override { return mEventHub.get(); }
354 int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); }
355
356 void updateLedMetaState(int32_t metaState) override{};
357 int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); };
Prabir Pradhanda20b172022-09-26 17:01:18 +0000358 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700359};
360
361} // namespace android