Chethan Kumar R E | 041415a | 2021-11-23 17:42:22 +0530 | [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 <BufferStateLayer.h> |
| 18 | #include <Client.h> |
| 19 | #include <DisplayDevice.h> |
| 20 | #include <EffectLayer.h> |
| 21 | #include <LayerRejecter.h> |
| 22 | #include <LayerRenderArea.h> |
| 23 | #include <MonitoredProducer.h> |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame^] | 24 | #include <ftl/future.h> |
Chethan Kumar R E | 041415a | 2021-11-23 17:42:22 +0530 | [diff] [blame] | 25 | #include <fuzzer/FuzzedDataProvider.h> |
| 26 | #include <gui/IProducerListener.h> |
| 27 | #include <gui/LayerDebugInfo.h> |
| 28 | #include <gui/SurfaceComposerClient.h> |
| 29 | #include <gui/WindowInfo.h> |
| 30 | #include <renderengine/mock/FakeExternalTexture.h> |
| 31 | #include <ui/DisplayStatInfo.h> |
| 32 | |
| 33 | #include <FuzzableDataspaces.h> |
| 34 | #include <surfaceflinger_fuzzers_utils.h> |
| 35 | |
| 36 | namespace android::fuzzer { |
| 37 | using namespace renderengine; |
| 38 | |
| 39 | constexpr uint16_t kRandomStringLength = 256; |
| 40 | |
| 41 | class LayerFuzzer { |
| 42 | public: |
| 43 | LayerFuzzer(const uint8_t* data, size_t size) : mFdp(data, size){}; |
| 44 | void init(); |
| 45 | void invokeBufferStateLayer(); |
| 46 | void invokeEffectLayer(); |
| 47 | LayerCreationArgs createLayerCreationArgs(TestableSurfaceFlinger* flinger, sp<Client> client); |
| 48 | Rect getFuzzedRect(); |
| 49 | FrameTimelineInfo getFuzzedFrameTimelineInfo(); |
| 50 | |
| 51 | private: |
| 52 | FuzzedDataProvider mFdp; |
| 53 | }; |
| 54 | |
| 55 | Rect LayerFuzzer::getFuzzedRect() { |
| 56 | return Rect(mFdp.ConsumeIntegral<int32_t>() /*left*/, mFdp.ConsumeIntegral<int32_t>() /*top*/, |
| 57 | mFdp.ConsumeIntegral<int32_t>() /*right*/, |
| 58 | mFdp.ConsumeIntegral<int32_t>() /*bottom*/); |
| 59 | } |
| 60 | |
| 61 | FrameTimelineInfo LayerFuzzer::getFuzzedFrameTimelineInfo() { |
| 62 | return FrameTimelineInfo{.vsyncId = mFdp.ConsumeIntegral<int64_t>(), |
| 63 | .inputEventId = mFdp.ConsumeIntegral<int32_t>()}; |
| 64 | } |
| 65 | |
| 66 | LayerCreationArgs LayerFuzzer::createLayerCreationArgs(TestableSurfaceFlinger* flinger, |
| 67 | sp<Client> client) { |
| 68 | flinger->setupScheduler(std::make_unique<android::mock::VsyncController>(), |
| 69 | std::make_unique<android::mock::VSyncTracker>(), |
| 70 | std::make_unique<android::mock::EventThread>(), |
| 71 | std::make_unique<android::mock::EventThread>()); |
| 72 | |
| 73 | return LayerCreationArgs(flinger->flinger(), client, |
| 74 | mFdp.ConsumeRandomLengthString(kRandomStringLength) /*name*/, |
| 75 | mFdp.ConsumeIntegral<uint32_t>() /*flags*/, {} /*metadata*/); |
| 76 | } |
| 77 | |
| 78 | void LayerFuzzer::invokeEffectLayer() { |
| 79 | TestableSurfaceFlinger flinger; |
| 80 | sp<Client> client = sp<Client>::make(flinger.flinger()); |
| 81 | const LayerCreationArgs layerCreationArgs = createLayerCreationArgs(&flinger, client); |
| 82 | sp<EffectLayer> effectLayer = sp<EffectLayer>::make(layerCreationArgs); |
| 83 | |
| 84 | effectLayer->setColor({(mFdp.ConsumeFloatingPointInRange<float>(0, 255) /*x*/, |
| 85 | mFdp.ConsumeFloatingPointInRange<float>(0, 255) /*y*/, |
| 86 | mFdp.ConsumeFloatingPointInRange<float>(0, 255) /*z*/)}); |
| 87 | effectLayer->setDataspace(mFdp.PickValueInArray(kDataspaces)); |
| 88 | sp<EffectLayer> parent = sp<EffectLayer>::make(layerCreationArgs); |
| 89 | effectLayer->setChildrenDrawingParent(parent); |
| 90 | |
| 91 | const FrameTimelineInfo frameInfo = getFuzzedFrameTimelineInfo(); |
| 92 | const int64_t postTime = mFdp.ConsumeIntegral<int64_t>(); |
| 93 | effectLayer->setFrameTimelineVsyncForBufferTransaction(frameInfo, postTime); |
| 94 | effectLayer->setFrameTimelineVsyncForBufferlessTransaction(frameInfo, postTime); |
| 95 | auto surfaceFrame = effectLayer->createSurfaceFrameForTransaction(frameInfo, postTime); |
| 96 | auto surfaceFrame1 = |
| 97 | effectLayer->createSurfaceFrameForBuffer(frameInfo, postTime, |
| 98 | mFdp.ConsumeRandomLengthString( |
| 99 | kRandomStringLength) /*bufferName*/); |
| 100 | effectLayer->addSurfaceFramePresentedForBuffer(surfaceFrame, |
| 101 | mFdp.ConsumeIntegral<int64_t>() /*acquireTime*/, |
| 102 | mFdp.ConsumeIntegral<int64_t>() /*currentTime*/); |
| 103 | effectLayer->addSurfaceFrameDroppedForBuffer(surfaceFrame1); |
| 104 | |
| 105 | parent.clear(); |
| 106 | client.clear(); |
| 107 | effectLayer.clear(); |
| 108 | } |
| 109 | |
| 110 | void LayerFuzzer::invokeBufferStateLayer() { |
| 111 | TestableSurfaceFlinger flinger; |
| 112 | sp<Client> client = sp<Client>::make(flinger.flinger()); |
| 113 | sp<BufferStateLayer> layer = |
| 114 | sp<BufferStateLayer>::make(createLayerCreationArgs(&flinger, client)); |
| 115 | sp<Fence> fence = sp<Fence>::make(); |
| 116 | const std::shared_ptr<FenceTime> fenceTime = std::make_shared<FenceTime>(fence); |
| 117 | |
| 118 | const CompositorTiming compositor = {mFdp.ConsumeIntegral<int64_t>(), |
| 119 | mFdp.ConsumeIntegral<int64_t>(), |
| 120 | mFdp.ConsumeIntegral<int64_t>()}; |
Dominik Laskowski | bb448ce | 2022-05-07 15:52:55 -0700 | [diff] [blame^] | 121 | |
| 122 | layer->onLayerDisplayed(ftl::yield<FenceResult>(fence).share()); |
| 123 | layer->onLayerDisplayed( |
| 124 | ftl::yield<FenceResult>(base::unexpected(mFdp.ConsumeIntegral<status_t>())).share()); |
| 125 | |
Chethan Kumar R E | 041415a | 2021-11-23 17:42:22 +0530 | [diff] [blame] | 126 | layer->releasePendingBuffer(mFdp.ConsumeIntegral<int64_t>()); |
| 127 | layer->finalizeFrameEventHistory(fenceTime, compositor); |
| 128 | layer->onPostComposition(nullptr, fenceTime, fenceTime, compositor); |
| 129 | layer->isBufferDue(mFdp.ConsumeIntegral<int64_t>()); |
| 130 | |
| 131 | layer->setTransform(mFdp.ConsumeIntegral<uint32_t>()); |
| 132 | layer->setTransformToDisplayInverse(mFdp.ConsumeBool()); |
| 133 | layer->setCrop(getFuzzedRect()); |
| 134 | |
| 135 | layer->setHdrMetadata(getFuzzedHdrMetadata(&mFdp)); |
| 136 | layer->setDataspace(mFdp.PickValueInArray(kDataspaces)); |
| 137 | if (mFdp.ConsumeBool()) { |
| 138 | layer->setSurfaceDamageRegion(Region()); |
| 139 | layer->setTransparentRegionHint(Region()); |
| 140 | } else { |
| 141 | layer->setSurfaceDamageRegion(Region(getFuzzedRect())); |
| 142 | layer->setTransparentRegionHint(Region(getFuzzedRect())); |
| 143 | } |
| 144 | layer->setApi(mFdp.ConsumeIntegral<int32_t>()); |
| 145 | |
| 146 | native_handle_t* testHandle = native_handle_create(0, 1); |
| 147 | const bool ownsHandle = mFdp.ConsumeBool(); |
| 148 | sp<NativeHandle> nativeHandle = sp<NativeHandle>::make(testHandle, ownsHandle); |
| 149 | layer->setSidebandStream(nativeHandle); |
| 150 | layer->addFrameEvent(fence, mFdp.ConsumeIntegral<int64_t>() /*postedTime*/, |
| 151 | mFdp.ConsumeIntegral<int64_t>() /*requestedTime*/); |
| 152 | layer->computeSourceBounds(getFuzzedFloatRect(&mFdp)); |
| 153 | |
| 154 | layer->fenceHasSignaled(); |
| 155 | layer->framePresentTimeIsCurrent(mFdp.ConsumeIntegral<int64_t>()); |
| 156 | layer->onPreComposition(mFdp.ConsumeIntegral<int64_t>()); |
| 157 | const std::vector<sp<CallbackHandle>> callbacks; |
| 158 | layer->setTransactionCompletedListeners(callbacks); |
| 159 | |
| 160 | std::shared_ptr<renderengine::ExternalTexture> texture = std::make_shared< |
| 161 | renderengine::mock::FakeExternalTexture>(mFdp.ConsumeIntegral<uint32_t>(), |
| 162 | mFdp.ConsumeIntegral<uint32_t>(), |
| 163 | mFdp.ConsumeIntegral<uint64_t>(), |
| 164 | static_cast<android::PixelFormat>( |
| 165 | mFdp.PickValueInArray(kPixelFormats)), |
| 166 | mFdp.ConsumeIntegral<uint64_t>()); |
| 167 | layer->setBuffer(texture, {} /*bufferData*/, mFdp.ConsumeIntegral<nsecs_t>() /*postTime*/, |
| 168 | mFdp.ConsumeIntegral<nsecs_t>() /*desiredTime*/, |
| 169 | mFdp.ConsumeBool() /*isAutoTimestamp*/, |
| 170 | {mFdp.ConsumeIntegral<nsecs_t>()} /*dequeue*/, {} /*info*/); |
| 171 | |
| 172 | LayerRenderArea layerArea(*(flinger.flinger()), layer, getFuzzedRect(), |
| 173 | {mFdp.ConsumeIntegral<int32_t>(), |
| 174 | mFdp.ConsumeIntegral<int32_t>()} /*reqSize*/, |
| 175 | mFdp.PickValueInArray(kDataspaces), mFdp.ConsumeBool(), |
| 176 | getFuzzedRect(), mFdp.ConsumeBool()); |
| 177 | layerArea.render([]() {} /*drawLayers*/); |
| 178 | |
| 179 | if (!ownsHandle) { |
| 180 | native_handle_close(testHandle); |
| 181 | native_handle_delete(testHandle); |
| 182 | } |
| 183 | nativeHandle.clear(); |
| 184 | fence.clear(); |
| 185 | client.clear(); |
| 186 | layer.clear(); |
| 187 | } |
| 188 | |
| 189 | void LayerFuzzer::init() { |
| 190 | invokeBufferStateLayer(); |
| 191 | invokeEffectLayer(); |
| 192 | } |
| 193 | |
| 194 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 195 | LayerFuzzer layerFuzzer(data, size); |
| 196 | layerFuzzer.init(); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | } // namespace android::fuzzer |