Views guarded while they are mapped stay writable; tests take turns on WARP
The runtime maps a view and then tells the write watch. A refresh on another thread could guard the new blocks in between, and the telling then forgot them and left them read-only: the title's next write to one was an access violation nobody takes. Told of a new view, the watch now gives the write access back; before an unmapping it only forgets, as before. The test binary's random crashes were d3d10warp.dll's own, with two tests on the software adapter at once: they take turns now. The watch's tests forget their guards before their memory is freed (another test's memory could get the address and inherit them), and three more are no longer tied to other tests' memory, the adapter's other work or a busy PC's timing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
c897fa28ff
commit
160d22222c
6 files changed
+144
-32
No files matched your search
@@ -732,10 +732,38 @@ pub fn title_memory_reading_now() -> Option<Reading> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The runtime mapped or unmapped title memory `[at, at + len)`: what it
|
||||
/// shows is new and read-write, so it is no longer guarded (nor known
|
||||
/// unmapped).
|
||||
/// The runtime mapped title memory `[at, at + len)`: what it shows is new,
|
||||
/// so it is no longer guarded (nor known unmapped), and read-write.
|
||||
///
|
||||
/// The view is there before we are told of it, and a copy's refresh on
|
||||
/// another thread may have guarded its blocks in between (it finds blocks
|
||||
/// it knew absent committed, and holds [`GUARDS`], which we wait for).
|
||||
/// Forgetting such a block and leaving it read-only would make the title's
|
||||
/// next write to it an access violation nobody takes (`on_write` finds it
|
||||
/// neither guarded nor writable) and a kernel read into it fail, so the
|
||||
/// write access is given back.
|
||||
pub fn title_memory_remapped(at: usize, len: usize) {
|
||||
open(&mut title_memory_changed(at, len), at, at.saturating_add(len));
|
||||
}
|
||||
|
||||
/// The runtime is about to unmap title memory `[at, at + len)`: it is
|
||||
/// nothing of ours any more. Its protection goes with it.
|
||||
pub fn title_memory_unmapping(at: usize, len: usize) {
|
||||
let mut g = title_memory_changed(at, len);
|
||||
let blocks: Vec<usize> = g
|
||||
.protected
|
||||
.range(at / BLOCK..at.saturating_add(len).div_ceil(BLOCK))
|
||||
.copied()
|
||||
.collect();
|
||||
for block in blocks {
|
||||
g.protected.remove(&block);
|
||||
g.clean.remove(&block);
|
||||
}
|
||||
}
|
||||
|
||||
/// Tells what keeps track of what title memory shows; the guards, with no
|
||||
/// block of the range known unmapped.
|
||||
fn title_memory_changed(at: usize, len: usize) -> std::sync::MutexGuard<'static, Guards> {
|
||||
// A placed texture there arrives, goes or changes with no descriptor
|
||||
// written.
|
||||
crate::descriptor::host::remapped(at, len);
|
||||
@@ -745,15 +773,11 @@ pub fn title_memory_remapped(at: usize, len: usize) {
|
||||
crate::inplace::title_memory_freeing(at, len);
|
||||
let mut g = guards();
|
||||
let range = at / BLOCK..at.saturating_add(len).div_ceil(BLOCK);
|
||||
let absent: Vec<usize> = g.absent.range(range.clone()).copied().collect();
|
||||
let absent: Vec<usize> = g.absent.range(range).copied().collect();
|
||||
for block in absent {
|
||||
g.absent.remove(&block);
|
||||
}
|
||||
let blocks: Vec<usize> = g.protected.range(range).copied().collect();
|
||||
for block in blocks {
|
||||
g.protected.remove(&block);
|
||||
g.clean.remove(&block);
|
||||
}
|
||||
g
|
||||
}
|
||||
|
||||
/// Registers the handler that takes writes to guarded blocks, once, ahead
|
||||
@@ -843,16 +867,18 @@ mod tests {
|
||||
} as usize;
|
||||
assert_ne!(p, 0);
|
||||
let (start, end) = (p, p + 4 * PAGE);
|
||||
let (_, before) = writes(start, end, false).expect("watched");
|
||||
assert!(before.is_empty());
|
||||
// The journal may hold pages of another test's memory that was here.
|
||||
let (none, before) = writes(start, end, false).expect("watched");
|
||||
assert!(before.iter().all(|&(_, w)| w < none), "{before:?}");
|
||||
// SAFETY: committed pages of ours.
|
||||
unsafe { ((p + PAGE + 8) as *mut u8).write(1) };
|
||||
let (now, after) = writes(start, end, false).expect("watched");
|
||||
assert_eq!(after, [(p / PAGE + 1, now)]);
|
||||
let new = |w: Vec<(usize, u64)>| w.into_iter().filter(|w| w.1 > none).collect::<Vec<_>>();
|
||||
assert_eq!(new(after), [(p / PAGE + 1, now)]);
|
||||
// Another asker after the kernel's bit was cleared still sees it.
|
||||
let (later, again) = writes(p + PAGE, p + 2 * PAGE, false).expect("watched");
|
||||
assert!(later > now);
|
||||
assert_eq!(again, [(p / PAGE + 1, now)]);
|
||||
assert_eq!(new(again), [(p / PAGE + 1, now)]);
|
||||
let mut plain = vec![0u8; PAGE];
|
||||
assert!(
|
||||
writes(
|
||||
@@ -911,6 +937,9 @@ mod tests {
|
||||
guard(view, view + 2 * BLOCK),
|
||||
Some((view / PAGE..view / PAGE + 1).collect())
|
||||
);
|
||||
// Before the view goes, as the runtime does: the address may be
|
||||
// another test's the moment it is free.
|
||||
title_memory_unmapping(view, 2 * BLOCK);
|
||||
// SAFETY: our view.
|
||||
unsafe {
|
||||
UnmapViewOfFile(
|
||||
@@ -919,7 +948,52 @@ mod tests {
|
||||
},
|
||||
)
|
||||
};
|
||||
title_memory_remapped(view, 2 * BLOCK);
|
||||
}
|
||||
|
||||
/// The runtime maps a view and then tells us; a copy's refresh on another
|
||||
/// thread can guard it in between. Told, it is the title's to write: a
|
||||
/// block left read-only and forgotten would be an access violation in
|
||||
/// title code.
|
||||
#[test]
|
||||
fn a_block_guarded_before_we_were_told_of_its_view_can_be_written() {
|
||||
use windows_sys::Win32::Foundation::{CloseHandle, INVALID_HANDLE_VALUE};
|
||||
use windows_sys::Win32::System::Memory::{
|
||||
CreateFileMappingW, FILE_MAP_WRITE, MapViewOfFile, UnmapViewOfFile,
|
||||
};
|
||||
// SAFETY: a pagefile section of two blocks, mapped read-write; the
|
||||
// view holds it.
|
||||
let view = unsafe {
|
||||
let section = CreateFileMappingW(
|
||||
INVALID_HANDLE_VALUE,
|
||||
std::ptr::null(),
|
||||
PAGE_READWRITE,
|
||||
0,
|
||||
(2 * BLOCK) as u32,
|
||||
std::ptr::null(),
|
||||
);
|
||||
assert!(!section.is_null());
|
||||
let view = MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, 2 * BLOCK);
|
||||
CloseHandle(section);
|
||||
view
|
||||
};
|
||||
let at = view.Value as usize;
|
||||
assert_ne!(at, 0);
|
||||
// The refresh, then the runtime's word.
|
||||
assert!(guard(at, at + 2 * BLOCK).is_some());
|
||||
assert!(!writable(at) && !writable(at + BLOCK));
|
||||
title_memory_remapped(at, 2 * BLOCK);
|
||||
assert!(writable(at) && writable(at + BLOCK));
|
||||
// SAFETY: inside the view; a fault would end the process.
|
||||
unsafe { ((at + 8) as *mut u8).write_volatile(1) };
|
||||
// Looked at again as new, and guarded again.
|
||||
assert_eq!(
|
||||
guard(at, at + 2 * BLOCK),
|
||||
Some((at / PAGE..at / PAGE + 2).collect())
|
||||
);
|
||||
assert!(!writable(at));
|
||||
title_memory_unmapping(at, 2 * BLOCK);
|
||||
// SAFETY: our view.
|
||||
unsafe { UnmapViewOfFile(view) };
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -934,7 +1008,7 @@ mod tests {
|
||||
struct View(MEMORY_MAPPED_VIEW_ADDRESS);
|
||||
impl Drop for View {
|
||||
fn drop(&mut self) {
|
||||
title_memory_remapped(self.0.Value as usize, SIZE);
|
||||
title_memory_unmapping(self.0.Value as usize, SIZE);
|
||||
// SAFETY: our view is still mapped.
|
||||
unsafe { UnmapViewOfFile(self.0) };
|
||||
}
|
||||
@@ -1060,6 +1134,7 @@ mod tests {
|
||||
let mut local = [0u8; 16];
|
||||
let at = local.as_mut_ptr() as usize;
|
||||
assert!(!is_view(at, at + local.len()));
|
||||
title_memory_unmapping(p, 3 * BLOCK);
|
||||
// SAFETY: our views and placeholder.
|
||||
unsafe {
|
||||
UnmapViewOfFile2(GetCurrentProcess(), first, MEM_PRESERVE_PLACEHOLDER);
|
||||
@@ -1069,7 +1144,6 @@ mod tests {
|
||||
}
|
||||
windows_sys::Win32::Foundation::CloseHandle(section);
|
||||
}
|
||||
title_memory_remapped(p, 3 * BLOCK);
|
||||
}
|
||||
|
||||
/// Blocks of a placeholder between views are reported once, then not
|
||||
@@ -1144,6 +1218,7 @@ mod tests {
|
||||
title_memory_remapped(p + BLOCK, BLOCK);
|
||||
assert_eq!(guard(p, p + 3 * BLOCK), Some(pages(1).collect()));
|
||||
assert_eq!(guard(p, p + 3 * BLOCK), Some(Vec::new()));
|
||||
title_memory_unmapping(p, 3 * BLOCK);
|
||||
// SAFETY: our views and placeholder.
|
||||
unsafe {
|
||||
UnmapViewOfFile2(GetCurrentProcess(), first, MEM_PRESERVE_PLACEHOLDER);
|
||||
@@ -1153,7 +1228,6 @@ mod tests {
|
||||
}
|
||||
windows_sys::Win32::Foundation::CloseHandle(section);
|
||||
}
|
||||
title_memory_remapped(p, 3 * BLOCK);
|
||||
}
|
||||
|
||||
/// A write or mapping that runs past the end of the address space (a
|
||||
|
||||
Reference in new issue
Block a user