blob: 54977df2edccfc0c56c35b02fb498371c699b4c1 [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>
Harry Cuttse36d8352023-07-10 13:28:45 +000018#include <InputReaderBase.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070019#include <KeyboardInputMapper.h>
Harry Cutts656e0ad2023-06-16 16:39:55 +000020#include <MapperHelpers.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070021
22namespace android {
23
24const int32_t kMaxKeycodes = 100;
25
Aditya Wazireea5be02022-10-31 12:40:10 +053026static void addProperty(FuzzContainer& fuzzer, std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -070027 // Pick a random property to set for the mapper to have set.
28 fdp->PickValueInArray<std::function<void()>>(
29 {[&]() -> void { fuzzer.addProperty("keyboard.orientationAware", "1"); },
30 [&]() -> void {
31 fuzzer.addProperty("keyboard.orientationAware",
32 fdp->ConsumeRandomLengthString(100).data());
33 },
34 [&]() -> void {
35 fuzzer.addProperty("keyboard.doNotWakeByDefault",
36 fdp->ConsumeRandomLengthString(100).data());
37 },
38 [&]() -> void {
39 fuzzer.addProperty("keyboard.handlesKeyRepeat",
40 fdp->ConsumeRandomLengthString(100).data());
41 }})();
42}
43
44extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
Aditya Wazireea5be02022-10-31 12:40:10 +053045 std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp =
46 std::make_shared<ThreadSafeFuzzedDataProvider>(data, size);
Michael Ensingb8d93262020-05-12 00:41:30 -070047 FuzzContainer fuzzer(fdp);
48
Arpit Singh8e6fb252023-04-06 11:49:17 +000049 KeyboardInputMapper& mapper =
Harry Cuttse36d8352023-07-10 13:28:45 +000050 fuzzer.getMapper<KeyboardInputMapper>(InputReaderConfiguration{},
51 fdp->ConsumeIntegral<uint32_t>(),
Arpit Singh8e6fb252023-04-06 11:49:17 +000052 fdp->ConsumeIntegral<int32_t>());
Michael Ensingb8d93262020-05-12 00:41:30 -070053
54 // Loop through mapper operations until randomness is exhausted.
55 while (fdp->remaining_bytes() > 0) {
56 fdp->PickValueInArray<std::function<void()>>({
57 [&]() -> void { addProperty(fuzzer, fdp); },
58 [&]() -> void {
59 std::string dump;
60 mapper.dump(dump);
61 },
62 [&]() -> void {
63 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +000064 mapper.populateDeviceInfo(info);
Michael Ensingb8d93262020-05-12 00:41:30 -070065 },
66 [&]() -> void { mapper.getSources(); },
67 [&]() -> void {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070068 std::list<NotifyArgs> unused =
Harry Cuttse36d8352023-07-10 13:28:45 +000069 mapper.reconfigure(fdp->ConsumeIntegral<nsecs_t>(), /*readerConfig=*/{},
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000070 InputReaderConfiguration::Change(
71 fdp->ConsumeIntegral<uint32_t>()));
Michael Ensingb8d93262020-05-12 00:41:30 -070072 },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070073 [&]() -> void {
74 std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>());
75 },
Michael Ensingb8d93262020-05-12 00:41:30 -070076 [&]() -> void {
Harry Cutts656e0ad2023-06-16 16:39:55 +000077 RawEvent rawEvent = getFuzzedRawEvent(*fdp);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070078 std::list<NotifyArgs> unused = mapper.process(&rawEvent);
Michael Ensingb8d93262020-05-12 00:41:30 -070079 },
80 [&]() -> void {
81 mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(),
82 fdp->ConsumeIntegral<int32_t>());
83 },
84 [&]() -> void {
85 mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(),
86 fdp->ConsumeIntegral<int32_t>());
87 },
88 [&]() -> void {
89 std::vector<int32_t> keyCodes;
90 int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes);
91 for (int32_t i = 0; i < numBytes; ++i) {
92 keyCodes.push_back(fdp->ConsumeIntegral<int32_t>());
93 }
94 mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes,
95 nullptr);
96 },
97 [&]() -> void { mapper.getMetaState(); },
98 [&]() -> void { mapper.updateMetaState(fdp->ConsumeIntegral<int32_t>()); },
99 [&]() -> void { mapper.getAssociatedDisplayId(); },
100 })();
101 }
102
103 return 0;
104}
105
106} // namespace android