blob: 854e2fd3669481baadbc13ed8e2d90663caaa465 [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 <KeyboardInputMapper.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("keyboard.orientationAware"), String8("1"));
28 },
29 [&]() -> void {
30 fuzzer.addProperty(String8("keyboard.orientationAware"),
31 String8(fdp->ConsumeRandomLengthString(100).data()));
32 },
33 [&]() -> void {
34 fuzzer.addProperty(String8("keyboard.doNotWakeByDefault"),
35 String8(fdp->ConsumeRandomLengthString(100).data()));
36 },
37 [&]() -> void {
38 fuzzer.addProperty(String8("keyboard.handlesKeyRepeat"),
39 String8(fdp->ConsumeRandomLengthString(100).data()));
40 }})();
41}
42
43extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
44 std::shared_ptr<FuzzedDataProvider> fdp = std::make_shared<FuzzedDataProvider>(data, size);
45 FuzzContainer fuzzer = FuzzContainer(fdp);
46
47 KeyboardInputMapper& mapper =
48 fuzzer.getMapper<KeyboardInputMapper>(fdp->ConsumeIntegral<uint32_t>(),
49 fdp->ConsumeIntegral<int32_t>());
50 auto policyConfig = fuzzer.getPolicyConfig();
51
52 // Loop through mapper operations until randomness is exhausted.
53 while (fdp->remaining_bytes() > 0) {
54 fdp->PickValueInArray<std::function<void()>>({
55 [&]() -> void { addProperty(fuzzer, fdp); },
56 [&]() -> void {
57 std::string dump;
58 mapper.dump(dump);
59 },
60 [&]() -> void {
61 InputDeviceInfo info;
62 mapper.populateDeviceInfo(&info);
63 },
64 [&]() -> void { mapper.getSources(); },
65 [&]() -> void {
66 mapper.configure(fdp->ConsumeIntegral<nsecs_t>(), &policyConfig,
67 fdp->ConsumeIntegral<uint32_t>());
68 },
69 [&]() -> void { mapper.reset(fdp->ConsumeIntegral<nsecs_t>()); },
70 [&]() -> void {
71 RawEvent rawEvent{fdp->ConsumeIntegral<nsecs_t>(),
72 fdp->ConsumeIntegral<int32_t>(),
73 fdp->ConsumeIntegral<int32_t>(),
74 fdp->ConsumeIntegral<int32_t>(),
75 fdp->ConsumeIntegral<int32_t>()};
76 mapper.process(&rawEvent);
77 },
78 [&]() -> void {
79 mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(),
80 fdp->ConsumeIntegral<int32_t>());
81 },
82 [&]() -> void {
83 mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(),
84 fdp->ConsumeIntegral<int32_t>());
85 },
86 [&]() -> void {
87 mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(),
88 fdp->ConsumeIntegral<size_t>(), nullptr, nullptr);
89 },
90 [&]() -> void { mapper.getMetaState(); },
91 [&]() -> void { mapper.updateMetaState(fdp->ConsumeIntegral<int32_t>()); },
92 [&]() -> void { mapper.getAssociatedDisplayId(); },
93 })();
94 }
95
96 return 0;
97}
98
99} // namespace android