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 | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 22 | #include "dispatcher/LatencyTracker.h" |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | namespace inputdispatcher { |
| 27 | |
| 28 | /** |
| 29 | * A processor of InputEventTimelines that does nothing with the provided data. |
| 30 | */ |
| 31 | class EmptyProcessor : public InputEventTimelineProcessor { |
| 32 | public: |
| 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 | }; |
| 41 | }; |
| 42 | |
| 43 | static sp<IBinder> getConnectionToken(FuzzedDataProvider& fdp, |
| 44 | std::array<sp<IBinder>, 10>& tokens) { |
| 45 | const bool useExistingToken = fdp.ConsumeBool(); |
| 46 | if (useExistingToken) { |
Siarhei Vishniakou | 96aed5f | 2021-07-13 10:56:15 -0700 | [diff] [blame] | 47 | return tokens[fdp.ConsumeIntegralInRange<size_t>(0ul, tokens.size() - 1)]; |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 48 | } |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 49 | return sp<BBinder>::make(); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) { |
| 53 | FuzzedDataProvider fdp(data, size); |
| 54 | |
| 55 | EmptyProcessor emptyProcessor; |
| 56 | LatencyTracker tracker(&emptyProcessor); |
| 57 | |
| 58 | // Make some pre-defined tokens to ensure that some timelines are complete. |
| 59 | std::array<sp<IBinder> /*token*/, 10> predefinedTokens; |
| 60 | for (size_t i = 0; i < predefinedTokens.size(); i++) { |
Siarhei Vishniakou | aed7ad0 | 2022-08-03 15:04:33 -0700 | [diff] [blame] | 61 | predefinedTokens[i] = sp<BBinder>::make(); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Randomly invoke LatencyTracker api's until randomness is exhausted. |
| 65 | while (fdp.remaining_bytes() > 0) { |
| 66 | fdp.PickValueInArray<std::function<void()>>({ |
| 67 | [&]() -> void { |
| 68 | int32_t inputEventId = fdp.ConsumeIntegral<int32_t>(); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 69 | nsecs_t eventTime = fdp.ConsumeIntegral<nsecs_t>(); |
| 70 | nsecs_t readTime = fdp.ConsumeIntegral<nsecs_t>(); |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 71 | const DeviceId deviceId = fdp.ConsumeIntegral<int32_t>(); |
| 72 | std::set<InputDeviceUsageSource> sources = { |
| 73 | fdp.ConsumeEnum<InputDeviceUsageSource>()}; |
jioana | 97cc8ac | 2024-09-09 15:01:43 +0000 | [diff] [blame] | 74 | const int32_t inputEventActionType = fdp.ConsumeIntegral<int32_t>(); |
jioana | 0bdbea1 | 2024-08-10 19:26:04 +0000 | [diff] [blame] | 75 | const InputEventType inputEventType = fdp.ConsumeEnum<InputEventType>(); |
| 76 | tracker.trackListener(inputEventId, eventTime, readTime, deviceId, sources, |
| 77 | inputEventActionType, inputEventType); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 78 | }, |
| 79 | [&]() -> void { |
| 80 | int32_t inputEventId = fdp.ConsumeIntegral<int32_t>(); |
| 81 | sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens); |
| 82 | nsecs_t deliveryTime = fdp.ConsumeIntegral<nsecs_t>(); |
| 83 | nsecs_t consumeTime = fdp.ConsumeIntegral<nsecs_t>(); |
| 84 | nsecs_t finishTime = fdp.ConsumeIntegral<nsecs_t>(); |
| 85 | tracker.trackFinishedEvent(inputEventId, connectionToken, deliveryTime, |
| 86 | consumeTime, finishTime); |
| 87 | }, |
| 88 | [&]() -> void { |
| 89 | int32_t inputEventId = fdp.ConsumeIntegral<int32_t>(); |
| 90 | sp<IBinder> connectionToken = getConnectionToken(fdp, predefinedTokens); |
| 91 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 92 | for (size_t i = 0; i < graphicsTimeline.size(); i++) { |
| 93 | graphicsTimeline[i] = fdp.ConsumeIntegral<nsecs_t>(); |
| 94 | } |
| 95 | tracker.trackGraphicsLatency(inputEventId, connectionToken, graphicsTimeline); |
| 96 | }, |
| 97 | })(); |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | } // namespace inputdispatcher |
| 104 | |
| 105 | } // namespace android |