Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 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> |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 18 | #include <linux/input.h> |
| 19 | |
| 20 | #include "../../InputDeviceMetricsSource.h" |
jioana | 0bdbea1 | 2024-08-10 19:26:04 +0000 | [diff] [blame] | 21 | #include "../InputEventTimeline.h" |
Siarhei Vishniakou | 93ee540 | 2024-10-09 00:07:23 +0000 | [diff] [blame^] | 22 | #include "NotifyArgsBuilders.h" |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 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 | */ |
| 37 | void processTimeline(const InputEventTimeline& timeline) override { |
| 38 | for (const auto& [token, connectionTimeline] : timeline.connectionTimelines) { |
| 39 | connectionTimeline.isComplete(); |
| 40 | } |
| 41 | }; |
jioana | 24878b5 | 2024-09-10 10:13:27 +0000 | [diff] [blame] | 42 | |
| 43 | void pushLatencyStatistics() override {} |
| 44 | |
| 45 | std::string dump(const char* prefix) const { return ""; }; |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static sp<IBinder> getConnectionToken(FuzzedDataProvider& fdp, |
| 49 | std::array<sp<IBinder>, 10>& tokens) { |
| 50 | const bool useExistingToken = fdp.ConsumeBool(); |
| 51 | if (useExistingToken) { |
Siarhei Vishniakou | 96aed5f | 2021-07-13 10:56:15 -0700 | [diff] [blame] | 52 | return tokens[fdp.ConsumeIntegralInRange<size_t>(0ul, tokens.size() - 1)]; |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 53 | } |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 54 | return sp<BBinder>::make(); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) { |
| 58 | FuzzedDataProvider fdp(data, size); |
| 59 | |
| 60 | EmptyProcessor emptyProcessor; |
jioana | 24878b5 | 2024-09-10 10:13:27 +0000 | [diff] [blame] | 61 | LatencyTracker tracker(emptyProcessor); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 62 | |
| 63 | // Make some pre-defined tokens to ensure that some timelines are complete. |
| 64 | std::array<sp<IBinder> /*token*/, 10> predefinedTokens; |
Siarhei Vishniakou | 93ee540 | 2024-10-09 00:07:23 +0000 | [diff] [blame^] | 65 | for (sp<IBinder>& token : predefinedTokens) { |
| 66 | token = sp<BBinder>::make(); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 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 { |
Siarhei Vishniakou | 93ee540 | 2024-10-09 00:07:23 +0000 | [diff] [blame^] | 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>(); |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 76 | const DeviceId deviceId = fdp.ConsumeIntegral<int32_t>(); |
Siarhei Vishniakou | 93ee540 | 2024-10-09 00:07:23 +0000 | [diff] [blame^] | 77 | const int32_t source = fdp.ConsumeIntegral<int32_t>(); |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 78 | std::set<InputDeviceUsageSource> sources = { |
| 79 | fdp.ConsumeEnum<InputDeviceUsageSource>()}; |
jioana | 97cc8ac | 2024-09-09 15:01:43 +0000 | [diff] [blame] | 80 | const int32_t inputEventActionType = fdp.ConsumeIntegral<int32_t>(); |
jioana | 0bdbea1 | 2024-08-10 19:26:04 +0000 | [diff] [blame] | 81 | const InputEventType inputEventType = fdp.ConsumeEnum<InputEventType>(); |
Siarhei Vishniakou | 93ee540 | 2024-10-09 00:07:23 +0000 | [diff] [blame^] | 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); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 92 | }, |
| 93 | [&]() -> void { |
Siarhei Vishniakou | 93ee540 | 2024-10-09 00:07:23 +0000 | [diff] [blame^] | 94 | const int32_t inputEventId = fdp.ConsumeIntegral<int32_t>(); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 95 | sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens); |
Siarhei Vishniakou | 93ee540 | 2024-10-09 00:07:23 +0000 | [diff] [blame^] | 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>(); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 99 | tracker.trackFinishedEvent(inputEventId, connectionToken, deliveryTime, |
| 100 | consumeTime, finishTime); |
| 101 | }, |
| 102 | [&]() -> void { |
Siarhei Vishniakou | 93ee540 | 2024-10-09 00:07:23 +0000 | [diff] [blame^] | 103 | const int32_t inputEventId = fdp.ConsumeIntegral<int32_t>(); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 104 | sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens); |
Siarhei Vishniakou | 93ee540 | 2024-10-09 00:07:23 +0000 | [diff] [blame^] | 105 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline{}; |
| 106 | for (nsecs_t& t : graphicsTimeline) { |
| 107 | t = fdp.ConsumeIntegral<nsecs_t>(); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 108 | } |
| 109 | tracker.trackGraphicsLatency(inputEventId, connectionToken, graphicsTimeline); |
| 110 | }, |
| 111 | })(); |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | } // namespace inputdispatcher |
| 118 | |
| 119 | } // namespace android |