rust/src/shims/backtrace.rs

155 lines
6.0 KiB
Rust
Raw Normal View History

2021-05-16 04:28:01 -05:00
use crate::rustc_target::abi::LayoutOf as _;
2020-09-24 17:52:17 -05:00
use crate::*;
use rustc_ast::ast::Mutability;
2021-05-16 04:28:01 -05:00
use rustc_middle::ty::{self, TypeAndMut};
use rustc_span::{BytePos, Symbol};
use rustc_target::{abi::Size, spec::abi::Abi};
2020-09-24 17:52:17 -05:00
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_get_backtrace(
&mut self,
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();
let tcx = this.tcx;
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 {
throw_unsup_format!("unknown `miri_get_backtrace` flags {}", flags);
}
let mut data = Vec::new();
for frame in this.active_thread_stack().iter().rev() {
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.
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();
2020-09-24 17:52:17 -05:00
let len = ptrs.len();
2021-05-16 04:28:01 -05:00
let ptr_ty = tcx.mk_ptr(TypeAndMut { ty: tcx.types.unit, mutbl: Mutability::Mut });
2020-09-24 17:52:17 -05:00
let array_ty = tcx.mk_array(ptr_ty, ptrs.len().try_into().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_immediate_to_mplace(ptr.into(), &place)?;
2020-09-24 17:52:17 -05:00
}
2021-05-16 04:28:01 -05:00
this.write_immediate(
Immediate::new_slice(alloc.ptr.into(), len.try_into().unwrap(), this),
dest,
)?;
2020-09-24 17:52:17 -05:00
Ok(())
}
fn handle_miri_resolve_frame(
&mut self,
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();
let tcx = this.tcx;
let &[ref ptr, 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 {
throw_unsup_format!("unknown `miri_resolve_frame` flags {}", flags);
}
2020-11-05 10:09:44 -06:00
let ptr = this.force_ptr(this.read_scalar(ptr)?.check_init()?)?;
2020-09-24 17:52:17 -05:00
2021-05-16 04:28:01 -05:00
let fn_instance = if let Some(GlobalAlloc::Function(instance)) =
this.tcx.get_global_alloc(ptr.alloc_id)
{
2020-09-24 17:52:17 -05:00
instance
} else {
throw_ub_format!("expected function pointer, found {:?}", ptr);
};
// Reconstruct the original function pointer,
// which we pass to user code.
let mut fn_ptr = ptr;
fn_ptr.offset = Size::from_bytes(0);
let fn_ptr = Scalar::Ptr(fn_ptr);
let num_fields = dest.layout.layout.fields.count();
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 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.prefer_remapped().to_string();
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;
2021-05-23 05:26:37 -05:00
// 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);
2020-09-24 17:52:17 -05:00
let lineno_alloc = Scalar::from_u32(lineno);
let colno_alloc = Scalar::from_u32(colno);
2020-09-26 13:40:41 -05:00
let dest = this.force_allocation(dest)?;
if let ty::Adt(adt, _) = dest.layout.ty.kind() {
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
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())?;
2020-10-11 16:57:49 -05:00
// Support a 4-field struct for now - this is deprecated
// and slated for removal.
if num_fields == 5 {
this.write_scalar(fn_ptr, &this.mplace_field(&dest, 4)?.into())?;
}
2020-09-24 17:52:17 -05:00
Ok(())
}
}