about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2023-11-18 20:48:42 -0500
committerBen Kimock <kimockb@gmail.com>2023-11-20 09:12:54 -0500
commitb99165842df821a4f067ff20d8119c53fc6d54f6 (patch)
tree6bb482803ab3d8ef39374362f94b1ae9ff51920f
parent0ec82fa9d3d68dd2f29f721a6db6a3af5332242d (diff)
downloadrust-b99165842df821a4f067ff20d8119c53fc6d54f6.tar.gz
rust-b99165842df821a4f067ff20d8119c53fc6d54f6.zip
Test that the GC consults the extra_fn_ptr map
-rw-r--r--src/tools/miri/tests/pass-dep/extra_fn_ptr_gc.rs21
-rw-r--r--src/tools/miri/tests/utils/miri_extern.rs5
-rw-r--r--src/tools/miri/tests/utils/mod.rs7
3 files changed, 33 insertions, 0 deletions
diff --git a/src/tools/miri/tests/pass-dep/extra_fn_ptr_gc.rs b/src/tools/miri/tests/pass-dep/extra_fn_ptr_gc.rs
new file mode 100644
index 00000000000..716119a0fc3
--- /dev/null
+++ b/src/tools/miri/tests/pass-dep/extra_fn_ptr_gc.rs
@@ -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()) };
+}
diff --git a/src/tools/miri/tests/utils/miri_extern.rs b/src/tools/miri/tests/utils/miri_extern.rs
index cd734bab7d5..7363c189840 100644
--- a/src/tools/miri/tests/utils/miri_extern.rs
+++ b/src/tools/miri/tests/utils/miri_extern.rs
@@ -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();
 }
diff --git a/src/tools/miri/tests/utils/mod.rs b/src/tools/miri/tests/utils/mod.rs
index 7b7dc231a50..6386162e095 100644
--- a/src/tools/miri/tests/utils/mod.rs
+++ b/src/tools/miri/tests/utils/mod.rs
@@ -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()
+    }
+}