blob: fdf9f4187318f231a17e9c25d0cb7fb4b80f7bf3 [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
17#include <FuzzContainer.h>
18#include <MultiTouchInputMapper.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070019
20namespace android {
21
22const int32_t kMaxKeycodes = 100;
23
Aditya Wazireea5be02022-10-31 12:40:10 +053024static void addProperty(FuzzContainer& fuzzer, std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -070025 // Pick a random property to set for the mapper to have set.
26 fdp->PickValueInArray<std::function<void()>>(
27 {[&]() -> void { fuzzer.addProperty("touch.deviceType", "touchScreen"); },
28 [&]() -> void {
29 fuzzer.addProperty("touch.deviceType", fdp->ConsumeRandomLengthString(8).data());
30 },
31 [&]() -> void {
32 fuzzer.addProperty("touch.size.scale", fdp->ConsumeRandomLengthString(8).data());
33 },
34 [&]() -> void {
35 fuzzer.addProperty("touch.size.bias", fdp->ConsumeRandomLengthString(8).data());
36 },
37 [&]() -> void {
38 fuzzer.addProperty("touch.size.isSummed",
39 fdp->ConsumeRandomLengthString(8).data());
40 },
41 [&]() -> void {
42 fuzzer.addProperty("touch.size.calibration",
43 fdp->ConsumeRandomLengthString(8).data());
44 },
45 [&]() -> void {
46 fuzzer.addProperty("touch.pressure.scale",
47 fdp->ConsumeRandomLengthString(8).data());
48 },
49 [&]() -> void {
50 fuzzer.addProperty("touch.size.calibration",
51 fdp->ConsumeBool() ? "diameter" : "area");
52 },
53 [&]() -> void {
54 fuzzer.addProperty("touch.pressure.calibration",
55 fdp->ConsumeRandomLengthString(8).data());
56 }})();
57}
58
59extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
Aditya Wazireea5be02022-10-31 12:40:10 +053060 std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp =
61 std::make_shared<ThreadSafeFuzzedDataProvider>(data, size);
Michael Ensingb8d93262020-05-12 00:41:30 -070062 FuzzContainer fuzzer(fdp);
63
Michael Ensingb8d93262020-05-12 00:41:30 -070064 auto policyConfig = fuzzer.getPolicyConfig();
Arpit Singh8e6fb252023-04-06 11:49:17 +000065 MultiTouchInputMapper& mapper = fuzzer.getMapper<MultiTouchInputMapper>(policyConfig);
Michael Ensingb8d93262020-05-12 00:41:30 -070066
67 // Loop through mapper operations until randomness is exhausted.
68 while (fdp->remaining_bytes() > 0) {
69 fdp->PickValueInArray<std::function<void()>>({
70 [&]() -> void { addProperty(fuzzer, fdp); },
71 [&]() -> void {
72 std::string dump;
73 mapper.dump(dump);
74 },
75 [&]() -> void {
76 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +000077 mapper.populateDeviceInfo(info);
Michael Ensingb8d93262020-05-12 00:41:30 -070078 },
79 [&]() -> void { mapper.getSources(); },
80 [&]() -> void {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070081 std::list<NotifyArgs> unused =
Arpit Singhed6c3de2023-04-05 19:24:37 +000082 mapper.reconfigure(fdp->ConsumeIntegral<nsecs_t>(), policyConfig,
Arpit Singh4be4eef2023-03-28 14:26:01 +000083 fdp->ConsumeIntegral<uint32_t>());
Michael Ensingb8d93262020-05-12 00:41:30 -070084 },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070085 [&]() -> void {
86 std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>());
87 },
Michael Ensingb8d93262020-05-12 00:41:30 -070088 [&]() -> void {
89 int32_t type = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidTypes)
90 : fdp->ConsumeIntegral<int32_t>();
91 int32_t code = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidCodes)
92 : fdp->ConsumeIntegral<int32_t>();
93 RawEvent rawEvent{fdp->ConsumeIntegral<nsecs_t>(),
94 fdp->ConsumeIntegral<nsecs_t>(),
95 fdp->ConsumeIntegral<int32_t>(),
96 type,
97 code,
98 fdp->ConsumeIntegral<int32_t>()};
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070099 std::list<NotifyArgs> unused = mapper.process(&rawEvent);
Michael Ensingb8d93262020-05-12 00:41:30 -0700100 },
101 [&]() -> void {
102 mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(),
103 fdp->ConsumeIntegral<int32_t>());
104 },
105 [&]() -> void {
106 mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(),
107 fdp->ConsumeIntegral<int32_t>());
108 },
109 [&]() -> void {
110 std::vector<int32_t> keyCodes;
111 int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes);
112 for (int32_t i = 0; i < numBytes; ++i) {
113 keyCodes.push_back(fdp->ConsumeIntegral<int32_t>());
114 }
115 mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes,
116 nullptr);
117 },
118 [&]() -> void {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700119 std::list<NotifyArgs> unused =
120 mapper.cancelTouch(fdp->ConsumeIntegral<nsecs_t>(),
121 fdp->ConsumeIntegral<nsecs_t>());
Michael Ensingb8d93262020-05-12 00:41:30 -0700122 },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700123 [&]() -> void {
124 std::list<NotifyArgs> unused =
125 mapper.timeoutExpired(fdp->ConsumeIntegral<nsecs_t>());
126 },
Michael Ensingb8d93262020-05-12 00:41:30 -0700127 [&]() -> void {
128 StylusState state{fdp->ConsumeIntegral<nsecs_t>(),
129 fdp->ConsumeFloatingPoint<float>(),
Arpit Singh4be4eef2023-03-28 14:26:01 +0000130 fdp->ConsumeIntegral<uint32_t>(), getFuzzedToolType(*fdp)};
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700131 std::list<NotifyArgs> unused = mapper.updateExternalStylusState(state);
Michael Ensingb8d93262020-05-12 00:41:30 -0700132 },
133 [&]() -> void { mapper.getAssociatedDisplayId(); },
134 })();
135 }
136
137 return 0;
138}
139
140} // namespace android