blob: e020ca9d620405ed6e8b6b11821e66fe36a6a14c [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>
Harry Cuttsf61c0472023-07-10 14:45:15 +000019#include <memory>
Harry Cuttsccb75e82023-06-23 14:08:06 +000020
21#include <EventHub.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070022#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Aditya Wazireea5be02022-10-31 12:40:10 +053025#include <ThreadSafeFuzzedDataProvider.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070026
27constexpr size_t kValidTypes[] = {EV_SW,
28 EV_SYN,
Michael Ensingb8d93262020-05-12 00:41:30 -070029 EV_ABS,
30 EV_KEY,
31 EV_MSC,
32 EV_REL,
33 android::EventHubInterface::DEVICE_ADDED,
34 android::EventHubInterface::DEVICE_REMOVED,
35 android::EventHubInterface::FINISHED_DEVICE_SCAN};
36
37constexpr size_t kValidCodes[] = {
38 SYN_REPORT,
39 ABS_MT_SLOT,
40 SYN_MT_REPORT,
41 ABS_MT_POSITION_X,
42 ABS_MT_POSITION_Y,
43 ABS_MT_TOUCH_MAJOR,
44 ABS_MT_TOUCH_MINOR,
45 ABS_MT_WIDTH_MAJOR,
46 ABS_MT_WIDTH_MINOR,
47 ABS_MT_ORIENTATION,
48 ABS_MT_TRACKING_ID,
49 ABS_MT_PRESSURE,
50 ABS_MT_DISTANCE,
51 ABS_MT_TOOL_TYPE,
Michael Ensingb8d93262020-05-12 00:41:30 -070052 MSC_SCAN,
53 REL_X,
54 REL_Y,
55 REL_WHEEL,
56 REL_HWHEEL,
57 BTN_LEFT,
58 BTN_RIGHT,
59 BTN_MIDDLE,
60 BTN_BACK,
61 BTN_SIDE,
62 BTN_FORWARD,
63 BTN_EXTRA,
64 BTN_TASK,
65};
66
Michael Ensingb8d93262020-05-12 00:41:30 -070067constexpr size_t kMaxSize = 256;
68
69namespace android {
70
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070071template<class Fdp>
72ToolType getFuzzedToolType(Fdp& fdp) {
73 const int32_t toolType = fdp.template ConsumeIntegralInRange<int32_t>(
74 static_cast<int32_t>(ToolType::ftl_first),
75 static_cast<int32_t>(ToolType::ftl_last));
76 return static_cast<ToolType>(toolType);
77}
78
Harry Cutts656e0ad2023-06-16 16:39:55 +000079template <class Fdp>
80RawEvent getFuzzedRawEvent(Fdp& fdp) {
81 const int32_t type = fdp.ConsumeBool() ? fdp.PickValueInArray(kValidTypes)
82 : fdp.template ConsumeIntegral<int32_t>();
83 const int32_t code = fdp.ConsumeBool() ? fdp.PickValueInArray(kValidCodes)
84 : fdp.template ConsumeIntegral<int32_t>();
85 return RawEvent{
86 .when = fdp.template ConsumeIntegral<nsecs_t>(),
87 .readTime = fdp.template ConsumeIntegral<nsecs_t>(),
88 .deviceId = fdp.template ConsumeIntegral<int32_t>(),
89 .type = type,
90 .code = code,
91 .value = fdp.template ConsumeIntegral<int32_t>(),
92 };
93}
94
Michael Ensingb8d93262020-05-12 00:41:30 -070095class FuzzEventHub : public EventHubInterface {
96 InputDeviceIdentifier mIdentifier;
97 std::vector<TouchVideoFrame> mVideoFrames;
98 PropertyMap mFuzzConfig;
Harry Cuttsccb75e82023-06-23 14:08:06 +000099 std::map<int32_t /* deviceId */, std::map<int /* axis */, RawAbsoluteAxisInfo>> mAxes;
Aditya Wazireea5be02022-10-31 12:40:10 +0530100 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700101
102public:
Aditya Wazireea5be02022-10-31 12:40:10 +0530103 FuzzEventHub(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(std::move(fdp)) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700104 ~FuzzEventHub() {}
105 void addProperty(std::string key, std::string value) { mFuzzConfig.addProperty(key, value); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700106
107 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
108 return ftl::Flags<InputDeviceClass>(mFdp->ConsumeIntegral<uint32_t>());
109 }
110 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
111 return mIdentifier;
112 }
113 int32_t getDeviceControllerNumber(int32_t deviceId) const override {
114 return mFdp->ConsumeIntegral<int32_t>();
115 }
Harry Cuttsc34f7582023-03-07 16:23:30 +0000116 std::optional<PropertyMap> getConfiguration(int32_t deviceId) const override {
117 return mFuzzConfig;
Michael Ensingb8d93262020-05-12 00:41:30 -0700118 }
Harry Cuttsccb75e82023-06-23 14:08:06 +0000119 void setAbsoluteAxisInfo(int32_t deviceId, int axis, const RawAbsoluteAxisInfo& axisInfo) {
120 mAxes[deviceId][axis] = axisInfo;
121 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700122 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
123 RawAbsoluteAxisInfo* outAxisInfo) const override {
Harry Cuttsccb75e82023-06-23 14:08:06 +0000124 if (auto deviceAxesIt = mAxes.find(deviceId); deviceAxesIt != mAxes.end()) {
125 const std::map<int, RawAbsoluteAxisInfo>& deviceAxes = deviceAxesIt->second;
126 if (auto axisInfoIt = deviceAxes.find(axis); axisInfoIt != deviceAxes.end()) {
127 *outAxisInfo = axisInfoIt->second;
128 return OK;
129 }
130 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700131 return mFdp->ConsumeIntegral<status_t>();
132 }
133 bool hasRelativeAxis(int32_t deviceId, int axis) const override { return mFdp->ConsumeBool(); }
134 bool hasInputProperty(int32_t deviceId, int property) const override {
135 return mFdp->ConsumeBool();
136 }
137 bool hasMscEvent(int32_t deviceId, int mscEvent) const override { return mFdp->ConsumeBool(); }
138 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
139 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
140 return mFdp->ConsumeIntegral<status_t>();
141 }
142 status_t mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const override {
143 return mFdp->ConsumeIntegral<status_t>();
144 }
145 void setExcludedDevices(const std::vector<std::string>& devices) override {}
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700146 std::vector<RawEvent> getEvents(int timeoutMillis) override {
147 std::vector<RawEvent> events;
148 const size_t count = mFdp->ConsumeIntegralInRange<size_t>(0, kMaxSize);
149 for (size_t i = 0; i < count; ++i) {
Harry Cutts656e0ad2023-06-16 16:39:55 +0000150 events.push_back(getFuzzedRawEvent(*mFdp));
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700151 }
152 return events;
Michael Ensingb8d93262020-05-12 00:41:30 -0700153 }
154 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override { return mVideoFrames; }
155
156 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
157 int32_t deviceId, int32_t absCode) const override {
158 return base::ResultError("Fuzzer", UNKNOWN_ERROR);
159 };
160 // Raw batteries are sysfs power_supply nodes we found from the EventHub device sysfs node,
161 // containing the raw info of the sysfs node structure.
162 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
163 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
164 int32_t BatteryId) const override {
165 return std::nullopt;
166 };
167
168 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override { return {}; };
169 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
170 return std::nullopt;
171 };
172 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
173 return std::nullopt;
174 };
175 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override{};
176 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
177 int32_t deviceId, int32_t lightId) const override {
178 return std::nullopt;
179 };
180 void setLightIntensities(int32_t deviceId, int32_t lightId,
181 std::unordered_map<LightColor, int32_t> intensities) override{};
182
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +0000183 std::optional<RawLayoutInfo> getRawLayoutInfo(int32_t deviceId) const override {
184 return std::nullopt;
Michael Ensingb8d93262020-05-12 00:41:30 -0700185 };
186
187 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
188 return mFdp->ConsumeIntegral<int32_t>();
189 }
190 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
191 return mFdp->ConsumeIntegral<int32_t>();
192 }
193 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
194 return mFdp->ConsumeIntegral<int32_t>();
195 }
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000196 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700197 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
198 return mFdp->ConsumeIntegral<int32_t>();
199 }
200 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
201 int32_t* outValue) const override {
202 return mFdp->ConsumeIntegral<status_t>();
203 }
Arpit Singh4b4a4572023-11-24 18:19:56 +0000204 base::Result<std::vector<int32_t>> getMtSlotValues(int32_t deviceId, int32_t axis,
205 size_t slotCount) const override {
206 if (mFdp->ConsumeBool()) {
207 std::vector<int32_t> outValues(slotCount + 1);
208 for (size_t i = 0; i < outValues.size(); i++) {
209 outValues.push_back(mFdp->ConsumeIntegral<int32_t>());
210 }
211 return std::move(outValues);
212 } else {
213 return base::ResultError("Fuzzer", UNKNOWN_ERROR);
214 }
215 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700216 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
217 uint8_t* outFlags) const override {
218 return mFdp->ConsumeBool();
219 }
220 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
221 return mFdp->ConsumeBool();
222 }
223 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
224 return mFdp->ConsumeBool();
225 }
226 bool hasLed(int32_t deviceId, int32_t led) const override { return mFdp->ConsumeBool(); }
227 void setLedState(int32_t deviceId, int32_t led, bool on) override {}
228 void getVirtualKeyDefinitions(
229 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {}
230 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t deviceId) const override {
231 return nullptr;
232 }
233 bool setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) override {
234 return mFdp->ConsumeBool();
235 }
236 void vibrate(int32_t deviceId, const VibrationElement& effect) override {}
237 void cancelVibrate(int32_t deviceId) override {}
238
239 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return {}; };
240
241 /* Query battery level. */
242 std::optional<int32_t> getBatteryCapacity(int32_t deviceId, int32_t batteryId) const override {
243 return std::nullopt;
244 };
245
246 /* Query battery status. */
247 std::optional<int32_t> getBatteryStatus(int32_t deviceId, int32_t batteryId) const override {
248 return std::nullopt;
249 };
250
251 void requestReopenDevices() override {}
252 void wake() override {}
253 void dump(std::string& dump) const override {}
254 void monitor() const override {}
255 bool isDeviceEnabled(int32_t deviceId) const override { return mFdp->ConsumeBool(); }
256 status_t enableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
257 status_t disableDevice(int32_t deviceId) override { return mFdp->ConsumeIntegral<status_t>(); }
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000258 void sysfsNodeChanged(const std::string& sysfsNodePath) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700259};
260
Michael Ensingb8d93262020-05-12 00:41:30 -0700261class FuzzInputReaderPolicy : public InputReaderPolicyInterface {
262 TouchAffineTransformation mTransform;
Aditya Wazireea5be02022-10-31 12:40:10 +0530263 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700264
265protected:
266 ~FuzzInputReaderPolicy() {}
267
268public:
Prabir Pradhan2120b9e2024-05-04 00:06:37 +0000269 FuzzInputReaderPolicy(std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp) : mFdp(mFdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700270 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700271 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {}
272 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
Vaibhav Devmuraridec30802023-07-11 15:02:03 +0000273 const InputDeviceIdentifier& identifier,
274 const std::optional<KeyboardLayoutInfo> layoutInfo) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700275 return nullptr;
276 }
277 std::string getDeviceAlias(const InputDeviceIdentifier& identifier) {
278 return mFdp->ConsumeRandomLengthString(32);
279 }
280 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000281 ui::Rotation surfaceRotation) override {
Michael Ensingb8d93262020-05-12 00:41:30 -0700282 return mTransform;
283 }
284 void setTouchAffineTransformation(const TouchAffineTransformation t) { mTransform = t; }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000285 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Arpit Singhb3b3f732023-07-04 14:30:05 +0000286 bool isInputMethodConnectionActive() override { return mFdp->ConsumeBool(); }
Prabir Pradhan19767602023-11-03 16:53:31 +0000287 std::optional<DisplayViewport> getPointerViewportForAssociatedDisplay(
Byoungho Jungda10dd32023-10-06 17:03:45 +0900288 int32_t associatedDisplayId) override {
289 return {};
290 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700291};
292
293class FuzzInputListener : public virtual InputListenerInterface {
294public:
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000295 void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs& args) override {}
Prabir Pradhanc392d8f2023-04-13 19:32:51 +0000296 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{};
Michael Ensingb8d93262020-05-12 00:41:30 -0700304};
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,
Harry Cuttsf61c0472023-07-10 14:45:15 +0000313 std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp)
314 : mEventHub(eventHub), mPolicy(sp<FuzzInputReaderPolicy>::make(fdp)), mFdp(fdp) {}
Michael Ensingb8d93262020-05-12 00:41:30 -0700315 ~FuzzInputReaderContext() {}
316 void updateGlobalMetaState() override {}
317 int32_t getGlobalMetaState() { return mFdp->ConsumeIntegral<int32_t>(); }
318 void disableVirtualKeysUntil(nsecs_t time) override {}
319 bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override {
320 return mFdp->ConsumeBool();
321 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700322 void requestTimeoutAtTime(nsecs_t when) override {}
323 int32_t bumpGeneration() override { return mFdp->ConsumeIntegral<int32_t>(); }
324 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700325 std::list<NotifyArgs> dispatchExternalStylusState(const StylusState& outState) override {
326 return {};
327 }
Michael Ensingb8d93262020-05-12 00:41:30 -0700328 InputReaderPolicyInterface* getPolicy() override { return mPolicy.get(); }
Michael Ensingb8d93262020-05-12 00:41:30 -0700329 EventHubInterface* getEventHub() override { return mEventHub.get(); }
330 int32_t getNextId() override { return mFdp->ConsumeIntegral<int32_t>(); }
331
332 void updateLedMetaState(int32_t metaState) override{};
333 int32_t getLedMetaState() override { return mFdp->ConsumeIntegral<int32_t>(); };
Prabir Pradhanda20b172022-09-26 17:01:18 +0000334 void notifyStylusGestureStarted(int32_t, nsecs_t) {}
Arpit Singha5ea7c12023-07-05 15:39:25 +0000335
336 void setPreventingTouchpadTaps(bool prevent) {}
337 bool isPreventingTouchpadTaps() { return mFdp->ConsumeBool(); };
Arpit Singh82e413e2023-10-10 19:30:58 +0000338
339 void setLastKeyDownTimestamp(nsecs_t when) { mLastKeyDownTimestamp = when; };
340 nsecs_t getLastKeyDownTimestamp() { return mLastKeyDownTimestamp; };
341
342private:
343 nsecs_t mLastKeyDownTimestamp;
Michael Ensingb8d93262020-05-12 00:41:30 -0700344};
345
Harry Cuttsf61c0472023-07-10 14:45:15 +0000346template <class Fdp>
347InputDevice getFuzzedInputDevice(Fdp& fdp, FuzzInputReaderContext* context) {
348 InputDeviceIdentifier identifier;
349 identifier.name = fdp.ConsumeRandomLengthString(16);
350 identifier.location = fdp.ConsumeRandomLengthString(12);
351 int32_t deviceID = fdp.ConsumeIntegralInRange(0, 5);
352 int32_t deviceGeneration = fdp.ConsumeIntegralInRange(0, 5);
353 return InputDevice(context, deviceID, deviceGeneration, identifier);
354}
355
356template <class Fdp>
357void configureAndResetDevice(Fdp& fdp, InputDevice& device) {
358 nsecs_t arbitraryTime = fdp.template ConsumeIntegral<nsecs_t>();
359 std::list<NotifyArgs> out;
360 out += device.configure(arbitraryTime, /*readerConfig=*/{}, /*changes=*/{});
361 out += device.reset(arbitraryTime);
362}
363
364template <class Fdp, class T, typename... Args>
365T& getMapperForDevice(Fdp& fdp, InputDevice& device, Args... args) {
366 int32_t eventhubId = fdp.template ConsumeIntegral<int32_t>();
367 // ensure a device entry exists for this eventHubId
368 device.addEmptyEventHubDevice(eventhubId);
369 configureAndResetDevice(fdp, device);
370
371 return device.template constructAndAddMapper<T>(eventhubId, args...);
372}
373
Michael Ensingb8d93262020-05-12 00:41:30 -0700374} // namespace android