Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d7661d5

Browse files
committedNov 19, 2023
Test that the GC consults the extra_fn_ptr map
1 parent ee12232 commit d7661d5

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ignore-target-windows: No libc on Windows
2+
//@compile-flags: -Zmiri-permissive-provenance
3+
4+
#[path = "../utils/mod.rs"]
5+
mod utils;
6+
7+
type GetEntropyFn = unsafe extern "C" fn(*mut u8, libc::size_t) -> libc::c_int;
8+
9+
fn main() {
10+
let name = "getentropy\0";
11+
let addr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr() as *const _) as usize };
12+
// If the GC does not account for the extra_fn_ptr entry that this dlsym just added, this GC
13+
// run will delete our entry for the base addr of the function pointer we will transmute to,
14+
// and the call through the function pointer will report UB.
15+
utils::run_provenance_gc();
16+
17+
let ptr = addr as *mut libc::c_void;
18+
let func: GetEntropyFn = unsafe { std::mem::transmute(ptr) };
19+
let dest = &mut [0u8];
20+
unsafe { func(dest.as_mut_ptr(), dest.len()) };
21+
}

‎src/tools/miri/tests/utils/miri_extern.rs

+5
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,9 @@ extern "Rust" {
137137
out: *mut std::ffi::c_char,
138138
out_size: usize,
139139
) -> usize;
140+
141+
/// Run the provenance GC. The GC will run automatically at some cadence,
142+
/// but in tests we want to for sure run it at certain points to check
143+
/// that it doesn't break anything.
144+
pub fn miri_run_provenance_gc();
140145
}

‎src/tools/miri/tests/utils/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ mod miri_extern;
99

1010
pub use fs::*;
1111
pub use miri_extern::*;
12+
13+
pub fn run_provenance_gc() {
14+
// SAFETY: No preconditions. The GC is fine to run at any time.
15+
unsafe {
16+
miri_run_provenance_gc()
17+
}
18+
}

0 commit comments

Comments
 (0)
Please sign in to comment.