Skip to content

Commit d9c27e7

Browse files
committed
Move things around
1 parent 5080ac4 commit d9c27e7

File tree

6 files changed

+120
-83
lines changed

6 files changed

+120
-83
lines changed

src/shims/backtrace.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
use crate::*;
2+
use helpers::check_arg_count;
3+
use rustc_middle::ty::TypeAndMut;
4+
use rustc_ast::ast::Mutability;
5+
use rustc_span::BytePos;
6+
use rustc_target::abi::Size;
7+
use std::convert::TryInto as _;
8+
use crate::rustc_target::abi::LayoutOf as _;
9+
10+
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
11+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
12+
13+
fn handle_miri_get_backtrace(
14+
&mut self,
15+
args: &[OpTy<'tcx, Tag>],
16+
dest: PlaceTy<'tcx, Tag>
17+
) -> InterpResult<'tcx> {
18+
let this = self.eval_context_mut();
19+
let tcx = this.tcx;
20+
let &[flags] = check_arg_count(args)?;
21+
22+
let flags = this.read_scalar(flags)?.to_u64()?;
23+
if flags != 0 {
24+
throw_unsup_format!("unknown `miri_get_backtrace` flags {}", flags);
25+
}
26+
27+
let mut data = Vec::new();
28+
for frame in this.active_thread_stack().iter().rev() {
29+
data.push((frame.instance, frame.current_span().lo()));
30+
}
31+
32+
let ptrs: Vec<_> = data.into_iter().map(|(instance, pos)| {
33+
let mut fn_ptr = this.memory.create_fn_alloc(FnVal::Instance(instance));
34+
fn_ptr.offset = Size::from_bytes(pos.0);
35+
Scalar::Ptr(fn_ptr)
36+
}).collect();
37+
38+
let len = ptrs.len();
39+
40+
let ptr_ty = tcx.mk_ptr(TypeAndMut {
41+
ty: tcx.types.unit,
42+
mutbl: Mutability::Mut
43+
});
44+
45+
let array_ty = tcx.mk_array(ptr_ty, ptrs.len().try_into().unwrap());
46+
47+
// Write pointers into array
48+
let alloc = this.allocate(this.layout_of(array_ty).unwrap(), MiriMemoryKind::Rust.into());
49+
for (i, ptr) in ptrs.into_iter().enumerate() {
50+
let place = this.mplace_index(alloc, i as u64)?;
51+
this.write_immediate_to_mplace(ptr.into(), place)?;
52+
}
53+
54+
this.write_immediate(Immediate::new_slice(alloc.ptr.into(), len.try_into().unwrap(), this), dest)?;
55+
Ok(())
56+
}
57+
58+
fn handle_miri_resolve_frame(
59+
&mut self,
60+
args: &[OpTy<'tcx, Tag>],
61+
dest: PlaceTy<'tcx, Tag>
62+
) -> InterpResult<'tcx> {
63+
let this = self.eval_context_mut();
64+
let tcx = this.tcx;
65+
let &[ptr, flags] = check_arg_count(args)?;
66+
67+
let flags = this.read_scalar(flags)?.to_u64()?;
68+
if flags != 0 {
69+
throw_unsup_format!("unknown `miri_resolve_frame` flags {}", flags);
70+
}
71+
72+
let ptr = match this.read_scalar(ptr)?.check_init()? {
73+
Scalar::Ptr(ptr) => ptr,
74+
Scalar::Raw { .. } => throw_ub_format!("Expected a pointer in `rust_miri_resolve_frame`, found {:?}", ptr)
75+
};
76+
77+
let fn_instance = if let Some(GlobalAlloc::Function(instance)) = this.tcx.get_global_alloc(ptr.alloc_id) {
78+
instance
79+
} else {
80+
throw_ub_format!("expected function pointer, found {:?}", ptr);
81+
};
82+
83+
if dest.layout.layout.fields.count() != 4 {
84+
throw_ub_format!("Bad declaration of miri_resolve_frame - should return a struct with 4 fields");
85+
}
86+
87+
let pos = BytePos(ptr.offset.bytes().try_into().unwrap());
88+
let name = fn_instance.to_string();
89+
90+
let lo = tcx.sess.source_map().lookup_char_pos(pos);
91+
92+
let filename = lo.file.name.to_string();
93+
let lineno: u32 = lo.line as u32;
94+
// `lo.col` is 0-based - add 1 to make it 1-based for the caller.
95+
let colno: u32 = lo.col.0 as u32 + 1;
96+
97+
let name_alloc = this.allocate_str(&name, MiriMemoryKind::Rust.into());
98+
let filename_alloc = this.allocate_str(&filename, MiriMemoryKind::Rust.into());
99+
let lineno_alloc = Scalar::from_u32(lineno);
100+
let colno_alloc = Scalar::from_u32(colno);
101+
102+
let dest = this.force_allocation_maybe_sized(dest, MemPlaceMeta::None)?.0;
103+
104+
this.write_immediate(name_alloc.to_ref(), this.mplace_field(dest, 0)?.into())?;
105+
this.write_immediate(filename_alloc.to_ref(), this.mplace_field(dest, 1)?.into())?;
106+
this.write_scalar(lineno_alloc, this.mplace_field(dest, 2)?.into())?;
107+
this.write_scalar(colno_alloc, this.mplace_field(dest, 3)?.into())?;
108+
Ok(())
109+
}
110+
}

src/shims/foreign_items.rs

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ use log::trace;
55
use rustc_hir::def_id::DefId;
66
use rustc_middle::mir;
77
use rustc_target::{abi::{Align, Size}, spec::PanicStrategy};
8-
use rustc_middle::ty::{self, ParamEnv, TypeAndMut};
9-
use rustc_ast::ast::Mutability;
8+
use rustc_middle::ty;
109
use rustc_apfloat::Float;
1110
use rustc_span::symbol::sym;
12-
use rustc_span::BytePos;
1311

1412
use crate::*;
13+
use super::backtrace::EvalContextExt as _;
1514
use helpers::check_arg_count;
1615

1716
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
@@ -216,84 +215,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
216215

217216
// Obtains a Miri backtrace. See the README for details.
218217
"miri_get_backtrace" => {
219-
let tcx = this.tcx;
220-
let mut data = Vec::new();
221-
for frame in this.active_thread_stack().iter().rev() {
222-
data.push((frame.instance, frame.current_span().lo()));
223-
}
224-
225-
let ptrs: Vec<_> = data.into_iter().map(|(instance, pos)| {
226-
let mut fn_ptr = this.memory.create_fn_alloc(FnVal::Instance(instance));
227-
fn_ptr.offset = Size::from_bytes(pos.0);
228-
Scalar::Ptr(fn_ptr)
229-
}).collect();
230-
231-
let len = ptrs.len();
232-
233-
let ptr_ty = tcx.mk_ptr(TypeAndMut {
234-
ty: tcx.types.unit,
235-
mutbl: Mutability::Mut
236-
});
237-
238-
let array_ty = tcx.mk_array(ptr_ty, ptrs.len().try_into().unwrap());
239-
let array_ty_and_env = ParamEnv::empty().and(array_ty);
240-
241-
// Write pointers into array
242-
let alloc = this.allocate(tcx.layout_of(array_ty_and_env).unwrap(), MiriMemoryKind::Rust.into());
243-
for (i, ptr) in ptrs.into_iter().enumerate() {
244-
let place = this.mplace_index(alloc, i as u64)?;
245-
this.write_immediate_to_mplace(ptr.into(), place)?;
246-
}
247-
248-
this.write_immediate(Immediate::new_slice(alloc.ptr.into(), len.try_into().unwrap(), this), dest)?;
218+
this.handle_miri_get_backtrace(args, dest)?;
249219
}
250220

251221
// Resolves a Miri backtrace frame. See the README for details.
252222
"miri_resolve_frame" => {
253-
let tcx = this.tcx;
254-
let &[ptr, flags] = check_arg_count(args)?;
255-
256-
let flags = this.read_scalar(flags)?.to_u64()?;
257-
if flags != 0 {
258-
throw_ub_format!("Unknown `miri_resolve_frame` flags {}", flags);
259-
}
260-
261-
let ptr = match this.read_scalar(ptr)?.check_init()? {
262-
Scalar::Ptr(ptr) => ptr,
263-
Scalar::Raw { .. } => throw_ub_format!("Expected a pointer in `rust_miri_resolve_frame`, found {:?}", ptr)
264-
};
265-
266-
let fn_instance = if let Some(GlobalAlloc::Function(instance)) = this.tcx.get_global_alloc(ptr.alloc_id) {
267-
instance
268-
} else {
269-
throw_ub_format!("Expect function pointer, found {:?}", ptr);
270-
};
271-
272-
if dest.layout.layout.fields.count() != 4 {
273-
throw_ub_format!("Bad declaration of miri_resolve_frame - should return a struct with 4 fields");
274-
}
275-
276-
let pos = BytePos(ptr.offset.bytes().try_into().unwrap());
277-
let name = fn_instance.to_string();
278-
279-
let lo = tcx.sess.source_map().lookup_char_pos(pos);
280-
281-
let filename = lo.file.name.to_string();
282-
let lineno: u32 = lo.line as u32;
283-
// `lo.col` is 0-based - add 1 to make it 1-based for the caller.
284-
let colno: u32 = lo.col.0 as u32 + 1;
285-
286-
let name_alloc = this.allocate_str(&name, MiriMemoryKind::Rust.into());
287-
let filename_alloc = this.allocate_str(&filename, MiriMemoryKind::Rust.into());
288-
let lineno_alloc = Scalar::from_u32(lineno);
289-
let colno_alloc = Scalar::from_u32(colno);
290-
291-
let dest = this.force_allocation_maybe_sized(dest, MemPlaceMeta::None)?.0;
292-
293-
this.write_immediate(name_alloc.to_ref(), this.mplace_field(dest, 0)?.into())?;
294-
this.write_immediate(filename_alloc.to_ref(), this.mplace_field(dest, 1)?.into())?;
295-
this.write_scalar(lineno_alloc, this.mplace_field(dest, 2)?.into())?;
296-
this.write_scalar(colno_alloc, this.mplace_field(dest, 3)?.into())?;
223+
this.handle_miri_resolve_frame(args, dest)?;
297224
}
298225

299226

src/shims/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
mod backtrace;
22
pub mod foreign_items;
33
pub mod intrinsics;
44
pub mod posix;

tests/compile-fail/backtrace/bad-backtrace-decl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
extern "Rust" {
2-
fn miri_get_backtrace() -> Box<[*mut ()]>;
2+
fn miri_get_backtrace(flags: u64) -> Box<[*mut ()]>;
33
fn miri_resolve_frame(ptr: *mut (), flags: u64);
44
}
55

66
fn main() {
7-
let frames = unsafe { miri_get_backtrace() };
7+
let frames = unsafe { miri_get_backtrace(0) };
88
for frame in frames.into_iter() {
99
unsafe {
1010
miri_resolve_frame(*frame, 0); //~ ERROR Undefined Behavior: Bad declaration of miri_resolve_frame - should return a struct with 4 fields

tests/compile-fail/backtrace/bad-backtrace-version.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ extern "Rust" {
44

55
fn main() {
66
unsafe {
7-
miri_resolve_frame(0 as *mut _, 1); //~ ERROR Undefined Behavior: Unknown `miri_resolve_frame` flags 1
7+
miri_resolve_frame(0 as *mut _, 1); //~ ERROR unsupported operation: unknown `miri_resolve_frame` flags 1
88
}
99
}

tests/run-pass/backtrace-api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// normalize-stderr-test "RUSTLIB/(.*):\d+:\d+ "-> "RUSTLIB/$1:LL:COL "
33

44
extern "Rust" {
5-
fn miri_get_backtrace() -> Box<[*mut ()]>;
5+
fn miri_get_backtrace(flags: u64) -> Box<[*mut ()]>;
66
fn miri_resolve_frame(ptr: *mut (), flags: u64) -> MiriFrame;
77
}
88

@@ -16,7 +16,7 @@ struct MiriFrame {
1616

1717
fn func_a() -> Box<[*mut ()]> { func_b::<u8>() }
1818
fn func_b<T>() -> Box<[*mut ()]> { func_c() }
19-
fn func_c() -> Box<[*mut ()]> { unsafe { miri_get_backtrace() } }
19+
fn func_c() -> Box<[*mut ()]> { unsafe { miri_get_backtrace(0) } }
2020

2121
fn main() {
2222
let mut seen_main = false;

0 commit comments

Comments
 (0)