xref: /aosp_15_r20/frameworks/native/services/inputflinger/tests/fuzzers/LatencyTrackerFuzzer.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
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 <linux/input.h>
19 
20 #include "../../InputDeviceMetricsSource.h"
21 #include "../InputEventTimeline.h"
22 #include "NotifyArgsBuilders.h"
23 #include "dispatcher/LatencyTracker.h"
24 
25 namespace android {
26 
27 namespace inputdispatcher {
28 
29 /**
30  * A processor of InputEventTimelines that does nothing with the provided data.
31  */
32 class EmptyProcessor : public InputEventTimelineProcessor {
33 public:
34     /**
35      * Just ignore the provided timeline
36      */
processTimeline(const InputEventTimeline & timeline)37     void processTimeline(const InputEventTimeline& timeline) override {
38         for (const auto& [token, connectionTimeline] : timeline.connectionTimelines) {
39             connectionTimeline.isComplete();
40         }
41     };
42 
pushLatencyStatistics()43     void pushLatencyStatistics() override {}
44 
dump(const char * prefix) const45     std::string dump(const char* prefix) const { return ""; };
46 };
47 
getConnectionToken(FuzzedDataProvider & fdp,std::array<sp<IBinder>,10> & tokens)48 static sp<IBinder> getConnectionToken(FuzzedDataProvider& fdp,
49                                       std::array<sp<IBinder>, 10>& tokens) {
50     const bool useExistingToken = fdp.ConsumeBool();
51     if (useExistingToken) {
52         return tokens[fdp.ConsumeIntegralInRange<size_t>(0ul, tokens.size() - 1)];
53     }
54     return sp<BBinder>::make();
55 }
56 
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
58     FuzzedDataProvider fdp(data, size);
59 
60     EmptyProcessor emptyProcessor;
61     LatencyTracker tracker(emptyProcessor);
62 
63     // Make some pre-defined tokens to ensure that some timelines are complete.
64     std::array<sp<IBinder> /*token*/, 10> predefinedTokens;
65     for (sp<IBinder>& token : predefinedTokens) {
66         token = sp<BBinder>::make();
67     }
68 
69     // Randomly invoke LatencyTracker api's until randomness is exhausted.
70     while (fdp.remaining_bytes() > 0) {
71         fdp.PickValueInArray<std::function<void()>>({
72                 [&]() -> void {
73                     const int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
74                     const nsecs_t eventTime = fdp.ConsumeIntegral<nsecs_t>();
75                     const nsecs_t readTime = fdp.ConsumeIntegral<nsecs_t>();
76                     const DeviceId deviceId = fdp.ConsumeIntegral<int32_t>();
77                     const int32_t source = fdp.ConsumeIntegral<int32_t>();
78                     std::set<InputDeviceUsageSource> sources = {
79                             fdp.ConsumeEnum<InputDeviceUsageSource>()};
80                     const int32_t inputEventActionType = fdp.ConsumeIntegral<int32_t>();
81                     const InputEventType inputEventType = fdp.ConsumeEnum<InputEventType>();
82                     const NotifyMotionArgs args =
83                             MotionArgsBuilder(inputEventActionType, source, inputEventId)
84                                     .eventTime(eventTime)
85                                     .readTime(readTime)
86                                     .deviceId(deviceId)
87                                     .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER)
88                                                      .x(100)
89                                                      .y(200))
90                                     .build();
91                     tracker.trackListener(args);
92                 },
93                 [&]() -> void {
94                     const int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
95                     sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens);
96                     const nsecs_t deliveryTime = fdp.ConsumeIntegral<nsecs_t>();
97                     const nsecs_t consumeTime = fdp.ConsumeIntegral<nsecs_t>();
98                     const nsecs_t finishTime = fdp.ConsumeIntegral<nsecs_t>();
99                     tracker.trackFinishedEvent(inputEventId, connectionToken, deliveryTime,
100                                                consumeTime, finishTime);
101                 },
102                 [&]() -> void {
103                     const int32_t inputEventId = fdp.ConsumeIntegral<int32_t>();
104                     sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens);
105                     std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline{};
106                     for (nsecs_t& t : graphicsTimeline) {
107                         t = fdp.ConsumeIntegral<nsecs_t>();
108                     }
109                     tracker.trackGraphicsLatency(inputEventId, connectionToken, graphicsTimeline);
110                 },
111         })();
112     }
113 
114     return 0;
115 }
116 
117 } // namespace inputdispatcher
118 
119 } // namespace android