blob: 0446d76218fa431e27cddd38241f17d0eba99acb [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>
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070019#include "FuzzedInputStream.h"
Michael Ensing910968d2020-07-19 17:19:31 -070020#include "InputCommonConverter.h"
21#include "InputProcessor.h"
22
23namespace android {
24
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070025namespace {
Michael Ensing910968d2020-07-19 17:19:31 -070026
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070027constexpr int32_t MAX_RANDOM_DISPLAYS = 4;
Michael Ensing910968d2020-07-19 17:19:31 -070028
Michael Ensing910968d2020-07-19 17:19:31 -070029}
30
31extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
32 FuzzedDataProvider fdp(data, size);
33
34 std::unique_ptr<FuzzInputListener> mFuzzListener = std::make_unique<FuzzInputListener>();
35 std::unique_ptr<InputProcessorInterface> mClassifier =
36 std::make_unique<InputProcessor>(*mFuzzListener);
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070037 IdGenerator idGenerator(IdGenerator::Source::OTHER);
Michael Ensing910968d2020-07-19 17:19:31 -070038
39 while (fdp.remaining_bytes() > 0) {
40 fdp.PickValueInArray<std::function<void()>>({
41 [&]() -> void {
42 // SendToNextStage_NotifyConfigurationChangedArgs
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000043 mClassifier->notifyConfigurationChanged(
44 {/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
45 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -070046 },
47 [&]() -> void {
48 // SendToNextStage_NotifyKeyArgs
Atharva_Deshpande2389f162023-08-01 16:19:09 +053049 const nsecs_t eventTime =
50 fdp.ConsumeIntegralInRange<nsecs_t>(0,
51 systemTime(SYSTEM_TIME_MONOTONIC));
52 const nsecs_t readTime = fdp.ConsumeIntegralInRange<
53 nsecs_t>(eventTime, std::numeric_limits<nsecs_t>::max());
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000054 mClassifier->notifyKey({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
55 eventTime, readTime,
56 /*deviceId=*/fdp.ConsumeIntegral<int32_t>(),
Linnan Li13bf76a2024-05-05 19:18:02 +080057 AINPUT_SOURCE_KEYBOARD, ui::ADISPLAY_ID_DEFAULT,
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000058 /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(),
59 AKEY_EVENT_ACTION_DOWN,
60 /*flags=*/fdp.ConsumeIntegral<int32_t>(), AKEYCODE_HOME,
61 /*scanCode=*/fdp.ConsumeIntegral<int32_t>(), AMETA_NONE,
62 /*downTime=*/fdp.ConsumeIntegral<nsecs_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -070063 },
64 [&]() -> void {
65 // SendToNextStage_NotifyMotionArgs
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070066 mClassifier->notifyMotion(
67 generateFuzzedMotionArgs(idGenerator, fdp, MAX_RANDOM_DISPLAYS));
Michael Ensing910968d2020-07-19 17:19:31 -070068 },
69 [&]() -> void {
70 // SendToNextStage_NotifySwitchArgs
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000071 mClassifier->notifySwitch({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
72 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(),
73 /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(),
74 /*switchValues=*/fdp.ConsumeIntegral<uint32_t>(),
75 /*switchMask=*/fdp.ConsumeIntegral<uint32_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -070076 },
77 [&]() -> void {
78 // SendToNextStage_NotifyDeviceResetArgs
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000079 mClassifier->notifyDeviceReset({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
80 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(),
81 /*deviceId=*/fdp.ConsumeIntegral<int32_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -070082 },
83 [&]() -> void {
84 // InputClassifierConverterTest
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070085 const NotifyMotionArgs motionArgs =
86 generateFuzzedMotionArgs(idGenerator, fdp, MAX_RANDOM_DISPLAYS);
Michael Ensing910968d2020-07-19 17:19:31 -070087 aidl::android::hardware::input::common::MotionEvent motionEvent =
88 notifyMotionArgsToHalMotionEvent(motionArgs);
89 },
90 })();
91 }
92 return 0;
93}
94
95} // namespace android