blob: 3e4a19becdadf5b1fbaab8641ed7d2c0c04d2646 [file] [log] [blame]
Michael Ensing0f0ca6e2021-01-12 16:13:20 -08001/*
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 */
16
17#include <InputReader.h>
18#include <MapperHelpers.h>
19#include <fuzzer/FuzzedDataProvider.h>
20#include <input/InputDevice.h>
21#include <chrono>
22#include <thread>
23
24namespace android {
25
26constexpr InputDeviceSensorType kInputDeviceSensorType[] = {
27 InputDeviceSensorType::ACCELEROMETER,
28 InputDeviceSensorType::MAGNETIC_FIELD,
29 InputDeviceSensorType::ORIENTATION,
30 InputDeviceSensorType::GYROSCOPE,
31 InputDeviceSensorType::LIGHT,
32 InputDeviceSensorType::PRESSURE,
33 InputDeviceSensorType::TEMPERATURE,
34 InputDeviceSensorType::PROXIMITY,
35 InputDeviceSensorType::GRAVITY,
36 InputDeviceSensorType::LINEAR_ACCELERATION,
37 InputDeviceSensorType::ROTATION_VECTOR,
38 InputDeviceSensorType::RELATIVE_HUMIDITY,
39 InputDeviceSensorType::AMBIENT_TEMPERATURE,
40 InputDeviceSensorType::MAGNETIC_FIELD_UNCALIBRATED,
41 InputDeviceSensorType::GAME_ROTATION_VECTOR,
42 InputDeviceSensorType::GYROSCOPE_UNCALIBRATED,
43 InputDeviceSensorType::SIGNIFICANT_MOTION,
44};
45
46class FuzzInputReader : public InputReaderInterface {
47public:
48 FuzzInputReader(std::shared_ptr<EventHubInterface> fuzzEventHub,
49 const sp<InputReaderPolicyInterface>& fuzzPolicy,
50 InputListenerInterface& fuzzListener) {
51 reader = std::make_unique<InputReader>(fuzzEventHub, fuzzPolicy, fuzzListener);
52 }
53
54 void dump(std::string& dump) { reader->dump(dump); }
55
56 void monitor() { reader->monitor(); }
57
Michael Ensing0f0ca6e2021-01-12 16:13:20 -080058 status_t start() { return reader->start(); }
59
60 status_t stop() { return reader->stop(); }
61
62 std::vector<InputDeviceInfo> getInputDevices() const { return reader->getInputDevices(); }
63
64 int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) {
65 return reader->getScanCodeState(deviceId, sourceMask, scanCode);
66 }
67
68 int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) {
69 return reader->getKeyCodeState(deviceId, sourceMask, keyCode);
70 }
71
72 int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) {
73 return reader->getSwitchState(deviceId, sourceMask, sw);
74 }
75
76 void toggleCapsLockState(int32_t deviceId) { reader->toggleCapsLockState(deviceId); }
77
78 bool hasKeys(int32_t deviceId, uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
79 uint8_t* outFlags) {
80 return reader->hasKeys(deviceId, sourceMask, keyCodes, outFlags);
81 }
82
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000083 void requestRefreshConfiguration(ConfigurationChanges changes) {
Michael Ensing0f0ca6e2021-01-12 16:13:20 -080084 reader->requestRefreshConfiguration(changes);
85 }
86
87 void vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
88 int32_t token) {
89 reader->vibrate(deviceId, sequence, repeat, token);
90 }
91
92 void cancelVibrate(int32_t deviceId, int32_t token) { reader->cancelVibrate(deviceId, token); }
93
94 bool isVibrating(int32_t deviceId) { return reader->isVibrating(deviceId); }
95
96 std::vector<int32_t> getVibratorIds(int32_t deviceId) {
97 return reader->getVibratorIds(deviceId);
98 }
99
100 std::optional<int32_t> getBatteryCapacity(int32_t deviceId) {
101 return reader->getBatteryCapacity(deviceId);
102 }
103
104 std::optional<int32_t> getBatteryStatus(int32_t deviceId) {
105 return reader->getBatteryStatus(deviceId);
106 }
107
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000108 std::optional<std::string> getBatteryDevicePath(int32_t deviceId) {
109 return reader->getBatteryDevicePath(deviceId);
110 }
111
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800112 std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) {
113 return reader->getLights(deviceId);
114 }
115
116 std::vector<InputDeviceSensorInfo> getSensors(int32_t deviceId) {
117 return reader->getSensors(deviceId);
118 }
119
Omar Abdelmonem5e70e962024-08-06 09:38:42 +0000120 std::optional<HardwareProperties> getTouchpadHardwareProperties(int32_t deviceId) {
121 return reader->getTouchpadHardwareProperties(deviceId);
122 }
123
Linnan Li13bf76a2024-05-05 19:18:02 +0800124 bool canDispatchToDisplay(int32_t deviceId, ui::LogicalDisplayId displayId) {
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800125 return reader->canDispatchToDisplay(deviceId, displayId);
126 }
127
128 bool enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
129 std::chrono::microseconds samplingPeriod,
130 std::chrono::microseconds maxBatchReportLatency) {
131 return reader->enableSensor(deviceId, sensorType, samplingPeriod, maxBatchReportLatency);
132 }
133
134 void disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
135 return reader->disableSensor(deviceId, sensorType);
136 }
137
138 void flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) {
139 return reader->flushSensor(deviceId, sensorType);
140 }
141
142 bool setLightColor(int32_t deviceId, int32_t lightId, int32_t color) {
143 return reader->setLightColor(deviceId, lightId, color);
144 }
145
146 bool setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) {
147 return reader->setLightPlayerId(deviceId, lightId, playerId);
148 }
149
150 std::optional<int32_t> getLightColor(int32_t deviceId, int32_t lightId) {
151 return reader->getLightColor(deviceId, lightId);
152 }
153
154 std::optional<int32_t> getLightPlayerId(int32_t deviceId, int32_t lightId) {
155 return reader->getLightPlayerId(deviceId, lightId);
156 }
157
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +0000158 void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) const {
159 reader->addKeyRemapping(deviceId, fromKeyCode, toKeyCode);
160 }
161
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800162 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const {
163 return reader->getKeyCodeForKeyLocation(deviceId, locationKeyCode);
164 }
165
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000166 std::optional<std::string> getBluetoothAddress(int32_t deviceId) const {
167 return reader->getBluetoothAddress(deviceId);
168 }
169
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000170 void sysfsNodeChanged(const std::string& sysfsNodePath) {
171 reader->sysfsNodeChanged(sysfsNodePath);
172 }
173
Prabir Pradhan018faea2024-05-08 21:52:54 +0000174 DeviceId getLastUsedInputDeviceId() override { return reader->getLastUsedInputDeviceId(); }
175
Arpit Singh849beb42024-06-06 07:14:17 +0000176 void notifyMouseCursorFadedOnTyping() override { reader->notifyMouseCursorFadedOnTyping(); }
177
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800178private:
179 std::unique_ptr<InputReaderInterface> reader;
180};
181
182extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
Aditya Wazireea5be02022-10-31 12:40:10 +0530183 std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp =
184 std::make_shared<ThreadSafeFuzzedDataProvider>(data, size);
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800185
186 FuzzInputListener fuzzListener;
187 sp<FuzzInputReaderPolicy> fuzzPolicy = sp<FuzzInputReaderPolicy>::make(fdp);
188 std::shared_ptr<FuzzEventHub> fuzzEventHub = std::make_shared<FuzzEventHub>(fdp);
189 std::unique_ptr<FuzzInputReader> reader =
190 std::make_unique<FuzzInputReader>(fuzzEventHub, fuzzPolicy, fuzzListener);
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800191 size_t patternCount = fdp->ConsumeIntegralInRange<size_t>(1, 260);
192 VibrationSequence pattern(patternCount);
193 for (size_t i = 0; i < patternCount; ++i) {
194 VibrationElement element(i);
Harry Cutts33476232023-01-30 19:57:29 +0000195 element.addChannel(/*vibratorId=*/fdp->ConsumeIntegral<int32_t>(),
196 /*amplitude=*/fdp->ConsumeIntegral<uint8_t>());
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800197 pattern.addElement(element);
198 }
199 reader->vibrate(fdp->ConsumeIntegral<int32_t>(), pattern,
Harry Cutts33476232023-01-30 19:57:29 +0000200 /*repeat=*/fdp->ConsumeIntegral<ssize_t>(),
201 /*token=*/fdp->ConsumeIntegral<int32_t>());
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800202 reader->start();
203
204 // Loop through mapper operations until randomness is exhausted.
205 while (fdp->remaining_bytes() > 0) {
206 fdp->PickValueInArray<std::function<void()>>({
207 [&]() -> void {
208 std::string dump;
209 reader->dump(dump);
210 },
211 [&]() -> void { reader->monitor(); },
212 [&]() -> void { reader->getInputDevices(); },
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800213 [&]() -> void {
214 reader->getScanCodeState(fdp->ConsumeIntegral<int32_t>(),
215 fdp->ConsumeIntegral<uint32_t>(),
216 fdp->ConsumeIntegral<int32_t>());
217 },
218 [&]() -> void {
219 reader->getKeyCodeState(fdp->ConsumeIntegral<int32_t>(),
220 fdp->ConsumeIntegral<uint32_t>(),
221 fdp->ConsumeIntegral<int32_t>());
222 },
223 [&]() -> void {
224 reader->getSwitchState(fdp->ConsumeIntegral<int32_t>(),
225 fdp->ConsumeIntegral<uint32_t>(),
226 fdp->ConsumeIntegral<int32_t>());
227 },
228 [&]() -> void { reader->toggleCapsLockState(fdp->ConsumeIntegral<int32_t>()); },
229 [&]() -> void {
230 size_t count = fdp->ConsumeIntegralInRange<size_t>(1, 1024);
231 std::vector<uint8_t> outFlags(count);
232 std::vector<int32_t> keyCodes;
233 for (size_t i = 0; i < count; ++i) {
234 keyCodes.push_back(fdp->ConsumeIntegral<int32_t>());
235 }
236 reader->hasKeys(fdp->ConsumeIntegral<int32_t>(),
237 fdp->ConsumeIntegral<uint32_t>(), keyCodes, outFlags.data());
238 },
239 [&]() -> void {
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000240 reader->requestRefreshConfiguration(
241 InputReaderConfiguration::Change(fdp->ConsumeIntegral<uint32_t>()));
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800242 },
243 [&]() -> void {
244 reader->cancelVibrate(fdp->ConsumeIntegral<int32_t>(),
245 fdp->ConsumeIntegral<int32_t>());
246 },
247 [&]() -> void {
248 reader->canDispatchToDisplay(fdp->ConsumeIntegral<int32_t>(),
Linnan Li13bf76a2024-05-05 19:18:02 +0800249 ui::LogicalDisplayId{
250 fdp->ConsumeIntegral<int32_t>()});
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800251 },
252 [&]() -> void {
253 reader->getKeyCodeForKeyLocation(fdp->ConsumeIntegral<int32_t>(),
254 fdp->ConsumeIntegral<int32_t>());
255 },
256 [&]() -> void { reader->getBatteryCapacity(fdp->ConsumeIntegral<int32_t>()); },
257 [&]() -> void { reader->getBatteryStatus(fdp->ConsumeIntegral<int32_t>()); },
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000258 [&]() -> void { reader->getBatteryDevicePath(fdp->ConsumeIntegral<int32_t>()); },
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800259 [&]() -> void { reader->getLights(fdp->ConsumeIntegral<int32_t>()); },
260 [&]() -> void { reader->getSensors(fdp->ConsumeIntegral<int32_t>()); },
261 [&]() -> void {
262 reader->getLightPlayerId(fdp->ConsumeIntegral<int32_t>(),
263 fdp->ConsumeIntegral<int32_t>());
264 },
265 [&]() -> void {
266 reader->getLightColor(fdp->ConsumeIntegral<int32_t>(),
267 fdp->ConsumeIntegral<int32_t>());
268 },
269 [&]() -> void {
270 reader->setLightPlayerId(fdp->ConsumeIntegral<int32_t>(),
271 fdp->ConsumeIntegral<int32_t>(),
272 fdp->ConsumeIntegral<int32_t>());
273 },
274 [&]() -> void {
275 reader->setLightColor(fdp->ConsumeIntegral<int32_t>(),
276 fdp->ConsumeIntegral<int32_t>(),
277 fdp->ConsumeIntegral<int32_t>());
278 },
279 [&]() -> void {
280 reader->flushSensor(fdp->ConsumeIntegral<int32_t>(),
281 fdp->PickValueInArray<InputDeviceSensorType>(
282 kInputDeviceSensorType));
283 },
284 [&]() -> void {
285 reader->disableSensor(fdp->ConsumeIntegral<int32_t>(),
286 fdp->PickValueInArray<InputDeviceSensorType>(
287 kInputDeviceSensorType));
288 },
289 [&]() -> void {
290 reader->enableSensor(fdp->ConsumeIntegral<int32_t>(),
291 fdp->PickValueInArray<InputDeviceSensorType>(
292 kInputDeviceSensorType),
293 std::chrono::microseconds(fdp->ConsumeIntegral<size_t>()),
294 std::chrono::microseconds(fdp->ConsumeIntegral<size_t>()));
295 },
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000296 [&]() -> void { reader->getBluetoothAddress(fdp->ConsumeIntegral<int32_t>()); },
Michael Ensing0f0ca6e2021-01-12 16:13:20 -0800297 })();
298 }
299
300 reader->stop();
301 return 0;
302}
303
304} // namespace android