1 /*
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 <InputDevice.h>
18 #include <InputReaderBase.h>
19 #include <MapperHelpers.h>
20 #include <SwitchInputMapper.h>
21
22 namespace android {
23
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)24 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
25 std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp =
26 std::make_shared<ThreadSafeFuzzedDataProvider>(data, size);
27
28 // Create mocked objects to support the fuzzed input mapper.
29 std::shared_ptr<FuzzEventHub> eventHub = std::make_shared<FuzzEventHub>(fdp);
30 FuzzInputReaderContext context(eventHub, fdp);
31 InputDevice device = getFuzzedInputDevice(*fdp, &context);
32
33 SwitchInputMapper& mapper =
34 getMapperForDevice<ThreadSafeFuzzedDataProvider,
35 SwitchInputMapper>(*fdp.get(), device, InputReaderConfiguration{});
36
37 // Loop through mapper operations until randomness is exhausted.
38 while (fdp->remaining_bytes() > 0) {
39 fdp->PickValueInArray<std::function<void()>>({
40 [&]() -> void {
41 std::string dump;
42 mapper.dump(dump);
43 },
44 [&]() -> void { mapper.getSources(); },
45 [&]() -> void {
46 RawEvent rawEvent = getFuzzedRawEvent(*fdp);
47 std::list<NotifyArgs> unused = mapper.process(rawEvent);
48 },
49 [&]() -> void {
50 mapper.getSwitchState(fdp->ConsumeIntegral<uint32_t>(),
51 fdp->ConsumeIntegral<int32_t>());
52 },
53 })();
54 }
55
56 return 0;
57 }
58
59 } // namespace android
60