Skip to content

[CTX-0202] fix(render): bound headless surface allocation (CR-RENDER-01) - #346

Merged
Xuepoo merged 2 commits into
mainfrom
ctx-0202/fix-headless-surface-cap
Sep 6, 2026
Merged

Xuepoo merged 2 commits into
mainfrom
ctx-0202/fix-headless-surface-cap

Conversation

@Xuepoo

@Xuepoo Xuepoo commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Priority: P1 | Area: render | Labels: fix,area:render,P1 | Milestone: v0.1.0 | RFC: FIND-0002 | Task: CTX-0202

Closes #345

Summary

Headless present paths in crates/bitty-render/src/gpu.rs allocated width*height*4 with unchecked as usize arithmetic, so extreme extents could OOM the host (FIND-0002 CR-RENDER-01, memory/DoS).

  • Adds MAX_HEADLESS_SURFACE_BYTES (64 MiB), mirroring software::MAX_SURFACE_BYTES (lives in gpu.rs because software is sw-fallback-gated; a cfg-gated parity test pins them together).
  • Adds u64 checked helper headless_buffer_len(); both present_draw_list (headless branch) and headless_present fail closed with RenderError::InvalidInput before allocating.
  • Matches the already-documented error contract (both methods documented buffer-cap overflow as InvalidInput; implementation was missing).

Tests

  • headless_buffer_len_enforces_byte_cap_without_allocating: exact-cap boundary (4096x4096 ok), one-row-over fails, u32::MAX extremes fail.
  • headless_present_rejects_extreme_extents_without_allocating: 5000x5000 (100 MiB) fails closed, stores no partial buffer, normal 32x16 frames still present afterwards.
  • headless_cap_matches_software_cap (sw-fallback only): mirror cannot drift.

Verification

  • cargo fmt -p bitty-render -- --check: clean
  • cargo clippy -p bitty-render --all-targets -- -D warnings: clean
  • cargo test -p bitty-render --lib: 131/131 pass
  • cargo test -p bitty-render --features sw-fallback --lib: 141/141 pass
  • cargo test -p bitty-render --all-targets: pass
  • cargo test --workspace --locked: 0 failures
  • cargo check --target x86_64-pc-windows-gnu -p bitty-render --all-targets: clean

CarryCtx: CTX-0202 | Session: 01M1VDCXZEK98MFA6RNB7YTACG | Agent: core-impl-0202

…(CR-RENDER-01)

Headless present paths allocated width*height*4 unchecked, allowing
extreme extents to OOM the host. Add MAX_HEADLESS_SURFACE_BYTES (64 MiB,
mirroring software::MAX_SURFACE_BYTES) plus a u64 checked helper and
enforce it in both present_draw_list and headless_present before any
allocation. Adds fail-closed regression tests and a sw-fallback cap
parity test.

Task: CTX-0202 | FIND-0002 CR-RENDER-01
@Xuepoo Xuepoo added this to the v0.1.0 milestone Sep 6, 2026
@Xuepoo Xuepoo added fix Bug fix P1 Priority: high area:render Area: rendering labels Sep 6, 2026
… (CR-RENDER-01)

Plain u64 products overflow for extreme u32 extents ((u32::MAX^2)*4
exceeds u64::MAX), panicking under default overflow checks instead of
returning InvalidInput. Use a checked_mul chain mapping any overflow
to InvalidInput, same error contract.
@Xuepoo
Xuepoo merged commit 4af50e7 into main Sep 6, 2026
10 checks passed
@Xuepoo
Xuepoo deleted the ctx-0202/fix-headless-surface-cap branch September 6, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:render Area: rendering fix Bug fix P1 Priority: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(render): unbounded headless surface allocation in gpu.rs (CR-RENDER-01)

1 participant