blob: 212462d700688c071bfd4d697cb6f173dceb086f [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,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000083 InputReaderConfiguration::Change(
84 fdp->ConsumeIntegral<uint32_t>()));
Michael Ensingb8d93262020-05-12 00:41:30 -070085 },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070086 [&]() -> void {
87 std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>());
88 },
Michael Ensingb8d93262020-05-12 00:41:30 -070089 [&]() -> void {
90 int32_t type = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidTypes)
91 : fdp->ConsumeIntegral<int32_t>();
92 int32_t code = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidCodes)
93 : fdp->ConsumeIntegral<int32_t>();
94 RawEvent rawEvent{fdp->ConsumeIntegral<nsecs_t>(),
95 fdp->ConsumeIntegral<nsecs_t>(),
96 fdp->ConsumeIntegral<int32_t>(),
97 type,
98 code,
99 fdp->ConsumeIntegral<int32_t>()};
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700100 std::list<NotifyArgs> unused = mapper.process(&rawEvent);
Michael Ensingb8d93262020-05-12 00:41:30 -0700101 },
102 [&]() -> void {
103 mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(),
104 fdp->ConsumeIntegral<int32_t>());
105 },
106 [&]() -> void {
107 mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(),
108 fdp->ConsumeIntegral<int32_t>());
109 },
110 [&]() -> void {
111 std::vector<int32_t> keyCodes;
112 int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes);
113 for (int32_t i = 0; i < numBytes; ++i) {
114 keyCodes.push_back(fdp->ConsumeIntegral<int32_t>());
115 }
116 mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes,
117 nullptr);
118 },
119 [&]() -> void {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700120 std::list<NotifyArgs> unused =
121 mapper.cancelTouch(fdp->ConsumeIntegral<nsecs_t>(),
122 fdp->ConsumeIntegral<nsecs_t>());
Michael Ensingb8d93262020-05-12 00:41:30 -0700123 },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700124 [&]() -> void {
125 std::list<NotifyArgs> unused =
126 mapper.timeoutExpired(fdp->ConsumeIntegral<nsecs_t>());
127 },
Michael Ensingb8d93262020-05-12 00:41:30 -0700128 [&]() -> void {
129 StylusState state{fdp->ConsumeIntegral<nsecs_t>(),
130 fdp->ConsumeFloatingPoint<float>(),
Arpit Singh4be4eef2023-03-28 14:26:01 +0000131 fdp->ConsumeIntegral<uint32_t>(), getFuzzedToolType(*fdp)};
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700132 std::list<NotifyArgs> unused = mapper.updateExternalStylusState(state);
Michael Ensingb8d93262020-05-12 00:41:30 -0700133 },
134 [&]() -> void { mapper.getAssociatedDisplayId(); },
135 })();
136 }
137
138 return 0;
139}
140
141} // namespace android