Repository navigation
Stop recording the unused Hi-Z pyramid (#269) #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8623310
f56b2fa
596eed6
2ce0da5
36757da
c666937
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
load_texture_async now uses read_capped_image. A regular file over 64 MiB is rejected from its length. A fifo that fills the cap is rejected without a further read. The fifo drop test is unix-only, lives in a temp directory, and opens its cleanup writer with O_NONBLOCK. The trace stamp is 2ce0da5. The 0.275 ms Hi-Z figure is that 1660 Ti capture; the default frame does not record it. Co-authored-by: Andrew Brown <lazyxeon@users.noreply.github.com>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,50 @@ use image::{DynamicImage, GenericImageView}; | |
| #[cfg(feature = "textures")] | ||
| use std::path::Path; | ||
|
|
||
| /// Read an image file without exceeding [`astraweave_asset::image_limits::MAX_IMAGE_ALLOC`]. | ||
| /// | ||
| /// A regular file longer than the cap is rejected from its metadata, before | ||
| /// any byte is read. A regular file is then read only up to that length. | ||
| /// A fifo or other non-regular file is read with `take(cap)` and stopped | ||
| /// there: probing one byte past the cap would block if the writer stalled | ||
| /// after exactly that many bytes. | ||
| pub(crate) async fn read_capped_image(path: &str) -> Result<Vec<u8>, String> { | ||
| use tokio::io::AsyncReadExt; | ||
|
|
||
| let cap = astraweave_asset::image_limits::MAX_IMAGE_ALLOC; | ||
| let meta = tokio::fs::metadata(path) | ||
| .await | ||
| .map_err(|err| format!("read failed: {err}"))?; | ||
| if meta.is_file() { | ||
| if meta.len() > cap { | ||
| return Err(format!("image is {} bytes, cap is {cap}", meta.len())); | ||
| } | ||
| let mut file = tokio::fs::File::open(path) | ||
| .await | ||
| .map_err(|err| format!("read failed: {err}"))?; | ||
| let mut bytes = Vec::new(); | ||
| (&mut file) | ||
| .take(meta.len()) | ||
| .read_to_end(&mut bytes) | ||
| .await | ||
| .map_err(|err| format!("read failed: {err}"))?; | ||
| return Ok(bytes); | ||
| } | ||
| let mut file = tokio::fs::File::open(path) | ||
| .await | ||
| .map_err(|err| format!("read failed: {err}"))?; | ||
| let mut bytes = Vec::new(); | ||
| (&mut file) | ||
| .take(cap) | ||
| .read_to_end(&mut bytes) | ||
| .await | ||
| .map_err(|err| format!("read failed: {err}"))?; | ||
| if bytes.len() as u64 == cap { | ||
| return Err(format!("image exceeds {cap} bytes")); | ||
| } | ||
| Ok(bytes) | ||
|
Comment on lines
+15
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Callers of that function include The policy in this function holds. A regular file over the cap is rejected from metadata, and Put the bounded read in |
||
| } | ||
|
|
||
| /// Defines how a texture should be interpreted for correct color space handling | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| #[non_exhaustive] | ||
|
|
@@ -326,13 +370,12 @@ impl Texture { | |
| path: &str, | ||
| usage: TextureUsage, | ||
| ) -> Result<Self> { | ||
| use std::path::Path; | ||
|
|
||
| // Phase 1: Async file I/O (off main thread) | ||
| let path_buf = Path::new(path).to_path_buf(); | ||
| let bytes = tokio::fs::read(&path_buf) | ||
| // Phase 1: Async file I/O (off main thread). Same 64 MiB cap as | ||
| // `texture_streaming::decode_texture`: length is checked before the | ||
| // read, and a non-regular file is not probed one byte past the cap. | ||
| let bytes = read_capped_image(path) | ||
| .await | ||
| .map_err(|e| anyhow::anyhow!("Failed to read texture file {}: {}", path, e))?; | ||
| .map_err(|e| anyhow::anyhow!("Failed to read texture file {path}: {e}"))?; | ||
|
|
||
| // Phase 2: Async image decoding (on thread pool) | ||
| let label = path.to_string(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.