blob: 59b064249af5b90ce7e6a3d3abfa41389746c303 [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>
19#include <fuzzer/FuzzedDataProvider.h>
20
21namespace android {
22
23const int32_t kMaxKeycodes = 100;
24
25static void addProperty(FuzzContainer& fuzzer, std::shared_ptr<FuzzedDataProvider> fdp) {
26 // Pick a random property to set for the mapper to have set.
27 fdp->PickValueInArray<std::function<void()>>(
28 {[&]() -> void { fuzzer.addProperty("touch.deviceType", "touchScreen"); },
29 [&]() -> void {
30 fuzzer.addProperty("touch.deviceType", fdp->ConsumeRandomLengthString(8).data());
31 },
32 [&]() -> void {
33 fuzzer.addProperty("touch.size.scale", fdp->ConsumeRandomLengthString(8).data());
34 },
35 [&]() -> void {
36 fuzzer.addProperty("touch.size.bias", fdp->ConsumeRandomLengthString(8).data());
37 },
38 [&]() -> void {
39 fuzzer.addProperty("touch.size.isSummed",
40 fdp->ConsumeRandomLengthString(8).data());
41 },
42 [&]() -> void {
43 fuzzer.addProperty("touch.size.calibration",
44 fdp->ConsumeRandomLengthString(8).data());
45 },
46 [&]() -> void {
47 fuzzer.addProperty("touch.pressure.scale",
48 fdp->ConsumeRandomLengthString(8).data());
49 },
50 [&]() -> void {
51 fuzzer.addProperty("touch.size.calibration",
52 fdp->ConsumeBool() ? "diameter" : "area");
53 },
54 [&]() -> void {
55 fuzzer.addProperty("touch.pressure.calibration",
56 fdp->ConsumeRandomLengthString(8).data());
57 }})();
58}
59
60extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
61 std::shared_ptr<FuzzedDataProvider> fdp = std::make_shared<FuzzedDataProvider>(data, size);
62 FuzzContainer fuzzer(fdp);
63
64 MultiTouchInputMapper& mapper = fuzzer.getMapper<MultiTouchInputMapper>();
65 auto policyConfig = fuzzer.getPolicyConfig();
66
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;
77 mapper.populateDeviceInfo(&info);
78 },
79 [&]() -> void { mapper.getSources(); },
80 [&]() -> void {
81 mapper.configure(fdp->ConsumeIntegral<nsecs_t>(), &policyConfig,
82 fdp->ConsumeIntegral<uint32_t>());
83 },
84 [&]() -> void { mapper.reset(fdp->ConsumeIntegral<nsecs_t>()); },
85 [&]() -> void {
86 int32_t type = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidTypes)
87 : fdp->ConsumeIntegral<int32_t>();
88 int32_t code = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidCodes)
89 : fdp->ConsumeIntegral<int32_t>();
90 RawEvent rawEvent{fdp->ConsumeIntegral<nsecs_t>(),
91 fdp->ConsumeIntegral<nsecs_t>(),
92 fdp->ConsumeIntegral<int32_t>(),
93 type,
94 code,
95 fdp->ConsumeIntegral<int32_t>()};
96 mapper.process(&rawEvent);
97 },
98 [&]() -> void {
99 mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(),
100 fdp->ConsumeIntegral<int32_t>());
101 },
102 [&]() -> void {
103 mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(),
104 fdp->ConsumeIntegral<int32_t>());
105 },
106 [&]() -> void {
107 std::vector<int32_t> keyCodes;
108 int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes);
109 for (int32_t i = 0; i < numBytes; ++i) {
110 keyCodes.push_back(fdp->ConsumeIntegral<int32_t>());
111 }
112 mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes,
113 nullptr);
114 },
115 [&]() -> void {
116 mapper.cancelTouch(fdp->ConsumeIntegral<nsecs_t>(),
117 fdp->ConsumeIntegral<nsecs_t>());
118 },
119 [&]() -> void { mapper.timeoutExpired(fdp->ConsumeIntegral<nsecs_t>()); },
120 [&]() -> void {
121 StylusState state{fdp->ConsumeIntegral<nsecs_t>(),
122 fdp->ConsumeFloatingPoint<float>(),
123 fdp->ConsumeIntegral<uint32_t>(),
124 fdp->ConsumeIntegral<int32_t>()};
125 mapper.updateExternalStylusState(state);
126 },
127 [&]() -> void { mapper.getAssociatedDisplayId(); },
128 })();
129 }
130
131 return 0;
132}
133
134} // namespace android