Skip to content

Commit

Permalink
Test that the GC consults the extra_fn_ptr map
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Nov 20, 2023
1 parent 0ec82fa commit b991658
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/tools/miri/tests/pass-dep/extra_fn_ptr_gc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ignore-target-windows: No libc on Windows
//@compile-flags: -Zmiri-permissive-provenance

#[path = "../utils/mod.rs"]
mod utils;

type GetEntropyFn = unsafe extern "C" fn(*mut u8, libc::size_t) -> libc::c_int;

fn main() {
let name = "getentropy\0";
let addr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr() as *const _) as usize };
// If the GC does not account for the extra_fn_ptr entry that this dlsym just added, this GC
// run will delete our entry for the base addr of the function pointer we will transmute to,
// and the call through the function pointer will report UB.
utils::run_provenance_gc();

let ptr = addr as *mut libc::c_void;
let func: GetEntropyFn = unsafe { std::mem::transmute(ptr) };
let dest = &mut [0u8];
unsafe { func(dest.as_mut_ptr(), dest.len()) };
}
5 changes: 5 additions & 0 deletions src/tools/miri/tests/utils/miri_extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,9 @@ extern "Rust" {
out: *mut std::ffi::c_char,
out_size: usize,
) -> usize;

/// Run the provenance GC. The GC will run automatically at some cadence,
/// but in tests we want to for sure run it at certain points to check
/// that it doesn't break anything.
pub fn miri_run_provenance_gc();
}
7 changes: 7 additions & 0 deletions src/tools/miri/tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ mod miri_extern;

pub use fs::*;
pub use miri_extern::*;

pub fn run_provenance_gc() {
// SAFETY: No preconditions. The GC is fine to run at any time.
unsafe {
miri_run_provenance_gc()
}
}

0 comments on commit b991658

Please sign in to comment.