blob: 336ecb6409890f42e4c2ba630562135d6d05822f [file] [log] [blame]
Michael Ensing7c58e652020-05-12 00:41:30 -07001/*
2 * Copyright 2020 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
23void addProperty(FuzzContainer& fuzzer, std::shared_ptr<FuzzedDataProvider> fdp) {
24 // Pick a random property to set for the mapper to have set.
25 fdp->PickValueInArray<std::function<void()>>(
26 {[&]() -> void {
27 fuzzer.addProperty(String8("touch.deviceType"), String8("touchScreen"));
28 },
29 [&]() -> void {
30 fuzzer.addProperty(String8("touch.deviceType"),
31 String8(fdp->ConsumeRandomLengthString(8).data()));
32 },
33 [&]() -> void {
34 fuzzer.addProperty(String8("touch.size.scale"),
35 String8(fdp->ConsumeRandomLengthString(8).data()));
36 },
37 [&]() -> void {
38 fuzzer.addProperty(String8("touch.size.bias"),
39 String8(fdp->ConsumeRandomLengthString(8).data()));
40 },
41 [&]() -> void {
42 fuzzer.addProperty(String8("touch.size.isSummed"),
43 String8(fdp->ConsumeRandomLengthString(8).data()));
44 },
45 [&]() -> void {
46 fuzzer.addProperty(String8("touch.size.calibration"),
47 String8(fdp->ConsumeRandomLengthString(8).data()));
48 },
49 [&]() -> void {
50 fuzzer.addProperty(String8("touch.pressure.scale"),
51 String8(fdp->ConsumeRandomLengthString(8).data()));
52 },
53 [&]() -> void {
54 fuzzer.addProperty(String8("touch.size.calibration"),
55 fdp->ConsumeBool() ? String8("diameter") : String8("area"));
56 },
57 [&]() -> void {
58 fuzzer.addProperty(String8("touch.pressure.calibration"),
59 String8(fdp->ConsumeRandomLengthString(8).data()));
60 }})();
61}
62
63extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
64 std::shared_ptr<FuzzedDataProvider> fdp = std::make_shared<FuzzedDataProvider>(data, size);
65 FuzzContainer fuzzer = FuzzContainer(fdp);
66
67 MultiTouchInputMapper& mapper = fuzzer.getMapper<MultiTouchInputMapper>();
68 auto policyConfig = fuzzer.getPolicyConfig();
69
70 // Loop through mapper operations until randomness is exhausted.
71 while (fdp->remaining_bytes() > 0) {
72 fdp->PickValueInArray<std::function<void()>>({
73 [&]() -> void { addProperty(fuzzer, fdp); },
74 [&]() -> void {
75 std::string dump;
76 mapper.dump(dump);
77 },
78 [&]() -> void {
79 InputDeviceInfo info;
80 mapper.populateDeviceInfo(&info);
81 },
82 [&]() -> void { mapper.getSources(); },
83 [&]() -> void {
84 mapper.configure(fdp->ConsumeIntegral<nsecs_t>(), &policyConfig,
85 fdp->ConsumeIntegral<uint32_t>());
86 },
87 [&]() -> void { mapper.reset(fdp->ConsumeIntegral<nsecs_t>()); },
88 [&]() -> void {
89 RawEvent rawEvent{fdp->ConsumeIntegral<nsecs_t>(),
90 fdp->ConsumeIntegral<int32_t>(),
91 fdp->ConsumeIntegral<int32_t>(),
92 fdp->ConsumeIntegral<int32_t>(),
93 fdp->ConsumeIntegral<int32_t>()};
94 mapper.process(&rawEvent);
95 },
96 [&]() -> void {
97 mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(),
98 fdp->ConsumeIntegral<int32_t>());
99 },
100 [&]() -> void {
101 mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(),
102 fdp->ConsumeIntegral<int32_t>());
103 },
104 [&]() -> void {
105 mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(),
106 fdp->ConsumeIntegral<size_t>(), nullptr, nullptr);
107 },
108 [&]() -> void { mapper.cancelTouch(fdp->ConsumeIntegral<nsecs_t>()); },
109 [&]() -> void { mapper.timeoutExpired(fdp->ConsumeIntegral<nsecs_t>()); },
110 [&]() -> void {
111 StylusState state{fdp->ConsumeIntegral<nsecs_t>(),
112 fdp->ConsumeFloatingPoint<float>(),
113 fdp->ConsumeIntegral<uint32_t>(),
114 fdp->ConsumeIntegral<int32_t>()};
115 mapper.updateExternalStylusState(state);
116 },
117 [&]() -> void { mapper.getAssociatedDisplayId(); },
118 })();
119 }
120
121 return 0;
122}
123
124} // namespace android