Skip to content

D3D11 graphics: fail a PNG whose pixel stream does not decode - #1263

Draft
iamfatness wants to merge 1 commit into
mainfrom
codex/win-d3d11-corrupt-png
Draft

iamfatness wants to merge 1 commit into
mainfrom
codex/win-d3d11-corrupt-png

Conversation

@iamfatness

Copy link
Copy Markdown
Owner

Problem

On Windows, a graphics image asset can be a PNG with a valid header and a zeroed IDAT payload. The D3D11 raster prepared that asset as ready with an empty warning, so it aired a blank. The Mac raster fails it with Graphics image could not be decoded: <id>. Because of this, three tests were red on main:

  • OverlayRasterContract.FailedScaleKeepsTheAcceptedRaster
  • OverlayRasterContract.ImageDecodeFailureWording
  • GraphicsTemplateRegistry.ABrokenTemplateThumbnailDoesNotBlockTheOthers

Root cause (measured, build 26300)

The brief guessed that WIC decodes lazily, but that is not the cause. WIC never reports the failure anywhere. For the zeroed payload, the frame's CopyPixels, the format converter's CopyPixels, CreateBitmapFromWicBitmap on the software WIC render target, and EndDraw all return S_OK. The pixels come back as zeros. A standalone probe also tried a bad zlib header, a valid header with a zeroed body, a bad Adler-32, a bad stored LEN/NLEN, a truncated stream and a reserved block type. WIC returned S_OK and all-zero pixels for every one. A stream that is valid but short returns a partial picture. So no HRESULT check in decodeOverlayImage (CompositorOverlayRaster.cpp) can catch it. Two comments, in the Mac raster and in the contract fixture, claimed that WIC's CreateBitmapFromWicBitmap surfaces this failure. That is wrong.

Fix

  • openPackagedImage now runs pngPixelStreamDecodes once on the preparation worker and records the result in ImageInput::pixelsDecode. This is a strict zlib/deflate verifier over the IDAT chunks. The stream must inflate completely, match its Adler-32, and produce at least the byte count that IHDR implies. The count includes Adam7 and every colour type and bit depth. Work is capped at twice the expected bytes.
  • decodeOverlayImage now takes the ImageInput and refuses one whose pixels do not decode. Both existing call sites therefore report the existing failure wording (the template path: Graphics image could not be decoded: <id>).
  • Non-PNG bytes (JPEG) and file-URI images are unchanged.
  • The stale comments are corrected, and a gotcha entry is added.

The verifier matches WIC's behaviour exactly on all 10 probe variants: every case where WIC zero-fills or returns a partial picture is rejected. A bad filter byte and trailing extra data are accepted, as WIC accepts them. It also accepts all 280 real PNGs tried: 74 from this repo (macOS and Windows screenshots, goldens, app assets) and 206 generated ones. The generated set covers colour types 0/2/3/4/6, depths 1 to 16, interlaced and non-interlaced images, zlib levels 0/1/6/9, and IDAT split across chunks. Cost: about 26 ms for a 1440p screenshot, 76 ms for a 4096x4096 gradient, and 298 ms for 67 MB of incompressible 16 MP data, which is above the 32 MB asset limit anyway. This runs on the worker, not the render thread.

Tests (Release, native/build-dev, Windows build 26300)

Filter Before After
OverlayRasterContract.*:GraphicsRasterParity*:GraphicsCompositionPixel.*:GraphicsTemplateRegistry.* 50 passed, 4 failed 53 passed, 1 failed
Graphics*:D3D11*:OverlayRaster*:OverlayTile*:ParityPng.* 247 passed, 4 failed 250 passed, 1 failed

The one failure that remains is OverlayRasterContract.CrawlAtTheItemLimitPreparesOrFailsWithAtlasWording. Before and after, it fails with the same message: "Graphics preparation exceeded its admitted budget." It is the long-crawl admission defect, and its fix is in draft PR #1258, which this PR does not include. No test, tolerance or fixture logic changed. Only one fixture comment was edited.

Not verified

  • Mac CI for the comment-only .mm edit.
  • JPEG corruption on WIC (out of scope).
  • A live app run.

🤖 Generated with Claude Code

WIC's PNG decoder never reports a corrupt IDAT. For a bad zlib header, a
malformed deflate block, a truncated stream, or a wrong Adler-32,
CopyPixels and CreateBitmapFromWicBitmap return S_OK with zeroed pixels.
A short stream returns a partial picture. This was measured on build
26300. As a result, the D3D11 raster prepared such an asset as ready
with an empty warning and aired a blank. The Mac raster fails it.

openPackagedImage now inflates the IDAT data once on the preparation
worker. The stream must inflate completely, match its Adler-32, and
carry every byte that IHDR implies. If it does not, decodeOverlayImage
refuses the image, and both call sites report the existing wording,
"Graphics image could not be decoded: <id>". JPEG and file-URI images
are unchanged.

This fixes OverlayRasterContract.FailedScaleKeepsTheAcceptedRaster,
OverlayRasterContract.ImageDecodeFailureWording and
GraphicsTemplateRegistry.ABrokenTemplateThumbnailDoesNotBlockTheOthers
on D3D11. It also corrects two comments that claimed WIC surfaces the
failure, and adds a gotcha entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants