startup: check the draw-dump BMP writer's limits and header fields #1710

Merged
Asimmimx merged 1 commit from upstream-pr/1717 into main 2026-11-24 18:08:04 +00:00
Asimmimx commented 2026-11-24 17:40:13 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Asimmimx on 2026-11-24T17:40:13Z.


Refs #1149.

What the header does

src/startup_stubs/draw_bmp.h writes the per-draw dumps of the D3D12 stand-in (X2W_DRAW_DUMP_FRAME, src/startup_stubs/d3d12_draw.inl). supported() takes the four 8-bit RGBA/BGRA formats and write() turns one copy footprint (rows padded to the footprint's RowPitch) into a 32-bit bottom-up BMP: it refuses a null file or texel pointer, an unsupported format, a zero width or height, a pitch shorter than width * 4 (computed in 64 bits) and an image whose file size does not fit the 32-bit bfSize together with the 54 header bytes. It swaps red and blue for RGBA input and writes alpha 255.

Tests that reach it

startup_draw_bmp (tests/startup_draw_bmp.cpp) is the only check that includes the header; it is device-free. d3d12_xs_device.cpp includes it for the dumps, but no test runs a dump.

Mutation probe

Each mutant was applied alone to draw_bmp.h, startup_draw_bmp_check was rebuilt and ctest -R '^startup_draw_bmp$' was run, then the header was restored.

Mutant Existing check With new cases
row size width * 4 computed in 32 bits survived killed (FAIL row size above 4 GiB)
stride >= row_pitch (pitch equal to the row size refused) survived killed (FAIL pitch equal to the row size is accepted)
pitch check dropped killed killed
4 GiB file-size check dropped survived killed (faults reading past the texels)
4 GiB check without the 54 header bytes survived killed (faults reading past the texels)
null file check dropped survived killed (CRT invalid-parameter break, 0x80000003)
null texel check dropped survived killed (faults on the null read)
zero width check dropped survived killed (FAIL zero width)
zero height check dropped survived killed (FAIL zero height)
supported() check dropped killed killed
bfSize without the header bytes killed killed
bfOffBits = 14 killed killed
biSize = 44 survived killed
biPlanes = 0 survived killed
biBitCount = 24 killed killed
biSizeImage = 0 survived killed
biSizeImage = bfSize survived killed
negative biHeight (top-down) killed killed
rows read top-down killed killed
R8G8B8A8_UNORM_SRGB not swapped killed killed
alpha copied from the source killed killed
green read from the red byte killed killed
B8G8R8A8_UNORM_SRGB not supported killed killed
pitch taken as the row size killed killed
row fwrite result ignored survived survived
header fwrite results ignored survived survived

Existing check: 10 of 26 killed. With the new cases: 24 of 26 killed.

New cases

  • Every BITMAPINFOHEADER field the writer sets is read: biSize 40, biPlanes 1, biCompression BI_RGB and biSizeImage equal to the image bytes, as well as the fields the check already read.
  • A 2x2 R8G8B8A8 footprint whose pitch is exactly 8 bytes, with a distinct byte in every channel, so both rows, both columns and the red/blue swap are visible. A pitch equal to the row size is the normal case for a render target whose width is a multiple of 64 texels (for example 1920).
  • Calls the writer must refuse before it writes a byte, each with its own empty temporary file: no file, no texels, zero width, zero height, a pitch one byte short, 0x40000001 texels per row (0x1'00000004 bytes, which is one texel when cut to 32 bits), 0x3ffffff3 one-texel rows (0xffffffcc image bytes, past 4 GiB only with the header bytes) and 0x10000 x 0x10000 (16 GiB). Every call passes a 4-byte texel buffer.

Four mutants are killed by a fault rather than a FAIL line: a writer that accepts those calls goes on to read past the 4-byte buffer or through a null pointer. The unmutated header refuses them before any read.

Negative controls

Registered with WILL_FAIL TRUE, next to startup_draw_bmp:

  • startup_draw_bmp_tight_control (--mutate tight): expects a pitch equal to the row size to be refused. Prints FAIL pitch equal to the row size is accepted.
  • startup_draw_bmp_wrap_control (--mutate wrap): expects the 0x40000001-texel row to be accepted. Prints FAIL row size above 4 GiB is accepted.
  • startup_draw_bmp_size-image_control (--mutate size-image): expects biSizeImage 0. Fails the four format cases and the tight case.

Not pinned

  • The results of the header and row fwrite calls. Telling them apart from a writer that ignores them needs a FILE that accepts the 54 header bytes and then fails a row write. A read-only or closed stream fails the header write as well, after which both versions return false.

Commands and results

cmake --preset dev
cmake --build --preset dev --target startup_draw_bmp_check
ctest --preset dev --no-tests=error -R '^startup_draw_bmp' --output-on-failure
1/4 Test #78: startup_draw_bmp ......................   Passed    0.02 sec
2/4 Test #79: startup_draw_bmp_tight_control ........   Passed    0.01 sec
3/4 Test #80: startup_draw_bmp_wrap_control .........   Passed    0.01 sec
4/4 Test #81: startup_draw_bmp_size-image_control ...   Passed    0.02 sec
100% tests passed, 0 tests failed out of 4

No capability skips. The check uses no device.

Environment

Windows 11 Pro 26300.9974, MSVC from Visual Studio, dev preset (Debug). Commit 5338a26c0c84c38b7d4d5a681dd081dc10fc5510 on main at 18a2942a.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6587820096 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1717). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-24T17:40:13Z. --- Refs #1149. ## What the header does `src/startup_stubs/draw_bmp.h` writes the per-draw dumps of the D3D12 stand-in (`X2W_DRAW_DUMP_FRAME`, `src/startup_stubs/d3d12_draw.inl`). `supported()` takes the four 8-bit RGBA/BGRA formats and `write()` turns one copy footprint (rows padded to the footprint's `RowPitch`) into a 32-bit bottom-up BMP: it refuses a null file or texel pointer, an unsupported format, a zero width or height, a pitch shorter than `width * 4` (computed in 64 bits) and an image whose file size does not fit the 32-bit `bfSize` together with the 54 header bytes. It swaps red and blue for RGBA input and writes alpha 255. ## Tests that reach it `startup_draw_bmp` (`tests/startup_draw_bmp.cpp`) is the only check that includes the header; it is device-free. `d3d12_xs_device.cpp` includes it for the dumps, but no test runs a dump. ## Mutation probe Each mutant was applied alone to `draw_bmp.h`, `startup_draw_bmp_check` was rebuilt and `ctest -R '^startup_draw_bmp$'` was run, then the header was restored. | Mutant | Existing check | With new cases | |---|---|---| | row size `width * 4` computed in 32 bits | survived | killed (`FAIL row size above 4 GiB`) | | `stride >= row_pitch` (pitch equal to the row size refused) | survived | killed (`FAIL pitch equal to the row size is accepted`) | | pitch check dropped | killed | killed | | 4 GiB file-size check dropped | survived | killed (faults reading past the texels) | | 4 GiB check without the 54 header bytes | survived | killed (faults reading past the texels) | | null file check dropped | survived | killed (CRT invalid-parameter break, 0x80000003) | | null texel check dropped | survived | killed (faults on the null read) | | zero width check dropped | survived | killed (`FAIL zero width`) | | zero height check dropped | survived | killed (`FAIL zero height`) | | `supported()` check dropped | killed | killed | | `bfSize` without the header bytes | killed | killed | | `bfOffBits` = 14 | killed | killed | | `biSize` = 44 | survived | killed | | `biPlanes` = 0 | survived | killed | | `biBitCount` = 24 | killed | killed | | `biSizeImage` = 0 | survived | killed | | `biSizeImage` = `bfSize` | survived | killed | | negative `biHeight` (top-down) | killed | killed | | rows read top-down | killed | killed | | R8G8B8A8_UNORM_SRGB not swapped | killed | killed | | alpha copied from the source | killed | killed | | green read from the red byte | killed | killed | | B8G8R8A8_UNORM_SRGB not supported | killed | killed | | pitch taken as the row size | killed | killed | | row `fwrite` result ignored | survived | survived | | header `fwrite` results ignored | survived | survived | Existing check: 10 of 26 killed. With the new cases: 24 of 26 killed. ## New cases - Every `BITMAPINFOHEADER` field the writer sets is read: `biSize` 40, `biPlanes` 1, `biCompression` `BI_RGB` and `biSizeImage` equal to the image bytes, as well as the fields the check already read. - A 2x2 R8G8B8A8 footprint whose pitch is exactly 8 bytes, with a distinct byte in every channel, so both rows, both columns and the red/blue swap are visible. A pitch equal to the row size is the normal case for a render target whose width is a multiple of 64 texels (for example 1920). - Calls the writer must refuse before it writes a byte, each with its own empty temporary file: no file, no texels, zero width, zero height, a pitch one byte short, 0x40000001 texels per row (0x1'00000004 bytes, which is one texel when cut to 32 bits), 0x3ffffff3 one-texel rows (0xffffffcc image bytes, past 4 GiB only with the header bytes) and 0x10000 x 0x10000 (16 GiB). Every call passes a 4-byte texel buffer. Four mutants are killed by a fault rather than a FAIL line: a writer that accepts those calls goes on to read past the 4-byte buffer or through a null pointer. The unmutated header refuses them before any read. ## Negative controls Registered with `WILL_FAIL TRUE`, next to `startup_draw_bmp`: - `startup_draw_bmp_tight_control` (`--mutate tight`): expects a pitch equal to the row size to be refused. Prints `FAIL pitch equal to the row size is accepted`. - `startup_draw_bmp_wrap_control` (`--mutate wrap`): expects the 0x40000001-texel row to be accepted. Prints `FAIL row size above 4 GiB is accepted`. - `startup_draw_bmp_size-image_control` (`--mutate size-image`): expects `biSizeImage` 0. Fails the four format cases and the tight case. ## Not pinned - The results of the header and row `fwrite` calls. Telling them apart from a writer that ignores them needs a `FILE` that accepts the 54 header bytes and then fails a row write. A read-only or closed stream fails the header write as well, after which both versions return false. ## Commands and results ```powershell cmake --preset dev cmake --build --preset dev --target startup_draw_bmp_check ctest --preset dev --no-tests=error -R '^startup_draw_bmp' --output-on-failure ``` ``` 1/4 Test #78: startup_draw_bmp ...................... Passed 0.02 sec 2/4 Test #79: startup_draw_bmp_tight_control ........ Passed 0.01 sec 3/4 Test #80: startup_draw_bmp_wrap_control ......... Passed 0.01 sec 4/4 Test #81: startup_draw_bmp_size-image_control ... Passed 0.02 sec 100% tests passed, 0 tests failed out of 4 ``` No capability skips. The check uses no device. ## Environment Windows 11 Pro 26300.9974, MSVC from Visual Studio, `dev` preset (Debug). Commit `5338a26c0c84c38b7d4d5a681dd081dc10fc5510` on `main` at `18a2942a`. AI-assisted (Claude Code).
tedin7 commented 2026-11-24 18:02:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T17:46:56Z.

Review: COMMENTED


Review of head 5338a26c0c84c38b7d4d5a681dd081dc10fc5510. Changes: tests/startup_draw_bmp.cpp and test-only CMake entries (a startup_draw_bmp_<mutation>_control loop with WILL_FAIL); no production code, src/startup_stubs/draw_bmp.h untouched. Verdict: 0 P1, 0 P2.

Linux limits. Linux x86_64. Built with x86_64-w64-mingw32-g++ -std=c++17 -Wall -Wextra -static and run under wine, not on Windows, not through CTest or CMake.

What I ran

  • Build: 0 warnings. Run: startup_draw_bmp: ok, exit 0.
  • --mutate tight, --mutate wrap, --mutate size-image each exit 1 with the expected FAIL lines, so the three WILL_FAIL controls are meaningful. Unknown mutation names return 2.
  • git diff --check, docs_links_check, bidi_controls_check clean; no non-ASCII bytes.
  • Assertions: the old header check is replaced by headers(), which keeps every old condition (type, offset, size, width, height, bit count) and adds biSize, biPlanes, biCompression, biSizeImage. The old && chain, which stopped at the first failing format, now runs all four formats. Nothing is weakened.
  • Mutations on the real draw_bmp.h (not just the test flags), restored with git checkout -- .:
    • stride > row_pitch -> >=: FAILED (pitch equal to the row size is accepted).
    • std::uint64_t stride -> UINT stride: caught, the run faults (exit 5 under wine); the test comment says some mutants fault instead of printing.
    • biSizeImage line removed: FAILED (all four formats and tight headers).
    • MAXDWORD - header_bytes -> MAXDWORD: caught by a fault, exit 5.
    • red/blue swap removed for RGBA: FAILED.

Notes

  • The --mutate controls flip the test's own expectation, not the writer, so they show that the assertions are sensitive and rely on the WILL_FAIL exit code; the real regression protection is the first list above. A crash also satisfies WILL_FAIL, which is acceptable here but means a fault in a control would not be told apart from a failed assertion.
  • Not run: the Windows build and the CTest entries.

Not approving (comment only).

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6044448700 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1717#pullrequestreview-6044448700). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T17:46:56Z. **Review:** COMMENTED --- Review of head `5338a26c0c84c38b7d4d5a681dd081dc10fc5510`. Changes: `tests/startup_draw_bmp.cpp` and test-only CMake entries (a `startup_draw_bmp_<mutation>_control` loop with `WILL_FAIL`); no production code, `src/startup_stubs/draw_bmp.h` untouched. **Verdict: 0 P1, 0 P2.** **Linux limits.** Linux x86_64. Built with `x86_64-w64-mingw32-g++ -std=c++17 -Wall -Wextra -static` and run under `wine`, not on Windows, not through CTest or CMake. **What I ran** - Build: 0 warnings. Run: `startup_draw_bmp: ok`, exit 0. - `--mutate tight`, `--mutate wrap`, `--mutate size-image` each exit 1 with the expected `FAIL` lines, so the three `WILL_FAIL` controls are meaningful. Unknown mutation names return 2. - `git diff --check`, `docs_links_check`, `bidi_controls_check` clean; no non-ASCII bytes. - Assertions: the old header check is replaced by `headers()`, which keeps every old condition (type, offset, size, width, height, bit count) and adds `biSize`, `biPlanes`, `biCompression`, `biSizeImage`. The old `&&` chain, which stopped at the first failing format, now runs all four formats. Nothing is weakened. - Mutations on the real `draw_bmp.h` (not just the test flags), restored with `git checkout -- .`: - `stride > row_pitch` -> `>=`: FAILED (`pitch equal to the row size is accepted`). - `std::uint64_t stride` -> `UINT stride`: caught, the run faults (exit 5 under wine); the test comment says some mutants fault instead of printing. - `biSizeImage` line removed: FAILED (all four formats and `tight headers`). - `MAXDWORD - header_bytes` -> `MAXDWORD`: caught by a fault, exit 5. - red/blue swap removed for RGBA: FAILED. **Notes** - The `--mutate` controls flip the test's own expectation, not the writer, so they show that the assertions are sensitive and rely on the `WILL_FAIL` exit code; the real regression protection is the first list above. A crash also satisfies `WILL_FAIL`, which is acceptable here but means a fault in a control would not be told apart from a failed assertion. - Not run: the Windows build and the CTest entries. Not approving (comment only). AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-24 18:02:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T17:55:44Z.

Review: COMMENTED


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) and Linux at commit 5338a26c0c84c38b7d4d5a681dd081dc10fc5510 (0 P1, 0 P2; test-only CMake + test, zero production files changed, zero assertions weakened):

  • Linux checks:
    • git diff --check origin/main...HEAD: PASS
    • python3 tests/docs_links_check.py: PASS (17 Markdown files, 364 links)
    • python3 tests/bidi_controls_check.py: PASS (1283 tracked files)
  • Win2025 Debug (--preset dev):
    • cmake --build --preset dev --target cpu_trap cpu_trap_drop cpu_trap_harness cpu_trap_lifted cpu_trap_nostore cpu_trap_swap startup_draw_bmp_check -- -j 6: PASS (3.59s)
    • ctest --preset dev -R '^(cpu_aot_trap|startup_draw_bmp|startup_draw_bmp_size-image_control|startup_draw_bmp_tight_control|startup_draw_bmp_wrap_control)$' --parallel 4 --no-tests=error --output-on-failure: 5/5 passed (2.01s)
  • Win2025 Release /O2 (--preset release):
    • cmake --build --preset release --target cpu_trap cpu_trap_drop cpu_trap_harness cpu_trap_lifted cpu_trap_nostore cpu_trap_swap startup_draw_bmp_check -- -j 6: PASS (1.72s)
    • ctest --preset release -R '^(cpu_aot_trap|startup_draw_bmp|startup_draw_bmp_size-image_control|startup_draw_bmp_tight_control|startup_draw_bmp_wrap_control)$' --parallel 4 --no-tests=error --output-on-failure: 5/5 passed (1.96s)
  • Mutation checks:
    • Mutated src/startup_stubs/draw_bmp.h on Win2025 (stride > row_pitch -> >= and info.biSizeImage = 0): startup_draw_bmp failed as expected (FAIL R8G8B8A8_UNORM, FAIL B8G8R8A8_UNORM, FAIL R8G8B8A8_UNORM_SRGB, FAIL B8G8R8A8_UNORM_SRGB, FAIL pitch equal to the row size is accepted).

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6044573865 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1717#pullrequestreview-6044573865). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T17:55:44Z. **Review:** COMMENTED --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) and Linux at commit `5338a26c0c84c38b7d4d5a681dd081dc10fc5510` (0 P1, 0 P2; test-only CMake + test, zero production files changed, zero assertions weakened): - **Linux checks**: - `git diff --check origin/main...HEAD`: PASS - `python3 tests/docs_links_check.py`: PASS (17 Markdown files, 364 links) - `python3 tests/bidi_controls_check.py`: PASS (1283 tracked files) - **Win2025 Debug (`--preset dev`)**: - `cmake --build --preset dev --target cpu_trap cpu_trap_drop cpu_trap_harness cpu_trap_lifted cpu_trap_nostore cpu_trap_swap startup_draw_bmp_check -- -j 6`: PASS (`3.59s`) - `ctest --preset dev -R '^(cpu_aot_trap|startup_draw_bmp|startup_draw_bmp_size-image_control|startup_draw_bmp_tight_control|startup_draw_bmp_wrap_control)$' --parallel 4 --no-tests=error --output-on-failure`: **5/5 passed** (`2.01s`) - **Win2025 Release `/O2` (`--preset release`)**: - `cmake --build --preset release --target cpu_trap cpu_trap_drop cpu_trap_harness cpu_trap_lifted cpu_trap_nostore cpu_trap_swap startup_draw_bmp_check -- -j 6`: PASS (`1.72s`) - `ctest --preset release -R '^(cpu_aot_trap|startup_draw_bmp|startup_draw_bmp_size-image_control|startup_draw_bmp_tight_control|startup_draw_bmp_wrap_control)$' --parallel 4 --no-tests=error --output-on-failure`: **5/5 passed** (`1.96s`) - **Mutation checks**: - Mutated `src/startup_stubs/draw_bmp.h` on Win2025 (`stride > row_pitch` -> `>=` and `info.biSizeImage = 0`): `startup_draw_bmp` failed as expected (`FAIL R8G8B8A8_UNORM`, `FAIL B8G8R8A8_UNORM`, `FAIL R8G8B8A8_UNORM_SRGB`, `FAIL B8G8R8A8_UNORM_SRGB`, `FAIL pitch equal to the row size is accepted`). AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-24 19:01:04 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1710
No description provided.