Skip to content

Commit 5778667

Browse files
committed
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 #1996
2 parents 5d72cd9 + 2c670b1 commit 5778667

15 files changed

+333
-80
lines changed

README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,23 +398,28 @@ extern "Rust" {
398398
/// `ptr` has to point to the beginning of an allocated block.
399399
fn miri_static_root(ptr: *const u8);
400400

401+
// Miri-provided extern function to get the amount of frames in the current backtrace.
402+
// The `flags` argument must be `0`.
403+
fn miri_backtrace_size(flags: u64) -> usize;
404+
401405
/// Miri-provided extern function to obtain a backtrace of the current call stack.
402-
/// This returns a boxed slice of pointers - each pointer is an opaque value
403-
/// that is only useful when passed to `miri_resolve_frame`
404-
/// The `flags` argument must be `0`.
405-
fn miri_get_backtrace(flags: u64) -> Box<[*mut ()]>;
406+
/// This writes a slice of pointers into `buf` - each pointer is an opaque value
407+
/// that is only useful when passed to `miri_resolve_frame`.
408+
/// `buf` must have `miri_backtrace_size(0) * pointer_size` bytes of space.
409+
/// The `flags` argument must be `1`.
410+
fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
406411

407412
/// Miri-provided extern function to resolve a frame pointer obtained
408-
/// from `miri_get_backtrace`. The `flags` argument must be `0`,
413+
/// from `miri_get_backtrace`. The `flags` argument must be `1`,
409414
/// and `MiriFrame` should be declared as follows:
410415
///
411416
/// ```rust
412417
/// #[repr(C)]
413418
/// struct MiriFrame {
414-
/// // The name of the function being executed, encoded in UTF-8
415-
/// name: Box<[u8]>,
416-
/// // The filename of the function being executed, encoded in UTF-8
417-
/// filename: Box<[u8]>,
419+
/// // The size of the name of the function being executed, encoded in UTF-8
420+
/// name_len: usize,
421+
/// // The size of filename of the function being executed, encoded in UTF-8
422+
/// filename_len: usize,
418423
/// // The line number currently being executed in `filename`, starting from '1'.
419424
/// lineno: u32,
420425
/// // The column number currently being executed in `filename`, starting from '1'.
@@ -430,6 +435,11 @@ extern "Rust" {
430435
/// This function can be called on any thread (not just the one which obtained `frame`).
431436
fn miri_resolve_frame(frame: *mut (), flags: u64) -> MiriFrame;
432437

438+
/// Miri-provided extern function to get the name and filename of the frame provided by `miri_resolve_frame`.
439+
/// `name_buf` and `filename_buf` should be allocated with the `name_len` and `filename_len` fields of `MiriFrame`.
440+
/// The flags argument must be `0`.
441+
fn miri_resolve_frame_names(ptr: *mut (), flags: u64, name_buf: *mut u8, filename_buf: *mut u8);
442+
433443
/// Miri-provided extern function to begin unwinding with the given payload.
434444
///
435445
/// This is internal and unstable and should not be used; we give it here

src/shims/backtrace.rs

Lines changed: 148 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
11
use crate::*;
22
use rustc_ast::ast::Mutability;
33
use rustc_middle::ty::layout::LayoutOf as _;
4-
use rustc_middle::ty::{self, TypeAndMut};
5-
use rustc_span::{BytePos, Symbol};
4+
use rustc_middle::ty::{self, Instance, TypeAndMut};
5+
use rustc_span::{BytePos, Loc, Symbol};
66
use rustc_target::{abi::Size, spec::abi::Abi};
77
use std::convert::TryInto as _;
88

99
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
1010
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
11-
fn handle_miri_get_backtrace(
11+
fn handle_miri_backtrace_size(
1212
&mut self,
1313
abi: Abi,
1414
link_name: Symbol,
1515
args: &[OpTy<'tcx, Tag>],
1616
dest: &PlaceTy<'tcx, Tag>,
1717
) -> InterpResult<'tcx> {
1818
let this = self.eval_context_mut();
19-
let tcx = this.tcx;
2019
let &[ref flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
2120

2221
let flags = this.read_scalar(flags)?.to_u64()?;
2322
if flags != 0 {
24-
throw_unsup_format!("unknown `miri_get_backtrace` flags {}", flags);
23+
throw_unsup_format!("unknown `miri_backtrace_size` flags {}", flags);
2524
}
2625

26+
let frame_count = this.active_thread_stack().len();
27+
28+
this.write_scalar(Scalar::from_machine_usize(frame_count.try_into().unwrap(), this), dest)
29+
}
30+
31+
fn handle_miri_get_backtrace(
32+
&mut self,
33+
abi: Abi,
34+
link_name: Symbol,
35+
args: &[OpTy<'tcx, Tag>],
36+
dest: &PlaceTy<'tcx, Tag>,
37+
) -> InterpResult<'tcx> {
38+
let this = self.eval_context_mut();
39+
let tcx = this.tcx;
40+
41+
let flags = if let Some(flags_op) = args.get(0) {
42+
this.read_scalar(flags_op)?.to_u64()?
43+
} else {
44+
throw_ub_format!("expected at least 1 argument")
45+
};
46+
2747
let mut data = Vec::new();
2848
for frame in this.active_thread_stack().iter().rev() {
2949
let mut span = frame.current_span();
@@ -49,46 +69,60 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4969
})
5070
.collect();
5171

52-
let len = ptrs.len();
72+
let len: u64 = ptrs.len().try_into().unwrap();
5373

5474
let ptr_ty = tcx.mk_ptr(TypeAndMut { ty: tcx.types.unit, mutbl: Mutability::Mut });
5575

56-
let array_ty = tcx.mk_array(ptr_ty, ptrs.len().try_into().unwrap());
76+
let array_layout = this.layout_of(tcx.mk_array(ptr_ty, len)).unwrap();
5777

58-
// Write pointers into array
59-
let alloc =
60-
this.allocate(this.layout_of(array_ty).unwrap(), MiriMemoryKind::Rust.into())?;
61-
for (i, ptr) in ptrs.into_iter().enumerate() {
62-
let place = this.mplace_index(&alloc, i as u64)?;
63-
this.write_pointer(ptr, &place.into())?;
64-
}
78+
match flags {
79+
// storage for pointers is allocated by miri
80+
// deallocating the slice is undefined behavior with a custom global allocator
81+
0 => {
82+
let &[_flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
83+
84+
let alloc = this.allocate(array_layout, MiriMemoryKind::Rust.into())?;
85+
86+
// Write pointers into array
87+
for (i, ptr) in ptrs.into_iter().enumerate() {
88+
let place = this.mplace_index(&alloc, i as u64)?;
89+
90+
this.write_pointer(ptr, &place.into())?;
91+
}
92+
93+
this.write_immediate(
94+
Immediate::new_slice(Scalar::from_maybe_pointer(alloc.ptr, this), len, this),
95+
dest,
96+
)?;
97+
}
98+
// storage for pointers is allocated by the caller
99+
1 => {
100+
let &[_flags, ref buf] = this.check_shim(abi, Abi::Rust, link_name, args)?;
101+
102+
let buf_place = this.deref_operand(buf)?;
103+
104+
let ptr_layout = this.layout_of(ptr_ty)?;
105+
106+
for (i, ptr) in ptrs.into_iter().enumerate() {
107+
let offset = ptr_layout.size * i.try_into().unwrap();
108+
109+
let op_place =
110+
buf_place.offset(offset, MemPlaceMeta::None, ptr_layout, this)?;
111+
112+
this.write_pointer(ptr, &op_place.into())?;
113+
}
114+
}
115+
_ => throw_unsup_format!("unknown `miri_get_backtrace` flags {}", flags),
116+
};
65117

66-
this.write_immediate(
67-
Immediate::new_slice(
68-
Scalar::from_maybe_pointer(alloc.ptr, this),
69-
len.try_into().unwrap(),
70-
this,
71-
),
72-
dest,
73-
)?;
74118
Ok(())
75119
}
76120

77-
fn handle_miri_resolve_frame(
121+
fn resolve_frame_pointer(
78122
&mut self,
79-
abi: Abi,
80-
link_name: Symbol,
81-
args: &[OpTy<'tcx, Tag>],
82-
dest: &PlaceTy<'tcx, Tag>,
83-
) -> InterpResult<'tcx> {
123+
ptr: &OpTy<'tcx, Tag>,
124+
) -> InterpResult<'tcx, (Instance<'tcx>, Loc, String, String)> {
84125
let this = self.eval_context_mut();
85-
let tcx = this.tcx;
86-
let &[ref ptr, ref flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
87-
88-
let flags = this.read_scalar(flags)?.to_u64()?;
89-
if flags != 0 {
90-
throw_unsup_format!("unknown `miri_resolve_frame` flags {}", flags);
91-
}
92126

93127
let ptr = this.read_pointer(ptr)?;
94128
// Take apart the pointer, we need its pieces.
@@ -101,6 +135,29 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
101135
throw_ub_format!("expected function pointer, found {:?}", ptr);
102136
};
103137

138+
let lo =
139+
this.tcx.sess.source_map().lookup_char_pos(BytePos(offset.bytes().try_into().unwrap()));
140+
141+
let name = fn_instance.to_string();
142+
let filename = lo.file.name.prefer_remapped().to_string();
143+
144+
Ok((fn_instance, lo, name, filename))
145+
}
146+
147+
fn handle_miri_resolve_frame(
148+
&mut self,
149+
abi: Abi,
150+
link_name: Symbol,
151+
args: &[OpTy<'tcx, Tag>],
152+
dest: &PlaceTy<'tcx, Tag>,
153+
) -> InterpResult<'tcx> {
154+
let this = self.eval_context_mut();
155+
let &[ref ptr, ref flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
156+
157+
let flags = this.read_scalar(flags)?.to_u64()?;
158+
159+
let (fn_instance, lo, name, filename) = this.resolve_frame_pointer(ptr)?;
160+
104161
// Reconstruct the original function pointer,
105162
// which we pass to user code.
106163
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
115172
);
116173
}
117174

118-
let pos = BytePos(offset.bytes().try_into().unwrap());
119-
let name = fn_instance.to_string();
120-
121-
let lo = tcx.sess.source_map().lookup_char_pos(pos);
122-
123-
let filename = lo.file.name.prefer_remapped().to_string();
124175
let lineno: u32 = lo.line as u32;
125176
// `lo.col` is 0-based - add 1 to make it 1-based for the caller.
126177
let colno: u32 = lo.col.0 as u32 + 1;
127178

128-
// These are "mutable" allocations as we consider them to be owned by the callee.
129-
let name_alloc = this.allocate_str(&name, MiriMemoryKind::Rust.into(), Mutability::Mut);
130-
let filename_alloc =
131-
this.allocate_str(&filename, MiriMemoryKind::Rust.into(), Mutability::Mut);
132-
let lineno_alloc = Scalar::from_u32(lineno);
133-
let colno_alloc = Scalar::from_u32(colno);
134-
135179
let dest = this.force_allocation(dest)?;
136180
if let ty::Adt(adt, _) = dest.layout.ty.kind() {
137181
if !adt.repr().c() {
@@ -141,10 +185,38 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
141185
}
142186
}
143187

144-
this.write_immediate(name_alloc.to_ref(this), &this.mplace_field(&dest, 0)?.into())?;
145-
this.write_immediate(filename_alloc.to_ref(this), &this.mplace_field(&dest, 1)?.into())?;
146-
this.write_scalar(lineno_alloc, &this.mplace_field(&dest, 2)?.into())?;
147-
this.write_scalar(colno_alloc, &this.mplace_field(&dest, 3)?.into())?;
188+
match flags {
189+
0 => {
190+
// These are "mutable" allocations as we consider them to be owned by the callee.
191+
let name_alloc =
192+
this.allocate_str(&name, MiriMemoryKind::Rust.into(), Mutability::Mut);
193+
let filename_alloc =
194+
this.allocate_str(&filename, MiriMemoryKind::Rust.into(), Mutability::Mut);
195+
196+
this.write_immediate(
197+
name_alloc.to_ref(this),
198+
&this.mplace_field(&dest, 0)?.into(),
199+
)?;
200+
this.write_immediate(
201+
filename_alloc.to_ref(this),
202+
&this.mplace_field(&dest, 1)?.into(),
203+
)?;
204+
}
205+
1 => {
206+
this.write_scalar(
207+
Scalar::from_machine_usize(name.len().try_into().unwrap(), this),
208+
&this.mplace_field(&dest, 0)?.into(),
209+
)?;
210+
this.write_scalar(
211+
Scalar::from_machine_usize(filename.len().try_into().unwrap(), this),
212+
&this.mplace_field(&dest, 1)?.into(),
213+
)?;
214+
}
215+
_ => throw_unsup_format!("unknown `miri_resolve_frame` flags {}", flags),
216+
}
217+
218+
this.write_scalar(Scalar::from_u32(lineno), &this.mplace_field(&dest, 2)?.into())?;
219+
this.write_scalar(Scalar::from_u32(colno), &this.mplace_field(&dest, 3)?.into())?;
148220

149221
// Support a 4-field struct for now - this is deprecated
150222
// and slated for removal.
@@ -154,4 +226,28 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
154226

155227
Ok(())
156228
}
229+
230+
fn handle_miri_resolve_frame_names(
231+
&mut self,
232+
abi: Abi,
233+
link_name: Symbol,
234+
args: &[OpTy<'tcx, Tag>],
235+
) -> InterpResult<'tcx> {
236+
let this = self.eval_context_mut();
237+
238+
let &[ref ptr, ref flags, ref name_ptr, ref filename_ptr] =
239+
this.check_shim(abi, Abi::Rust, link_name, args)?;
240+
241+
let flags = this.read_scalar(flags)?.to_u64()?;
242+
if flags != 0 {
243+
throw_unsup_format!("unknown `miri_resolve_frame_names` flags {}", flags);
244+
}
245+
246+
let (_, _, name, filename) = this.resolve_frame_pointer(ptr)?;
247+
248+
this.memory.write_bytes(this.read_pointer(name_ptr)?, name.bytes())?;
249+
this.memory.write_bytes(this.read_pointer(filename_ptr)?, filename.bytes())?;
250+
251+
Ok(())
252+
}
157253
}

src/shims/foreign_items.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
380380
this.machine.static_roots.push(alloc_id);
381381
}
382382

383+
// Obtains the size of a Miri backtrace. See the README for details.
384+
"miri_backtrace_size" => {
385+
this.handle_miri_backtrace_size(abi, link_name, args, dest)?;
386+
}
387+
383388
// Obtains a Miri backtrace. See the README for details.
384389
"miri_get_backtrace" => {
385390
// `check_shim` happens inside `handle_miri_get_backtrace`.
@@ -392,6 +397,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
392397
this.handle_miri_resolve_frame(abi, link_name, args, dest)?;
393398
}
394399

400+
// Writes the function and file names of a Miri backtrace frame into a user provided buffer. See the README for details.
401+
"miri_resolve_frame_names" => {
402+
this.handle_miri_resolve_frame_names(abi, link_name, args)?;
403+
}
395404

396405
// Standard C allocation
397406
"malloc" => {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern "Rust" {
2+
fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
3+
}
4+
5+
fn main() {
6+
unsafe {
7+
miri_get_backtrace(2, 0 as *mut _); //~ ERROR unsupported operation: unknown `miri_get_backtrace` flags 2
8+
}
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#[repr(C)]
2+
struct MiriFrame {
3+
name_len: usize,
4+
filename_len: usize,
5+
lineno: u32,
6+
colno: u32,
7+
fn_ptr: *mut (),
8+
}
9+
10+
extern "Rust" {
11+
fn miri_backtrace_size(flags: u64) -> usize;
12+
fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
13+
fn miri_resolve_frame(ptr: *mut (), flags: u64) -> MiriFrame;
14+
}
15+
16+
fn main() {
17+
unsafe {
18+
let mut buf = vec![0 as *mut _; miri_backtrace_size(0)];
19+
20+
miri_get_backtrace(1, buf.as_mut_ptr());
21+
22+
// miri_resolve_frame will error from an invalid backtrace before it will from invalid flags
23+
miri_resolve_frame(buf[0], 2); //~ ERROR unsupported operation: unknown `miri_resolve_frame` flags 2
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
extern "Rust" {
2+
fn miri_backtrace_size(flags: u64) -> usize;
3+
fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
4+
fn miri_resolve_frame_names(ptr: *mut (), flags: u64, name_buf: *mut u8, filename_buf: *mut u8);
5+
}
6+
7+
fn main() {
8+
unsafe {
9+
let mut buf = vec![0 as *mut _; miri_backtrace_size(0)];
10+
11+
miri_get_backtrace(1, buf.as_mut_ptr());
12+
13+
// miri_resolve_frame_names will error from an invalid backtrace before it will from invalid flags
14+
miri_resolve_frame_names(buf[0], 2, 0 as *mut _, 0 as *mut _); //~ ERROR unsupported operation: unknown `miri_resolve_frame_names` flags 2
15+
}
16+
}

0 commit comments

Comments
 (0)