Auto merge of #1975 - DrMeepster:backtrace_fix, r=RalfJung
Make backtraces work with #[global_allocator] Currently, backtraces break when the global allocator is overridden because the allocator will attempt to deallocate memory allocated directly by Miri. ~~This PR fixes that by using a new memory kind and providing a function to deallocate it. We can't call the custom allocator to allocate because it's not possible to call a function in the middle of a shim.~~ This PR fixes that by adding a new version of the backtrace API accessible by setting `flags` to 1. Existing code still functions. backtrace-rs PR: rust-lang/backtrace-rs#462 Fixes https://github.com/rust-lang/miri/issues/1996
This commit is contained in:
commit
57786678d4
28
README.md
28
README.md
@ -398,23 +398,28 @@ 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 get the amount of frames in the current backtrace.
|
||||
// The `flags` argument must be `0`.
|
||||
fn miri_backtrace_size(flags: u64) -> usize;
|
||||
|
||||
/// 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`
|
||||
/// The `flags` argument must be `0`.
|
||||
fn miri_get_backtrace(flags: u64) -> Box<[*mut ()]>;
|
||||
/// This writes a slice of pointers into `buf` - each pointer is an opaque value
|
||||
/// that is only useful when passed to `miri_resolve_frame`.
|
||||
/// `buf` must have `miri_backtrace_size(0) * pointer_size` bytes of space.
|
||||
/// The `flags` argument must be `1`.
|
||||
fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
|
||||
|
||||
/// Miri-provided extern function to resolve a frame pointer obtained
|
||||
/// from `miri_get_backtrace`. The `flags` argument must be `0`,
|
||||
/// from `miri_get_backtrace`. The `flags` argument must be `1`,
|
||||
/// and `MiriFrame` should be declared as follows:
|
||||
///
|
||||
/// ```rust
|
||||
/// #[repr(C)]
|
||||
/// 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 size of the name of the function being executed, encoded in UTF-8
|
||||
/// name_len: usize,
|
||||
/// // The size of filename of the function being executed, encoded in UTF-8
|
||||
/// filename_len: usize,
|
||||
/// // The line number currently being executed in `filename`, starting from '1'.
|
||||
/// lineno: u32,
|
||||
/// // The column number currently being executed in `filename`, starting from '1'.
|
||||
@ -430,6 +435,11 @@ extern "Rust" {
|
||||
/// This function can be called on any thread (not just the one which obtained `frame`).
|
||||
fn miri_resolve_frame(frame: *mut (), flags: u64) -> MiriFrame;
|
||||
|
||||
/// Miri-provided extern function to get the name and filename of the frame provided by `miri_resolve_frame`.
|
||||
/// `name_buf` and `filename_buf` should be allocated with the `name_len` and `filename_len` fields of `MiriFrame`.
|
||||
/// The flags argument must be `0`.
|
||||
fn miri_resolve_frame_names(ptr: *mut (), flags: u64, name_buf: *mut u8, filename_buf: *mut u8);
|
||||
|
||||
/// 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
|
||||
|
@ -1,13 +1,33 @@
|
||||
use crate::*;
|
||||
use rustc_ast::ast::Mutability;
|
||||
use rustc_middle::ty::layout::LayoutOf as _;
|
||||
use rustc_middle::ty::{self, TypeAndMut};
|
||||
use rustc_span::{BytePos, Symbol};
|
||||
use rustc_middle::ty::{self, Instance, TypeAndMut};
|
||||
use rustc_span::{BytePos, Loc, Symbol};
|
||||
use rustc_target::{abi::Size, spec::abi::Abi};
|
||||
use std::convert::TryInto as _;
|
||||
|
||||
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
||||
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
||||
fn handle_miri_backtrace_size(
|
||||
&mut self,
|
||||
abi: Abi,
|
||||
link_name: Symbol,
|
||||
args: &[OpTy<'tcx, Tag>],
|
||||
dest: &PlaceTy<'tcx, Tag>,
|
||||
) -> InterpResult<'tcx> {
|
||||
let this = self.eval_context_mut();
|
||||
let &[ref flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
|
||||
let flags = this.read_scalar(flags)?.to_u64()?;
|
||||
if flags != 0 {
|
||||
throw_unsup_format!("unknown `miri_backtrace_size` flags {}", flags);
|
||||
}
|
||||
|
||||
let frame_count = this.active_thread_stack().len();
|
||||
|
||||
this.write_scalar(Scalar::from_machine_usize(frame_count.try_into().unwrap(), this), dest)
|
||||
}
|
||||
|
||||
fn handle_miri_get_backtrace(
|
||||
&mut self,
|
||||
abi: Abi,
|
||||
@ -17,12 +37,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
||||
) -> InterpResult<'tcx> {
|
||||
let this = self.eval_context_mut();
|
||||
let tcx = this.tcx;
|
||||
let &[ref flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
|
||||
let flags = this.read_scalar(flags)?.to_u64()?;
|
||||
if flags != 0 {
|
||||
throw_unsup_format!("unknown `miri_get_backtrace` flags {}", flags);
|
||||
}
|
||||
let flags = if let Some(flags_op) = args.get(0) {
|
||||
this.read_scalar(flags_op)?.to_u64()?
|
||||
} else {
|
||||
throw_ub_format!("expected at least 1 argument")
|
||||
};
|
||||
|
||||
let mut data = Vec::new();
|
||||
for frame in this.active_thread_stack().iter().rev() {
|
||||
@ -49,46 +69,60 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
||||
})
|
||||
.collect();
|
||||
|
||||
let len = ptrs.len();
|
||||
let len: u64 = ptrs.len().try_into().unwrap();
|
||||
|
||||
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_layout = this.layout_of(tcx.mk_array(ptr_ty, len)).unwrap();
|
||||
|
||||
// Write pointers into array
|
||||
let alloc =
|
||||
this.allocate(this.layout_of(array_ty).unwrap(), MiriMemoryKind::Rust.into())?;
|
||||
for (i, ptr) in ptrs.into_iter().enumerate() {
|
||||
let place = this.mplace_index(&alloc, i as u64)?;
|
||||
this.write_pointer(ptr, &place.into())?;
|
||||
}
|
||||
match flags {
|
||||
// storage for pointers is allocated by miri
|
||||
// deallocating the slice is undefined behavior with a custom global allocator
|
||||
0 => {
|
||||
let &[_flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
|
||||
let alloc = this.allocate(array_layout, MiriMemoryKind::Rust.into())?;
|
||||
|
||||
// Write pointers into array
|
||||
for (i, ptr) in ptrs.into_iter().enumerate() {
|
||||
let place = this.mplace_index(&alloc, i as u64)?;
|
||||
|
||||
this.write_pointer(ptr, &place.into())?;
|
||||
}
|
||||
|
||||
this.write_immediate(
|
||||
Immediate::new_slice(Scalar::from_maybe_pointer(alloc.ptr, this), len, this),
|
||||
dest,
|
||||
)?;
|
||||
}
|
||||
// storage for pointers is allocated by the caller
|
||||
1 => {
|
||||
let &[_flags, ref buf] = this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
|
||||
let buf_place = this.deref_operand(buf)?;
|
||||
|
||||
let ptr_layout = this.layout_of(ptr_ty)?;
|
||||
|
||||
for (i, ptr) in ptrs.into_iter().enumerate() {
|
||||
let offset = ptr_layout.size * i.try_into().unwrap();
|
||||
|
||||
let op_place =
|
||||
buf_place.offset(offset, MemPlaceMeta::None, ptr_layout, this)?;
|
||||
|
||||
this.write_pointer(ptr, &op_place.into())?;
|
||||
}
|
||||
}
|
||||
_ => throw_unsup_format!("unknown `miri_get_backtrace` flags {}", flags),
|
||||
};
|
||||
|
||||
this.write_immediate(
|
||||
Immediate::new_slice(
|
||||
Scalar::from_maybe_pointer(alloc.ptr, this),
|
||||
len.try_into().unwrap(),
|
||||
this,
|
||||
),
|
||||
dest,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_miri_resolve_frame(
|
||||
fn resolve_frame_pointer(
|
||||
&mut self,
|
||||
abi: Abi,
|
||||
link_name: Symbol,
|
||||
args: &[OpTy<'tcx, Tag>],
|
||||
dest: &PlaceTy<'tcx, Tag>,
|
||||
) -> InterpResult<'tcx> {
|
||||
ptr: &OpTy<'tcx, Tag>,
|
||||
) -> InterpResult<'tcx, (Instance<'tcx>, Loc, String, String)> {
|
||||
let this = self.eval_context_mut();
|
||||
let tcx = this.tcx;
|
||||
let &[ref ptr, ref flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
|
||||
let flags = this.read_scalar(flags)?.to_u64()?;
|
||||
if flags != 0 {
|
||||
throw_unsup_format!("unknown `miri_resolve_frame` flags {}", flags);
|
||||
}
|
||||
|
||||
let ptr = this.read_pointer(ptr)?;
|
||||
// Take apart the pointer, we need its pieces.
|
||||
@ -101,6 +135,29 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
||||
throw_ub_format!("expected function pointer, found {:?}", ptr);
|
||||
};
|
||||
|
||||
let lo =
|
||||
this.tcx.sess.source_map().lookup_char_pos(BytePos(offset.bytes().try_into().unwrap()));
|
||||
|
||||
let name = fn_instance.to_string();
|
||||
let filename = lo.file.name.prefer_remapped().to_string();
|
||||
|
||||
Ok((fn_instance, lo, name, filename))
|
||||
}
|
||||
|
||||
fn handle_miri_resolve_frame(
|
||||
&mut self,
|
||||
abi: Abi,
|
||||
link_name: Symbol,
|
||||
args: &[OpTy<'tcx, Tag>],
|
||||
dest: &PlaceTy<'tcx, Tag>,
|
||||
) -> InterpResult<'tcx> {
|
||||
let this = self.eval_context_mut();
|
||||
let &[ref ptr, ref flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
|
||||
let flags = this.read_scalar(flags)?.to_u64()?;
|
||||
|
||||
let (fn_instance, lo, name, filename) = this.resolve_frame_pointer(ptr)?;
|
||||
|
||||
// Reconstruct the original function pointer,
|
||||
// which we pass to user code.
|
||||
let fn_ptr = this.memory.create_fn_alloc(FnVal::Instance(fn_instance));
|
||||
@ -115,23 +172,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
||||
);
|
||||
}
|
||||
|
||||
let pos = BytePos(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.prefer_remapped().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;
|
||||
|
||||
// These are "mutable" allocations as we consider them to be owned by the callee.
|
||||
let name_alloc = this.allocate_str(&name, MiriMemoryKind::Rust.into(), Mutability::Mut);
|
||||
let filename_alloc =
|
||||
this.allocate_str(&filename, MiriMemoryKind::Rust.into(), Mutability::Mut);
|
||||
let lineno_alloc = Scalar::from_u32(lineno);
|
||||
let colno_alloc = Scalar::from_u32(colno);
|
||||
|
||||
let dest = this.force_allocation(dest)?;
|
||||
if let ty::Adt(adt, _) = dest.layout.ty.kind() {
|
||||
if !adt.repr().c() {
|
||||
@ -141,10 +185,38 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
||||
}
|
||||
}
|
||||
|
||||
this.write_immediate(name_alloc.to_ref(this), &this.mplace_field(&dest, 0)?.into())?;
|
||||
this.write_immediate(filename_alloc.to_ref(this), &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())?;
|
||||
match flags {
|
||||
0 => {
|
||||
// These are "mutable" allocations as we consider them to be owned by the callee.
|
||||
let name_alloc =
|
||||
this.allocate_str(&name, MiriMemoryKind::Rust.into(), Mutability::Mut);
|
||||
let filename_alloc =
|
||||
this.allocate_str(&filename, MiriMemoryKind::Rust.into(), Mutability::Mut);
|
||||
|
||||
this.write_immediate(
|
||||
name_alloc.to_ref(this),
|
||||
&this.mplace_field(&dest, 0)?.into(),
|
||||
)?;
|
||||
this.write_immediate(
|
||||
filename_alloc.to_ref(this),
|
||||
&this.mplace_field(&dest, 1)?.into(),
|
||||
)?;
|
||||
}
|
||||
1 => {
|
||||
this.write_scalar(
|
||||
Scalar::from_machine_usize(name.len().try_into().unwrap(), this),
|
||||
&this.mplace_field(&dest, 0)?.into(),
|
||||
)?;
|
||||
this.write_scalar(
|
||||
Scalar::from_machine_usize(filename.len().try_into().unwrap(), this),
|
||||
&this.mplace_field(&dest, 1)?.into(),
|
||||
)?;
|
||||
}
|
||||
_ => throw_unsup_format!("unknown `miri_resolve_frame` flags {}", flags),
|
||||
}
|
||||
|
||||
this.write_scalar(Scalar::from_u32(lineno), &this.mplace_field(&dest, 2)?.into())?;
|
||||
this.write_scalar(Scalar::from_u32(colno), &this.mplace_field(&dest, 3)?.into())?;
|
||||
|
||||
// Support a 4-field struct for now - this is deprecated
|
||||
// and slated for removal.
|
||||
@ -154,4 +226,28 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_miri_resolve_frame_names(
|
||||
&mut self,
|
||||
abi: Abi,
|
||||
link_name: Symbol,
|
||||
args: &[OpTy<'tcx, Tag>],
|
||||
) -> InterpResult<'tcx> {
|
||||
let this = self.eval_context_mut();
|
||||
|
||||
let &[ref ptr, ref flags, ref name_ptr, ref filename_ptr] =
|
||||
this.check_shim(abi, Abi::Rust, link_name, args)?;
|
||||
|
||||
let flags = this.read_scalar(flags)?.to_u64()?;
|
||||
if flags != 0 {
|
||||
throw_unsup_format!("unknown `miri_resolve_frame_names` flags {}", flags);
|
||||
}
|
||||
|
||||
let (_, _, name, filename) = this.resolve_frame_pointer(ptr)?;
|
||||
|
||||
this.memory.write_bytes(this.read_pointer(name_ptr)?, name.bytes())?;
|
||||
this.memory.write_bytes(this.read_pointer(filename_ptr)?, filename.bytes())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -380,6 +380,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
||||
this.machine.static_roots.push(alloc_id);
|
||||
}
|
||||
|
||||
// Obtains the size of a Miri backtrace. See the README for details.
|
||||
"miri_backtrace_size" => {
|
||||
this.handle_miri_backtrace_size(abi, link_name, args, dest)?;
|
||||
}
|
||||
|
||||
// Obtains a Miri backtrace. See the README for details.
|
||||
"miri_get_backtrace" => {
|
||||
// `check_shim` happens inside `handle_miri_get_backtrace`.
|
||||
@ -392,6 +397,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
|
||||
this.handle_miri_resolve_frame(abi, link_name, args, dest)?;
|
||||
}
|
||||
|
||||
// Writes the function and file names of a Miri backtrace frame into a user provided buffer. See the README for details.
|
||||
"miri_resolve_frame_names" => {
|
||||
this.handle_miri_resolve_frame_names(abi, link_name, args)?;
|
||||
}
|
||||
|
||||
// Standard C allocation
|
||||
"malloc" => {
|
||||
|
9
tests/compile-fail/backtrace/bad-backtrace-flags.rs
Normal file
9
tests/compile-fail/backtrace/bad-backtrace-flags.rs
Normal file
@ -0,0 +1,9 @@
|
||||
extern "Rust" {
|
||||
fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
miri_get_backtrace(2, 0 as *mut _); //~ ERROR unsupported operation: unknown `miri_get_backtrace` flags 2
|
||||
}
|
||||
}
|
25
tests/compile-fail/backtrace/bad-backtrace-resolve-flags.rs
Normal file
25
tests/compile-fail/backtrace/bad-backtrace-resolve-flags.rs
Normal file
@ -0,0 +1,25 @@
|
||||
#[repr(C)]
|
||||
struct MiriFrame {
|
||||
name_len: usize,
|
||||
filename_len: usize,
|
||||
lineno: u32,
|
||||
colno: u32,
|
||||
fn_ptr: *mut (),
|
||||
}
|
||||
|
||||
extern "Rust" {
|
||||
fn miri_backtrace_size(flags: u64) -> usize;
|
||||
fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
|
||||
fn miri_resolve_frame(ptr: *mut (), flags: u64) -> MiriFrame;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
let mut buf = vec![0 as *mut _; miri_backtrace_size(0)];
|
||||
|
||||
miri_get_backtrace(1, buf.as_mut_ptr());
|
||||
|
||||
// miri_resolve_frame will error from an invalid backtrace before it will from invalid flags
|
||||
miri_resolve_frame(buf[0], 2); //~ ERROR unsupported operation: unknown `miri_resolve_frame` flags 2
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
extern "Rust" {
|
||||
fn miri_backtrace_size(flags: u64) -> usize;
|
||||
fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
|
||||
fn miri_resolve_frame_names(ptr: *mut (), flags: u64, name_buf: *mut u8, filename_buf: *mut u8);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
let mut buf = vec![0 as *mut _; miri_backtrace_size(0)];
|
||||
|
||||
miri_get_backtrace(1, buf.as_mut_ptr());
|
||||
|
||||
// miri_resolve_frame_names will error from an invalid backtrace before it will from invalid flags
|
||||
miri_resolve_frame_names(buf[0], 2, 0 as *mut _, 0 as *mut _); //~ ERROR unsupported operation: unknown `miri_resolve_frame_names` flags 2
|
||||
}
|
||||
}
|
9
tests/compile-fail/backtrace/bad-backtrace-size-flags.rs
Normal file
9
tests/compile-fail/backtrace/bad-backtrace-size-flags.rs
Normal file
@ -0,0 +1,9 @@
|
||||
extern "Rust" {
|
||||
fn miri_backtrace_size(flags: u64) -> usize;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
miri_backtrace_size(2); //~ ERROR unsupported operation: unknown `miri_backtrace_size` flags 2
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
extern "Rust" {
|
||||
fn miri_resolve_frame(ptr: *mut (), flags: u64);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
miri_resolve_frame(0 as *mut _, 1); //~ ERROR unsupported operation: unknown `miri_resolve_frame` flags 1
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
$DIR/backtrace-api.rs:13:59 (func_d)
|
||||
$DIR/backtrace-api.rs:12:50 (func_c)
|
||||
$DIR/backtrace-api.rs:6:53 (func_b)
|
||||
$DIR/backtrace-api.rs:5:50 (func_a)
|
||||
$DIR/backtrace-api.rs:17:18 (main)
|
||||
$DIR/backtrace-api-v0.rs:13:59 (func_d)
|
||||
$DIR/backtrace-api-v0.rs:12:50 (func_c)
|
||||
$DIR/backtrace-api-v0.rs:6:53 (func_b)
|
||||
$DIR/backtrace-api-v0.rs:5:50 (func_a)
|
||||
$DIR/backtrace-api-v0.rs:17:18 (main)
|
||||
RUSTLIB/core/src/ops/function.rs:LL:COL (<fn() as std::ops::FnOnce<()>>::call_once - shim(fn()))
|
||||
RUSTLIB/std/src/sys_common/backtrace.rs:LL:COL (std::sys_common::backtrace::__rust_begin_short_backtrace)
|
||||
RUSTLIB/std/src/rt.rs:LL:COL (std::rt::lang_start::{closure#0})
|
5
tests/run-pass/backtrace-api-v0.stdout
Normal file
5
tests/run-pass/backtrace-api-v0.stdout
Normal file
@ -0,0 +1,5 @@
|
||||
$DIR/backtrace-api-v0.rs:13:59 (func_d)
|
||||
$DIR/backtrace-api-v0.rs:12:50 (func_c)
|
||||
$DIR/backtrace-api-v0.rs:6:53 (func_b::<u8>)
|
||||
$DIR/backtrace-api-v0.rs:5:50 (func_a)
|
||||
$DIR/backtrace-api-v0.rs:17:18 (main)
|
65
tests/run-pass/backtrace-api-v1.rs
Normal file
65
tests/run-pass/backtrace-api-v1.rs
Normal file
@ -0,0 +1,65 @@
|
||||
// normalize-stderr-test ".*/(rust[^/]*|checkout)/library/" -> "RUSTLIB/"
|
||||
// normalize-stderr-test "RUSTLIB/(.*):\d+:\d+ "-> "RUSTLIB/$1:LL:COL "
|
||||
// normalize-stderr-test "::<.*>" -> ""
|
||||
|
||||
#[inline(never)] fn func_a() -> Box<[*mut ()]> { func_b::<u8>() }
|
||||
#[inline(never)] fn func_b<T>() -> Box<[*mut ()]> { func_c() }
|
||||
|
||||
macro_rules! invoke_func_d {
|
||||
() => { func_d() }
|
||||
}
|
||||
|
||||
#[inline(never)] fn func_c() -> Box<[*mut ()]> { invoke_func_d!() }
|
||||
#[inline(never)] fn func_d() -> Box<[*mut ()]> { unsafe { let count = miri_backtrace_size(0); let mut buf = vec![std::ptr::null_mut(); count]; miri_get_backtrace(1, buf.as_mut_ptr()); buf.into() } }
|
||||
|
||||
fn main() {
|
||||
let mut seen_main = false;
|
||||
let frames = func_a();
|
||||
for frame in frames.into_iter() {
|
||||
let miri_frame = unsafe { miri_resolve_frame(*frame, 1) };
|
||||
|
||||
let mut name = vec![0; miri_frame.name_len];
|
||||
let mut filename = vec![0; miri_frame.filename_len];
|
||||
|
||||
unsafe {
|
||||
miri_resolve_frame_names(*frame, 0, name.as_mut_ptr(), filename.as_mut_ptr());
|
||||
}
|
||||
|
||||
let name = String::from_utf8(name).unwrap();
|
||||
let filename = String::from_utf8(filename).unwrap();
|
||||
|
||||
if name == "func_a" {
|
||||
assert_eq!(func_a as *mut (), miri_frame.fn_ptr);
|
||||
}
|
||||
|
||||
// Print every frame to stderr.
|
||||
let out = format!("{}:{}:{} ({})", filename, miri_frame.lineno, miri_frame.colno, name);
|
||||
eprintln!("{}", out);
|
||||
// Print the 'main' frame (and everything before it) to stdout, skipping
|
||||
// the printing of internal (and possibly fragile) libstd frames.
|
||||
if !seen_main {
|
||||
println!("{}", out);
|
||||
seen_main = name == "main";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This goes at the bottom of the file so that we can change it
|
||||
// without disturbing line numbers of the functions in the backtrace.
|
||||
|
||||
extern "Rust" {
|
||||
fn miri_backtrace_size(flags: u64) -> usize;
|
||||
fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
|
||||
fn miri_resolve_frame(ptr: *mut (), flags: u64) -> MiriFrame;
|
||||
fn miri_resolve_frame_names(ptr: *mut (), flags: u64, name_buf: *mut u8, filename_buf: *mut u8);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[repr(C)]
|
||||
struct MiriFrame {
|
||||
name_len: usize,
|
||||
filename_len: usize,
|
||||
lineno: u32,
|
||||
colno: u32,
|
||||
fn_ptr: *mut (),
|
||||
}
|
18
tests/run-pass/backtrace-api-v1.stderr
Normal file
18
tests/run-pass/backtrace-api-v1.stderr
Normal file
@ -0,0 +1,18 @@
|
||||
$DIR/backtrace-api-v1.rs:13:144 (func_d)
|
||||
$DIR/backtrace-api-v1.rs:12:50 (func_c)
|
||||
$DIR/backtrace-api-v1.rs:6:53 (func_b)
|
||||
$DIR/backtrace-api-v1.rs:5:50 (func_a)
|
||||
$DIR/backtrace-api-v1.rs:17:18 (main)
|
||||
RUSTLIB/core/src/ops/function.rs:LL:COL (<fn() as std::ops::FnOnce<()>>::call_once - shim(fn()))
|
||||
RUSTLIB/std/src/sys_common/backtrace.rs:LL:COL (std::sys_common::backtrace::__rust_begin_short_backtrace)
|
||||
RUSTLIB/std/src/rt.rs:LL:COL (std::rt::lang_start::{closure#0})
|
||||
RUSTLIB/core/src/ops/function.rs:LL:COL (std::ops::function::impls::call_once)
|
||||
RUSTLIB/std/src/panicking.rs:LL:COL (std::panicking::r#try::do_call)
|
||||
RUSTLIB/std/src/panicking.rs:LL:COL (std::panicking::r#try)
|
||||
RUSTLIB/std/src/panic.rs:LL:COL (std::panic::catch_unwind)
|
||||
RUSTLIB/std/src/rt.rs:LL:COL (std::rt::lang_start_internal::{closure#2})
|
||||
RUSTLIB/std/src/panicking.rs:LL:COL (std::panicking::r#try::do_call)
|
||||
RUSTLIB/std/src/panicking.rs:LL:COL (std::panicking::r#try)
|
||||
RUSTLIB/std/src/panic.rs:LL:COL (std::panic::catch_unwind)
|
||||
RUSTLIB/std/src/rt.rs:LL:COL (std::rt::lang_start_internal)
|
||||
RUSTLIB/std/src/rt.rs:LL:COL (std::rt::lang_start)
|
5
tests/run-pass/backtrace-api-v1.stdout
Normal file
5
tests/run-pass/backtrace-api-v1.stdout
Normal file
@ -0,0 +1,5 @@
|
||||
$DIR/backtrace-api-v1.rs:13:144 (func_d)
|
||||
$DIR/backtrace-api-v1.rs:12:50 (func_c)
|
||||
$DIR/backtrace-api-v1.rs:6:53 (func_b::<u8>)
|
||||
$DIR/backtrace-api-v1.rs:5:50 (func_a)
|
||||
$DIR/backtrace-api-v1.rs:17:18 (main)
|
@ -1,5 +0,0 @@
|
||||
$DIR/backtrace-api.rs:13:59 (func_d)
|
||||
$DIR/backtrace-api.rs:12:50 (func_c)
|
||||
$DIR/backtrace-api.rs:6:53 (func_b::<u8>)
|
||||
$DIR/backtrace-api.rs:5:50 (func_a)
|
||||
$DIR/backtrace-api.rs:17:18 (main)
|
Loading…
x
Reference in New Issue
Block a user