Skip to content

Commit a7d791b

Browse files
committed
Auto merge of #66646 - RalfJung:unwind_to_block, r=oli-obk
refactor goto_block and also add unwind_to_block r? @oli-obk
2 parents 797fd92 + 6797d52 commit a7d791b

File tree

6 files changed

+136
-135
lines changed

6 files changed

+136
-135
lines changed

src/librustc_mir/const_eval.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
323323
ecx: &mut InterpCx<'mir, 'tcx, Self>,
324324
instance: ty::Instance<'tcx>,
325325
args: &[OpTy<'tcx>],
326-
dest: Option<PlaceTy<'tcx>>,
327-
ret: Option<mir::BasicBlock>,
326+
ret: Option<(PlaceTy<'tcx>, mir::BasicBlock)>,
328327
_unwind: Option<mir::BasicBlock> // unwinding is not supported in consts
329328
) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
330329
debug!("eval_fn_call: {:?}", instance);
@@ -337,8 +336,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
337336
// Some functions we support even if they are non-const -- but avoid testing
338337
// that for const fn! We certainly do *not* want to actually call the fn
339338
// though, so be sure we return here.
340-
return if ecx.hook_panic_fn(instance, args, dest)? {
341-
ecx.goto_block(ret)?; // fully evaluated and done
339+
return if ecx.hook_panic_fn(instance, args, ret)? {
342340
Ok(None)
343341
} else {
344342
throw_unsup_format!("calling non-const function `{}`", instance)
@@ -364,8 +362,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
364362
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
365363
fn_val: !,
366364
_args: &[OpTy<'tcx>],
367-
_dest: Option<PlaceTy<'tcx>>,
368-
_ret: Option<mir::BasicBlock>,
365+
_ret: Option<(PlaceTy<'tcx>, mir::BasicBlock)>,
366+
_unwind: Option<mir::BasicBlock>
369367
) -> InterpResult<'tcx> {
370368
match fn_val {}
371369
}
@@ -375,11 +373,10 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
375373
span: Span,
376374
instance: ty::Instance<'tcx>,
377375
args: &[OpTy<'tcx>],
378-
dest: Option<PlaceTy<'tcx>>,
379-
_ret: Option<mir::BasicBlock>,
376+
ret: Option<(PlaceTy<'tcx>, mir::BasicBlock)>,
380377
_unwind: Option<mir::BasicBlock>
381378
) -> InterpResult<'tcx> {
382-
if ecx.emulate_intrinsic(span, instance, args, dest)? {
379+
if ecx.emulate_intrinsic(span, instance, args, ret)? {
383380
return Ok(());
384381
}
385382
// An intrinsic that we do not support

src/librustc_mir/interpret/eval_context.rs

+33-5
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,37 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
555555
}
556556
}
557557

558+
/// Jump to the given block.
559+
#[inline]
560+
pub fn go_to_block(&mut self, target: mir::BasicBlock) {
561+
let frame = self.frame_mut();
562+
frame.block = Some(target);
563+
frame.stmt = 0;
564+
}
565+
566+
/// *Return* to the given `target` basic block.
567+
/// Do *not* use for unwinding! Use `unwind_to_block` instead.
568+
///
569+
/// If `target` is `None`, that indicates the function cannot return, so we raise UB.
570+
pub fn return_to_block(&mut self, target: Option<mir::BasicBlock>) -> InterpResult<'tcx> {
571+
if let Some(target) = target {
572+
Ok(self.go_to_block(target))
573+
} else {
574+
throw_ub!(Unreachable)
575+
}
576+
}
577+
578+
/// *Unwind* to the given `target` basic block.
579+
/// Do *not* use for returning! Use `return_to_block` instead.
580+
///
581+
/// If `target` is `None`, that indicates the function does not need cleanup during
582+
/// unwinding, and we will just keep propagating that upwards.
583+
pub fn unwind_to_block(&mut self, target: Option<mir::BasicBlock>) {
584+
let frame = self.frame_mut();
585+
frame.block = target;
586+
frame.stmt = 0;
587+
}
588+
558589
/// Pops the current frame from the stack, deallocating the
559590
/// memory for allocated locals.
560591
///
@@ -630,10 +661,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
630661
if cur_unwinding {
631662
// Follow the unwind edge.
632663
let unwind = next_block.expect("Encounted StackPopCleanup::None when unwinding!");
633-
let next_frame = self.frame_mut();
634-
// If `unwind` is `None`, we'll leave that function immediately again.
635-
next_frame.block = unwind;
636-
next_frame.stmt = 0;
664+
self.unwind_to_block(unwind);
637665
} else {
638666
// Follow the normal return edge.
639667
// Validate the return value. Do this after deallocating so that we catch dangling
@@ -660,7 +688,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
660688

661689
// Jump to new block -- *after* validation so that the spans make more sense.
662690
if let Some(ret) = next_block {
663-
self.goto_block(ret)?;
691+
self.return_to_block(ret)?;
664692
}
665693
}
666694

src/librustc_mir/interpret/intrinsics.rs

+44-31
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ use rustc::ty::layout::{LayoutOf, Primitive, Size};
99
use rustc::ty::subst::SubstsRef;
1010
use rustc::hir::def_id::DefId;
1111
use rustc::ty::TyCtxt;
12-
use rustc::mir::BinOp;
13-
use rustc::mir::interpret::{InterpResult, Scalar, GlobalId, ConstValue};
12+
use rustc::mir::{
13+
self, BinOp,
14+
interpret::{InterpResult, Scalar, GlobalId, ConstValue}
15+
};
1416

1517
use super::{
1618
Machine, PlaceTy, OpTy, InterpCx, ImmTy,
@@ -91,16 +93,20 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
9193
span: Span,
9294
instance: ty::Instance<'tcx>,
9395
args: &[OpTy<'tcx, M::PointerTag>],
94-
dest: Option<PlaceTy<'tcx, M::PointerTag>>,
96+
ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
9597
) -> InterpResult<'tcx, bool> {
9698
let substs = instance.substs;
99+
let intrinsic_name = &*self.tcx.item_name(instance.def_id()).as_str();
97100

98-
// We currently do not handle any diverging intrinsics.
99-
let dest = match dest {
100-
Some(dest) => dest,
101-
None => return Ok(false)
101+
// We currently do not handle any intrinsics that are *allowed* to diverge,
102+
// but `transmute` could lack a return place in case of UB.
103+
let (dest, ret) = match ret {
104+
Some(p) => p,
105+
None => match intrinsic_name {
106+
"transmute" => throw_ub!(Unreachable),
107+
_ => return Ok(false),
108+
}
102109
};
103-
let intrinsic_name = &*self.tcx.item_name(instance.def_id()).as_str();
104110

105111
match intrinsic_name {
106112
"caller_location" => {
@@ -268,34 +274,39 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
268274
// exception from the exception.)
269275
// This is the dual to the special exception for offset-by-0
270276
// in the inbounds pointer offset operation (see the Miri code, `src/operator.rs`).
271-
if a.is_bits() && b.is_bits() {
277+
//
278+
// Control flow is weird because we cannot early-return (to reach the
279+
// `go_to_block` at the end).
280+
let done = if a.is_bits() && b.is_bits() {
272281
let a = a.to_machine_usize(self)?;
273282
let b = b.to_machine_usize(self)?;
274283
if a == b && a != 0 {
275284
self.write_scalar(Scalar::from_int(0, isize_layout.size), dest)?;
276-
return Ok(true);
277-
}
278-
}
285+
true
286+
} else { false }
287+
} else { false };
279288

280-
// General case: we need two pointers.
281-
let a = self.force_ptr(a)?;
282-
let b = self.force_ptr(b)?;
283-
if a.alloc_id != b.alloc_id {
284-
throw_ub_format!(
285-
"ptr_offset_from cannot compute offset of pointers into different \
286-
allocations.",
287-
);
289+
if !done {
290+
// General case: we need two pointers.
291+
let a = self.force_ptr(a)?;
292+
let b = self.force_ptr(b)?;
293+
if a.alloc_id != b.alloc_id {
294+
throw_ub_format!(
295+
"ptr_offset_from cannot compute offset of pointers into different \
296+
allocations.",
297+
);
298+
}
299+
let usize_layout = self.layout_of(self.tcx.types.usize)?;
300+
let a_offset = ImmTy::from_uint(a.offset.bytes(), usize_layout);
301+
let b_offset = ImmTy::from_uint(b.offset.bytes(), usize_layout);
302+
let (val, _overflowed, _ty) = self.overflowing_binary_op(
303+
BinOp::Sub, a_offset, b_offset,
304+
)?;
305+
let pointee_layout = self.layout_of(substs.type_at(0))?;
306+
let val = ImmTy::from_scalar(val, isize_layout);
307+
let size = ImmTy::from_int(pointee_layout.size.bytes(), isize_layout);
308+
self.exact_div(val, size, dest)?;
288309
}
289-
let usize_layout = self.layout_of(self.tcx.types.usize)?;
290-
let a_offset = ImmTy::from_uint(a.offset.bytes(), usize_layout);
291-
let b_offset = ImmTy::from_uint(b.offset.bytes(), usize_layout);
292-
let (val, _overflowed, _ty) = self.overflowing_binary_op(
293-
BinOp::Sub, a_offset, b_offset,
294-
)?;
295-
let pointee_layout = self.layout_of(substs.type_at(0))?;
296-
let val = ImmTy::from_scalar(val, isize_layout);
297-
let size = ImmTy::from_int(pointee_layout.size.bytes(), isize_layout);
298-
self.exact_div(val, size, dest)?;
299310
}
300311

301312
"transmute" => {
@@ -350,6 +361,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
350361
_ => return Ok(false),
351362
}
352363

364+
self.dump_place(*dest);
365+
self.go_to_block(ret);
353366
Ok(true)
354367
}
355368

@@ -360,7 +373,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
360373
&mut self,
361374
instance: ty::Instance<'tcx>,
362375
args: &[OpTy<'tcx, M::PointerTag>],
363-
_dest: Option<PlaceTy<'tcx, M::PointerTag>>,
376+
_ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,
364377
) -> InterpResult<'tcx, bool> {
365378
let def_id = instance.def_id();
366379
if Some(def_id) == self.tcx.lang_items().panic_fn() {

src/librustc_mir/interpret/machine.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
141141
/// Returns either the mir to use for the call, or `None` if execution should
142142
/// just proceed (which usually means this hook did all the work that the
143143
/// called function should usually have done). In the latter case, it is
144-
/// this hook's responsibility to call `goto_block(ret)` to advance the instruction pointer!
144+
/// this hook's responsibility to advance the instruction pointer!
145145
/// (This is to support functions like `__rust_maybe_catch_panic` that neither find a MIR
146146
/// nor just jump to `ret`, but instead push their own stack frame.)
147147
/// Passing `dest`and `ret` in the same `Option` proved very annoying when only one of them
@@ -150,30 +150,28 @@ pub trait Machine<'mir, 'tcx>: Sized {
150150
ecx: &mut InterpCx<'mir, 'tcx, Self>,
151151
instance: ty::Instance<'tcx>,
152152
args: &[OpTy<'tcx, Self::PointerTag>],
153-
dest: Option<PlaceTy<'tcx, Self::PointerTag>>,
154-
ret: Option<mir::BasicBlock>,
155-
unwind: Option<mir::BasicBlock>
153+
ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
154+
unwind: Option<mir::BasicBlock>,
156155
) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>>;
157156

158-
/// Execute `fn_val`. it is the hook's responsibility to advance the instruction
157+
/// Execute `fn_val`. It is the hook's responsibility to advance the instruction
159158
/// pointer as appropriate.
160159
fn call_extra_fn(
161160
ecx: &mut InterpCx<'mir, 'tcx, Self>,
162161
fn_val: Self::ExtraFnVal,
163162
args: &[OpTy<'tcx, Self::PointerTag>],
164-
dest: Option<PlaceTy<'tcx, Self::PointerTag>>,
165-
ret: Option<mir::BasicBlock>,
163+
ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
164+
unwind: Option<mir::BasicBlock>,
166165
) -> InterpResult<'tcx>;
167166

168-
/// Directly process an intrinsic without pushing a stack frame.
169-
/// If this returns successfully, the engine will take care of jumping to the next block.
167+
/// Directly process an intrinsic without pushing a stack frame. It is the hook's
168+
/// responsibility to advance the instruction pointer as appropriate.
170169
fn call_intrinsic(
171170
ecx: &mut InterpCx<'mir, 'tcx, Self>,
172171
span: Span,
173172
instance: ty::Instance<'tcx>,
174173
args: &[OpTy<'tcx, Self::PointerTag>],
175-
dest: Option<PlaceTy<'tcx, Self::PointerTag>>,
176-
ret: Option<mir::BasicBlock>,
174+
ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
177175
unwind: Option<mir::BasicBlock>,
178176
) -> InterpResult<'tcx>;
179177

0 commit comments

Comments
 (0)