blob: 695eb3c5ddb62e282c7eefa046ac4ec3bcab2b2c [file] [log] [blame]
Siarhei Vishniakou363e7292021-07-09 03:22:42 +00001/*
2 * Copyright 2021 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 <fuzzer/FuzzedDataProvider.h>
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000018#include <linux/input.h>
19
20#include "../../InputDeviceMetricsSource.h"
jioana0bdbea12024-08-10 19:26:04 +000021#include "../InputEventTimeline.h"
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000022#include "dispatcher/LatencyTracker.h"
23
24namespace android {
25
26namespace inputdispatcher {
27
28/**
29 * A processor of InputEventTimelines that does nothing with the provided data.
30 */
31class EmptyProcessor : public InputEventTimelineProcessor {
32public:
33 /**
34 * Just ignore the provided timeline
35 */
36 void processTimeline(const InputEventTimeline& timeline) override {
37 for (const auto& [token, connectionTimeline] : timeline.connectionTimelines) {
38 connectionTimeline.isComplete();
39 }
40 };
41};
42
43static sp<IBinder> getConnectionToken(FuzzedDataProvider& fdp,
44 std::array<sp<IBinder>, 10>& tokens) {
45 const bool useExistingToken = fdp.ConsumeBool();
46 if (useExistingToken) {
Siarhei Vishniakou96aed5f2021-07-13 10:56:15 -070047 return tokens[fdp.ConsumeIntegralInRange<size_t>(0ul, tokens.size() - 1)];
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000048 }
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070049 return sp<BBinder>::make();
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000050}
51
52extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
53 FuzzedDataProvider fdp(data, size);
54
55 EmptyProcessor emptyProcessor;
56 LatencyTracker tracker(&emptyProcessor);
57
58 // Make some pre-defined tokens to ensure that some timelines are complete.
59 std::array<sp<IBinder> /*token*/, 10> predefinedTokens;
60 for (size_t i = 0; i < predefinedTokens.size(); i++) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070061 predefinedTokens[i] = sp<BBinder>::make();
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000062 }
63
64 // Randomly invoke LatencyTracker api's until randomness is exhausted.
65 while (fdp.remaining_bytes() > 0) {
66 fdp.PickValueInArray<std::function<void()>>({
67 [&]() -> void {
68 int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000069 nsecs_t eventTime = fdp.ConsumeIntegral<nsecs_t>();
70 nsecs_t readTime = fdp.ConsumeIntegral<nsecs_t>();
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000071 const DeviceId deviceId = fdp.ConsumeIntegral<int32_t>();
72 std::set<InputDeviceUsageSource> sources = {
73 fdp.ConsumeEnum<InputDeviceUsageSource>()};
jioana97cc8ac2024-09-09 15:01:43 +000074 const int32_t inputEventActionType = fdp.ConsumeIntegral<int32_t>();
jioana0bdbea12024-08-10 19:26:04 +000075 const InputEventType inputEventType = fdp.ConsumeEnum<InputEventType>();
76 tracker.trackListener(inputEventId, eventTime, readTime, deviceId, sources,
77 inputEventActionType, inputEventType);
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000078 },
79 [&]() -> void {
80 int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
81 sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens);
82 nsecs_t deliveryTime = fdp.ConsumeIntegral<nsecs_t>();
83 nsecs_t consumeTime = fdp.ConsumeIntegral<nsecs_t>();
84 nsecs_t finishTime = fdp.ConsumeIntegral<nsecs_t>();
85 tracker.trackFinishedEvent(inputEventId, connectionToken, deliveryTime,
86 consumeTime, finishTime);
87 },
88 [&]() -> void {
89 int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
90 sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens);
91 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
92 for (size_t i = 0; i < graphicsTimeline.size(); i++) {
93 graphicsTimeline[i] = fdp.ConsumeIntegral<nsecs_t>();
94 }
95 tracker.trackGraphicsLatency(inputEventId, connectionToken, graphicsTimeline);
96 },
97 })();
98 }
99
100 return 0;
101}
102
103} // namespace inputdispatcher
104
105} // namespace android