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