DirectStorage: stage raw reads rejected by Windows #237

Open
github-bot wants to merge 2 commits from github-bot/gh-237 into main AGit
Owner

@⁠loyodea on GitHub · 2026-11-26 08:01 UTC · original
loyodea:victory-dstorage-read-compat → main · +203 −10 · 1 files · 2 commits · open

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

**@⁠loyodea** on GitHub · 2026-11-26 08:01 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237) `loyodea:victory-dstorage-read-compat` → `main` · +203 −10 · 1 files · 2 commits · open 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://git.gtahub.st/github/Belobug--VIctory-Belobug-AMD/pulls/237)
On ERROR_INVALID_USER_BUFFER, finish the unread suffix through a bounded host buffer. Preserve short reads, destination watch handling, EOF, and HRESULTs. Related to #211.

Co-authored-by: Codex <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:18:10 +00:00
Author
Owner

@⁠gut0to on GitHub · 2026-11-26 16:18 UTC · original · reviewed

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** on GitHub · 2026-11-26 16:18 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237#pullrequestreview-6077434878) · **reviewed** 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.
@ -951,0 +951,4 @@
) -> Result<(), HRESULT> {
let mut done = 0;
while done < out.len() {
let at = offset.checked_add(done as u64).ok_or(E_INVALIDARG)?;
Author
Owner

@⁠gut0to on GitHub · 2026-11-26 16:18 UTC · original

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.

**@⁠gut0to** on GitHub · 2026-11-26 16:18 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237#discussion_r4711011192) 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.
@ -951,0 +951,4 @@
) -> Result<(), HRESULT> {
let mut done = 0;
while done < out.len() {
let at = offset.checked_add(done as u64).ok_or(E_INVALIDARG)?;
Author
Owner

@⁠loyodea on GitHub · 2026-11-26 18:53 UTC · original

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.

**@⁠loyodea** on GitHub · 2026-11-26 18:53 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237#discussion_r4712405349) 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.
Exercise the checked offset in direct and staged reads and verify that no additional read sees a wrapped offset. Follow-up to PR #237.

Co-authored-by: Codex <[email protected]>
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 21:42 UTC · original

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.

**@⁠adrianmjim** on GitHub · 2026-11-26 21:42 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/237#issuecomment-7203987009) 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 can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/237/head:github-bot/gh-237'
git switch 'github-bot/gh-237'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-237'
git switch 'github-bot/gh-237'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-237'
git switch 'github-bot/gh-237'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-237'
git switch 'main'
git merge --squash 'github-bot/gh-237'
git switch 'main'
git merge --ff-only 'github-bot/gh-237'
git switch 'main'
git merge 'github-bot/gh-237'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.