blob: 3b3ed9ba0d88afa46786ef598c4ca61bdc92f3dd [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;
Siarhei Vishniakou09a8fe42022-07-21 17:27:03 -070031 properties.toolType = getFuzzedToolType(fdp);
Michael Ensing910968d2020-07-19 17:19:31 -070032 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
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000070 mClassifier->notifyConfigurationChanged(
71 {/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
72 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -070073 },
74 [&]() -> void {
75 // SendToNextStage_NotifyKeyArgs
Atharva_Deshpande2389f162023-08-01 16:19:09 +053076 const nsecs_t eventTime =
77 fdp.ConsumeIntegralInRange<nsecs_t>(0,
78 systemTime(SYSTEM_TIME_MONOTONIC));
79 const nsecs_t readTime = fdp.ConsumeIntegralInRange<
80 nsecs_t>(eventTime, std::numeric_limits<nsecs_t>::max());
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000081 mClassifier->notifyKey({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
82 eventTime, readTime,
83 /*deviceId=*/fdp.ConsumeIntegral<int32_t>(),
84 AINPUT_SOURCE_KEYBOARD, ADISPLAY_ID_DEFAULT,
85 /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(),
86 AKEY_EVENT_ACTION_DOWN,
87 /*flags=*/fdp.ConsumeIntegral<int32_t>(), AKEYCODE_HOME,
88 /*scanCode=*/fdp.ConsumeIntegral<int32_t>(), AMETA_NONE,
89 /*downTime=*/fdp.ConsumeIntegral<nsecs_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -070090 },
91 [&]() -> void {
92 // SendToNextStage_NotifyMotionArgs
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000093 mClassifier->notifyMotion(generateFuzzedMotionArgs(fdp));
Michael Ensing910968d2020-07-19 17:19:31 -070094 },
95 [&]() -> void {
96 // SendToNextStage_NotifySwitchArgs
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000097 mClassifier->notifySwitch({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
98 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(),
99 /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(),
100 /*switchValues=*/fdp.ConsumeIntegral<uint32_t>(),
101 /*switchMask=*/fdp.ConsumeIntegral<uint32_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -0700102 },
103 [&]() -> void {
104 // SendToNextStage_NotifyDeviceResetArgs
Prabir Pradhanc392d8f2023-04-13 19:32:51 +0000105 mClassifier->notifyDeviceReset({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
106 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(),
107 /*deviceId=*/fdp.ConsumeIntegral<int32_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -0700108 },
109 [&]() -> void {
110 // InputClassifierConverterTest
111 const NotifyMotionArgs motionArgs = generateFuzzedMotionArgs(fdp);
112 aidl::android::hardware::input::common::MotionEvent motionEvent =
113 notifyMotionArgsToHalMotionEvent(motionArgs);
114 },
115 })();
116 }
117 return 0;
118}
119
120} // namespace android