blob: 2909129126845ab40132ea3704c420ae6bf48f93 [file] [log] [blame]
Michael Ensing910968d2020-07-19 17:19:31 -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 <MapperHelpers.h>
18#include <fuzzer/FuzzedDataProvider.h>
19#include "InputCommonConverter.h"
20#include "InputProcessor.h"
21
22namespace android {
23
24static constexpr int32_t MAX_AXES = 64;
25
26// Used by two fuzz operations and a bit lengthy, so pulled out into a function.
27NotifyMotionArgs generateFuzzedMotionArgs(FuzzedDataProvider &fdp) {
28 // Create a basic motion event for testing
29 PointerProperties properties;
30 properties.id = 0;
31 properties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
32 PointerCoords coords;
33 coords.clear();
34 for (int32_t i = 0; i < fdp.ConsumeIntegralInRange<int32_t>(0, MAX_AXES); i++) {
35 coords.setAxisValue(fdp.ConsumeIntegral<int32_t>(), fdp.ConsumeFloatingPoint<float>());
36 }
37
38 const nsecs_t downTime = 2;
39 const nsecs_t readTime = downTime + fdp.ConsumeIntegralInRange<nsecs_t>(0, 1E8);
Harry Cutts33476232023-01-30 19:57:29 +000040 NotifyMotionArgs motionArgs(/*sequenceNum=*/fdp.ConsumeIntegral<uint32_t>(),
41 /*eventTime=*/downTime, readTime,
42 /*deviceId=*/fdp.ConsumeIntegral<int32_t>(), AINPUT_SOURCE_ANY,
Michael Ensing910968d2020-07-19 17:19:31 -070043 ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +000044 /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(),
Michael Ensing910968d2020-07-19 17:19:31 -070045 AMOTION_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +000046 /*actionButton=*/fdp.ConsumeIntegral<int32_t>(),
47 /*flags=*/fdp.ConsumeIntegral<int32_t>(), AMETA_NONE,
48 /*buttonState=*/fdp.ConsumeIntegral<int32_t>(),
Michael Ensing910968d2020-07-19 17:19:31 -070049 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE,
Harry Cutts33476232023-01-30 19:57:29 +000050 /*pointerCount=*/1, &properties, &coords,
51 /*xPrecision=*/fdp.ConsumeFloatingPoint<float>(),
52 /*yPrecision=*/fdp.ConsumeFloatingPoint<float>(),
Michael Ensing910968d2020-07-19 17:19:31 -070053 AMOTION_EVENT_INVALID_CURSOR_POSITION,
54 AMOTION_EVENT_INVALID_CURSOR_POSITION, downTime,
Harry Cutts33476232023-01-30 19:57:29 +000055 /*videoFrames=*/{});
Michael Ensing910968d2020-07-19 17:19:31 -070056 return motionArgs;
57}
58
59extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
60 FuzzedDataProvider fdp(data, size);
61
62 std::unique_ptr<FuzzInputListener> mFuzzListener = std::make_unique<FuzzInputListener>();
63 std::unique_ptr<InputProcessorInterface> mClassifier =
64 std::make_unique<InputProcessor>(*mFuzzListener);
65
66 while (fdp.remaining_bytes() > 0) {
67 fdp.PickValueInArray<std::function<void()>>({
68 [&]() -> void {
69 // SendToNextStage_NotifyConfigurationChangedArgs
70 NotifyConfigurationChangedArgs
Harry Cutts33476232023-01-30 19:57:29 +000071 args(/*sequenceNum=*/fdp.ConsumeIntegral<uint32_t>(),
72 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>());
Michael Ensing910968d2020-07-19 17:19:31 -070073 mClassifier->notifyConfigurationChanged(&args);
74 },
75 [&]() -> void {
76 // SendToNextStage_NotifyKeyArgs
77 const nsecs_t eventTime = fdp.ConsumeIntegral<nsecs_t>();
78 const nsecs_t readTime =
79 eventTime + fdp.ConsumeIntegralInRange<nsecs_t>(0, 1E8);
Harry Cutts33476232023-01-30 19:57:29 +000080 NotifyKeyArgs keyArgs(/*sequenceNum=*/fdp.ConsumeIntegral<uint32_t>(),
Michael Ensing910968d2020-07-19 17:19:31 -070081 eventTime, readTime,
Harry Cutts33476232023-01-30 19:57:29 +000082 /*deviceId=*/fdp.ConsumeIntegral<int32_t>(),
Michael Ensing910968d2020-07-19 17:19:31 -070083 AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_DEFAULT,
Harry Cutts33476232023-01-30 19:57:29 +000084 /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(),
Michael Ensing910968d2020-07-19 17:19:31 -070085 AKEY_EVENT_ACTION_DOWN,
Harry Cutts33476232023-01-30 19:57:29 +000086 /*flags=*/fdp.ConsumeIntegral<int32_t>(), AKEYCODE_HOME,
87 /*scanCode=*/fdp.ConsumeIntegral<int32_t>(), AMETA_NONE,
88 /*downTime=*/fdp.ConsumeIntegral<nsecs_t>());
Michael Ensing910968d2020-07-19 17:19:31 -070089
90 mClassifier->notifyKey(&keyArgs);
91 },
92 [&]() -> void {
93 // SendToNextStage_NotifyMotionArgs
94 NotifyMotionArgs motionArgs = generateFuzzedMotionArgs(fdp);
95 mClassifier->notifyMotion(&motionArgs);
96 },
97 [&]() -> void {
98 // SendToNextStage_NotifySwitchArgs
Harry Cutts33476232023-01-30 19:57:29 +000099 NotifySwitchArgs switchArgs(/*sequenceNum=*/fdp.ConsumeIntegral<uint32_t>(),
100 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(),
101 /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(),
102 /*switchValues=*/fdp.ConsumeIntegral<uint32_t>(),
103 /*switchMask=*/fdp.ConsumeIntegral<uint32_t>());
Michael Ensing910968d2020-07-19 17:19:31 -0700104
105 mClassifier->notifySwitch(&switchArgs);
106 },
107 [&]() -> void {
108 // SendToNextStage_NotifyDeviceResetArgs
Harry Cutts33476232023-01-30 19:57:29 +0000109 NotifyDeviceResetArgs resetArgs(
110 /*sequenceNum=*/fdp.ConsumeIntegral<uint32_t>(),
111 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(),
112 /*deviceId=*/fdp.ConsumeIntegral<int32_t>());
Michael Ensing910968d2020-07-19 17:19:31 -0700113
114 mClassifier->notifyDeviceReset(&resetArgs);
115 },
116 [&]() -> void {
117 // InputClassifierConverterTest
118 const NotifyMotionArgs motionArgs = generateFuzzedMotionArgs(fdp);
119 aidl::android::hardware::input::common::MotionEvent motionEvent =
120 notifyMotionArgsToHalMotionEvent(motionArgs);
121 },
122 })();
123 }
124 return 0;
125}
126
127} // namespace android