blob: 72780fb363c66cc3e309809bfec3d89a07a14d4d [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>
18#include "dispatcher/LatencyTracker.h"
19
20namespace android {
21
22namespace inputdispatcher {
23
24/**
25 * A processor of InputEventTimelines that does nothing with the provided data.
26 */
27class EmptyProcessor : public InputEventTimelineProcessor {
28public:
29 /**
30 * Just ignore the provided timeline
31 */
32 void processTimeline(const InputEventTimeline& timeline) override {
33 for (const auto& [token, connectionTimeline] : timeline.connectionTimelines) {
34 connectionTimeline.isComplete();
35 }
36 };
37};
38
39static sp<IBinder> getConnectionToken(FuzzedDataProvider& fdp,
40 std::array<sp<IBinder>, 10>& tokens) {
41 const bool useExistingToken = fdp.ConsumeBool();
42 if (useExistingToken) {
Siarhei Vishniakou96aed5f2021-07-13 10:56:15 -070043 return tokens[fdp.ConsumeIntegralInRange<size_t>(0ul, tokens.size() - 1)];
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000044 }
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070045 return sp<BBinder>::make();
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000046}
47
48extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
49 FuzzedDataProvider fdp(data, size);
50
51 EmptyProcessor emptyProcessor;
52 LatencyTracker tracker(&emptyProcessor);
53
54 // Make some pre-defined tokens to ensure that some timelines are complete.
55 std::array<sp<IBinder> /*token*/, 10> predefinedTokens;
56 for (size_t i = 0; i < predefinedTokens.size(); i++) {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070057 predefinedTokens[i] = sp<BBinder>::make();
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000058 }
59
60 // Randomly invoke LatencyTracker api's until randomness is exhausted.
61 while (fdp.remaining_bytes() > 0) {
62 fdp.PickValueInArray<std::function<void()>>({
63 [&]() -> void {
64 int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
65 int32_t isDown = fdp.ConsumeBool();
66 nsecs_t eventTime = fdp.ConsumeIntegral<nsecs_t>();
67 nsecs_t readTime = fdp.ConsumeIntegral<nsecs_t>();
68 tracker.trackListener(inputEventId, isDown, eventTime, readTime);
69 },
70 [&]() -> void {
71 int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
72 sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens);
73 nsecs_t deliveryTime = fdp.ConsumeIntegral<nsecs_t>();
74 nsecs_t consumeTime = fdp.ConsumeIntegral<nsecs_t>();
75 nsecs_t finishTime = fdp.ConsumeIntegral<nsecs_t>();
76 tracker.trackFinishedEvent(inputEventId, connectionToken, deliveryTime,
77 consumeTime, finishTime);
78 },
79 [&]() -> void {
80 int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
81 sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens);
82 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
83 for (size_t i = 0; i < graphicsTimeline.size(); i++) {
84 graphicsTimeline[i] = fdp.ConsumeIntegral<nsecs_t>();
85 }
86 tracker.trackGraphicsLatency(inputEventId, connectionToken, graphicsTimeline);
87 },
88 })();
89 }
90
91 return 0;
92}
93
94} // namespace inputdispatcher
95
96} // namespace android