Skip to content

Cap every image read and fix the 1×1 Hi-Z mip (#400) - #401

Merged
lazyxeon merged 1 commit into
mainfrom
cursor/cap-image-reads-ce1f
Oct 21, 2026
Merged

lazyxeon merged 1 commit into
mainfrom
cursor/cap-image-reads-ce1f

Conversation

@lazyxeon

@lazyxeon lazyxeon commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

Description

read_limited_image_bytes in astraweave_asset::image_limits is the sync 64 MiB read. A regular file over the cap is rejected from its length before any byte is read. A fifo stops at the cap and is not probed one byte further.

open_limited_image, Texture::from_file_with_usage, the ktx2 smoke-test fallback, load_ktx2_to_rgba, a non-data: glTF image file URI, and async read_capped_image call that helper. Tests cover a file over 64 MiB on open_limited_image, from_file_with_usage, and the ktx2 smoke fallback.

A 1×1 HizPyramid has one mip. log2(1) is 0, and wgpu rejects mip_level_count 0. hiz_pyramid_creation builds that frame.

Rebased onto main 040f24ae (#395). The render-trace notice is v1.53 and names that parent. That parent does not contain this read helper or the 1×1 mip fix. Main's atmosphere notices stay. The remaining uncapped readers are #408, not this PR.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Documentation update

Security Considerations

  • Does this PR introduce new dependencies? If yes, have they been vetted for security issues?
  • Does this PR modify how user input is handled? If yes, has it been validated/sanitized?
  • Does this PR change authentication or authorization mechanisms?
  • Does this PR access sensitive data or modify how it's stored?
  • Does this PR introduce new network communication or modify existing protocols?

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
Open in Web Open in Cursor 

@cursor

cursor Bot commented Oct 21, 2026

Copy link
Copy Markdown

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration
  • Configuration used: Repository: lazyxeon/AstraWeave/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 291d62ad-15d3-49e5-860c-08a287b2e83b
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@lazyxeon
lazyxeon marked this pull request as ready for review October 21, 2026 13:10
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T13:51:54.252604Z b6969ed Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cursor
cursor Bot force-pushed the cursor/cap-image-reads-ce1f branch from b6969ed to 05c83d0 Compare October 21, 2026 13:15

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment

Changes requested. The new sync cap and the 1×1 Hi-Z mip are sound. #400's exit criterion is still open: uncapped image reads remain in both crates, including the editor's glTF import.

What holds

read_limited_image_bytes rejects a regular file from metadata().len() when that length is over MAX_IMAGE_ALLOC (64 MiB) and then takes only that length. A non-file stops at the cap with no extra probe byte. A stat/open swap stays inside the cap, because the take limit is the length that was already compared. An exact-cap fifo is rejected; fifo_at_the_cap_does_not_read_one_more_byte still locks that. read_capped_image now runs the same helper on spawn_blocking, so a stalled fifo occupies a blocking thread. TextureStreamingManager still drops via shutdown_timeout of 500 ms. The comment on texture_streaming.rs:16 still names std::fs::read.

HizPyramid::new at 1×1 used to request mip_level_count 0 (log2(1) is 0). .max(1.0) yields 1, which is the wgpu maximum for a 1×1 texture. ceil(log2(n)) never exceeds floor(log2(n)) + 1, so larger frames stay legal. The copy shader returns when coords is outside textureDimensions, so the 8×8 workgroup on a 1×1 image is in bounds. The downsample loop is empty (saturating_sub(1)). Existing tests still lock 1920→11 and 1024→10. The new test builds the 1×1 pyramid; it records the copy and does not submit. Creation is the failure mode wgpu rejects.

No new dependency, no Cargo.toml change, no new unsafe. Cargo deny and cargo-audit succeeded (run 39561221108). Determinism is intact: Hi-Z is presentation-only (trace invariant 21; sample_hiz defaults off; nothing reads the pyramid back into the sim), and image I/O is off the tick. determinism-replay compare succeeded on Ubuntu, Windows, and macOS (run 39561221026). The v1.49 stamp correctly names parent 8b4caa27 and says that commit does not contain this helper. The phase-plan Occlusion / Hi-Z row matches the gated pyramid.

NaniteCullingPipeline (nanite_gpu_culling.rs:197) still passes (edge as f32).log2().ceil() as u32 straight to mip_level_count. A 1×1 Nanite target is still 0. draw_into does not record that pyramid. Latent, same formula.

What blocks

  1. data: glTF images are still uncapped in the match arm added to cap file URIs (lib.rs). load_uri_bytes base64-decodes the whole payload, then decode_image_bytes applies the limits. External buffer URIs still use uncapped std::fs::read (lib.rs:811) before a buffer-view image is sliced.
  2. The editor does not use this helper. mesh_gltf::load_gltf (mesh_gltf.rs:50), called from engine_adapter.rs load_gltf_model, loads images with gltf::import (gltf 1.4.1). Scheme::read / read_to_end allocates metadata.len() + 1 and reads the whole file. load_from_memory_with_format uses image 0.25.8 Limits::default: no edge cap, max_alloc 512 MiB (eight times MAX_IMAGE_ALLOC). load_rgba8 is image::open (mesh_gltf.rs:521) for Poly Haven sidecars. jail_gltf_uri returns Ok(()) for data: and checks the raw string. gltf then percent-decodes (urlencoding::decode) and honors file://. A URI whose raw components are the literal %2e%2e passes safe_under, and import resolves ... file:///etc/passwd is a relative path to the jail and an absolute read in Scheme::File.
  3. cell_loader::load_asset (AssetKind::Texture) is an uncapped tokio::fs::read. Scene streaming calls it for every asset on a cell (astraweave-scene/src/streaming.rs:193) and discards the Result.
  4. KTX2 dimensions. The file cap does not bound pixel_width / pixel_height (u32). The BC path allocates (width * height) as u32 math, which wraps in release, with no MAX_IMAGE_EDGE check. The new smoke test asserts the fallback expect, so an uncapped load_ktx2_to_rgba that fails the header parse still goes green.

The asset-crate file branch does not percent-decode and does not special-case file://, so it does not have the editor bypass. It jails to process cwd, not the glTF directory, and it duplicates load_uri_bytes instead of sharing one read. The v1.49 notice names the smoke-test fallback and omits load_ktx2_to_rgba.

Open in Web View Automation 

Sent by Cursor Automation: ASTRA PR AUDIT AGENT

Comment thread astraweave-asset/src/lib.rs Resolved
Comment thread docs/architecture/asset.md Resolved
@cursor

cursor Bot commented Oct 21, 2026

Copy link
Copy Markdown

The remaining uncapped image reads stay out of this PR. They are #408 (deferred-failure, owner @lazyxeon): data: glTF payloads capped before base64 decode, the editor gltf::import path and Poly Haven image::open routed through the shared capped helper, cell_loader texture reads capped, and KTX2 edges above 4096 rejected before allocation.

The render-trace notice on this head is v1.51 and names parent 49d289eb. That parent does not contain read_limited_image_bytes or the 1×1 mip fix.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment

Adversarial review of 05c83d09 against 49d289eb. The shared file cap is the right shape. Two image allocations this diff touches are still unbounded, and the issue #400 exit line (no uncapped image read left in astraweave-render or astraweave-asset) is not met. CI on this head was still pending, so replay, cargo-audit, and cargo-deny were not observed here. No Cargo.toml change and no new unsafe.

What holds

read_limited_image_bytes rejects a regular file from metadata().len() before read, then take(meta.len()) so a file that grows after the stat stays inside that length. A fifo or other non-file uses take(MAX_IMAGE_ALLOC) and fails closed when the read returns exactly the cap, so it does not block on one more byte. std::io::Take enforces that limit in read / read_buf (Rust 1.89). Symlinks are followed by metadata, so a link to a file over the cap is rejected. /dev/zero is not a regular file, so the read stops at 64 MiB and errors. open_limited_image, Texture::from_file_with_usage, read_capped_image (spawn_blocking; the ? unwraps only the JoinError), the ktx2 byte read, and a non-data: glTF image URI call that helper. No new crate.

The 1×1 Hi-Z change matches the existing contract. ceil(log2(1)) is 0 and wgpu rejects mip_level_count 0. .max(1.0) makes that one mip. Edges ≥ 2 stay on ceil(log2) (the 1024 → 10 and 1920 → 11 tests). build uses saturating_sub(1), so a one-mip pyramid only copies. copy_depth returns when coords is outside textureDimensions. record_hiz already clamps the extent with .max(1) and runs only when sample_hiz is set (default false). Nothing in draw_into samples HizPyramid::view.

Determinism

No sim write, no SimRng, no wall clock on the read path. log2(1.0) is exact on IEEE-754, so the 1×1 mip count does not fork across Windows, Linux, and macOS. Replay inputs are untouched. Texture I/O and the pyramid stay presentation-side. spawn_blocking changes which pool performs the read; it does not enter the ECS tick.

Does not hold

  1. load_ktx2_to_rgba caps the file and then allocates from pixel_width × pixel_height with no edge or alloc check. A file under 64 MiB can still request a multi-gigabyte pixel buffer. The new smoke test is a sparse cap + 1 file, so it never reaches that header.
  2. The glTF data: arm still base64-decodes the entire payload before decode_limited_image. The file arm copies the current_dir() jail instead of the glTF directory used by the dependency scan.
  3. mesh_gltf::load_rgba8 (image::open, feature gltf-assets) and cell_loader::load_asset for AssetKind::Texture (fs::read) are still uncapped. load_uri_bytes still std::fs::reads buffer files with no cap.
  4. from_file_with_usage_rejects_a_file_over_the_cap returns success when no wgpu adapter exists, so that sync path is unlocked on a CPU-only run. The 1×1 Hi-Z assert sits behind the same kind of skip, and NaniteCullingPipeline (nanite_gpu_culling.rs:197) still requests mip_level_count 0 at 1×1. draw_into does not record that pyramid.
  5. The trace row still says “stamp updated on merge”. The commit message says that phrase is retired and that the stamp names 8b4caa27. The file correctly names parent 49d289eb.
Open in Web View Automation 

Sent by Cursor Automation: ASTRA PR AUDIT AGENT

Comment thread astraweave-asset/src/lib.rs Resolved
Comment thread docs/architecture/asset.md Resolved
Comment thread astraweave-render/src/hiz_pyramid.rs Resolved
@cursor
cursor Bot force-pushed the cursor/cap-image-reads-ce1f branch from 05c83d0 to 3383bad Compare October 21, 2026 15:24

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment

Adversarial re-review of 3383badd against 040f24ae. The shared 64 MiB reader and the 1×1 Hi-Z mip are correct. This PR still does not meet #400's exit line (no uncapped image read left in astraweave-render or astraweave-asset). #408 records four of those holes and leaves two that this diff touches.

What holds

read_limited_image_bytes rejects a regular file from metadata().len() before any byte is read, then takes only that length, so a file that grows after the stat stays inside the length already compared. A fifo or other non-file uses take(MAX_IMAGE_ALLOC) and fails closed when the read returns exactly the cap, so it does not block on one more byte. std::io::Take enforces that limit. Symlinks are followed by metadata, so a link to a file over the cap is rejected. /dev/zero is not a regular file, so the read stops at 64 MiB and errors. open_limited_image, Texture::from_file_with_usage, read_capped_image, load_ktx2_to_rgba's file read, the ktx2 smoke fallback, and a non-data: glTF image URI call that helper. fifo_at_the_cap_does_not_read_one_more_byte still drives the helper through spawn_blocking. No new crate, no Cargo.toml change, no new unsafe.

HizPyramid::new(1, 1) used to request mip_level_count 0 because log2(1) is 0 and wgpu rejects that. .max(1.0) makes one mip. Edges ≥ 2 stay on the existing ceil(log2) contract (1024 → 10, 1920 → 11). That count never exceeds floor(log2(n)) + 1, so larger frames stay legal. build uses saturating_sub(1), so a one-mip pyramid only copies. copy_depth returns when coords is outside textureDimensions, so the 8×8 workgroup on a 1×1 image is in bounds. record_hiz already clamps the extent with .max(1) and runs only when sample_hiz is set (default false). Nothing in draw_into samples HizPyramid::view.

Security

The new file-URI arm jails with safe_under against the process cwd. On this platform file:///etc/passwd is a relative path (file:/etc/passwd), and a raw %2e%2e component is not .., so this arm does not have the editor gltf::import bypass. .. and absolute paths stay rejected. safe_under canonicalizes, then this arm opens that path again, so a symlink swapped in between is followed. The read is still size-capped; the jail is not.

Determinism

No sim write, no SimRng, no wall clock on the read path. log2(1.0) is exact, so the 1×1 mip count does not fork across Windows, Linux, and macOS. Texture I/O and the pyramid stay presentation-side. spawn_blocking changes which pool performs the read; it does not enter the ECS tick. Replay compare passed on Ubuntu and Windows (run 39625465678). The macOS replay job was still pending at review time. Cargo deny and cargo-audit passed (run 39625465782). Gate (fmt, clippy, check) passed.

Still open

  1. data: glTF images still base64-decode the whole payload before decode_limited_image. External buffer URIs still use uncapped std::fs::read (load_uri_bytes, line 811) before a buffer-view image is sliced. #408 names the data: case and does not name the buffer read.
  2. load_ktx2_to_rgba caps the file, then allocates from pixel_width × pixel_height with no edge check. The u32 product wraps in release. A header under 64 MiB can still request 16384² (about 1 GiB of u32 pixels and 1 GiB of RGBA) before texture2ddecoder checks the level length. #408 item 4 matches this. The new smoke test never reaches the header.
  3. mesh_gltf::load_rgba8 (image::open) and cell_loader AssetKind::Texture (tokio::fs::read) are still uncapped. #408 owns both. The editor import path is the one engine_adapter calls.
  4. from_file_with_usage_rejects_a_file_over_the_cap returns success when no wgpu adapter exists. The read fails before the device is used. #408 does not cover this.
  5. asset.md says every glTF image URI uses the capped read. A data: URI does not. Stage B2 still says image file URIs live in load_uri_bytes. The v1.53 notice is the accurate one.
  6. The timestamp row still says “stamp updated on merge” for 36757dad. The current stamp correctly names parent 040f24ae. NaniteCullingPipeline (nanite_gpu_culling.rs:197) still requests mip_level_count 0 at 1×1. draw_into does not record that pyramid.

Do not close #400 on this PR. The helper and the forward 1×1 mip can land; the exit line cannot.

Open in Web View Automation 

Sent by Cursor Automation: ASTRA PR AUDIT AGENT

Comment thread astraweave-asset/src/lib.rs Resolved
Comment thread docs/architecture/asset.md Resolved
read_limited_image_bytes is the sync cap. open_limited_image,
from_file_with_usage, the ktx2 smoke fallback, and async
read_capped_image call it. A 1x1 Hi-Z pyramid is one mip.

The trace stamp names parent 8b4caa2. That merge holds the v1.48
io-util edit. The "stamp updated on merge" wording is retired.

Co-authored-by: Andrew Brown <lazyxeon@users.noreply.github.com>
@cursor
cursor Bot force-pushed the cursor/cap-image-reads-ce1f branch from 3383bad to 56c061f Compare October 21, 2026 17:00

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 56c061f2 against parent 417f267f. The sync cap and the 1×1 forward pyramid are sound. This still does not meet #400's exit line, and it should not close that issue.

What holds:

  • read_limited_image_bytes rejects a regular file from metadata before any byte is read, then takes only that length. A fifo or other non-regular file stops at 64 MiB and is rejected when the read is exactly the cap, so a writer that stalls after a full buffer is not probed one byte further. A stat/open swap still cannot return more than the cap: the regular arm is limited by the statted length, and the other arm by take(cap).
  • Callers of that helper: open_limited_image, Texture::from_file_with_usage, load_ktx2_to_rgba's file read, the ktx2 smoke fallback, a non-data: glTF image URI, and async read_capped_image (via spawn_blocking, so the tokio worker is not blocked on std::fs). No new dependency and no new unsafe.
  • HizPyramid::new forces one mip at a 1×1 edge. build uses saturating_sub(1), so that pyramid does not downsample. copy_depth returns when the invocation is outside textureDimensions. record_hiz already clamps the frame with .max(1) and runs only when sample_hiz is set. The default is false. Nothing in draw_into reads the pyramid back, and this diff does not write simulation state, sample a wall clock, or draw an unseeded RNG. Replay identity is unchanged. f32::log2 of these edges stays on the presentation path.
  • .. and a leading / are rejected by safe_under. file:/etc/passwd and file:///etc/passwd are relative components (file:, etc, passwd), not absolute. The glTF crate hands the URI through as raw JSON, so %2F is not a separator.

Still open on this head:

  • A data: image URI base64-decodes the whole payload before the 4096 / 64 MiB decoder limits. External .gltf buffer URIs still use uncapped std::fs::read in load_uri_bytes, and Source::View images are slices of those buffers. #408 names the data: image URI and does not name that buffer read.
  • The KTX2 file stops at 64 MiB. The header does not. A short file can still allocate the pixel buffers from pixel_width * pixel_height before texture2ddecoder checks the level length. #408 item 4.
  • mesh_gltf::load_gltf / load_rgba8 and cell_loader AssetKind::Texture stay uncapped. #408.
  • The new cap tests for from_file_with_usage and load_texture_async return success when the adapter probe fails, so a CPU-only run never calls the function under test.
  • asset.md still says a glTF image URI uses the capped read. That is the non-data: arm only. Stage B2 still says image file URIs live in load_uri_bytes.
  • The rewritten timestamp row still says 36757dad was "stamp updated on merge". The current stamp itself is right: 417f267f is the parent, and the v1.53 notice names the new callers.

Trace Sync and Doc-Truth Lint are green on this head. Determinism, Security Audit, Render device validation, and the Gate job were still running at review time. I did not treat those as passed.

NaniteCullingPipeline (nanite_gpu_culling.rs:197) still passes unclamped ceil(log2) as mip_level_count. draw_into does not record that pyramid.

Open in Web View Automation 

Sent by Cursor Automation: ASTRA PR AUDIT AGENT

Comment thread astraweave-asset/src/lib.rs Resolved
Comment thread docs/architecture/asset.md Resolved
Comment thread astraweave-render/src/hiz_pyramid.rs Resolved
@lazyxeon
lazyxeon merged commit f3ca15b into main Oct 21, 2026
36 checks passed
@lazyxeon
lazyxeon deleted the cursor/cap-image-reads-ce1f branch October 21, 2026 17:57
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