DirectStorage: stage raw reads rejected by Windows #237

Open
loyodea wants to merge 2 commits from upstream-pr/237 into main
loyodea commented 2026-11-26 08:01:59 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by loyodea on 2026-11-26T08:01:59Z.


Related to the raw-file-read item in #211. When Windows rejects an uncompressed DirectStorage read with ERROR_INVALID_USER_BUFFER, finish the unread suffix through a reusable host buffer capped at 1 MiB. Refresh the destination write-watch before copying each chunk. Preserve short reads, EOF, other HRESULTs, and successful direct reads.

The current diff in #50 retries the same destination pointer. Our synthetic error injection continues rejecting that pointer and succeeds with the host buffer. The regression uses a real temporary file and simulates the OS error.

Tested

  • Windows 11 build 26300, Rust 1.100.0 GNU x64.
  • All 16 focused DirectStorage tests passed. The new cases check a partial read followed by error 1784, staging across multiple reads, EOF, and error preservation.
  • HLE unit suite: 100 passed, five failed, one ignored. Unmodified main at 4f62c2a had 97 passed, the same five memory-mapping failures, and one ignored. The separate HLE integration test and staged asset-policy check passed.

Retail DirectStorage behavior remains unverified. Codex contributed the implementation and verification.

Review follow-up for partial-read offsets

Two synthetic tests cover offset overflow after a successful partial read, including a direct read and one that switches to the staging buffer. Both assert E_INVALIDARG, no callback at a wrapped offset, and preservation of the bytes already read and the unread suffix. A temporary wrapping_add mutation made both tests observe a callback at offset zero. With the checked arithmetic restored, all 18 DirectStorage tests passed on Windows, including an independent rerun. Review thread

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:issue:5506231340 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237). Originally posted by [loyodea](https://github.com/loyodea) on 2026-11-26T08:01:59Z. --- Related to the raw-file-read item in #211. When Windows rejects an uncompressed DirectStorage read with ERROR_INVALID_USER_BUFFER, finish the unread suffix through a reusable host buffer capped at 1 MiB. Refresh the destination write-watch before copying each chunk. Preserve short reads, EOF, other HRESULTs, and successful direct reads. The current diff in #50 retries the same destination pointer. Our synthetic error injection continues rejecting that pointer and succeeds with the host buffer. The regression uses a real temporary file and simulates the OS error. **Tested** - Windows 11 build 26300, Rust 1.100.0 GNU x64. - All 16 focused DirectStorage tests passed. The new cases check a partial read followed by error 1784, staging across multiple reads, EOF, and error preservation. - HLE unit suite: 100 passed, five failed, one ignored. Unmodified main at 4f62c2a had 97 passed, the same five memory-mapping failures, and one ignored. The separate HLE integration test and staged asset-policy check passed. Retail DirectStorage behavior remains unverified. Codex contributed the implementation and verification. ### Review follow-up for partial-read offsets Two synthetic tests cover offset overflow after a successful partial read, including a direct read and one that switches to the staging buffer. Both assert `E_INVALIDARG`, no callback at a wrapped offset, and preservation of the bytes already read and the unread suffix. A temporary `wrapping_add` mutation made both tests observe a callback at offset zero. With the checked arithmetic restored, all 18 DirectStorage tests passed on Windows, including an independent rerun. [Review thread](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237#discussion_r4711011192)
gut0to commented 2026-11-26 16:18:50 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by gut0to on 2026-11-26T16:18:50Z.

Review: COMMENTED


AI-assisted review. I inspected the new file-read fallback and its tests statically; I did not run tests or the game. This review suggests a focused offset-overflow boundary case.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review:6077434878 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237#pullrequestreview-6077434878). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-26T16:18:50Z. **Review:** COMMENTED --- AI-assisted review. I inspected the new file-read fallback and its tests statically; I did not run tests or the game. This review suggests a focused offset-overflow boundary case.
gut0to commented 2026-11-26 16:18:50 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by gut0to on 2026-11-26T16:18:50Z.

Code review location: crates/victory-hle/src/modules/dstorage.rs; line 954

@@ -933,19 +936,70 @@ fn read_into(request: &Request, out: &mut [u8]) -> Result<(), HRESULT> {
         .state
         .file;
     let started = std::time::Instant::now();
+    let result = read_file_into(out, request.offset, request.options == 0, |buf, at| {
+        file.seek_read(buf, at)
+    });
+    READING.fetch_add(started.elapsed().as_nanos() as u64, Ordering::Relaxed);
+    result
+}
+
+fn read_file_into(
+    out: &mut [u8],
+    offset: u64,
+    allow_staging: bool,
+    mut read: impl FnMut(&mut [u8], u64) -> std::io::Result<usize>,
+) -> Result<(), HRESULT> {
     let mut done = 0;
-    let result = loop {
-        if done == out.len() {
-            break Ok(());
-        }
-        match file.seek_read(&mut out[done..], request.offset + done as u64) {
-            Ok(0) => break Err(END_OF_FILE),
+    while done < out.len() {
+        let at = offset.checked_add(done as u64).ok_or(E_INVALIDARG)?;

AI-assisted review; I did not run tests. Could a unit test cover offset overflow after a successful partial read, such as starting near u64::MAX and having the callback return bytes that leave a suffix? It should assert E_INVALIDARG and that the next read is never called with a wrapped offset; current tests cover partial reads, EOF, and I/O errors, but not this checked-add boundary.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review-comment:4711011192 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237#discussion_r4711011192). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-26T16:18:50Z. **Code review location:** `crates/victory-hle/src/modules/dstorage.rs`; line 954 ~~~~diff @@ -933,19 +936,70 @@ fn read_into(request: &Request, out: &mut [u8]) -> Result<(), HRESULT> { .state .file; let started = std::time::Instant::now(); + let result = read_file_into(out, request.offset, request.options == 0, |buf, at| { + file.seek_read(buf, at) + }); + READING.fetch_add(started.elapsed().as_nanos() as u64, Ordering::Relaxed); + result +} + +fn read_file_into( + out: &mut [u8], + offset: u64, + allow_staging: bool, + mut read: impl FnMut(&mut [u8], u64) -> std::io::Result<usize>, +) -> Result<(), HRESULT> { let mut done = 0; - let result = loop { - if done == out.len() { - break Ok(()); - } - match file.seek_read(&mut out[done..], request.offset + done as u64) { - Ok(0) => break Err(END_OF_FILE), + while done < out.len() { + let at = offset.checked_add(done as u64).ok_or(E_INVALIDARG)?; ~~~~ --- AI-assisted review; I did not run tests. Could a unit test cover offset overflow after a successful partial read, such as starting near `u64::MAX` and having the callback return bytes that leave a suffix? It should assert `E_INVALIDARG` and that the next read is never called with a wrapped offset; current tests cover partial reads, EOF, and I/O errors, but not this checked-add boundary.
loyodea commented 2026-11-26 18:53:14 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by loyodea on 2026-11-26T18:53:14Z.

Code review location: crates/victory-hle/src/modules/dstorage.rs; line 954

@@ -933,19 +936,70 @@ fn read_into(request: &Request, out: &mut [u8]) -> Result<(), HRESULT> {
         .state
         .file;
     let started = std::time::Instant::now();
+    let result = read_file_into(out, request.offset, request.options == 0, |buf, at| {
+        file.seek_read(buf, at)
+    });
+    READING.fetch_add(started.elapsed().as_nanos() as u64, Ordering::Relaxed);
+    result
+}
+
+fn read_file_into(
+    out: &mut [u8],
+    offset: u64,
+    allow_staging: bool,
+    mut read: impl FnMut(&mut [u8], u64) -> std::io::Result<usize>,
+) -> Result<(), HRESULT> {
     let mut done = 0;
-    let result = loop {
-        if done == out.len() {
-            break Ok(());
-        }
-        match file.seek_read(&mut out[done..], request.offset + done as u64) {
-            Ok(0) => break Err(END_OF_FILE),
+    while done < out.len() {
+        let at = offset.checked_add(done as u64).ok_or(E_INVALIDARG)?;

Thanks for the suggestion. Both read loops check offset.checked_add(done as u64) before calling the reader. A two-byte partial read at u64::MAX - 1 with one byte left would therefore return E_INVALIDARG before the next call. The current tests do not exercise that boundary.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review-comment:4712405349 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237#discussion_r4712405349). Originally posted by [loyodea](https://github.com/loyodea) on 2026-11-26T18:53:14Z. **Code review location:** `crates/victory-hle/src/modules/dstorage.rs`; line 954 ~~~~diff @@ -933,19 +936,70 @@ fn read_into(request: &Request, out: &mut [u8]) -> Result<(), HRESULT> { .state .file; let started = std::time::Instant::now(); + let result = read_file_into(out, request.offset, request.options == 0, |buf, at| { + file.seek_read(buf, at) + }); + READING.fetch_add(started.elapsed().as_nanos() as u64, Ordering::Relaxed); + result +} + +fn read_file_into( + out: &mut [u8], + offset: u64, + allow_staging: bool, + mut read: impl FnMut(&mut [u8], u64) -> std::io::Result<usize>, +) -> Result<(), HRESULT> { let mut done = 0; - let result = loop { - if done == out.len() { - break Ok(()); - } - match file.seek_read(&mut out[done..], request.offset + done as u64) { - Ok(0) => break Err(END_OF_FILE), + while done < out.len() { + let at = offset.checked_add(done as u64).ok_or(E_INVALIDARG)?; ~~~~ --- Thanks for the suggestion. Both read loops check `offset.checked_add(done as u64)` before calling the reader. A two-byte partial read at `u64::MAX - 1` with one byte left would therefore return `E_INVALIDARG` before the next call. The current tests do not exercise that boundary.
adrianmjim commented 2026-11-26 21:42:41 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by adrianmjim on 2026-11-26T21:42:41Z.


Tested 7162d28 on top of main 4f62c2a, natively and without the game: RX 7900 XT, Windows 11 26300, Rust 1.98.1. This follows up on my comment in #211. The new head 3a37326 only adds the two offset-overflow tests. I have not run those.

Which errors does Windows return? I used a local probe test. It flips the last 64 KiB of a pagefile-view destination between read-only and read-write while a synchronous seek_read runs. I tried buffered and unbuffered reads of 64 KiB, 1 MiB and 16 MiB.

  • If the block is read-only when the read starts, the read fails with 998 (ERROR_NOACCESS). In my runs these reads wrote nothing to the destination.
  • If the protection changes while the read is in flight, the read fails with 1784 (ERROR_INVALID_USER_BUFFER, 0x800706f8). This is the first time I got 0x800706f8 locally. In #211 I only got 998.

This PR stages only on 1784. The race I described in #211 makes the block read-only again before the read starts (the fast path of guard() ignores the pins). So it gives 998:

  • My #211 probe test applied onto 7162d28 still fails with ReadFile 998 (3 of 3 runs). This PR does not touch watch.rs.
  • Calling read_file_into(..., true, seek_read) on a destination whose second block is read-only makes a single call. It returns 0x800703e6 and does not stage.
  • After that race, a staged finish like this PR's works: read into a host buffer, then kernel_will_write, then copy (3 of 3 runs). So staging on 998 as well would cover that case. Fixing the fast path in watch.rs would avoid it.

Partial writes before a 1784 failure:

  • A 1784 failure often leaves part of the range already written. In 1 MiB reads, 83 of 120 such failures had written some bytes, up to 1,044,480. The call returns no byte count.
  • So the staged path reads and writes those bytes a second time. They come from the same file offsets, so the final content is the same. The cost is repeated work, not torn data, as long as the file does not change.

The title's own ReadFile/fread (files.rs:530, 538) have the same window. This PR does not cover them.

Checks:

  • With the staging branch disabled, the 3 tests from 7162d28 fail, so they test the change.
  • Clippy on victory-hle reports no new warnings in dstorage.rs.

Not tested: the game, and the rx9070xt / a-publier branches.

I have the probe tests on a local branch if they are useful.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7203987009 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237#issuecomment-7203987009). Originally posted by [adrianmjim](https://github.com/adrianmjim) on 2026-11-26T21:42:41Z. --- Tested 7162d28 on top of main 4f62c2a, natively and without the game: RX 7900 XT, Windows 11 26300, Rust 1.98.1. This follows up on my comment in #211. The new head 3a37326 only adds the two offset-overflow tests. I have not run those. Which errors does Windows return? I used a local probe test. It flips the last 64 KiB of a pagefile-view destination between read-only and read-write while a synchronous `seek_read` runs. I tried buffered and unbuffered reads of 64 KiB, 1 MiB and 16 MiB. - If the block is read-only when the read starts, the read fails with 998 (ERROR_NOACCESS). In my runs these reads wrote nothing to the destination. - If the protection changes while the read is in flight, the read fails with 1784 (ERROR_INVALID_USER_BUFFER, 0x800706f8). This is the first time I got 0x800706f8 locally. In #211 I only got 998. This PR stages only on 1784. The race I described in #211 makes the block read-only again before the read starts (the fast path of `guard()` ignores the pins). So it gives 998: - My #211 probe test applied onto 7162d28 still fails with ReadFile 998 (3 of 3 runs). This PR does not touch watch.rs. - Calling `read_file_into(..., true, seek_read)` on a destination whose second block is read-only makes a single call. It returns `0x800703e6` and does not stage. - After that race, a staged finish like this PR's works: read into a host buffer, then `kernel_will_write`, then copy (3 of 3 runs). So staging on 998 as well would cover that case. Fixing the fast path in watch.rs would avoid it. Partial writes before a 1784 failure: - A 1784 failure often leaves part of the range already written. In 1 MiB reads, 83 of 120 such failures had written some bytes, up to 1,044,480. The call returns no byte count. - So the staged path reads and writes those bytes a second time. They come from the same file offsets, so the final content is the same. The cost is repeated work, not torn data, as long as the file does not change. The title's own `ReadFile`/`fread` (files.rs:530, 538) have the same window. This PR does not cover them. Checks: - With the staging branch disabled, the 3 tests from 7162d28 fail, so they test the change. - Clippy on victory-hle reports no new warnings in dstorage.rs. Not tested: the game, and the rx9070xt / a-publier branches. I have the probe tests on a local branch if they are useful.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/237:upstream-pr/237'
git switch 'upstream-pr/237'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/237'
git switch 'upstream-pr/237'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/237'
git switch 'upstream-pr/237'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/237'
git switch 'main'
git merge --squash 'upstream-pr/237'
git switch 'main'
git merge --ff-only 'upstream-pr/237'
git switch 'main'
git merge 'upstream-pr/237'
git push origin 'main'
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
Belobug/VIctory-Belobug-AMD!237
No description provided.