about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-07-24 22:00:23 +0000
committerbors <bors@rust-lang.org>2020-07-24 22:00:23 +0000
commit5ef299eb9805b4c86b227b718b39084e8bf24454 (patch)
tree3e2e397069a66471fe17991bbdcdef37bf4ef489 /src/libstd/sys
parentd8cf749570c87a4dcf05071bb5b280febb7f6657 (diff)
parent67b4f3b1482971f6eaecec0c5e01f8be467c491a (diff)
downloadrust-5ef299eb9805b4c86b227b718b39084e8bf24454.tar.gz
rust-5ef299eb9805b4c86b227b718b39084e8bf24454.zip
Auto merge of #74681 - RalfJung:miri-extern-fn, r=oli-obk
 Miri: use extern fn to expose interpreter operations to program; fix leak checker on Windows

This PR realizes an idea that @oli-obk has been suggesting for a while: to use Miri-specific `extern` functions to provide some extra capabilities to the program. Initially, we have two of these methods, which libstd itself needs:
* `miri_start_panic`, which replaces the intrinsic of the same name (mostly for consistency, to avoid having multiple mechanisms for Miri-specific functionality).
* `miri_static_root`, which adds an allocation to a list of static "roots" that Miri considers as not having leaked (including all memory reachable through them). This is needed for https://github.com/rust-lang/miri/issues/1302.

We use `extern` functions instead of intrinsics for this so that user code can more easily call these Miri hoolks -- e.g. `miri_static_root` should be useful for https://github.com/rust-lang/miri/issues/1318.

The Miri side of this is at https://github.com/rust-lang/miri/pull/1485.

r? @oli-obk
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/windows/thread_local_key.rs18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/libstd/sys/windows/thread_local_key.rs b/src/libstd/sys/windows/thread_local_key.rs
index e0bb102b3af..82901871e78 100644
--- a/src/libstd/sys/windows/thread_local_key.rs
+++ b/src/libstd/sys/windows/thread_local_key.rs
@@ -110,6 +110,16 @@ struct Node {
     next: *mut Node,
 }
 
+#[cfg(miri)]
+extern "Rust" {
+    /// Miri-provided extern function to mark the block `ptr` points to as a "root"
+    /// for some static memory. This memory and everything reachable by it is not
+    /// considered leaking even if it still exists when the program terminates.
+    ///
+    /// `ptr` has to point to the beginning of an allocated block.
+    fn miri_static_root(ptr: *const u8);
+}
+
 unsafe fn register_dtor(key: Key, dtor: Dtor) {
     let mut node = Box::new(Node { key, dtor, next: ptr::null_mut() });
 
@@ -117,7 +127,13 @@ unsafe fn register_dtor(key: Key, dtor: Dtor) {
     loop {
         node.next = head;
         match DTORS.compare_exchange(head, &mut *node, SeqCst, SeqCst) {
-            Ok(_) => return mem::forget(node),
+            Ok(_) => {
+                #[cfg(miri)]
+                miri_static_root(&*node as *const _ as *const u8);
+
+                mem::forget(node);
+                return;
+            }
             Err(cur) => head = cur,
         }
     }