blob: 9e02502b6b4dfad59338a516c2b386721385c9b0 [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
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +000053 KeyboardInputMapper& mapper =
54 getMapperForDevice<ThreadSafeFuzzedDataProvider,
55 KeyboardInputMapper>(*fdp.get(), device, InputReaderConfiguration{},
56 /*source=*/fdp->ConsumeIntegral<uint32_t>());
Michael Ensingb8d93262020-05-12 00:41:30 -070057
58 // Loop through mapper operations until randomness is exhausted.
59 while (fdp->remaining_bytes() > 0) {
60 fdp->PickValueInArray<std::function<void()>>({
Harry Cuttsf61c0472023-07-10 14:45:15 +000061 [&]() -> void { addProperty(*eventHub.get(), fdp); },
Michael Ensingb8d93262020-05-12 00:41:30 -070062 [&]() -> void {
63 std::string dump;
64 mapper.dump(dump);
65 },
66 [&]() -> void {
67 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +000068 mapper.populateDeviceInfo(info);
Michael Ensingb8d93262020-05-12 00:41:30 -070069 },
70 [&]() -> void { mapper.getSources(); },
71 [&]() -> void {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070072 std::list<NotifyArgs> unused =
Harry Cuttse36d8352023-07-10 13:28:45 +000073 mapper.reconfigure(fdp->ConsumeIntegral<nsecs_t>(), /*readerConfig=*/{},
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000074 InputReaderConfiguration::Change(
75 fdp->ConsumeIntegral<uint32_t>()));
Michael Ensingb8d93262020-05-12 00:41:30 -070076 },
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070077 [&]() -> void {
78 std::list<NotifyArgs> unused = mapper.reset(fdp->ConsumeIntegral<nsecs_t>());
79 },
Michael Ensingb8d93262020-05-12 00:41:30 -070080 [&]() -> void {
Harry Cutts656e0ad2023-06-16 16:39:55 +000081 RawEvent rawEvent = getFuzzedRawEvent(*fdp);
Harry Cuttsa32a1192024-06-04 15:10:31 +000082 std::list<NotifyArgs> unused = mapper.process(rawEvent);
Michael Ensingb8d93262020-05-12 00:41:30 -070083 },
84 [&]() -> void {
85 mapper.getKeyCodeState(fdp->ConsumeIntegral<uint32_t>(),
86 fdp->ConsumeIntegral<int32_t>());
87 },
88 [&]() -> void {
89 mapper.getScanCodeState(fdp->ConsumeIntegral<uint32_t>(),
90 fdp->ConsumeIntegral<int32_t>());
91 },
92 [&]() -> void {
93 std::vector<int32_t> keyCodes;
94 int32_t numBytes = fdp->ConsumeIntegralInRange<int32_t>(0, kMaxKeycodes);
95 for (int32_t i = 0; i < numBytes; ++i) {
96 keyCodes.push_back(fdp->ConsumeIntegral<int32_t>());
97 }
98 mapper.markSupportedKeyCodes(fdp->ConsumeIntegral<uint32_t>(), keyCodes,
99 nullptr);
100 },
101 [&]() -> void { mapper.getMetaState(); },
102 [&]() -> void { mapper.updateMetaState(fdp->ConsumeIntegral<int32_t>()); },
103 [&]() -> void { mapper.getAssociatedDisplayId(); },
104 })();
105 }
106
107 return 0;
108}
109
110} // namespace android