diff --git a/README.md b/README.md index 657ea096e6c8..524c8e10cf36 100644 --- a/README.md +++ b/README.md @@ -266,6 +266,32 @@ extern "Rust" { /// `ptr` has to point to the beginning of an allocated block. fn miri_static_root(ptr: *const u8); + /// Miri-provided extern function to obtain a backtrace of the current call stack. + /// This returns a boxed slice of pointers - each pointer is an opaque value + /// that is only useful when passed to `miri_resolve_frame` + fn miri_get_backtrace() -> Box<[*mut ()]>; + + /// Miri-provided extern function to resolve a frame pointer obtained + /// from `miri_get_backtrace`. The `version` argument must be `0`, + /// and `MiriFrame` should be declared as follows: + /// + /// ```rust + /// struct MiriFrame { + /// // The name of the function being executed, encoded in UTF-8 + /// name: Box<[u8]>, + /// // The filename of the function being executed, encoded in UTF-8 + /// filename: Box<[u8]>, + /// // The line number currently being executed in `filename`, starting from '1'. + /// lineno: u32, + /// // The column number currently being executed in `filename`, starting from '1'. + /// colno: u32, + /// } + /// ``` + /// + /// The fields must be declared in exactly the same order as they appear in `MiriFrame` above. + /// This function can be called on any thread (not just the one which obtained `frame`) + fn miri_resolve_frame(version: u8, frame: *mut ()) -> MiriFrame; + /// Miri-provided extern function to begin unwinding with the given payload. /// /// This is internal and unstable and should not be used; we give it here diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 5bcbd797ca3c..4d1ead8f0f82 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -3,10 +3,13 @@ use std::{convert::{TryInto, TryFrom}, iter}; use log::trace; use rustc_hir::def_id::DefId; -use rustc_middle::{mir, ty}; +use rustc_middle::mir; use rustc_target::{abi::{Align, Size}, spec::PanicStrategy}; +use rustc_middle::ty::{self, ParamEnv, TypeAndMut}; +use rustc_ast::ast::Mutability; use rustc_apfloat::Float; use rustc_span::symbol::sym; +use rustc_span::BytePos; use crate::*; use helpers::check_arg_count; @@ -211,6 +214,89 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx this.machine.static_roots.push(ptr.alloc_id); } + // Obtains a Miri backtrace. See the README for details. + "miri_get_backtrace" => { + let tcx = this.tcx; + let mut data = Vec::new(); + for frame in this.active_thread_stack().iter().rev() { + data.push((frame.instance, frame.current_span().lo())); + } + + let ptrs: Vec<_> = data.into_iter().map(|(instance, pos)| { + let mut fn_ptr = this.memory.create_fn_alloc(FnVal::Instance(instance)); + fn_ptr.offset = Size::from_bytes(pos.0); + Scalar::Ptr(fn_ptr) + }).collect(); + + let len = ptrs.len(); + + let ptr_ty = tcx.mk_ptr(TypeAndMut { + ty: tcx.types.unit, + mutbl: Mutability::Mut + }); + + let array_ty = tcx.mk_array(ptr_ty, ptrs.len().try_into().unwrap()); + let array_ty_and_env = ParamEnv::empty().and(array_ty); + + // Write pointers into array + let alloc = this.allocate(tcx.layout_of(array_ty_and_env).unwrap(), MiriMemoryKind::Rust.into()); + for (i, ptr) in ptrs.into_iter().enumerate() { + let place = this.mplace_index(alloc, i as u64)?; + this.write_immediate_to_mplace(ptr.into(), place)?; + } + + this.write_immediate(Immediate::new_slice(alloc.ptr.into(), len.try_into().unwrap(), this), dest)?; + } + + // Resolves a Miri backtrace frame. See the README for details. + "miri_resolve_frame" => { + let tcx = this.tcx; + let &[version, ptr] = check_arg_count(args)?; + + let version = this.read_scalar(version)?.to_u8()?; + if version != 0 { + throw_ub_format!("Unknown `miri_resolve_frame` version {}", version); + } + + let ptr = match this.read_scalar(ptr)?.check_init()? { + Scalar::Ptr(ptr) => ptr, + Scalar::Raw { .. } => throw_ub_format!("Expected a pointer in `rust_miri_resolve_frame`, found {:?}", ptr) + }; + + let fn_instance = if let Some(GlobalAlloc::Function(instance)) = this.tcx.get_global_alloc(ptr.alloc_id) { + instance + } else { + throw_ub_format!("Expect function pointer, found {:?}", ptr); + }; + + if dest.layout.layout.fields.count() != 4 { + throw_ub_format!("Bad declaration of miri_resolve_frame - should return a struct with 4 fields"); + } + + let pos = BytePos(ptr.offset.bytes().try_into().unwrap()); + let name = fn_instance.to_string(); + + let lo = tcx.sess.source_map().lookup_char_pos(pos); + + let filename = lo.file.name.to_string(); + let lineno: u32 = lo.line as u32; + // `lo.col` is 0-based - add 1 to make it 1-based for the caller. + let colno: u32 = lo.col.0 as u32 + 1; + + let name_alloc = this.allocate_str(&name, MiriMemoryKind::Rust.into()); + let filename_alloc = this.allocate_str(&filename, MiriMemoryKind::Rust.into()); + let lineno_alloc = Scalar::from_u32(lineno); + let colno_alloc = Scalar::from_u32(colno); + + let dest = this.force_allocation_maybe_sized(dest, MemPlaceMeta::None)?.0; + + this.write_immediate(name_alloc.to_ref(), this.mplace_field(dest, 0)?.into())?; + this.write_immediate(filename_alloc.to_ref(), this.mplace_field(dest, 1)?.into())?; + this.write_scalar(lineno_alloc, this.mplace_field(dest, 2)?.into())?; + this.write_scalar(colno_alloc, this.mplace_field(dest, 3)?.into())?; + } + + // Standard C allocation "malloc" => { let &[size] = check_arg_count(args)?; diff --git a/tests/compile-fail/backtrace/bad-backtrace-decl.rs b/tests/compile-fail/backtrace/bad-backtrace-decl.rs new file mode 100644 index 000000000000..7c250fbbe3a4 --- /dev/null +++ b/tests/compile-fail/backtrace/bad-backtrace-decl.rs @@ -0,0 +1,13 @@ +extern "Rust" { + fn miri_get_backtrace() -> Box<[*mut ()]>; + fn miri_resolve_frame(version: u8, ptr: *mut ()); +} + +fn main() { + let frames = unsafe { miri_get_backtrace() }; + for frame in frames.into_iter() { + unsafe { + miri_resolve_frame(0, *frame); //~ ERROR Undefined Behavior: Bad declaration of miri_resolve_frame - should return a struct with 4 fields + } + } +} diff --git a/tests/compile-fail/backtrace/bad-backtrace-ptr.rs b/tests/compile-fail/backtrace/bad-backtrace-ptr.rs new file mode 100644 index 000000000000..49b8ac88494d --- /dev/null +++ b/tests/compile-fail/backtrace/bad-backtrace-ptr.rs @@ -0,0 +1,9 @@ +extern "Rust" { + fn miri_resolve_frame(version: u8, ptr: *mut ()); +} + +fn main() { + unsafe { + miri_resolve_frame(0, 0 as *mut _); //~ ERROR Undefined Behavior: Expected a pointer + } +} diff --git a/tests/compile-fail/backtrace/bad-backtrace-version.rs b/tests/compile-fail/backtrace/bad-backtrace-version.rs new file mode 100644 index 000000000000..b0183ca99e94 --- /dev/null +++ b/tests/compile-fail/backtrace/bad-backtrace-version.rs @@ -0,0 +1,9 @@ +extern "Rust" { + fn miri_resolve_frame(version: u8, ptr: *mut ()); +} + +fn main() { + unsafe { + miri_resolve_frame(1, 0 as *mut _); //~ ERROR Undefined Behavior: Unknown `miri_resolve_frame` version 1 + } +} diff --git a/tests/run-pass/backtrace-api.rs b/tests/run-pass/backtrace-api.rs new file mode 100644 index 000000000000..fa86debf17d5 --- /dev/null +++ b/tests/run-pass/backtrace-api.rs @@ -0,0 +1,24 @@ +// normalize-stderr-test ".*rustlib" -> "RUSTLIB" + +extern "Rust" { + fn miri_get_backtrace() -> Box<[*mut ()]>; + fn miri_resolve_frame(version: u8, ptr: *mut ()) -> MiriFrame; +} + +#[derive(Debug)] +struct MiriFrame { + name: Box<[u8]>, + filename: Box<[u8]>, + lineno: u32, + colno: u32 +} + +fn main() { + let frames = unsafe { miri_get_backtrace() }; + for frame in frames.into_iter() { + let miri_frame = unsafe { miri_resolve_frame(0, *frame) }; + let name = String::from_utf8(miri_frame.name.into()).unwrap(); + let filename = String::from_utf8(miri_frame.filename.into()).unwrap(); + eprintln!("{}:{}:{} ({})", filename, miri_frame.lineno, miri_frame.colno, name); + } +} diff --git a/tests/run-pass/backtrace-api.stderr b/tests/run-pass/backtrace-api.stderr new file mode 100644 index 000000000000..91a99070eb64 --- /dev/null +++ b/tests/run-pass/backtrace-api.stderr @@ -0,0 +1,10 @@ +$DIR/backtrace-api.rs:17:27 (main) +RUSTLIB/src/rust/library/core/src/ops/function.rs:227:5 (>::call_once - shim(fn())) +RUSTLIB/src/rust/library/std/src/sys_common/backtrace.rs:137:18 (std::sys_common::backtrace::__rust_begin_short_backtrace::) +RUSTLIB/src/rust/library/std/src/rt.rs:66:18 (std::rt::lang_start::<()>::{{closure}}#0) +RUSTLIB/src/rust/library/core/src/ops/function.rs:259:13 (std::ops::function::impls:: for &dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe>::call_once) +RUSTLIB/src/rust/library/std/src/panicking.rs:381:40 (std::panicking::r#try::do_call::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>) +RUSTLIB/src/rust/library/std/src/panicking.rs:345:19 (std::panicking::r#try:: i32 + std::marker::Sync + std::panic::RefUnwindSafe>) +RUSTLIB/src/rust/library/std/src/panic.rs:382:14 (std::panic::catch_unwind::<&dyn std::ops::Fn() -> i32 + std::marker::Sync + std::panic::RefUnwindSafe, i32>) +RUSTLIB/src/rust/library/std/src/rt.rs:51:25 (std::rt::lang_start_internal) +RUSTLIB/src/rust/library/std/src/rt.rs:65:5 (std::rt::lang_start::<()>)