blob: 46a6189d0f86d5ba5592d7e1223b28fed3f37113 [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 {
Michael Ensing910968d2020-07-19 17:19:31 -070042 // SendToNextStage_NotifyKeyArgs
Atharva_Deshpande2389f162023-08-01 16:19:09 +053043 const nsecs_t eventTime =
44 fdp.ConsumeIntegralInRange<nsecs_t>(0,
45 systemTime(SYSTEM_TIME_MONOTONIC));
46 const nsecs_t readTime = fdp.ConsumeIntegralInRange<
47 nsecs_t>(eventTime, std::numeric_limits<nsecs_t>::max());
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000048 mClassifier->notifyKey({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
49 eventTime, readTime,
50 /*deviceId=*/fdp.ConsumeIntegral<int32_t>(),
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070051 AINPUT_SOURCE_KEYBOARD, ui::LogicalDisplayId::DEFAULT,
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000052 /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(),
53 AKEY_EVENT_ACTION_DOWN,
54 /*flags=*/fdp.ConsumeIntegral<int32_t>(), AKEYCODE_HOME,
55 /*scanCode=*/fdp.ConsumeIntegral<int32_t>(), AMETA_NONE,
56 /*downTime=*/fdp.ConsumeIntegral<nsecs_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -070057 },
58 [&]() -> void {
59 // SendToNextStage_NotifyMotionArgs
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070060 mClassifier->notifyMotion(
61 generateFuzzedMotionArgs(idGenerator, fdp, MAX_RANDOM_DISPLAYS));
Michael Ensing910968d2020-07-19 17:19:31 -070062 },
63 [&]() -> void {
64 // SendToNextStage_NotifySwitchArgs
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000065 mClassifier->notifySwitch({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
66 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(),
67 /*policyFlags=*/fdp.ConsumeIntegral<uint32_t>(),
68 /*switchValues=*/fdp.ConsumeIntegral<uint32_t>(),
69 /*switchMask=*/fdp.ConsumeIntegral<uint32_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -070070 },
71 [&]() -> void {
72 // SendToNextStage_NotifyDeviceResetArgs
Prabir Pradhanc392d8f2023-04-13 19:32:51 +000073 mClassifier->notifyDeviceReset({/*sequenceNum=*/fdp.ConsumeIntegral<int32_t>(),
74 /*eventTime=*/fdp.ConsumeIntegral<nsecs_t>(),
75 /*deviceId=*/fdp.ConsumeIntegral<int32_t>()});
Michael Ensing910968d2020-07-19 17:19:31 -070076 },
77 [&]() -> void {
78 // InputClassifierConverterTest
Siarhei Vishniakou2defec02023-06-08 17:24:44 -070079 const NotifyMotionArgs motionArgs =
80 generateFuzzedMotionArgs(idGenerator, fdp, MAX_RANDOM_DISPLAYS);
Michael Ensing910968d2020-07-19 17:19:31 -070081 aidl::android::hardware::input::common::MotionEvent motionEvent =
82 notifyMotionArgsToHalMotionEvent(motionArgs);
83 },
84 })();
85 }
86 return 0;
87}
88
89} // namespace android