blob: 8c3189e089e19a6d5f8851e5d0653ef2a72323d1 [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
Harry Cuttsf61c0472023-07-10 14:45:15 +000017#include <InputDevice.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
Harry Cuttsf61c0472023-07-10 14:45:15 +000026static void addProperty(FuzzEventHub& eventHub, 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()>>(
Harry Cuttsf61c0472023-07-10 14:45:15 +000029 {[&]() -> void { eventHub.addProperty("keyboard.orientationAware", "1"); },
Michael Ensingb8d93262020-05-12 00:41:30 -070030 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000031 eventHub.addProperty("keyboard.orientationAware",
32 fdp->ConsumeRandomLengthString(100).data());
Michael Ensingb8d93262020-05-12 00:41:30 -070033 },
34 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000035 eventHub.addProperty("keyboard.doNotWakeByDefault",
36 fdp->ConsumeRandomLengthString(100).data());
Michael Ensingb8d93262020-05-12 00:41:30 -070037 },
38 [&]() -> void {
Harry Cuttsf61c0472023-07-10 14:45:15 +000039 eventHub.addProperty("keyboard.handlesKeyRepeat",
40 fdp->ConsumeRandomLengthString(100).data());
Michael Ensingb8d93262020-05-12 00:41:30 -070041 }})();
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
Harry Cuttsf61c0472023-07-10 14:45:15 +000048 // Create mocked objects to support the fuzzed input mapper.
49 std::shared_ptr<FuzzEventHub> eventHub = std::make_shared<FuzzEventHub>(fdp);
50 FuzzInputReaderContext context(eventHub, fdp);
51 InputDevice device = getFuzzedInputDevice(*fdp, &context);
52
53 KeyboardInputMapper& mapper = getMapperForDevice<
54 ThreadSafeFuzzedDataProvider,
55 KeyboardInputMapper>(*fdp.get(), device, InputReaderConfiguration{},
56 /*source=*/fdp->ConsumeIntegral<uint32_t>(),
57 /*keyboardType=*/fdp->ConsumeIntegral<int32_t>());
Michael Ensingb8d93262020-05-12 00:41:30 -070058
59 // Loop through mapper operations until randomness is exhausted.
60 while (fdp->remaining_bytes() > 0) {
61 fdp->PickValueInArray<std::function<void()>>({
Harry Cuttsf61c0472023-07-10 14:45:15 +000062 [&]() -> void { addProperty(*eventHub.get(), fdp); },
Michael Ensingb8d93262020-05-12 00:41:30 -070063 [&]() -> void {
64 std::string dump;
65 mapper.dump(dump);
66 },
67 [&]() -> void {
68 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +000069 mapper.populateDeviceInfo(info);
Michael Ensingb8d93262020-05-12 00:41:30 -070070 },
71 [&]() -> void { mapper.getSources(); },
72 [&]() -> void {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070073 std::list<NotifyArgs> unused =
Harry Cuttse36d8352023-07-10 13:28:45 +000074 mapper.reconfigure(fdp->ConsumeIntegral<nsecs_t>(), /*readerConfig=*/{},
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000075 InputReaderConfiguration::Change(
76 fdp->ConsumeIntegral<uint32_t>()));
Michael Ensingb8d93262020-05-12 00:41:30 -070077 },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070078 [&]() -> void {
79 std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>());
80 },
Michael Ensingb8d93262020-05-12 00:41:30 -070081 [&]() -> void {
Harry Cutts656e0ad2023-06-16 16:39:55 +000082 RawEvent rawEvent = getFuzzedRawEvent(*fdp);
Harry Cuttsa32a1192024-06-04 15:10:31 +000083 std::list<NotifyArgs> unused = mapper.process(rawEvent);
Michael Ensingb8d93262020-05-12 00:41:30 -070084 },
85 [&]() -> void {
86 mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(),
87 fdp->ConsumeIntegral<int32_t>());
88 },
89 [&]() -> void {
90 mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(),
91 fdp->ConsumeIntegral<int32_t>());
92 },
93 [&]() -> void {
94 std::vector<int32_t> keyCodes;
95 int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes);
96 for (int32_t i = 0; i < numBytes; ++i) {
97 keyCodes.push_back(fdp->ConsumeIntegral<int32_t>());
98 }
99 mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes,
100 nullptr);
101 },
102 [&]() -> void { mapper.getMetaState(); },
103 [&]() -> void { mapper.updateMetaState(fdp->ConsumeIntegral<int32_t>()); },
104 [&]() -> void { mapper.getAssociatedDisplayId(); },
105 })();
106 }
107
108 return 0;
109}
110
111} // namespace android