Alec Mouri | b2089a3 | 2022-01-15 00:34:20 +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 | |
| 18 | #include <FuzzableDataspaces.h> |
| 19 | #include <binder/IServiceManager.h> |
| 20 | #include <fuzzer/FuzzedDataProvider.h> |
| 21 | #include <ui/DisplayStatInfo.h> |
| 22 | #include "surfaceflinger_fuzzers_utils.h" |
| 23 | |
| 24 | namespace android::fuzz { |
| 25 | |
| 26 | static constexpr LatchUnsignaledConfig kLatchUnsignaledConfig[] = { |
| 27 | LatchUnsignaledConfig::Always, |
Ady Abraham | 9dada82 | 2022-02-03 10:26:59 -0800 | [diff] [blame] | 28 | LatchUnsignaledConfig::AutoSingleLayer, |
Alec Mouri | b2089a3 | 2022-01-15 00:34:20 +0000 | [diff] [blame] | 29 | LatchUnsignaledConfig::Disabled, |
| 30 | }; |
| 31 | |
Alec Mouri | b2089a3 | 2022-01-15 00:34:20 +0000 | [diff] [blame] | 32 | static constexpr BnSurfaceComposer::ISurfaceComposerTag kSurfaceComposerTags[]{ |
| 33 | BnSurfaceComposer::BOOT_FINISHED, |
| 34 | BnSurfaceComposer::CREATE_CONNECTION, |
| 35 | BnSurfaceComposer::GET_STATIC_DISPLAY_INFO, |
| 36 | BnSurfaceComposer::CREATE_DISPLAY_EVENT_CONNECTION, |
| 37 | BnSurfaceComposer::CREATE_DISPLAY, |
| 38 | BnSurfaceComposer::DESTROY_DISPLAY, |
| 39 | BnSurfaceComposer::GET_PHYSICAL_DISPLAY_TOKEN, |
| 40 | BnSurfaceComposer::SET_TRANSACTION_STATE, |
| 41 | BnSurfaceComposer::AUTHENTICATE_SURFACE, |
| 42 | BnSurfaceComposer::GET_SUPPORTED_FRAME_TIMESTAMPS, |
| 43 | BnSurfaceComposer::GET_DISPLAY_MODES, |
| 44 | BnSurfaceComposer::GET_ACTIVE_DISPLAY_MODE, |
| 45 | BnSurfaceComposer::GET_DISPLAY_STATE, |
| 46 | BnSurfaceComposer::CAPTURE_DISPLAY, |
| 47 | BnSurfaceComposer::CAPTURE_LAYERS, |
| 48 | BnSurfaceComposer::CLEAR_ANIMATION_FRAME_STATS, |
| 49 | BnSurfaceComposer::GET_ANIMATION_FRAME_STATS, |
| 50 | BnSurfaceComposer::SET_POWER_MODE, |
| 51 | BnSurfaceComposer::GET_DISPLAY_STATS, |
| 52 | BnSurfaceComposer::GET_HDR_CAPABILITIES, |
| 53 | BnSurfaceComposer::GET_DISPLAY_COLOR_MODES, |
| 54 | BnSurfaceComposer::GET_ACTIVE_COLOR_MODE, |
| 55 | BnSurfaceComposer::SET_ACTIVE_COLOR_MODE, |
| 56 | BnSurfaceComposer::ENABLE_VSYNC_INJECTIONS, |
| 57 | BnSurfaceComposer::INJECT_VSYNC, |
| 58 | BnSurfaceComposer::GET_LAYER_DEBUG_INFO, |
| 59 | BnSurfaceComposer::GET_COMPOSITION_PREFERENCE, |
| 60 | BnSurfaceComposer::GET_COLOR_MANAGEMENT, |
| 61 | BnSurfaceComposer::GET_DISPLAYED_CONTENT_SAMPLING_ATTRIBUTES, |
| 62 | BnSurfaceComposer::SET_DISPLAY_CONTENT_SAMPLING_ENABLED, |
| 63 | BnSurfaceComposer::GET_DISPLAYED_CONTENT_SAMPLE, |
| 64 | BnSurfaceComposer::GET_PROTECTED_CONTENT_SUPPORT, |
| 65 | BnSurfaceComposer::IS_WIDE_COLOR_DISPLAY, |
| 66 | BnSurfaceComposer::GET_DISPLAY_NATIVE_PRIMARIES, |
| 67 | BnSurfaceComposer::GET_PHYSICAL_DISPLAY_IDS, |
| 68 | BnSurfaceComposer::ADD_REGION_SAMPLING_LISTENER, |
| 69 | BnSurfaceComposer::REMOVE_REGION_SAMPLING_LISTENER, |
| 70 | BnSurfaceComposer::SET_DESIRED_DISPLAY_MODE_SPECS, |
| 71 | BnSurfaceComposer::GET_DESIRED_DISPLAY_MODE_SPECS, |
| 72 | BnSurfaceComposer::GET_DISPLAY_BRIGHTNESS_SUPPORT, |
| 73 | BnSurfaceComposer::SET_DISPLAY_BRIGHTNESS, |
| 74 | BnSurfaceComposer::CAPTURE_DISPLAY_BY_ID, |
| 75 | BnSurfaceComposer::NOTIFY_POWER_BOOST, |
| 76 | BnSurfaceComposer::SET_GLOBAL_SHADOW_SETTINGS, |
| 77 | BnSurfaceComposer::GET_AUTO_LOW_LATENCY_MODE_SUPPORT, |
| 78 | BnSurfaceComposer::SET_AUTO_LOW_LATENCY_MODE, |
| 79 | BnSurfaceComposer::GET_GAME_CONTENT_TYPE_SUPPORT, |
| 80 | BnSurfaceComposer::SET_GAME_CONTENT_TYPE, |
| 81 | BnSurfaceComposer::SET_FRAME_RATE, |
| 82 | BnSurfaceComposer::ACQUIRE_FRAME_RATE_FLEXIBILITY_TOKEN, |
| 83 | BnSurfaceComposer::SET_FRAME_TIMELINE_INFO, |
| 84 | BnSurfaceComposer::ADD_TRANSACTION_TRACE_LISTENER, |
| 85 | BnSurfaceComposer::GET_GPU_CONTEXT_PRIORITY, |
| 86 | BnSurfaceComposer::GET_MAX_ACQUIRED_BUFFER_COUNT, |
| 87 | BnSurfaceComposer::GET_DYNAMIC_DISPLAY_INFO, |
| 88 | BnSurfaceComposer::ADD_FPS_LISTENER, |
| 89 | BnSurfaceComposer::REMOVE_FPS_LISTENER, |
| 90 | BnSurfaceComposer::OVERRIDE_HDR_TYPES, |
| 91 | BnSurfaceComposer::ADD_HDR_LAYER_INFO_LISTENER, |
| 92 | BnSurfaceComposer::REMOVE_HDR_LAYER_INFO_LISTENER, |
| 93 | BnSurfaceComposer::ON_PULL_ATOM, |
| 94 | BnSurfaceComposer::ADD_TUNNEL_MODE_ENABLED_LISTENER, |
| 95 | BnSurfaceComposer::REMOVE_TUNNEL_MODE_ENABLED_LISTENER, |
| 96 | BnSurfaceComposer::ADD_WINDOW_INFOS_LISTENER, |
| 97 | BnSurfaceComposer::REMOVE_WINDOW_INFOS_LISTENER, |
| 98 | }; |
| 99 | |
| 100 | static constexpr uint32_t kMinCode = 1000; |
| 101 | static constexpr uint32_t kMaxCode = 1050; |
| 102 | |
| 103 | class SurfaceFlingerFuzzer { |
| 104 | public: |
| 105 | SurfaceFlingerFuzzer(const uint8_t *data, size_t size) : mFdp(data, size) { |
| 106 | mFlinger = mTestableFlinger.flinger(); |
| 107 | }; |
| 108 | void process(const uint8_t *data, size_t size); |
| 109 | |
| 110 | private: |
| 111 | void setUp(); |
| 112 | void invokeFlinger(); |
| 113 | void setTransactionState(); |
| 114 | void setInternalDisplayPrimaries(); |
| 115 | void setDisplayStateLocked(); |
| 116 | void onTransact(const uint8_t *data, size_t size); |
| 117 | |
| 118 | FuzzedDataProvider mFdp; |
| 119 | TestableSurfaceFlinger mTestableFlinger; |
| 120 | sp<SurfaceFlinger> mFlinger = nullptr; |
| 121 | }; |
| 122 | |
| 123 | void SurfaceFlingerFuzzer::invokeFlinger() { |
| 124 | mFlinger->setSchedFifo(mFdp.ConsumeBool()); |
| 125 | mFlinger->setSchedAttr(mFdp.ConsumeBool()); |
| 126 | mFlinger->getServiceName(); |
| 127 | mFlinger->hasSyncFramework = mFdp.ConsumeBool(); |
| 128 | mFlinger->dispSyncPresentTimeOffset = mFdp.ConsumeIntegral<int64_t>(); |
| 129 | mFlinger->useHwcForRgbToYuv = mFdp.ConsumeBool(); |
| 130 | mFlinger->maxFrameBufferAcquiredBuffers = mFdp.ConsumeIntegral<int64_t>(); |
| 131 | mFlinger->maxGraphicsWidth = mFdp.ConsumeIntegral<uint32_t>(); |
| 132 | mFlinger->maxGraphicsHeight = mFdp.ConsumeIntegral<uint32_t>(); |
| 133 | mFlinger->hasWideColorDisplay = mFdp.ConsumeBool(); |
Alec Mouri | b2089a3 | 2022-01-15 00:34:20 +0000 | [diff] [blame] | 134 | mFlinger->useContextPriority = mFdp.ConsumeBool(); |
| 135 | |
| 136 | mFlinger->defaultCompositionDataspace = mFdp.PickValueInArray(kDataspaces); |
| 137 | mFlinger->defaultCompositionPixelFormat = mFdp.PickValueInArray(kPixelFormats); |
| 138 | mFlinger->wideColorGamutCompositionDataspace = mFdp.PickValueInArray(kDataspaces); |
| 139 | mFlinger->wideColorGamutCompositionPixelFormat = mFdp.PickValueInArray(kPixelFormats); |
| 140 | |
| 141 | mFlinger->enableLatchUnsignaledConfig = mFdp.PickValueInArray(kLatchUnsignaledConfig); |
| 142 | |
Dominik Laskowski | dd5827a | 2022-03-17 12:44:23 -0700 | [diff] [blame^] | 143 | using FrameHint = SurfaceFlinger::FrameHint; |
| 144 | mFlinger->scheduleComposite(mFdp.ConsumeBool() ? FrameHint::kActive : FrameHint::kNone); |
Alec Mouri | b2089a3 | 2022-01-15 00:34:20 +0000 | [diff] [blame] | 145 | mFlinger->scheduleRepaint(); |
| 146 | mFlinger->scheduleSample(); |
| 147 | |
| 148 | uint32_t texture = mFlinger->getNewTexture(); |
| 149 | mFlinger->deleteTextureAsync(texture); |
| 150 | |
| 151 | sp<IBinder> handle = defaultServiceManager()->checkService( |
| 152 | String16(mFdp.ConsumeRandomLengthString().c_str())); |
| 153 | mFlinger->fromHandle(handle); |
| 154 | mFlinger->windowInfosReported(); |
| 155 | mFlinger->disableExpensiveRendering(); |
| 156 | } |
| 157 | |
| 158 | void SurfaceFlingerFuzzer::setInternalDisplayPrimaries() { |
| 159 | ui::DisplayPrimaries primaries; |
| 160 | primaries.red.X = mFdp.ConsumeFloatingPoint<float>(); |
| 161 | primaries.red.Y = mFdp.ConsumeFloatingPoint<float>(); |
| 162 | primaries.red.Z = mFdp.ConsumeFloatingPoint<float>(); |
| 163 | primaries.green.X = mFdp.ConsumeFloatingPoint<float>(); |
| 164 | primaries.green.Y = mFdp.ConsumeFloatingPoint<float>(); |
| 165 | primaries.green.Z = mFdp.ConsumeFloatingPoint<float>(); |
| 166 | primaries.blue.X = mFdp.ConsumeFloatingPoint<float>(); |
| 167 | primaries.blue.Y = mFdp.ConsumeFloatingPoint<float>(); |
| 168 | primaries.blue.Z = mFdp.ConsumeFloatingPoint<float>(); |
| 169 | primaries.white.X = mFdp.ConsumeFloatingPoint<float>(); |
| 170 | primaries.white.Y = mFdp.ConsumeFloatingPoint<float>(); |
| 171 | primaries.white.Z = mFdp.ConsumeFloatingPoint<float>(); |
| 172 | mTestableFlinger.setInternalDisplayPrimaries(primaries); |
| 173 | } |
| 174 | |
| 175 | void SurfaceFlingerFuzzer::setTransactionState() { |
| 176 | Vector<ComposerState> states; |
| 177 | Vector<DisplayState> displays; |
| 178 | ComposerState composerState; |
| 179 | composerState.state.what = layer_state_t::eLayerChanged; |
| 180 | composerState.state.surface = nullptr; |
| 181 | states.add(composerState); |
| 182 | uint32_t flags = mFdp.ConsumeIntegral<uint32_t>(); |
| 183 | const sp<IBinder> applyToken = nullptr; |
| 184 | int64_t desiredPresentTime = mFdp.ConsumeIntegral<int64_t>(); |
| 185 | bool isAutoTimestamp = mFdp.ConsumeBool(); |
| 186 | bool hasListenerCallbacks = mFdp.ConsumeBool(); |
| 187 | std::vector<ListenerCallbacks> listenerCallbacks{}; |
| 188 | uint64_t transactionId = mFdp.ConsumeIntegral<uint64_t>(); |
| 189 | |
| 190 | mTestableFlinger.setTransactionState(FrameTimelineInfo{}, states, displays, flags, applyToken, |
| 191 | InputWindowCommands{}, desiredPresentTime, isAutoTimestamp, |
| 192 | {}, hasListenerCallbacks, listenerCallbacks, |
| 193 | transactionId); |
| 194 | } |
| 195 | |
| 196 | void SurfaceFlingerFuzzer::setDisplayStateLocked() { |
| 197 | DisplayState state{}; |
| 198 | mTestableFlinger.setDisplayStateLocked(state); |
| 199 | } |
| 200 | |
| 201 | void SurfaceFlingerFuzzer::onTransact(const uint8_t *data, size_t size) { |
| 202 | Parcel fuzzedData, reply; |
| 203 | fuzzedData.writeInterfaceToken(String16("android.ui.ISurfaceComposer")); |
| 204 | fuzzedData.setData(data, size); |
| 205 | fuzzedData.setDataPosition(0); |
| 206 | uint32_t code = mFdp.ConsumeBool() ? mFdp.PickValueInArray(kSurfaceComposerTags) |
| 207 | : mFdp.ConsumeIntegralInRange<uint32_t>(kMinCode, kMaxCode); |
| 208 | mTestableFlinger.onTransact(code, fuzzedData, &reply, 0); |
| 209 | } |
| 210 | |
| 211 | void SurfaceFlingerFuzzer::setUp() { |
| 212 | mTestableFlinger.setupScheduler(std::make_unique<android::mock::VsyncController>(), |
| 213 | std::make_unique<android::mock::VSyncTracker>(), |
| 214 | std::make_unique<android::mock::EventThread>(), |
| 215 | std::make_unique<android::mock::EventThread>()); |
| 216 | |
| 217 | mTestableFlinger.setupTimeStats(std::make_unique<android::mock::TimeStats>()); |
| 218 | |
| 219 | std::unique_ptr<android::renderengine::RenderEngine> renderEngine = |
| 220 | std::make_unique<android::renderengine::mock::RenderEngine>(); |
| 221 | mTestableFlinger.setupRenderEngine(std::move(renderEngine)); |
| 222 | mTestableFlinger.setupComposer(std::make_unique<android::Hwc2::mock::Composer>()); |
| 223 | } |
| 224 | |
| 225 | void SurfaceFlingerFuzzer::process(const uint8_t *data, size_t size) { |
| 226 | setUp(); |
| 227 | |
| 228 | invokeFlinger(); |
| 229 | |
| 230 | mTestableFlinger.fuzzSurfaceFlinger(data, size); |
| 231 | |
| 232 | mTestableFlinger.setCreateBufferQueueFunction( |
| 233 | surfaceflinger::test::Factory::CreateBufferQueueFunction()); |
| 234 | mTestableFlinger.setCreateNativeWindowSurface( |
| 235 | surfaceflinger::test::Factory::CreateNativeWindowSurfaceFunction()); |
| 236 | |
| 237 | setInternalDisplayPrimaries(); |
| 238 | |
| 239 | mTestableFlinger.enableHalVirtualDisplays(mFdp.ConsumeBool()); |
| 240 | |
| 241 | mTestableFlinger.commitTransactionsLocked(mFdp.ConsumeIntegral<uint32_t>()); |
| 242 | |
| 243 | mTestableFlinger.notifyPowerBoost(mFdp.ConsumeIntegral<int32_t>()); |
| 244 | |
| 245 | setDisplayStateLocked(); |
| 246 | |
| 247 | setTransactionState(); |
| 248 | mTestableFlinger.flushTransactionQueues(); |
| 249 | |
| 250 | onTransact(data, size); |
| 251 | } |
| 252 | |
| 253 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 254 | android::fuzz::SurfaceFlingerFuzzer surfaceFlingerFuzzer(data, size); |
| 255 | surfaceFlingerFuzzer.process(data, size); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | } // namespace android::fuzz |