blob: 908fa40f05f0bf99f111802d8760e5024e47d735 [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 };
jioana24878b52024-09-10 10:13:27 +000041
42 void pushLatencyStatistics() override {}
43
44 std::string dump(const char* prefix) const { return ""; };
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000045};
46
47static sp<IBinder> getConnectionToken(FuzzedDataProvider& fdp,
48 std::array<sp<IBinder>, 10>& tokens) {
49 const bool useExistingToken = fdp.ConsumeBool();
50 if (useExistingToken) {
Siarhei Vishniakou96aed5f2021-07-13 10:56:15 -070051 return tokens[fdp.ConsumeIntegralInRange<size_t>(0ul, tokens.size() - 1)];
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000052 }
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070053 return sp<BBinder>::make();
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000054}
55
56extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
57 FuzzedDataProvider fdp(data, size);
58
59 EmptyProcessor emptyProcessor;
jioana24878b52024-09-10 10:13:27 +000060 LatencyTracker tracker(emptyProcessor);
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000061
62 // Make some pre-defined tokens to ensure that some timelines are complete.
63 std::array<sp<IBinder> /*token*/, 10> predefinedTokens;
64 for (size_t i = 0; i < predefinedTokens.size(); i++) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070065 predefinedTokens[i] = sp<BBinder>::make();
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000066 }
67
68 // Randomly invoke LatencyTracker api's until randomness is exhausted.
69 while (fdp.remaining_bytes() > 0) {
70 fdp.PickValueInArray<std::function<void()>>({
71 [&]() -> void {
72 int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000073 nsecs_t eventTime = fdp.ConsumeIntegral<nsecs_t>();
74 nsecs_t readTime = fdp.ConsumeIntegral<nsecs_t>();
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000075 const DeviceId deviceId = fdp.ConsumeIntegral<int32_t>();
76 std::set<InputDeviceUsageSource> sources = {
77 fdp.ConsumeEnum<InputDeviceUsageSource>()};
jioana97cc8ac2024-09-09 15:01:43 +000078 const int32_t inputEventActionType = fdp.ConsumeIntegral<int32_t>();
jioana0bdbea12024-08-10 19:26:04 +000079 const InputEventType inputEventType = fdp.ConsumeEnum<InputEventType>();
80 tracker.trackListener(inputEventId, eventTime, readTime, deviceId, sources,
81 inputEventActionType, inputEventType);
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000082 },
83 [&]() -> void {
84 int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
85 sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens);
86 nsecs_t deliveryTime = fdp.ConsumeIntegral<nsecs_t>();
87 nsecs_t consumeTime = fdp.ConsumeIntegral<nsecs_t>();
88 nsecs_t finishTime = fdp.ConsumeIntegral<nsecs_t>();
89 tracker.trackFinishedEvent(inputEventId, connectionToken, deliveryTime,
90 consumeTime, finishTime);
91 },
92 [&]() -> void {
93 int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
94 sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens);
95 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
96 for (size_t i = 0; i < graphicsTimeline.size(); i++) {
97 graphicsTimeline[i] = fdp.ConsumeIntegral<nsecs_t>();
98 }
99 tracker.trackGraphicsLatency(inputEventId, connectionToken, graphicsTimeline);
100 },
101 })();
102 }
103
104 return 0;
105}
106
107} // namespace inputdispatcher
108
109} // namespace android