about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2022-10-23 12:32:45 -0400
committerBen Kimock <kimockb@gmail.com>2022-10-25 17:51:49 -0400
commitd86bbd5094badaf55ade2073fbb41835ffc641b6 (patch)
treebf76b7c60f5fc74e443d5fdaab5f8dfe1d554cc8 /src
parenta46ccba167086f0b562eb78403f7446b0fed5f65 (diff)
downloadrust-d86bbd5094badaf55ade2073fbb41835ffc641b6.tar.gz
rust-d86bbd5094badaf55ade2073fbb41835ffc641b6.zip
Rename, improve docs, fail better
Diffstat (limited to 'src')
-rw-r--r--src/tools/miri/README.md15
-rw-r--r--src/tools/miri/src/shims/foreign_items.rs8
-rw-r--r--src/tools/miri/src/stacked_borrows/mod.rs3
-rw-r--r--src/tools/miri/tests/pass/stacked-borrows/stack-printing.rs31
-rw-r--r--src/tools/miri/tests/pass/stacked-borrows/stack-printing.stdout1
5 files changed, 44 insertions, 14 deletions
diff --git a/src/tools/miri/README.md b/src/tools/miri/README.md
index 81c4f5ffef4..bd175b46b7a 100644
--- a/src/tools/miri/README.md
+++ b/src/tools/miri/README.md
@@ -538,15 +538,20 @@ extern "Rust" {
     fn miri_start_panic(payload: *mut u8) -> !;
 
     /// Miri-provided extern function to get the internal unique identifier for the allocation that a pointer
-    /// points to. This is only useful as an input to `miri_print_stacks`, and it is a separate call because
+    /// points to. This is only useful as an input to `miri_print_borrow_stacks`, and it is a separate call because
     /// getting a pointer to an allocation at runtime can change the borrow stacks in the allocation.
+    /// This function should be considered unstable. It exists only to support `miri_print_borrow_stacks` and so
+    /// inherits all of its instability.
     fn miri_get_alloc_id(ptr: *const ()) -> u64;
 
     /// Miri-provided extern function to print (from the interpreter, not the program) the contents of all
-    /// borrow stacks in an allocation. The format of what this emits is unstable and may change at any time.
-    /// In particular, users should be aware that Miri will periodically attempt to garbage collect the
-    /// contents of all stacks. Callers of this function may wish to pass `-Zmiri-tag-gc=0` to disable the GC.
-    fn miri_print_stacks(alloc_id: u64);
+    /// borrow stacks in an allocation. The leftmost tag is the bottom of the stack.
+    /// The format of what this emits is unstable and may change at any time. In particular, users should be
+    /// aware that Miri will periodically attempt to garbage collect the contents of all stacks. Callers of
+    /// this function may wish to pass `-Zmiri-tag-gc=0` to disable the GC.
+    /// This function is extremely unstable. At any time the format of its output may change, its signature may
+    /// change, or it may be removed entirely.
+    fn miri_print_borrow_stacks(alloc_id: u64);
 
     /// Miri-provided extern function to print (from the interpreter, not the
     /// program) the contents of a section of program memory, as bytes. Bytes
diff --git a/src/tools/miri/src/shims/foreign_items.rs b/src/tools/miri/src/shims/foreign_items.rs
index 9d0e47cd06f..1b3205aabc9 100644
--- a/src/tools/miri/src/shims/foreign_items.rs
+++ b/src/tools/miri/src/shims/foreign_items.rs
@@ -420,10 +420,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
             "miri_get_alloc_id" => {
                 let [ptr] = this.check_shim(abi, Abi::Rust, link_name, args)?;
                 let ptr = this.read_pointer(ptr)?;
-                let (alloc_id, _, _) = this.ptr_get_alloc_id(ptr)?;
+                let (alloc_id, _, _) = this.ptr_get_alloc_id(ptr).map_err(|_e| {
+                    err_machine_stop!(TerminationInfo::Abort(
+                        format!("pointer passed to miri_get_alloc_id must not be dangling, got {ptr:?}")
+                    ))
+                })?;
                 this.write_scalar(Scalar::from_u64(alloc_id.0.get()), dest)?;
             }
-            "miri_print_stacks" => {
+            "miri_print_borrow_stacks" => {
                 let [id] = this.check_shim(abi, Abi::Rust, link_name, args)?;
                 let id = this.read_scalar(id)?.to_u64()?;
                 if let Some(id) = std::num::NonZeroU64::new(id) {
diff --git a/src/tools/miri/src/stacked_borrows/mod.rs b/src/tools/miri/src/stacked_borrows/mod.rs
index a2f003e6cc8..cc27b71eb56 100644
--- a/src/tools/miri/src/stacked_borrows/mod.rs
+++ b/src/tools/miri/src/stacked_borrows/mod.rs
@@ -1154,6 +1154,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
         let stacks = alloc_extra.stacked_borrows.as_ref().unwrap().borrow();
         for (range, stack) in stacks.stacks.iter_all() {
             print!("{range:?}: [");
+            if let Some(bottom) = stack.unknown_bottom() {
+                print!(" unknown-bottom(..{bottom:?})");
+            }
             for i in 0..stack.len() {
                 let item = stack.get(i).unwrap();
                 print!(" {:?}{:?}", item.perm(), item.tag());
diff --git a/src/tools/miri/tests/pass/stacked-borrows/stack-printing.rs b/src/tools/miri/tests/pass/stacked-borrows/stack-printing.rs
index 8d96a2e1ca9..3ca937ae13d 100644
--- a/src/tools/miri/tests/pass/stacked-borrows/stack-printing.rs
+++ b/src/tools/miri/tests/pass/stacked-borrows/stack-printing.rs
@@ -1,3 +1,5 @@
+//@compile-flags: -Zmiri-permissive-provenance
+#![feature(strict_provenance)]
 use std::{
     alloc::{self, Layout},
     mem::ManuallyDrop,
@@ -5,25 +7,40 @@ use std::{
 
 extern "Rust" {
     fn miri_get_alloc_id(ptr: *const u8) -> u64;
-    fn miri_print_stacks(alloc_id: u64);
+    fn miri_print_borrow_stacks(alloc_id: u64);
+}
+
+fn get_alloc_id(ptr: *const u8) -> u64 {
+    unsafe { miri_get_alloc_id(ptr) }
+}
+
+fn print_borrow_stacks(alloc_id: u64) {
+    unsafe { miri_print_borrow_stacks(alloc_id) }
 }
 
 fn main() {
     let ptr = unsafe { alloc::alloc(Layout::new::<u8>()) };
-    let alloc_id = unsafe { miri_get_alloc_id(ptr) };
-    unsafe { miri_print_stacks(alloc_id) };
+    let alloc_id = get_alloc_id(ptr);
+    print_borrow_stacks(alloc_id);
 
     assert!(!ptr.is_null());
-    unsafe { miri_print_stacks(alloc_id) };
+    print_borrow_stacks(alloc_id);
 
     unsafe { *ptr = 42 };
-    unsafe { miri_print_stacks(alloc_id) };
+    print_borrow_stacks(alloc_id);
 
     let _b = unsafe { ManuallyDrop::new(Box::from_raw(ptr)) };
-    unsafe { miri_print_stacks(alloc_id) };
+    print_borrow_stacks(alloc_id);
 
     let _ptr = unsafe { &*ptr };
-    unsafe { miri_print_stacks(alloc_id) };
+    print_borrow_stacks(alloc_id);
+
+    // Create an unknown bottom, and print it
+    let ptr = ptr as usize as *mut u8;
+    unsafe {
+        *ptr = 5;
+    }
+    print_borrow_stacks(alloc_id);
 
     unsafe { alloc::dealloc(ptr, Layout::new::<u8>()) };
 }
diff --git a/src/tools/miri/tests/pass/stacked-borrows/stack-printing.stdout b/src/tools/miri/tests/pass/stacked-borrows/stack-printing.stdout
index 660ee71e6f5..83873307820 100644
--- a/src/tools/miri/tests/pass/stacked-borrows/stack-printing.stdout
+++ b/src/tools/miri/tests/pass/stacked-borrows/stack-printing.stdout
@@ -3,3 +3,4 @@
 0..1: [ SharedReadWrite<TAG> ]
 0..1: [ SharedReadWrite<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> Unique<TAG> ]
 0..1: [ SharedReadWrite<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> Disabled<TAG> SharedReadOnly<TAG> ]
+0..1: [ unknown-bottom(..<TAG>) ]