HLE: a view mapped during PageWindow stays clean until window drop #3481

Open
opened 2026-11-27 07:28:39 +00:00 by github-bot · 1 comment
Owner

@⁠playcations on GitHub · 2026-11-27 07:27 UTC · original

Separate follow-up requested in the discussion of #2566: a title view mapped after a DirectStorage PageWindow opens can remain clean after streamed bytes are visible, until that window drops. This is the pre-drop interval left outside #2566's pre-image fix; it is not the already-reported after-the-fact saved-byte regression.

Reproduced on exact PR2566 head 89a4b63df5e75252cb58202fdf81f0ca03474161, based on main 63bcca43c6a00a758510398fa43096ce7e4f7df0, with a deterministic CPU-only integration probe through the real xmem mapping and GPU write-watch code:

  1. Allocate one physical page and open page_window while no title view shows it.
  2. Reserve/map a title view of that page while the window stays open.
  3. Track that title view, take it and arm it.
  4. Write 0x22 through window.bytes(); the title mapping reads 0x22.
  5. Before dropping the window, Tracked::clean() is still true and nothing is saved.
  6. Drop the window: the tracker becomes dirty with no saved bytes, as #2566 intends.

The pre-drop assertion that the tracker must already be dirty fails. A positive control that holds kernel_will_write for the late view before tracking/taking it passes: it stays dirty during the write and retains no false pre-image. Both cases release their tracker and mappings before making the final assertions.

This demonstrates the unreported interval. It does not execute a GPU copy-back or establish an observed gameplay overwrite. The risk described in the earlier review is that copy-back sees a clean tracker during that interval. A fix needs late mappings to participate in the active physical write, or needs to defer their copy-back until the write is reported; reporting only at window drop leaves this interval.

Environment: Ubuntu 26.04.1, Wine 11.22 Staging; Windows/MSVC-target HLE test binary built with Rust 1.98.1/cargo-xwin. Filter modules::xmem::tests::late_window_: 1 passed, 1 failed at the intended pre-drop assertion. The container exposed no GPU and mounted no game data.

For observation only, the private harness widens visibility of watch, Tracked, and its existing track/take/clean/arm/saved APIs; it does not alter their memory behavior. Those visibility changes are not proposed as a public API. Complete synthetic harness:

Private integration harness patch
diff --git a/crates/victory-gpu/src/lib.rs b/crates/victory-gpu/src/lib.rs
index b47441d..ac1e78f 100644
--- a/crates/victory-gpu/src/lib.rs
+++ b/crates/victory-gpu/src/lib.rs
@@ -65,7 +65,7 @@ mod texture;
 #[cfg(windows)]
 mod twin;
 #[cfg(windows)]
-mod watch;
+pub mod watch; // Private visibility-only integration probe.
 
 #[cfg(all(windows, feature = "diagnostics"))]
 pub use copystat::CountingAlloc;
diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs
index 1da165a..3b2985b 100644
--- a/crates/victory-gpu/src/watch.rs
+++ b/crates/victory-gpu/src/watch.rs
@@ -175,7 +175,7 @@ impl Changed {
 pub(crate) type Runs = Vec<(usize, usize)>;
 
 /// Title memory a host copy or texture staging takes: bytes `[start, end)`.
-pub(crate) struct Tracked {
+pub struct Tracked {
     start: usize,
     end: usize,
     changed: Arc<Changed>,
@@ -318,7 +318,7 @@ fn guard_aliases(g: &mut Guards, pairs: &[(usize, usize)]) -> BTreeSet<usize> {
 ///
 /// Only the pages of the range that are such memory are tracked; the rest is
 /// never guarded and is copied whole at each use, as memory with no tracker.
-pub(crate) fn track(start: usize, len: usize, views: bool) -> Option<Tracked> {
+pub fn track(start: usize, len: usize, views: bool) -> Option<Tracked> {
     track_as(start, len, views, false)
 }
 
@@ -458,7 +458,7 @@ impl Tracked {
     /// now on mark them again) and returns the byte ranges to copy, cut to
     /// `[from, to)`, in order. A page whose part in the tracked memory lies
     /// only partly in `[from, to)` is copied in part and stays changed.
-    pub(crate) fn take(&self, from: usize, to: usize) -> Vec<(usize, usize)> {
+    pub fn take(&self, from: usize, to: usize) -> Vec<(usize, usize)> {
         let (copy, zero) = self.take_split(from, to);
         let mut runs: Vec<(usize, usize)> = copy.into_iter().chain(zero).collect();
         runs.sort_unstable();
@@ -562,7 +562,7 @@ impl Tracked {
     /// Whether no page changed since it was taken: then [`Tracked::take`]
     /// finds nothing, and asking costs no lock. A write racing this answer
     /// is found by the next asking, as one racing a take.
-    pub(crate) fn clean(&self) -> bool {
+    pub fn clean(&self) -> bool {
         clean_bit(self.changed.2)
     }
 
@@ -585,7 +585,7 @@ impl Tracked {
     /// Shaders may write the copy's bytes `[from, to)` until [`Tracked::disarm`]:
     /// pages there written next after they were taken have their bytes kept
     /// first ([`Tracked::saved`]).
-    pub(crate) fn arm(&self, from: usize, to: usize) {
+    pub fn arm(&self, from: usize, to: usize) {
         let (from, to) = (from.max(self.start), to.min(self.end));
         if from < to {
             self.changed.1.lock().unwrap_or_else(|e| e.into_inner()).arm(from / PAGE, to.div_ceil(PAGE));
@@ -612,7 +612,7 @@ impl Tracked {
     /// The bytes page `page` (a page number) held when it was first written
     /// since it was taken, if it was armed then (and so changed), with what
     /// the copy took of it since: the copy's bytes before the submission.
-    pub(crate) fn saved(&self, page: usize) -> Option<Box<[u8]>> {
+    pub fn saved(&self, page: usize) -> Option<Box<[u8]>> {
         self.changed.1.lock().unwrap_or_else(|e| e.into_inner()).bytes.remove(&page)
     }
 
diff --git a/crates/victory-hle/src/modules/xmem.rs b/crates/victory-hle/src/modules/xmem.rs
index 72b1ede..970e475 100644
--- a/crates/victory-hle/src/modules/xmem.rs
+++ b/crates/victory-hle/src/modules/xmem.rs
@@ -1467,6 +1467,49 @@ mod tests {
         assert_eq!(XMemFreePhysicalPages(1, b.as_ptr()), 1);
     }
 
+    /// Integration probe of a real window and a title view mapped later.
+    fn late_window_observation(open_late_view: bool) -> (bool, Option<u8>, bool, Option<u8>) {
+        let pages = allocate(1);
+        let mut window = page_window(&pages, 0, 1).expect("one physical page");
+        let address = XMemVirtualAlloc(
+            std::ptr::null_mut(), PAGE, MEM_RESERVE | PAGES_64K, 0, PAGE_READWRITE,
+        );
+        assert!(!address.is_null());
+        assert_eq!(XMemMapPhysicalPages(address, 1, pages.as_ptr()), address);
+        let at = address as usize;
+        // Positive control: open this late view before anyone takes it.
+        let held = open_late_view.then(|| victory_gpu::kernel_will_write(at, PAGE));
+        let tracked = victory_gpu::watch::track(at, PAGE, true).expect("mapped title view");
+        assert_eq!(tracked.take(at, at + PAGE), [(at, at + PAGE)]);
+        tracked.arm(at, at + PAGE);
+        window.bytes()[0] = 0x22;
+        // SAFETY: committed title mapping of the same physical page.
+        assert_eq!(unsafe { address.cast::<u8>().read_volatile() }, 0x22);
+        let before = (tracked.clean(), tracked.saved(at / PAGE).map(|bytes| bytes[0]));
+        drop(window);
+        drop(held);
+        let after = (!tracked.clean(), tracked.saved(at / PAGE).map(|bytes| bytes[0]));
+        drop(tracked);
+        assert_eq!(XMemMapPhysicalPages(address, 1, std::ptr::null()), address);
+        assert_eq!(XMemFreePhysicalPages(1, pages.as_ptr()), 1);
+        (before.0, before.1, after.0, after.1)
+    }
+
+    #[test]
+    fn late_window_view_must_be_dirty_before_window_drop() {
+        let (clean_before, saved_before, dirty_after, saved_after) = late_window_observation(false);
+        assert!(dirty_after);
+        assert_eq!(saved_after, None);
+        assert_eq!(saved_before, None);
+        assert!(!clean_before, "late title view is still clean after streamed bytes landed, before drop");
+    }
+
+    #[test]
+    fn late_window_view_opened_before_take_stays_dirty() {
+        let observed = late_window_observation(true);
+        assert_eq!(observed, (false, None, true, None));
+    }
+
     /// A page mapped at a second address while a window writes it is
     /// reported there too when the window is done (the title maps streamed
     /// pages into its GPU heap while their reads decode); an address that

AI-assisted with Codex GPT-6.1 Sol. Checked the existing issues, claims and open PageWindow PRs; #2504 and #2566 cover the existing-view/post-write paths, but I found no separate issue for this remaining interval.

**@⁠playcations** on GitHub · 2026-11-27 07:27 UTC · [original](https://github.com/Marvisak/VIctory/issues/3481) Separate follow-up requested in the discussion of #2566: a title view mapped after a DirectStorage `PageWindow` opens can remain clean after streamed bytes are visible, until that window drops. This is the pre-drop interval left outside #2566's pre-image fix; it is not the already-reported after-the-fact saved-byte regression. Reproduced on exact PR2566 head `89a4b63df5e75252cb58202fdf81f0ca03474161`, based on main `63bcca43c6a00a758510398fa43096ce7e4f7df0`, with a deterministic CPU-only integration probe through the real `xmem` mapping and GPU write-watch code: 1. Allocate one physical page and open `page_window` while no title view shows it. 2. Reserve/map a title view of that page while the window stays open. 3. Track that title view, take it and arm it. 4. Write `0x22` through `window.bytes()`; the title mapping reads `0x22`. 5. Before dropping the window, `Tracked::clean()` is still true and nothing is saved. 6. Drop the window: the tracker becomes dirty with no saved bytes, as #2566 intends. The pre-drop assertion that the tracker must already be dirty fails. A positive control that holds `kernel_will_write` for the late view before tracking/taking it passes: it stays dirty during the write and retains no false pre-image. Both cases release their tracker and mappings before making the final assertions. This demonstrates the unreported interval. It does not execute a GPU copy-back or establish an observed gameplay overwrite. The risk described in the earlier review is that copy-back sees a clean tracker during that interval. A fix needs late mappings to participate in the active physical write, or needs to defer their copy-back until the write is reported; reporting only at window drop leaves this interval. Environment: Ubuntu 26.04.1, Wine 11.22 Staging; Windows/MSVC-target HLE test binary built with Rust 1.98.1/cargo-xwin. Filter `modules::xmem::tests::late_window_`: 1 passed, 1 failed at the intended pre-drop assertion. The container exposed no GPU and mounted no game data. For observation only, the private harness widens visibility of `watch`, `Tracked`, and its existing `track/take/clean/arm/saved` APIs; it does not alter their memory behavior. Those visibility changes are not proposed as a public API. Complete synthetic harness: <details><summary>Private integration harness patch</summary> ```diff diff --git a/crates/victory-gpu/src/lib.rs b/crates/victory-gpu/src/lib.rs index b47441d..ac1e78f 100644 --- a/crates/victory-gpu/src/lib.rs +++ b/crates/victory-gpu/src/lib.rs @@ -65,7 +65,7 @@ mod texture; #[cfg(windows)] mod twin; #[cfg(windows)] -mod watch; +pub mod watch; // Private visibility-only integration probe. #[cfg(all(windows, feature = "diagnostics"))] pub use copystat::CountingAlloc; diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index 1da165a..3b2985b 100644 --- a/crates/victory-gpu/src/watch.rs +++ b/crates/victory-gpu/src/watch.rs @@ -175,7 +175,7 @@ impl Changed { pub(crate) type Runs = Vec<(usize, usize)>; /// Title memory a host copy or texture staging takes: bytes `[start, end)`. -pub(crate) struct Tracked { +pub struct Tracked { start: usize, end: usize, changed: Arc<Changed>, @@ -318,7 +318,7 @@ fn guard_aliases(g: &mut Guards, pairs: &[(usize, usize)]) -> BTreeSet<usize> { /// /// Only the pages of the range that are such memory are tracked; the rest is /// never guarded and is copied whole at each use, as memory with no tracker. -pub(crate) fn track(start: usize, len: usize, views: bool) -> Option<Tracked> { +pub fn track(start: usize, len: usize, views: bool) -> Option<Tracked> { track_as(start, len, views, false) } @@ -458,7 +458,7 @@ impl Tracked { /// now on mark them again) and returns the byte ranges to copy, cut to /// `[from, to)`, in order. A page whose part in the tracked memory lies /// only partly in `[from, to)` is copied in part and stays changed. - pub(crate) fn take(&self, from: usize, to: usize) -> Vec<(usize, usize)> { + pub fn take(&self, from: usize, to: usize) -> Vec<(usize, usize)> { let (copy, zero) = self.take_split(from, to); let mut runs: Vec<(usize, usize)> = copy.into_iter().chain(zero).collect(); runs.sort_unstable(); @@ -562,7 +562,7 @@ impl Tracked { /// Whether no page changed since it was taken: then [`Tracked::take`] /// finds nothing, and asking costs no lock. A write racing this answer /// is found by the next asking, as one racing a take. - pub(crate) fn clean(&self) -> bool { + pub fn clean(&self) -> bool { clean_bit(self.changed.2) } @@ -585,7 +585,7 @@ impl Tracked { /// Shaders may write the copy's bytes `[from, to)` until [`Tracked::disarm`]: /// pages there written next after they were taken have their bytes kept /// first ([`Tracked::saved`]). - pub(crate) fn arm(&self, from: usize, to: usize) { + pub fn arm(&self, from: usize, to: usize) { let (from, to) = (from.max(self.start), to.min(self.end)); if from < to { self.changed.1.lock().unwrap_or_else(|e| e.into_inner()).arm(from / PAGE, to.div_ceil(PAGE)); @@ -612,7 +612,7 @@ impl Tracked { /// The bytes page `page` (a page number) held when it was first written /// since it was taken, if it was armed then (and so changed), with what /// the copy took of it since: the copy's bytes before the submission. - pub(crate) fn saved(&self, page: usize) -> Option<Box<[u8]>> { + pub fn saved(&self, page: usize) -> Option<Box<[u8]>> { self.changed.1.lock().unwrap_or_else(|e| e.into_inner()).bytes.remove(&page) } diff --git a/crates/victory-hle/src/modules/xmem.rs b/crates/victory-hle/src/modules/xmem.rs index 72b1ede..970e475 100644 --- a/crates/victory-hle/src/modules/xmem.rs +++ b/crates/victory-hle/src/modules/xmem.rs @@ -1467,6 +1467,49 @@ mod tests { assert_eq!(XMemFreePhysicalPages(1, b.as_ptr()), 1); } + /// Integration probe of a real window and a title view mapped later. + fn late_window_observation(open_late_view: bool) -> (bool, Option<u8>, bool, Option<u8>) { + let pages = allocate(1); + let mut window = page_window(&pages, 0, 1).expect("one physical page"); + let address = XMemVirtualAlloc( + std::ptr::null_mut(), PAGE, MEM_RESERVE | PAGES_64K, 0, PAGE_READWRITE, + ); + assert!(!address.is_null()); + assert_eq!(XMemMapPhysicalPages(address, 1, pages.as_ptr()), address); + let at = address as usize; + // Positive control: open this late view before anyone takes it. + let held = open_late_view.then(|| victory_gpu::kernel_will_write(at, PAGE)); + let tracked = victory_gpu::watch::track(at, PAGE, true).expect("mapped title view"); + assert_eq!(tracked.take(at, at + PAGE), [(at, at + PAGE)]); + tracked.arm(at, at + PAGE); + window.bytes()[0] = 0x22; + // SAFETY: committed title mapping of the same physical page. + assert_eq!(unsafe { address.cast::<u8>().read_volatile() }, 0x22); + let before = (tracked.clean(), tracked.saved(at / PAGE).map(|bytes| bytes[0])); + drop(window); + drop(held); + let after = (!tracked.clean(), tracked.saved(at / PAGE).map(|bytes| bytes[0])); + drop(tracked); + assert_eq!(XMemMapPhysicalPages(address, 1, std::ptr::null()), address); + assert_eq!(XMemFreePhysicalPages(1, pages.as_ptr()), 1); + (before.0, before.1, after.0, after.1) + } + + #[test] + fn late_window_view_must_be_dirty_before_window_drop() { + let (clean_before, saved_before, dirty_after, saved_after) = late_window_observation(false); + assert!(dirty_after); + assert_eq!(saved_after, None); + assert_eq!(saved_before, None); + assert!(!clean_before, "late title view is still clean after streamed bytes landed, before drop"); + } + + #[test] + fn late_window_view_opened_before_take_stays_dirty() { + let observed = late_window_observation(true); + assert_eq!(observed, (false, None, true, None)); + } + /// A page mapped at a second address while a window writes it is /// reported there too when the window is done (the title maps streamed /// pages into its GPU heap while their reads decode); an address that ``` </details> AI-assisted with Codex GPT-6.1 Sol. Checked the existing issues, claims and open PageWindow PRs; #2504 and #2566 cover the existing-view/post-write paths, but I found no separate issue for this remaining interval.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 07:28:39 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-27 08:34 UTC · original

Fixed in #3488 — based on plain main since the pre-drop failure exists there independently of #2566. The issue's exact probe fails before ("late title view is still clean after streamed bytes landed, before drop") and passes after; full victory-hle suite green, victory-gpu regressions: none. Note for landing order: this and #2566 touch the same physical-write area — whichever lands second needs a routine rebase.

**@⁠kotekompotek** on GitHub · 2026-11-27 08:34 UTC · [original](https://github.com/Marvisak/VIctory/issues/3481#issuecomment-7213688962) Fixed in #3488 — based on plain main since the pre-drop failure exists there independently of #2566. The issue's exact probe fails before ("late title view is still clean after streamed bytes landed, before drop") and passes after; full victory-hle suite green, victory-gpu regressions: none. Note for landing order: this and #2566 touch the same physical-write area — whichever lands second needs a routine rebase.
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#3481
No description provided.