2020-09-24 17:52:17 -05:00
|
|
|
use crate::*;
|
|
|
|
use rustc_ast::ast::Mutability;
|
2021-09-06 10:05:48 -05:00
|
|
|
use rustc_middle::ty::layout::LayoutOf as _;
|
2022-03-26 13:29:58 -05:00
|
|
|
use rustc_middle::ty::{self, Instance};
|
2022-02-25 00:11:20 -06:00
|
|
|
use rustc_span::{BytePos, Loc, Symbol};
|
2021-06-06 02:25:16 -05:00
|
|
|
use rustc_target::{abi::Size, spec::abi::Abi};
|
2020-09-24 17:52:17 -05:00
|
|
|
|
|
|
|
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
|
|
|
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
2022-02-25 00:11:20 -06:00
|
|
|
fn handle_miri_backtrace_size(
|
2020-09-24 17:52:17 -05:00
|
|
|
&mut self,
|
2021-06-06 02:25:16 -05:00
|
|
|
abi: Abi,
|
|
|
|
link_name: Symbol,
|
2020-09-24 17:52:17 -05:00
|
|
|
args: &[OpTy<'tcx, Tag>],
|
2021-05-16 04:28:01 -05:00
|
|
|
dest: &PlaceTy<'tcx, Tag>,
|
2020-09-24 17:52:17 -05:00
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
let this = self.eval_context_mut();
|
2021-06-06 02:25:16 -05:00
|
|
|
let &[ref flags] = this.check_shim(abi, Abi::Rust, link_name, args)?;
|
2020-09-24 17:52:17 -05:00
|
|
|
|
|
|
|
let flags = this.read_scalar(flags)?.to_u64()?;
|
|
|
|
if flags != 0 {
|
2022-02-25 00:11:20 -06:00
|
|
|
throw_unsup_format!("unknown `miri_backtrace_size` flags {}", flags);
|
2020-09-24 17:52:17 -05:00
|
|
|
}
|
|
|
|
|
2022-02-25 00:11:20 -06:00
|
|
|
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,
|
|
|
|
link_name: Symbol,
|
|
|
|
args: &[OpTy<'tcx, Tag>],
|
|
|
|
dest: &PlaceTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
let this = self.eval_context_mut();
|
|
|
|
let tcx = this.tcx;
|
|
|
|
|
|
|
|
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")
|
|
|
|
};
|
|
|
|
|
2020-09-24 17:52:17 -05:00
|
|
|
let mut data = Vec::new();
|
|
|
|
for frame in this.active_thread_stack().iter().rev() {
|
2020-10-05 16:29:55 -05:00
|
|
|
let mut span = frame.current_span();
|
|
|
|
// Match the behavior of runtime backtrace spans
|
|
|
|
// by using a non-macro span in our backtrace. See `FunctionCx::debug_loc`.
|
|
|
|
if span.from_expansion() && !tcx.sess.opts.debugging_opts.debug_macros {
|
|
|
|
span = rustc_span::hygiene::walk_chain(span, frame.body.span.ctxt())
|
|
|
|
}
|
|
|
|
data.push((frame.instance, span.lo()));
|
2020-09-24 17:52:17 -05:00
|
|
|
}
|
|
|
|
|
2021-05-16 04:28:01 -05:00
|
|
|
let ptrs: Vec<_> = data
|
|
|
|
.into_iter()
|
|
|
|
.map(|(instance, pos)| {
|
|
|
|
// We represent a frame pointer by using the `span.lo` value
|
|
|
|
// as an offset into the function's allocation. This gives us an
|
|
|
|
// opaque pointer that we can return to user code, and allows us
|
|
|
|
// to reconstruct the needed frame information in `handle_miri_resolve_frame`.
|
|
|
|
// Note that we never actually read or write anything from/to this pointer -
|
|
|
|
// all of the data is represented by the pointer value itself.
|
2022-04-03 15:12:52 -05:00
|
|
|
let fn_ptr = this.create_fn_alloc_ptr(FnVal::Instance(instance));
|
2021-07-15 13:33:08 -05:00
|
|
|
fn_ptr.wrapping_offset(Size::from_bytes(pos.0), this)
|
2021-05-16 04:28:01 -05:00
|
|
|
})
|
|
|
|
.collect();
|
2020-09-24 17:52:17 -05:00
|
|
|
|
2022-02-25 00:11:20 -06:00
|
|
|
let len: u64 = ptrs.len().try_into().unwrap();
|
2020-09-24 17:52:17 -05:00
|
|
|
|
2022-03-26 13:29:58 -05:00
|
|
|
let ptr_ty = this.machine.layouts.mut_raw_ptr.ty;
|
2022-02-25 00:11:20 -06:00
|
|
|
let array_layout = this.layout_of(tcx.mk_array(ptr_ty, len)).unwrap();
|
2020-09-24 17:52:17 -05:00
|
|
|
|
2022-02-25 00:11:20 -06:00
|
|
|
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),
|
|
|
|
};
|
2020-09-24 17:52:17 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-25 00:11:20 -06:00
|
|
|
fn resolve_frame_pointer(
|
2020-09-24 17:52:17 -05:00
|
|
|
&mut self,
|
2022-02-25 00:11:20 -06:00
|
|
|
ptr: &OpTy<'tcx, Tag>,
|
|
|
|
) -> InterpResult<'tcx, (Instance<'tcx>, Loc, String, String)> {
|
2020-09-24 17:52:17 -05:00
|
|
|
let this = self.eval_context_mut();
|
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let ptr = this.read_pointer(ptr)?;
|
|
|
|
// Take apart the pointer, we need its pieces.
|
2022-04-03 15:12:52 -05:00
|
|
|
let (alloc_id, offset, ptr) = this.ptr_get_alloc_id(ptr)?;
|
2020-09-24 17:52:17 -05:00
|
|
|
|
2021-07-15 13:33:08 -05:00
|
|
|
let fn_instance =
|
|
|
|
if let Some(GlobalAlloc::Function(instance)) = this.tcx.get_global_alloc(alloc_id) {
|
|
|
|
instance
|
|
|
|
} else {
|
|
|
|
throw_ub_format!("expected function pointer, found {:?}", ptr);
|
|
|
|
};
|
2020-09-24 17:52:17 -05:00
|
|
|
|
2022-02-25 00:11:20 -06:00
|
|
|
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)?;
|
|
|
|
|
2020-10-10 12:07:57 -05:00
|
|
|
// Reconstruct the original function pointer,
|
|
|
|
// which we pass to user code.
|
2022-04-03 15:12:52 -05:00
|
|
|
let fn_ptr = this.create_fn_alloc_ptr(FnVal::Instance(fn_instance));
|
2020-10-10 12:07:57 -05:00
|
|
|
|
2022-03-07 18:57:44 -06:00
|
|
|
let num_fields = dest.layout.fields.count();
|
2020-10-10 12:07:57 -05:00
|
|
|
|
2020-10-11 10:05:14 -05:00
|
|
|
if !(4..=5).contains(&num_fields) {
|
2020-10-11 16:57:49 -05:00
|
|
|
// Always mention 5 fields, since the 4-field struct
|
|
|
|
// is deprecated and slated for removal.
|
2021-05-16 04:28:01 -05:00
|
|
|
throw_ub_format!(
|
|
|
|
"bad declaration of miri_resolve_frame - should return a struct with 5 fields"
|
|
|
|
);
|
2020-09-24 17:52:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-09-26 13:40:41 -05:00
|
|
|
let dest = this.force_allocation(dest)?;
|
|
|
|
if let ty::Adt(adt, _) = dest.layout.ty.kind() {
|
2022-03-12 08:46:10 -06:00
|
|
|
if !adt.repr().c() {
|
2021-05-16 04:28:01 -05:00
|
|
|
throw_ub_format!(
|
|
|
|
"miri_resolve_frame must be declared with a `#[repr(C)]` return type"
|
|
|
|
);
|
2020-09-26 13:40:41 -05:00
|
|
|
}
|
|
|
|
}
|
2020-09-24 17:52:17 -05:00
|
|
|
|
2022-02-25 00:11:20 -06:00
|
|
|
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())?;
|
2020-10-10 12:07:57 -05:00
|
|
|
|
2020-10-11 16:57:49 -05:00
|
|
|
// Support a 4-field struct for now - this is deprecated
|
|
|
|
// and slated for removal.
|
2020-10-10 12:07:57 -05:00
|
|
|
if num_fields == 5 {
|
2021-07-15 13:33:08 -05:00
|
|
|
this.write_pointer(fn_ptr, &this.mplace_field(&dest, 4)?.into())?;
|
2020-10-10 12:07:57 -05:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:52:17 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-25 00:11:20 -06:00
|
|
|
|
|
|
|
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)?;
|
|
|
|
|
2022-04-03 15:12:52 -05:00
|
|
|
this.write_bytes_ptr(this.read_pointer(name_ptr)?, name.bytes())?;
|
|
|
|
this.write_bytes_ptr(this.read_pointer(filename_ptr)?, filename.bytes())?;
|
2022-02-25 00:11:20 -06:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-09-24 17:52:17 -05:00
|
|
|
}
|