Skip to content

Commit 60e1c0c

Browse files
authored
Merge pull request #4172 from RalfJung/miri_get_backtrace
miri_get_backtrace: stop supporting the v0 protocol
2 parents 6b656cc + 726c9d8 commit 60e1c0c

File tree

4 files changed

+14
-51
lines changed

4 files changed

+14
-51
lines changed

Diff for: src/tools/miri/src/helpers.rs

-16
Original file line numberDiff line numberDiff line change
@@ -1211,22 +1211,6 @@ where
12111211
throw_ub_format!("incorrect number of arguments: got {}, expected {}", args.len(), N)
12121212
}
12131213

1214-
/// Check that the number of args is at least the minimum what we expect.
1215-
/// FIXME: Remove this function, use varargs and `check_min_vararg_count` instead.
1216-
pub fn check_min_arg_count<'a, 'tcx, const N: usize>(
1217-
name: &'a str,
1218-
args: &'a [OpTy<'tcx>],
1219-
) -> InterpResult<'tcx, &'a [OpTy<'tcx>; N]> {
1220-
if let Some((ops, _)) = args.split_first_chunk() {
1221-
return interp_ok(ops);
1222-
}
1223-
throw_ub_format!(
1224-
"incorrect number of arguments for `{name}`: got {}, expected at least {}",
1225-
args.len(),
1226-
N
1227-
)
1228-
}
1229-
12301214
/// Check that the number of varargs is at least the minimum what we expect.
12311215
/// Fixed args should not be included.
12321216
/// Use `check_vararg_fixed_arg_count` to extract the varargs slice from full function arguments.

Diff for: src/tools/miri/src/shims/backtrace.rs

+8-32
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_middle::ty::{self, Instance, Ty};
44
use rustc_span::{BytePos, Loc, Symbol, hygiene};
55
use rustc_target::callconv::{Conv, FnAbi};
66

7-
use crate::helpers::check_min_arg_count;
87
use crate::*;
98

109
impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
@@ -34,13 +33,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
3433
abi: &FnAbi<'tcx, Ty<'tcx>>,
3534
link_name: Symbol,
3635
args: &[OpTy<'tcx>],
37-
dest: &MPlaceTy<'tcx>,
3836
) -> InterpResult<'tcx> {
3937
let this = self.eval_context_mut();
40-
let tcx = this.tcx;
38+
let ptr_ty = this.machine.layouts.mut_raw_ptr.ty;
39+
let ptr_layout = this.layout_of(ptr_ty)?;
40+
41+
let [flags, buf] = this.check_shim(abi, Conv::Rust, link_name, args)?;
4142

42-
let [flags] = check_min_arg_count("miri_get_backtrace", args)?;
4343
let flags = this.read_scalar(flags)?.to_u64()?;
44+
let buf_place = this.deref_pointer_as(buf, ptr_layout)?;
4445

4546
let mut data = Vec::new();
4647
for frame in this.active_thread_stack().iter().rev() {
@@ -63,43 +64,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
6364
})
6465
.collect();
6566

66-
let len: u64 = ptrs.len().try_into().unwrap();
67-
68-
let ptr_ty = this.machine.layouts.mut_raw_ptr.ty;
69-
let array_layout = this.layout_of(Ty::new_array(tcx.tcx, ptr_ty, len)).unwrap();
70-
7167
match flags {
72-
// storage for pointers is allocated by miri
73-
// deallocating the slice is undefined behavior with a custom global allocator
7468
0 => {
75-
let [_flags] = this.check_shim(abi, Conv::Rust, link_name, args)?;
76-
77-
let alloc = this.allocate(array_layout, MiriMemoryKind::Rust.into())?;
78-
79-
// Write pointers into array
80-
for (i, ptr) in ptrs.into_iter().enumerate() {
81-
let place = this.project_index(&alloc, i as u64)?;
82-
83-
this.write_pointer(ptr, &place)?;
84-
}
85-
86-
this.write_immediate(Immediate::new_slice(alloc.ptr(), len, this), dest)?;
69+
throw_unsup_format!("miri_get_backtrace: v0 is not supported any more");
8770
}
88-
// storage for pointers is allocated by the caller
89-
1 => {
90-
let [_flags, buf] = this.check_shim(abi, Conv::Rust, link_name, args)?;
91-
92-
let ptr_layout = this.layout_of(ptr_ty)?;
93-
let buf_place = this.deref_pointer_as(buf, ptr_layout)?;
94-
71+
1 =>
9572
for (i, ptr) in ptrs.into_iter().enumerate() {
9673
let offset = ptr_layout.size.checked_mul(i.try_into().unwrap(), this).unwrap();
9774

9875
let op_place = buf_place.offset(offset, ptr_layout, this)?;
9976

10077
this.write_pointer(ptr, &op_place)?;
101-
}
102-
}
78+
},
10379
_ => throw_unsup_format!("unknown `miri_get_backtrace` flags {}", flags),
10480
};
10581

Diff for: src/tools/miri/src/shims/foreign_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
357357
// Obtains a Miri backtrace. See the README for details.
358358
"miri_get_backtrace" => {
359359
// `check_shim` happens inside `handle_miri_get_backtrace`.
360-
this.handle_miri_get_backtrace(abi, link_name, args, dest)?;
360+
this.handle_miri_get_backtrace(abi, link_name, args)?;
361361
}
362362
// Resolves a Miri backtrace frame. See the README for details.
363363
"miri_resolve_frame" => {

Diff for: src/tools/miri/tests/fail/shims/backtrace/bad-backtrace-decl.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
extern "Rust" {
2-
fn miri_get_backtrace(flags: u64) -> Box<[*mut ()]>;
2+
fn miri_backtrace_size(flags: u64) -> usize;
3+
fn miri_get_backtrace(flags: u64, buf: *mut *mut ());
34
fn miri_resolve_frame(ptr: *mut (), flags: u64);
45
}
56

67
fn main() {
7-
let frames = unsafe { miri_get_backtrace(0) };
8+
let size = unsafe { miri_backtrace_size(0) };
9+
let mut frames = vec![std::ptr::null_mut(); size];
10+
unsafe { miri_get_backtrace(1, frames.as_mut_ptr()) };
811
for frame in frames.iter() {
912
unsafe {
1013
miri_resolve_frame(*frame, 0); //~ ERROR: Undefined Behavior: bad declaration of miri_resolve_frame - should return a struct with 5 fields

0 commit comments

Comments
 (0)