blob: 616e87081150098c138830968bc095f21792baa7 [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 <KeyboardInputMapper.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("keyboard.orientationAware", "1"); },
28 [&]() -> void {
29 fuzzer.addProperty("keyboard.orientationAware",
30 fdp->ConsumeRandomLengthString(100).data());
31 },
32 [&]() -> void {
33 fuzzer.addProperty("keyboard.doNotWakeByDefault",
34 fdp->ConsumeRandomLengthString(100).data());
35 },
36 [&]() -> void {
37 fuzzer.addProperty("keyboard.handlesKeyRepeat",
38 fdp->ConsumeRandomLengthString(100).data());
39 }})();
40}
41
42extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
Aditya Wazireea5be02022-10-31 12:40:10 +053043 std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp =
44 std::make_shared<ThreadSafeFuzzedDataProvider>(data, size);
Michael Ensingb8d93262020-05-12 00:41:30 -070045 FuzzContainer fuzzer(fdp);
46
Michael Ensingb8d93262020-05-12 00:41:30 -070047 auto policyConfig = fuzzer.getPolicyConfig();
Arpit Singh8e6fb252023-04-06 11:49:17 +000048 KeyboardInputMapper& mapper =
49 fuzzer.getMapper<KeyboardInputMapper>(policyConfig, fdp->ConsumeIntegral<uint32_t>(),
50 fdp->ConsumeIntegral<int32_t>());
Michael Ensingb8d93262020-05-12 00:41:30 -070051
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;
Harry Cuttsd02ea102023-03-17 18:21:30 +000062 mapper.populateDeviceInfo(info);
Michael Ensingb8d93262020-05-12 00:41:30 -070063 },
64 [&]() -> void { mapper.getSources(); },
65 [&]() -> void {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070066 std::list<NotifyArgs> unused =
Arpit Singhed6c3de2023-04-05 19:24:37 +000067 mapper.reconfigure(fdp->ConsumeIntegral<nsecs_t>(), policyConfig,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000068 InputReaderConfiguration::Change(
69 fdp->ConsumeIntegral<uint32_t>()));
Michael Ensingb8d93262020-05-12 00:41:30 -070070 },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070071 [&]() -> void {
72 std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>());
73 },
Michael Ensingb8d93262020-05-12 00:41:30 -070074 [&]() -> void {
75 int32_t type, code;
76 type = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidTypes)
77 : fdp->ConsumeIntegral<int32_t>();
78 code = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidCodes)
79 : fdp->ConsumeIntegral<int32_t>();
80 RawEvent rawEvent{fdp->ConsumeIntegral<nsecs_t>(),
81 fdp->ConsumeIntegral<nsecs_t>(),
82 fdp->ConsumeIntegral<int32_t>(),
83 type,
84 code,
85 fdp->ConsumeIntegral<int32_t>()};
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070086 std::list<NotifyArgs> unused = mapper.process(&rawEvent);
Michael Ensingb8d93262020-05-12 00:41:30 -070087 },
88 [&]() -> void {
89 mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(),
90 fdp->ConsumeIntegral<int32_t>());
91 },
92 [&]() -> void {
93 mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(),
94 fdp->ConsumeIntegral<int32_t>());
95 },
96 [&]() -> void {
97 std::vector<int32_t> keyCodes;
98 int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes);
99 for (int32_t i = 0; i < numBytes; ++i) {
100 keyCodes.push_back(fdp->ConsumeIntegral<int32_t>());
101 }
102 mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes,
103 nullptr);
104 },
105 [&]() -> void { mapper.getMetaState(); },
106 [&]() -> void { mapper.updateMetaState(fdp->ConsumeIntegral<int32_t>()); },
107 [&]() -> void { mapper.getAssociatedDisplayId(); },
108 })();
109 }
110
111 return 0;
112}
113
114} // namespace android