Skip to content

Commit b5c70e9

Browse files
committed
Auto merge of #911 - RalfJung:typed-op, r=RalfJung
Adjust for typed binops
2 parents bfd6181 + d7ff7cc commit b5c70e9

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1cdcea920e56a5d0587307a4c9cf8fff5c77c4bc
1+
2111aed0a38c819acb140c7153e9366964a37f2f

src/machine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rand::rngs::StdRng;
1010
use syntax::attr;
1111
use syntax::symbol::sym;
1212
use rustc::hir::def_id::DefId;
13-
use rustc::ty::{self, layout::{Size, LayoutOf}, TyCtxt};
13+
use rustc::ty::{self, Ty, TyCtxt, layout::{Size, LayoutOf}};
1414
use rustc::mir;
1515

1616
use crate::*;
@@ -191,7 +191,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
191191
bin_op: mir::BinOp,
192192
left: ImmTy<'tcx, Tag>,
193193
right: ImmTy<'tcx, Tag>,
194-
) -> InterpResult<'tcx, (Scalar<Tag>, bool)> {
194+
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
195195
ecx.binary_ptr_op(bin_op, left, right)
196196
}
197197

src/operator.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub trait EvalContextExt<'tcx> {
1414
bin_op: mir::BinOp,
1515
left: ImmTy<'tcx, Tag>,
1616
right: ImmTy<'tcx, Tag>,
17-
) -> InterpResult<'tcx, (Scalar<Tag>, bool)>;
17+
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;
1818

1919
fn ptr_eq(
2020
&self,
@@ -43,7 +43,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
4343
bin_op: mir::BinOp,
4444
left: ImmTy<'tcx, Tag>,
4545
right: ImmTy<'tcx, Tag>,
46-
) -> InterpResult<'tcx, (Scalar<Tag>, bool)> {
46+
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
4747
use rustc::mir::BinOp::*;
4848

4949
trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
@@ -59,7 +59,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
5959
self.ptr_eq(left2.not_undef()?, right2.not_undef()?)?,
6060
_ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
6161
};
62-
(Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false)
62+
(Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false, self.tcx.types.bool)
6363
}
6464

6565
Lt | Le | Gt | Ge => {
@@ -74,7 +74,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
7474
Ge => left >= right,
7575
_ => bug!("We already established it has to be one of these operators."),
7676
};
77-
(Scalar::from_bool(res), false)
77+
(Scalar::from_bool(res), false, self.tcx.types.bool)
7878
}
7979

8080
Offset => {
@@ -87,7 +87,7 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
8787
pointee_ty,
8888
right.to_scalar()?.to_isize(self)?,
8989
)?;
90-
(ptr, false)
90+
(ptr, false, left.layout.ty)
9191
}
9292

9393
_ => bug!("Invalid operator on pointers: {:?}", bin_op)

src/shims/intrinsics.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc::ty::layout::{self, LayoutOf, Size, Align};
55
use rustc::ty;
66

77
use crate::{
8-
PlaceTy, OpTy, ImmTy, Immediate, Scalar, Tag,
8+
PlaceTy, OpTy, Immediate, Scalar, Tag,
99
OperatorEvalContextExt
1010
};
1111

@@ -120,7 +120,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
120120
this.memory().check_ptr_access(place.ptr, place.layout.size, align)?;
121121

122122
// binary_op will bail if either of them is not a scalar
123-
let (eq, _) = this.binary_op(mir::BinOp::Eq, old, expect_old)?;
123+
let eq = this.overflowing_binary_op(mir::BinOp::Eq, old, expect_old)?.0;
124124
let res = Immediate::ScalarPair(old.to_scalar_or_undef(), eq.into());
125125
this.write_immediate(res, dest)?; // old value is returned
126126
// update ptr depending on comparison
@@ -183,13 +183,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
183183
_ => bug!(),
184184
};
185185
// Atomics wrap around on overflow.
186-
let (val, _overflowed) = this.binary_op(op, old, rhs)?;
186+
let val = this.binary_op(op, old, rhs)?;
187187
let val = if neg {
188-
this.unary_op(mir::UnOp::Not, ImmTy::from_scalar(val, old.layout))?
188+
this.unary_op(mir::UnOp::Not, val)?
189189
} else {
190190
val
191191
};
192-
this.write_scalar(val, place.into())?;
192+
this.write_immediate(*val, place.into())?;
193193
}
194194

195195
"breakpoint" => unimplemented!(), // halt miri
@@ -312,7 +312,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
312312
let a = this.read_immediate(args[0])?;
313313
let b = this.read_immediate(args[1])?;
314314
// check x % y != 0
315-
if this.binary_op(mir::BinOp::Rem, a, b)?.0.to_bits(dest.layout.size)? != 0 {
315+
if this.overflowing_binary_op(mir::BinOp::Rem, a, b)?.0.to_bits(dest.layout.size)? != 0 {
316316
// Check if `b` is -1, which is the "min_value / -1" case.
317317
let minus1 = Scalar::from_int(-1, dest.layout.size);
318318
return Err(if b.to_scalar().unwrap() == minus1 {
@@ -515,7 +515,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
515515
"unchecked_mul" => mir::BinOp::Mul,
516516
_ => bug!(),
517517
};
518-
let (res, overflowed) = this.binary_op(op, l, r)?;
518+
let (res, overflowed, _ty) = this.overflowing_binary_op(op, l, r)?;
519519
if overflowed {
520520
throw_ub_format!("Overflowing arithmetic in {}", intrinsic_name.get());
521521
}

0 commit comments

Comments
 (0)