blob: d3f66900daf901380fda094c79a31001f62727d8 [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 */
16
Harry Cuttsf61c0472023-07-10 14:45:15 +000017#include <InputDevice.h>
Harry Cuttse36d8352023-07-10 13:28:45 +000018#include <InputReaderBase.h>
Harry Cutts656e0ad2023-06-16 16:39:55 +000019#include <MapperHelpers.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070020#include <MultiTouchInputMapper.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070021
22namespace android {
23
24const int32_t kMaxKeycodes = 100;
25
Harry Cuttsf61c0472023-07-10 14:45:15 +000026static void addProperty(FuzzEventHub& eventHub, std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -070027 // Pick a random property to set for the mapper to have set.
28 fdp->PickValueInArray<std::function<void()>>(
Harry Cuttsf61c0472023-07-10 14:45:15 +000029 {[&]() -> void { eventHub.addProperty("touch.deviceType", "touchScreen"); },
Michael Ensingb8d93262020-05-12 00:41:30 -070030 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000031 eventHub.addProperty("touch.deviceType", fdp->ConsumeRandomLengthString(8).data());
Michael Ensingb8d93262020-05-12 00:41:30 -070032 },
33 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000034 eventHub.addProperty("touch.size.scale", fdp->ConsumeRandomLengthString(8).data());
Michael Ensingb8d93262020-05-12 00:41:30 -070035 },
36 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000037 eventHub.addProperty("touch.size.bias", fdp->ConsumeRandomLengthString(8).data());
Michael Ensingb8d93262020-05-12 00:41:30 -070038 },
39 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000040 eventHub.addProperty("touch.size.isSummed",
41 fdp->ConsumeRandomLengthString(8).data());
Michael Ensingb8d93262020-05-12 00:41:30 -070042 },
43 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000044 eventHub.addProperty("touch.size.calibration",
45 fdp->ConsumeRandomLengthString(8).data());
Michael Ensingb8d93262020-05-12 00:41:30 -070046 },
47 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000048 eventHub.addProperty("touch.pressure.scale",
49 fdp->ConsumeRandomLengthString(8).data());
Michael Ensingb8d93262020-05-12 00:41:30 -070050 },
51 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000052 eventHub.addProperty("touch.size.calibration",
53 fdp->ConsumeBool() ? "diameter" : "area");
Michael Ensingb8d93262020-05-12 00:41:30 -070054 },
55 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000056 eventHub.addProperty("touch.pressure.calibration",
57 fdp->ConsumeRandomLengthString(8).data());
Michael Ensingb8d93262020-05-12 00:41:30 -070058 }})();
59}
60
61extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
Aditya Wazireea5be02022-10-31 12:40:10 +053062 std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp =
63 std::make_shared<ThreadSafeFuzzedDataProvider>(data, size);
Harry Cuttsf61c0472023-07-10 14:45:15 +000064
65 // Create mocked objects to support the fuzzed input mapper.
66 std::shared_ptr<FuzzEventHub> eventHub = std::make_shared<FuzzEventHub>(fdp);
67 FuzzInputReaderContext context(eventHub, fdp);
68 InputDevice device = getFuzzedInputDevice(*fdp, &context);
Michael Ensingb8d93262020-05-12 00:41:30 -070069
Harry Cuttse36d8352023-07-10 13:28:45 +000070 InputReaderConfiguration policyConfig;
Harry Cuttsf61c0472023-07-10 14:45:15 +000071 MultiTouchInputMapper& mapper =
72 getMapperForDevice<ThreadSafeFuzzedDataProvider, MultiTouchInputMapper>(*fdp.get(),
73 device,
74 policyConfig);
Michael Ensingb8d93262020-05-12 00:41:30 -070075
76 // Loop through mapper operations until randomness is exhausted.
77 while (fdp->remaining_bytes() > 0) {
78 fdp->PickValueInArray<std::function<void()>>({
Harry Cuttsf61c0472023-07-10 14:45:15 +000079 [&]() -> void {
80 addProperty(*eventHub.get(), fdp);
81 configureAndResetDevice(*fdp, device);
82 },
Michael Ensingb8d93262020-05-12 00:41:30 -070083 [&]() -> void {
84 std::string dump;
85 mapper.dump(dump);
86 },
87 [&]() -> void {
88 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +000089 mapper.populateDeviceInfo(info);
Michael Ensingb8d93262020-05-12 00:41:30 -070090 },
91 [&]() -> void { mapper.getSources(); },
92 [&]() -> void {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070093 std::list<NotifyArgs> unused =
Arpit Singhed6c3de2023-04-05 19:24:37 +000094 mapper.reconfigure(fdp->ConsumeIntegral<nsecs_t>(), policyConfig,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000095 InputReaderConfiguration::Change(
96 fdp->ConsumeIntegral<uint32_t>()));
Michael Ensingb8d93262020-05-12 00:41:30 -070097 },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070098 [&]() -> void {
99 std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>());
100 },
Michael Ensingb8d93262020-05-12 00:41:30 -0700101 [&]() -> void {
Harry Cutts656e0ad2023-06-16 16:39:55 +0000102 RawEvent rawEvent = getFuzzedRawEvent(*fdp);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700103 std::list<NotifyArgs> unused = mapper.process(&rawEvent);
Michael Ensingb8d93262020-05-12 00:41:30 -0700104 },
105 [&]() -> void {
106 mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(),
107 fdp->ConsumeIntegral<int32_t>());
108 },
109 [&]() -> void {
110 mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(),
111 fdp->ConsumeIntegral<int32_t>());
112 },
113 [&]() -> void {
114 std::vector<int32_t> keyCodes;
115 int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes);
116 for (int32_t i = 0; i < numBytes; ++i) {
117 keyCodes.push_back(fdp->ConsumeIntegral<int32_t>());
118 }
119 mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes,
120 nullptr);
121 },
122 [&]() -> void {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700123 std::list<NotifyArgs> unused =
124 mapper.cancelTouch(fdp->ConsumeIntegral<nsecs_t>(),
125 fdp->ConsumeIntegral<nsecs_t>());
Michael Ensingb8d93262020-05-12 00:41:30 -0700126 },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700127 [&]() -> void {
128 std::list<NotifyArgs> unused =
129 mapper.timeoutExpired(fdp->ConsumeIntegral<nsecs_t>());
130 },
Michael Ensingb8d93262020-05-12 00:41:30 -0700131 [&]() -> void {
132 StylusState state{fdp->ConsumeIntegral<nsecs_t>(),
133 fdp->ConsumeFloatingPoint<float>(),
Arpit Singh4be4eef2023-03-28 14:26:01 +0000134 fdp->ConsumeIntegral<uint32_t>(), getFuzzedToolType(*fdp)};
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700135 std::list<NotifyArgs> unused = mapper.updateExternalStylusState(state);
Michael Ensingb8d93262020-05-12 00:41:30 -0700136 },
137 [&]() -> void { mapper.getAssociatedDisplayId(); },
138 })();
139 }
140
141 return 0;
142}
143
144} // namespace android