Only changed dwords go back into title memory; cheap safe copies; title's own arguments; more shader forms

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 20:28:42 -06:00
1 parent 7913a98a3d
commit 66afb2b4cb
16 files changed
+973 -29

No files matched your search

+18
View File
@@ -5824,6 +5824,11 @@ pub(crate) fn read_into(src: *const u8, dst: &mut [u8]) -> bool {
fn read(src: *const u8, dst: *mut u8, len: usize) -> bool {
use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
if crate::safecopy::on() {
// SAFETY: the destination is mapped writable memory of `len` bytes;
// a source page that is not there fails the copy.
return unsafe { crate::safecopy::copy(dst, src, len) };
}
let mut done = 0;
// SAFETY: the destination is mapped writable memory of `len` bytes; the
// kernel checks the source.
@@ -5863,6 +5868,11 @@ pub(crate) fn write_mapped(dst: *mut u8, src: *const u8, len: usize) {
let direct = direct_writes();
let write = |at: usize, n: usize| {
let from = src.wrapping_add(at - dst as usize);
if crate::safecopy::on() {
// SAFETY: the source is ours; a destination page that is not
// there, or not writable, fails the copy.
return unsafe { crate::safecopy::copy(at as *mut u8, from, n) };
}
let mut done = 0;
if direct {
// SAFETY: as below; the kernel checks the destination and
@@ -6227,6 +6237,14 @@ impl Meter {
#[cfg(feature = "diagnostics")]
{
log_blame(frames);
let put = [0, 1, 2, 3].map(|i| crate::shadow::PUT[i].swap(0, Ordering::Relaxed) as f64 / frames);
tracing::info!(
"what shaders wrote put into title memory per frame (VICTORY_EXACT_BACK): {:.0} pieces in {:.0} writes, {:.1} MB; {:.0} pieces joined over gaps",
put[0],
put[1],
put[2] / 1e6,
put[3]
);
crate::copystat::log(std::mem::take(&mut self.copy_stats_frames) as f64);
for (why, n) in drops.iter().take(40) {
tracing::info!(
+2
View File
@@ -45,6 +45,8 @@ pub mod queue;
pub mod resource;
pub mod root;
#[cfg(windows)]
mod safecopy;
#[cfg(windows)]
mod sampler;
#[cfg(windows)]
pub mod shader_cache;
+1 -1
View File
@@ -178,7 +178,7 @@ fn translate(container: &[u8]) -> Result<Translated, String> {
// The translation depends on it (a view index past a capped heap).
crate::descriptor::host::probe_view_limit();
let c = Container::parse(container).map_err(|e| e.to_string())?;
let programs = match c.part(FourCC::DXIL) {
let programs = match c.part(FourCC::DXIL).or(c.part(FourCC::ACCC)) {
Some(part) => victory_shader::native_programs(part.data).map_err(|e| e.to_string())?,
None => Vec::new(),
};
+359
View File
@@ -0,0 +1,359 @@
//! Copies between our memory and the title's that fail instead of faulting.
//!
//! Title memory may be unmapped, remapped or made read-only by the title's
//! other threads while we copy it, so copies went through the kernel
//! (`ReadProcessMemory`, `NtWriteVirtualMemory`), which reports a missing
//! page instead of faulting: some 35,000 calls a frame, each of which locks
//! the pages it copies, 39 ms of kernel time on the submitting thread alone.
//!
//! [`copy`] is one `rep movsb`. A fault in it is taken by the handler of
//! [`crate::watch`] (`on_write`, the first in the process), which opens a
//! block we guard ourselves and lets the copy go on, and otherwise sends it
//! to its failing exit. `VICTORY_SAFE_COPY=0`: through the kernel, as before.
use std::sync::OnceLock;
std::arch::global_asm!(
".globl victory_safe_copy",
".globl victory_safe_copy_at",
".globl victory_safe_copy_after",
".globl victory_safe_copy_failed",
// rcx destination, rdx source, r8 bytes; eax: whether all were copied.
// A leaf that leaves the stack alone (the two registers it must give
// back are kept in volatile ones), so a stack walk through it works.
"victory_safe_copy:",
"mov r10, rsi",
"mov r11, rdi",
"mov rdi, rcx",
"mov rsi, rdx",
"mov rcx, r8",
"victory_safe_copy_at:",
"rep movsb",
"victory_safe_copy_after:",
"mov eax, 1",
"mov rsi, r10",
"mov rdi, r11",
"ret",
"victory_safe_copy_failed:",
"xor eax, eax",
"mov rsi, r10",
"mov rdi, r11",
"ret",
".globl victory_safe_put",
".globl victory_safe_put_at",
".globl victory_safe_put_after",
".globl victory_safe_put_failed",
// rcx destination, rdx what was there, r8 what is to be there, r9 blocks
// of eight dwords: stores the dwords in which the two differ, and no
// other byte. eax: whether all could be stored.
"victory_safe_put:",
"test r9, r9",
"jz 4f",
"vpcmpeqd ymm3, ymm3, ymm3",
"2:",
"vmovdqu ymm1, ymmword ptr [r8]",
"vpcmpeqd ymm2, ymm1, ymmword ptr [rdx]",
"vmovmskps eax, ymm2",
"cmp eax, 255",
"je 3f",
"vpxor ymm2, ymm2, ymm3",
"victory_safe_put_at:",
"vpmaskmovd ymmword ptr [rcx], ymm2, ymm1",
"victory_safe_put_after:",
"nop",
"3:",
"add rcx, 32",
"add rdx, 32",
"add r8, 32",
"dec r9",
"jnz 2b",
"vzeroupper",
"4:",
"mov eax, 1",
"ret",
"victory_safe_put_failed:",
"vzeroupper",
"xor eax, eax",
"ret",
);
unsafe extern "system" {
fn victory_safe_copy(dst: *mut u8, src: *const u8, len: usize) -> u32;
fn victory_safe_copy_at();
fn victory_safe_copy_after();
fn victory_safe_copy_failed();
fn victory_safe_put(dst: *mut u8, was: *const u8, now: *const u8, blocks: usize) -> u32;
fn victory_safe_put_at();
fn victory_safe_put_after();
fn victory_safe_put_failed();
}
/// Puts `now` at `dst` where it differs from `was`, whole dwords (as `dst`
/// is aligned) and not a byte beside them: other threads may be writing
/// those. At the ends, single bytes of a dword only partly in the stretch.
/// False if a page could not be written; then some may have been.
///
/// # Safety
/// As [`copy`]'s.
pub(crate) unsafe fn put_changed(dst: *mut u8, was: &[u8], now: &[u8]) -> bool {
assert_eq!(was.len(), now.len());
let n = now.len();
let head = dst.addr().wrapping_neg() & 3;
let head = head.min(n);
let blocks = (n - head) / 32;
let tail = head + blocks * 32;
let mut all = true;
if blocks > 0 {
// SAFETY: the two slices hold the blocks; the destination's faults
// leave through the failing exit.
all &= unsafe { victory_safe_put(dst.add(head), was[head..].as_ptr(), now[head..].as_ptr(), blocks) != 0 };
}
let single = |from: usize, to: usize| {
let mut all = true;
let mut i = from;
while i < to {
// A whole aligned dword, or the bytes there are of one.
let len = (4 - (dst.addr() + i) % 4).min(to - i);
if was[i..i + len] != now[i..i + len] {
// SAFETY: inside the stretch.
all &= unsafe { copy(dst.add(i), now[i..].as_ptr(), len) };
}
i += len;
}
all
};
all &= single(0, head);
all &= single(tail, n);
all
}
/// Whether copies are made here and not by the kernel.
pub(crate) fn on() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| {
let on = std::env::var_os("VICTORY_SAFE_COPY").is_none_or(|v| v != "0");
if on {
crate::watch::install();
}
on
})
}
/// Copies `len` bytes. False if a page of the source cannot be read or one
/// of the destination cannot be written; then some of the bytes may have
/// been copied.
///
/// # Safety
/// Of the two ranges, what is not title memory must be valid, and no Rust
/// reference may cover the destination. [`on`] must have been asked.
pub(crate) unsafe fn copy(dst: *mut u8, src: *const u8, len: usize) -> bool {
// SAFETY: a fault on either side leaves through the failing exit
// ([`failing_exit`]); the rest is the caller's.
unsafe { victory_safe_copy(dst, src, len) != 0 }
}
/// For the fault handler: where a fault at instruction `at` is to go on, if
/// it is [`copy`]'s.
pub(crate) fn failing_exit(at: usize) -> Option<usize> {
let (from, to) = (victory_safe_copy_at as *const () as usize, victory_safe_copy_after as *const () as usize);
if (from..to).contains(&at) {
return Some(victory_safe_copy_failed as *const () as usize);
}
let (from, to) = (victory_safe_put_at as *const () as usize, victory_safe_put_after as *const () as usize);
(from..to).contains(&at).then_some(victory_safe_put_failed as *const () as usize)
}
#[cfg(test)]
mod tests {
use super::*;
use windows_sys::Win32::System::Memory::{
MEM_COMMIT, MEM_DECOMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_NOACCESS, PAGE_READONLY, PAGE_READWRITE, VirtualAlloc,
VirtualFree, VirtualProtect,
};
struct Pages(*mut u8, usize);
impl Pages {
fn new(len: usize) -> Pages {
// SAFETY: plain allocation.
let at = unsafe { VirtualAlloc(std::ptr::null(), len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) };
assert!(!at.is_null());
Pages(at.cast(), len)
}
fn fill(&self) {
for i in 0..self.1 {
// SAFETY: inside the allocation.
unsafe { self.0.add(i).write((i * 31 + 7) as u8) };
}
}
}
impl Drop for Pages {
fn drop(&mut self) {
// SAFETY: our allocation.
unsafe { VirtualFree(self.0.cast(), 0, MEM_RELEASE) };
}
}
#[test]
fn copies_every_length_and_alignment() {
assert!(on());
let (a, b) = (Pages::new(1 << 16), Pages::new(1 << 16));
a.fill();
for len in [0usize, 1, 3, 4, 8, 31, 64, 255, 4095, 4096, 4097, 60_000] {
for (da, sa) in [(0usize, 0usize), (1, 0), (0, 3), (5, 2)] {
// SAFETY: inside the two allocations.
unsafe {
std::ptr::write_bytes(b.0, 0xEE, b.1);
assert!(copy(b.0.add(da), a.0.add(sa), len));
let (to, from) = (std::slice::from_raw_parts(b.0, b.1), std::slice::from_raw_parts(a.0, a.1));
assert_eq!(to[da..da + len], from[sa..sa + len]);
assert!(to[..da].iter().chain(&to[da + len..]).all(|&x| x == 0xEE));
}
}
}
}
/// The dwords that changed are stored, and no byte of any other, at
/// every alignment and length; a page that is not there fails.
#[test]
fn only_changed_dwords_are_put() {
assert!(on());
let pages = Pages::new(3 << 12);
for at in [0usize, 1, 2, 3, 4, 13, 4090] {
for len in [0usize, 1, 3, 4, 5, 31, 32, 33, 64, 100, 1000, 4096] {
let was: Vec<u8> = (0..len).map(|i| (i * 13 + 1) as u8).collect();
for step in [1usize, 5, 17, 64, 10_000] {
let mut now = was.clone();
for i in (0..len).step_by(step) {
now[i] ^= 0x5A;
}
// Other threads changed everything meanwhile.
// SAFETY: inside the allocation.
unsafe {
std::ptr::write_bytes(pages.0, 0xC3, pages.1);
assert!(put_changed(pages.0.add(at), &was, &now));
let there = std::slice::from_raw_parts(pages.0, pages.1);
let base = pages.0.addr();
for (k, &byte) in there.iter().enumerate() {
let expect = if (at..at + len).contains(&k) {
let dword = (base + k) / 4;
let changed = (0..len).any(|i| now[i] != was[i] && (base + at + i) / 4 == dword);
if changed { now[k - at] } else { 0xC3 }
} else {
0xC3
};
if byte != expect {
panic!("at {at} len {len} step {step} byte {k}: {byte:#x}, not {expect:#x}");
}
if k > at + len + 64 {
break;
}
}
}
}
}
}
// SAFETY: pages of our allocation.
unsafe {
VirtualFree(pages.0.add(4096).cast(), 4096, MEM_DECOMMIT);
let (was, now) = (vec![0u8; 8192], vec![1u8; 8192]);
assert!(!put_changed(pages.0, &was, &now));
// Nothing differs on the page that is not there: nothing fails.
let mut now = was.clone();
now[0] = 1;
assert!(put_changed(pages.0, &was, &now));
}
}
/// What the copies cost (ignored: a measurement): 64 KiB pieces of
/// scattered memory into a buffer the card reads, by the kernel and here.
#[test]
#[ignore]
fn copy_costs() {
use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
assert!(on());
let Some(host) = crate::host::host() else { return };
const N: usize = 128 << 20;
let from = Pages::new(N);
from.fill();
let (_buffer, shared) = host.shared_buffer(N as u64).expect("a shared buffer");
let plain = Pages::new(N);
plain.fill();
for (what, to) in [("a buffer the card reads", shared), ("plain memory", plain.0)] {
for piece in [256usize, 4 << 10, 64 << 10] {
let count = (N / piece).min(4096);
// A different, scattered piece each time.
let at = |i: usize| (i * 7919 % (N / piece)) * piece;
let time = |f: &dyn Fn(usize) -> bool| {
let mut best = f64::MAX;
for _ in 0..5 {
let t = std::time::Instant::now();
for i in 0..count {
assert!(f(at(i)));
}
best = best.min(t.elapsed().as_secs_f64());
}
(best * 1e6 / count as f64, count as f64 * piece as f64 / best / 1e9)
};
// SAFETY: inside the buffers.
let kernel = time(&|o| unsafe {
let mut done = 0;
ReadProcessMemory(GetCurrentProcess(), from.0.add(o).cast(), to.add(o).cast(), piece, &mut done) != 0
});
// SAFETY: as above.
let here = time(&|o| unsafe { copy(to.add(o), from.0.add(o), piece) });
// SAFETY: as above.
let std_copy = time(&|o| unsafe {
std::ptr::copy_nonoverlapping(from.0.add(o), to.add(o), piece);
true
});
println!(
"{piece:>6} bytes into {what}: kernel {:.2} us ({:.1} GB/s), rep movsb {:.2} us ({:.1} GB/s), memcpy {:.2} us ({:.1} GB/s)",
kernel.0, kernel.1, here.0, here.1, std_copy.0, std_copy.1
);
}
}
}
/// Missing, forbidden and read-only pages: the copy fails, at any byte,
/// and the process goes on.
#[test]
fn faults_are_failures() {
assert!(on());
let (a, b) = (Pages::new(3 << 12), Pages::new(3 << 12));
a.fill();
let mut old = 0;
// SAFETY: pages of our allocations.
unsafe {
assert!(copy(b.0, a.0, b.1));
VirtualProtect(a.0.add(4096).cast(), 4096, PAGE_NOACCESS, &mut old);
assert!(!copy(b.0, a.0, b.1));
assert!(copy(b.0, a.0, 4096));
assert!(!copy(b.0, a.0.add(8191), 1));
assert!(copy(b.0, a.0.add(8192), 4096));
VirtualProtect(a.0.add(4096).cast(), 4096, PAGE_READWRITE, &mut old);
VirtualFree(a.0.add(8192).cast(), 4096, MEM_DECOMMIT);
assert!(!copy(b.0, a.0, b.1));
VirtualProtect(b.0.add(4096).cast(), 4096, PAGE_READONLY, &mut old);
assert!(!copy(b.0, a.0, 8192));
assert!(!copy(std::ptr::without_provenance_mut(0x10), a.0, 4));
assert!(!copy(b.0, std::ptr::without_provenance(usize::MAX - 7), 4));
VirtualProtect(b.0.add(4096).cast(), 4096, PAGE_READWRITE, &mut old);
}
// Many faults, on several threads at once.
let at = a.0 as usize;
std::thread::scope(|s| {
for _ in 0..4 {
s.spawn(move || {
let mut to = [0u8; 64];
for _ in 0..500 {
// SAFETY: a decommitted page and our own buffer.
assert!(!unsafe { copy(to.as_mut_ptr(), (at + 8192) as *const u8, 64) });
}
});
}
});
}
}
+114 -13
View File
@@ -1409,9 +1409,114 @@ fn changed_runs(now: &[u8], was: &[u8]) -> Vec<(usize, usize)> {
runs
}
/// Whether what shaders wrote goes into title memory run by run
/// ([`put_changed`]; on by default, `VICTORY_EXACT_BACK=0` writes the whole
/// stretch from the first changed byte to the last, as before).
fn exact_back() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_EXACT_BACK").is_none_or(|v| v != "0"))
}
/// Since the last frame-rate line: pieces put back by [`put_changed`], the
/// writes they took, the bytes written, and the pieces whose runs were
/// joined over bytes that had not changed.
pub(crate) static PUT: [AtomicU64; 4] = [const { AtomicU64::new(0) }; 4];
/// The dwords (of title memory from `at` on) where `merged` differs from
/// `title`, as (offset, bytes) in order, joined where at most `gap` bytes
/// lie between.
fn dword_runs(at: usize, title: &[u8], merged: &[u8], gap: usize) -> Vec<(usize, usize)> {
let mut runs: Vec<(usize, usize)> = Vec::new();
for (from, n) in changed_runs(merged, title) {
// Shaders write dwords: the whole of each.
let lo = ((at + from) & !3).max(at) - at;
let hi = ((at + from + n + 3) & !3).min(at + title.len()) - at;
match runs.last_mut() {
Some((a, len)) if lo <= *a + *len + gap => *len = (*len).max(hi - *a),
_ => runs.push((lo, hi - lo)),
}
}
runs
}
/// What shaders wrote goes into title memory from `at` on: `title` is what
/// was read there, `merged` the same with their bytes put in. Only the
/// dwords that differ are written. The whole stretch from the first changed
/// byte to the last was: between the read and the write the title's own
/// threads, or data streamed in, may have written the bytes between (a heap
/// holds what shaders write beside what the CPU does), and those writes were
/// undone. Beside running title threads (the relay) that is a piece of
/// memory going back in time now and then: runs pool1, blame1 and seriess2
/// ended in the title decoding garbage.
///
/// shortcut: a piece with very many runs has them joined over small gaps,
/// where the old race is left.
fn put_changed(at: usize, title: &[u8], merged: &[u8]) -> usize {
crate::watch::landing(at, title.len());
if !exact_back() {
crate::host::write_mapped(at as *mut u8, merged.as_ptr(), merged.len());
return merged.len();
}
if crate::safecopy::on() {
PUT[0].fetch_add(1, Ordering::Relaxed);
PUT[2].fetch_add(merged.len() as u64, Ordering::Relaxed);
// SAFETY: title memory; a page of it that is not there fails the
// put, and the rest is put page by page.
if unsafe { crate::safecopy::put_changed(at as *mut u8, title, merged) } {
return merged.len();
}
let mut from = 0;
while from < merged.len() {
let n = (4096 - (at + from) % 4096).min(merged.len() - from);
// SAFETY: as above.
unsafe { crate::safecopy::put_changed((at + from) as *mut u8, &title[from..from + n], &merged[from..from + n]) };
from += n;
}
return merged.len();
}
let mut runs = dword_runs(at, title, merged, 0);
if runs.len() > 64 {
runs = dword_runs(at, title, merged, 32);
PUT[3].fetch_add(1, Ordering::Relaxed);
}
let mut written = 0;
for &(from, n) in &runs {
crate::host::write_mapped((at + from) as *mut u8, merged[from..].as_ptr(), n);
written += n;
}
PUT[0].fetch_add(1, Ordering::Relaxed);
PUT[1].fetch_add(runs.len() as u64, Ordering::Relaxed);
PUT[2].fetch_add(written as u64, Ordering::Relaxed);
written
}
#[cfg(test)]
mod merge_tests {
use super::changed_runs;
use super::{changed_runs, dword_runs};
/// Whole dwords around every changed byte and no other, whatever the
/// alignment; small gaps joined only when asked.
#[test]
fn dword_runs_hold_the_dwords_that_changed() {
let title: Vec<u8> = (0..200u32).map(|i| (i * 7 + 3) as u8).collect();
for at in [0x1000usize, 0x1001, 0x1002, 0x1003] {
for changes in [vec![0], vec![199], vec![5, 6], vec![8, 12, 40, 41, 43, 120]] {
let mut merged = title.clone();
for &c in &changes {
merged[c] ^= 0x55;
}
let runs = dword_runs(at, &title, &merged, 0);
let held: Vec<usize> = runs.iter().flat_map(|&(a, n)| a..a + n).collect();
let mut expect: Vec<usize> = (0..200).filter(|i| changes.iter().any(|c| (at + c) / 4 == (at + i) / 4)).collect();
expect.dedup();
assert_eq!(held, expect, "{at:#x} {changes:?}");
let joined = dword_runs(at, &title, &merged, 32);
assert!(joined.len() <= runs.len());
assert!(runs.iter().all(|r| joined.iter().any(|j| j.0 <= r.0 && r.0 + r.1 <= j.0 + j.1)));
}
}
assert!(dword_runs(0, &title, &title, 0).is_empty());
}
/// The runs hold exactly the bytes that differ: putting them into the
/// old bytes gives the new ones, and no run holds an equal byte.
@@ -2002,6 +2107,7 @@ impl Shadow {
// before the submission, else the title's. (With the two
// searches above this was 44 ms a frame for 970 pieces.)
let mut differs = 0u8;
let was = title.to_vec();
for ((t, h), o) in title.iter_mut().zip(host.iter_mut()).zip(old) {
let gpu = if *h != *o { 0xffu8 } else { 0 };
let merged = (*t & !gpu) | (*h & gpu);
@@ -2010,9 +2116,7 @@ impl Shadow {
*h = merged;
}
if differs != 0 {
crate::watch::landing(start + at, n);
crate::host::write_mapped((start + at) as *mut u8, title.as_ptr(), n);
written += n;
written += put_changed(start + at, &was, title);
}
}
// Our write shows as a title write at the copy's next refresh,
@@ -2096,12 +2200,11 @@ impl Shadow {
if !crate::host::read_into((start + at) as *const u8, title) {
return 0;
}
let was = title.clone();
if !merge_piece(now, held, title) {
return 0;
}
crate::watch::landing(start + at, n);
crate::host::write_mapped((start + at) as *mut u8, title.as_ptr(), n);
n
put_changed(start + at, &was, title)
}
/// The 64 KiB pieces (cut at multiples of 64 KiB of title memory) of
@@ -2619,6 +2722,7 @@ impl Shadow {
// As in `copy_back`: the card's byte where it is not what it
// was before the submission, else the title's.
let mut differs = 0u8;
let was = title.clone();
for ((t, g), o) in title.iter_mut().zip(gpu).zip(old.iter_mut()) {
let wrote = if *g != *o { 0xffu8 } else { 0 };
let merged = (*t & !wrote) | (*g & wrote);
@@ -2627,9 +2731,7 @@ impl Shadow {
*o = merged;
}
if differs != 0 {
crate::watch::landing(start + at, n);
crate::host::write_mapped((start + at) as *mut u8, title.as_ptr(), n);
written = n;
written = put_changed(start + at, &was, title);
}
} else {
// Title memory is not mapped there: the copy keeps what the
@@ -2692,6 +2794,7 @@ impl Shadow {
title.clear();
title.resize(n, 0);
let mapped = crate::host::read_into((start + at) as *const u8, title);
let was = title.clone();
let mut changed = false;
for (a, b) in outside(at, at + n, skip) {
let (a, b) = (a - at, b - at);
@@ -2702,9 +2805,7 @@ impl Shadow {
if !mapped || !changed {
return 0;
}
crate::watch::landing(start + at, n);
crate::host::write_mapped((start + at) as *mut u8, title.as_ptr(), n);
n
put_changed(start + at, &was, title)
}
}
+18 -5
View File
@@ -758,7 +758,7 @@ pub fn title_memory_remapped(at: usize, len: usize) {
/// Registers the handler that takes writes to guarded blocks, once, ahead
/// of the crash reporter's.
fn install() {
pub(crate) fn install() {
use windows_sys::Win32::Foundation::{EXCEPTION_ACCESS_VIOLATION, NTSTATUS};
use windows_sys::Win32::System::Diagnostics::Debug::{
AddVectoredExceptionHandler, EXCEPTION_CONTINUE_EXECUTION, EXCEPTION_CONTINUE_SEARCH,
@@ -768,11 +768,22 @@ fn install() {
// SAFETY: the system passes the exception being dispatched.
let r = unsafe { &*(*info).ExceptionRecord };
const WRITE: usize = 1;
// A fault of our own copies (`crate::safecopy`): whatever it is, if
// it is not a block we guard, the copy fails and goes on from there.
let ours = crate::safecopy::failing_exit(r.ExceptionAddress as usize);
let fail = |exit: usize| {
// SAFETY: the context the system passes, to go on from.
unsafe { (*(*info).ContextRecord).Rip = exit as u64 };
EXCEPTION_CONTINUE_EXECUTION
};
if r.ExceptionCode != EXCEPTION_ACCESS_VIOLATION as NTSTATUS
|| r.NumberParameters < 2
|| r.ExceptionInformation[0] != WRITE
{
return EXCEPTION_CONTINUE_SEARCH;
return match ours {
Some(exit) => fail(exit),
None => EXCEPTION_CONTINUE_SEARCH,
};
}
let at = r.ExceptionInformation[1];
// Held across both checks: another thread may have opened the block
@@ -785,9 +796,11 @@ fn install() {
return EXCEPTION_CONTINUE_EXECUTION;
}
if writable(at) {
EXCEPTION_CONTINUE_EXECUTION
} else {
EXCEPTION_CONTINUE_SEARCH
return EXCEPTION_CONTINUE_EXECUTION;
}
match ours {
Some(exit) => fail(exit),
None => EXCEPTION_CONTINUE_SEARCH,
}
}
static ONCE: std::sync::Once = std::sync::Once::new();