Skip to content

Commit 8214134

Browse files
committed
interpret/binary_int_op: avoid dropping to raw its until we determined the sign
1 parent 38104f3 commit 8214134

File tree

4 files changed

+116
-74
lines changed

4 files changed

+116
-74
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use std::assert_matches::assert_matches;
66
use either::{Either, Left, Right};
77

88
use rustc_hir::def::Namespace;
9+
use rustc_middle::mir::interpret::ScalarSizeMismatch;
910
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1011
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter};
11-
use rustc_middle::ty::{ConstInt, Ty, TyCtxt};
12+
use rustc_middle::ty::{ConstInt, ScalarInt, Ty, TyCtxt};
1213
use rustc_middle::{mir, ty};
1314
use rustc_target::abi::{self, Abi, HasDataLayout, Size};
1415

@@ -210,6 +211,12 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
210211
ImmTy { imm: Immediate::Uninit, layout }
211212
}
212213

214+
#[inline]
215+
pub fn from_scalar_int(s: ScalarInt, layout: TyAndLayout<'tcx>) -> Self {
216+
assert_eq!(s.size(), layout.size);
217+
Self::from_scalar(Scalar::from(s), layout)
218+
}
219+
213220
#[inline]
214221
pub fn try_from_uint(i: impl Into<u128>, layout: TyAndLayout<'tcx>) -> Option<Self> {
215222
Some(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout))
@@ -223,7 +230,6 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
223230
pub fn try_from_int(i: impl Into<i128>, layout: TyAndLayout<'tcx>) -> Option<Self> {
224231
Some(Self::from_scalar(Scalar::try_from_int(i, layout.size)?, layout))
225232
}
226-
227233
#[inline]
228234
pub fn from_int(i: impl Into<i128>, layout: TyAndLayout<'tcx>) -> Self {
229235
Self::from_scalar(Scalar::from_int(i, layout.size), layout)
@@ -242,6 +248,20 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
242248
Self::from_scalar(Scalar::from_i8(c as i8), layout)
243249
}
244250

251+
/// Return the immediate as a `ScalarInt`. Ensures that it has the size that the layout of the
252+
/// immediate indcates.
253+
#[inline]
254+
pub fn to_scalar_int(&self) -> InterpResult<'tcx, ScalarInt> {
255+
let s = self.to_scalar().to_scalar_int()?;
256+
if s.size() != self.layout.size {
257+
throw_ub!(ScalarSizeMismatch(ScalarSizeMismatch {
258+
target_size: self.layout.size.bytes(),
259+
data_size: s.size().bytes(),
260+
}));
261+
}
262+
Ok(s)
263+
}
264+
245265
#[inline]
246266
pub fn to_const_int(self) -> ConstInt {
247267
assert!(self.layout.ty.is_integral());

compiler/rustc_const_eval/src/interpret/operator.rs

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_apfloat::{Float, FloatConvert};
22
use rustc_middle::mir;
33
use rustc_middle::mir::interpret::{InterpResult, Scalar};
44
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
5-
use rustc_middle::ty::{self, FloatTy, Ty};
5+
use rustc_middle::ty::{self, FloatTy, ScalarInt, Ty};
66
use rustc_span::symbol::sym;
77
use rustc_target::abi::Abi;
88

@@ -146,14 +146,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
146146
fn binary_int_op(
147147
&self,
148148
bin_op: mir::BinOp,
149-
// passing in raw bits
150-
l: u128,
151-
left_layout: TyAndLayout<'tcx>,
152-
r: u128,
153-
right_layout: TyAndLayout<'tcx>,
149+
left: &ImmTy<'tcx, M::Provenance>,
150+
right: &ImmTy<'tcx, M::Provenance>,
154151
) -> InterpResult<'tcx, (ImmTy<'tcx, M::Provenance>, bool)> {
155152
use rustc_middle::mir::BinOp::*;
156153

154+
// This checks the size, so that we can just assert it below.
155+
let l = left.to_scalar_int()?;
156+
let r = right.to_scalar_int()?;
157+
// Prepare to convert the values to signed or unsigned form.
158+
let l_signed = || l.try_to_int(left.layout.size).unwrap();
159+
let l_unsigned = || l.try_to_uint(left.layout.size).unwrap();
160+
let r_signed = || r.try_to_int(right.layout.size).unwrap();
161+
let r_unsigned = || r.try_to_uint(right.layout.size).unwrap();
162+
157163
let throw_ub_on_overflow = match bin_op {
158164
AddUnchecked => Some(sym::unchecked_add),
159165
SubUnchecked => Some(sym::unchecked_sub),
@@ -165,69 +171,72 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
165171

166172
// Shift ops can have an RHS with a different numeric type.
167173
if matches!(bin_op, Shl | ShlUnchecked | Shr | ShrUnchecked) {
168-
let size = left_layout.size.bits();
174+
let size = left.layout.size.bits();
169175
// The shift offset is implicitly masked to the type size. (This is the one MIR operator
170176
// that does *not* directly map to a single LLVM operation.) Compute how much we
171177
// actually shift and whether there was an overflow due to shifting too much.
172-
let (shift_amount, overflow) = if right_layout.abi.is_signed() {
173-
let shift_amount = self.sign_extend(r, right_layout) as i128;
178+
let (shift_amount, overflow) = if right.layout.abi.is_signed() {
179+
let shift_amount = r_signed();
174180
let overflow = shift_amount < 0 || shift_amount >= i128::from(size);
181+
// Deliberately wrapping `as` casts: shift_amount *can* be negative, but the result
182+
// of the `as` will be equal modulo `size` (since it is a power of two).
175183
let masked_amount = (shift_amount as u128) % u128::from(size);
176-
debug_assert_eq!(overflow, shift_amount != (masked_amount as i128));
184+
assert_eq!(overflow, shift_amount != (masked_amount as i128));
177185
(masked_amount, overflow)
178186
} else {
179-
let shift_amount = r;
187+
let shift_amount = r_unsigned();
180188
let masked_amount = shift_amount % u128::from(size);
181189
(masked_amount, shift_amount != masked_amount)
182190
};
183191
let shift_amount = u32::try_from(shift_amount).unwrap(); // we masked so this will always fit
184192
// Compute the shifted result.
185-
let result = if left_layout.abi.is_signed() {
186-
let l = self.sign_extend(l, left_layout) as i128;
193+
let result = if left.layout.abi.is_signed() {
194+
let l = l_signed();
187195
let result = match bin_op {
188196
Shl | ShlUnchecked => l.checked_shl(shift_amount).unwrap(),
189197
Shr | ShrUnchecked => l.checked_shr(shift_amount).unwrap(),
190198
_ => bug!(),
191199
};
192-
result as u128
200+
ScalarInt::truncate_from_int(result, left.layout.size).0
193201
} else {
194-
match bin_op {
202+
let l = l_unsigned();
203+
let result = match bin_op {
195204
Shl | ShlUnchecked => l.checked_shl(shift_amount).unwrap(),
196205
Shr | ShrUnchecked => l.checked_shr(shift_amount).unwrap(),
197206
_ => bug!(),
198-
}
207+
};
208+
ScalarInt::truncate_from_uint(result, left.layout.size).0
199209
};
200-
let truncated = self.truncate(result, left_layout);
201210

202211
if overflow && let Some(intrinsic_name) = throw_ub_on_overflow {
203212
throw_ub_custom!(
204213
fluent::const_eval_overflow_shift,
205-
val = if right_layout.abi.is_signed() {
206-
(self.sign_extend(r, right_layout) as i128).to_string()
214+
val = if right.layout.abi.is_signed() {
215+
r_signed().to_string()
207216
} else {
208-
r.to_string()
217+
r_unsigned().to_string()
209218
},
210219
name = intrinsic_name
211220
);
212221
}
213222

214-
return Ok((ImmTy::from_uint(truncated, left_layout), overflow));
223+
return Ok((ImmTy::from_scalar_int(result, left.layout), overflow));
215224
}
216225

217226
// For the remaining ops, the types must be the same on both sides
218-
if left_layout.ty != right_layout.ty {
227+
if left.layout.ty != right.layout.ty {
219228
span_bug!(
220229
self.cur_span(),
221230
"invalid asymmetric binary op {bin_op:?}: {l:?} ({l_ty}), {r:?} ({r_ty})",
222-
l_ty = left_layout.ty,
223-
r_ty = right_layout.ty,
231+
l_ty = left.layout.ty,
232+
r_ty = right.layout.ty,
224233
)
225234
}
226235

227-
let size = left_layout.size;
236+
let size = left.layout.size;
228237

229238
// Operations that need special treatment for signed integers
230-
if left_layout.abi.is_signed() {
239+
if left.layout.abi.is_signed() {
231240
let op: Option<fn(&i128, &i128) -> bool> = match bin_op {
232241
Lt => Some(i128::lt),
233242
Le => Some(i128::le),
@@ -236,18 +245,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
236245
_ => None,
237246
};
238247
if let Some(op) = op {
239-
let l = self.sign_extend(l, left_layout) as i128;
240-
let r = self.sign_extend(r, right_layout) as i128;
241-
return Ok((ImmTy::from_bool(op(&l, &r), *self.tcx), false));
248+
return Ok((ImmTy::from_bool(op(&l_signed(), &r_signed()), *self.tcx), false));
242249
}
243250
if bin_op == Cmp {
244-
let l = self.sign_extend(l, left_layout) as i128;
245-
let r = self.sign_extend(r, right_layout) as i128;
246-
return Ok(self.three_way_compare(l, r));
251+
return Ok(self.three_way_compare(l_signed(), r_signed()));
247252
}
248253
let op: Option<fn(i128, i128) -> (i128, bool)> = match bin_op {
249-
Div if r == 0 => throw_ub!(DivisionByZero),
250-
Rem if r == 0 => throw_ub!(RemainderByZero),
254+
Div if r.is_null() => throw_ub!(DivisionByZero),
255+
Rem if r.is_null() => throw_ub!(RemainderByZero),
251256
Div => Some(i128::overflowing_div),
252257
Rem => Some(i128::overflowing_rem),
253258
Add | AddUnchecked => Some(i128::overflowing_add),
@@ -256,8 +261,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
256261
_ => None,
257262
};
258263
if let Some(op) = op {
259-
let l = self.sign_extend(l, left_layout) as i128;
260-
let r = self.sign_extend(r, right_layout) as i128;
264+
let l = l_signed();
265+
let r = r_signed();
261266

262267
// We need a special check for overflowing Rem and Div since they are *UB*
263268
// on overflow, which can happen with "int_min $OP -1".
@@ -272,17 +277,19 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
272277
}
273278

274279
let (result, oflo) = op(l, r);
275-
// This may be out-of-bounds for the result type, so we have to truncate ourselves.
280+
// This may be out-of-bounds for the result type, so we have to truncate.
276281
// If that truncation loses any information, we have an overflow.
277-
let result = result as u128;
278-
let truncated = self.truncate(result, left_layout);
279-
let overflow = oflo || self.sign_extend(truncated, left_layout) != result;
282+
let (result, lossy) = ScalarInt::truncate_from_int(result, left.layout.size);
283+
let overflow = oflo || lossy;
280284
if overflow && let Some(intrinsic_name) = throw_ub_on_overflow {
281285
throw_ub_custom!(fluent::const_eval_overflow, name = intrinsic_name);
282286
}
283-
return Ok((ImmTy::from_uint(truncated, left_layout), overflow));
287+
return Ok((ImmTy::from_scalar_int(result, left.layout), overflow));
284288
}
285289
}
290+
// From here on it's okay to treat everything as unsigned.
291+
let l = l_unsigned();
292+
let r = r_unsigned();
286293

287294
if bin_op == Cmp {
288295
return Ok(self.three_way_compare(l, r));
@@ -297,12 +304,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
297304
Gt => ImmTy::from_bool(l > r, *self.tcx),
298305
Ge => ImmTy::from_bool(l >= r, *self.tcx),
299306

300-
BitOr => ImmTy::from_uint(l | r, left_layout),
301-
BitAnd => ImmTy::from_uint(l & r, left_layout),
302-
BitXor => ImmTy::from_uint(l ^ r, left_layout),
307+
BitOr => ImmTy::from_uint(l | r, left.layout),
308+
BitAnd => ImmTy::from_uint(l & r, left.layout),
309+
BitXor => ImmTy::from_uint(l ^ r, left.layout),
303310

304311
Add | AddUnchecked | Sub | SubUnchecked | Mul | MulUnchecked | Rem | Div => {
305-
assert!(!left_layout.abi.is_signed());
312+
assert!(!left.layout.abi.is_signed());
306313
let op: fn(u128, u128) -> (u128, bool) = match bin_op {
307314
Add | AddUnchecked => u128::overflowing_add,
308315
Sub | SubUnchecked => u128::overflowing_sub,
@@ -316,21 +323,21 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
316323
let (result, oflo) = op(l, r);
317324
// Truncate to target type.
318325
// If that truncation loses any information, we have an overflow.
319-
let truncated = self.truncate(result, left_layout);
320-
let overflow = oflo || truncated != result;
326+
let (result, lossy) = ScalarInt::truncate_from_uint(result, left.layout.size);
327+
let overflow = oflo || lossy;
321328
if overflow && let Some(intrinsic_name) = throw_ub_on_overflow {
322329
throw_ub_custom!(fluent::const_eval_overflow, name = intrinsic_name);
323330
}
324-
return Ok((ImmTy::from_uint(truncated, left_layout), overflow));
331+
return Ok((ImmTy::from_scalar_int(result, left.layout), overflow));
325332
}
326333

327334
_ => span_bug!(
328335
self.cur_span(),
329336
"invalid binary op {:?}: {:?}, {:?} (both {})",
330337
bin_op,
331-
l,
332-
r,
333-
right_layout.ty,
338+
left,
339+
right,
340+
right.layout.ty,
334341
),
335342
};
336343

@@ -427,9 +434,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
427434
right.layout.ty
428435
);
429436

430-
let l = left.to_scalar().to_bits(left.layout.size)?;
431-
let r = right.to_scalar().to_bits(right.layout.size)?;
432-
self.binary_int_op(bin_op, l, left.layout, r, right.layout)
437+
self.binary_int_op(bin_op, left, right)
433438
}
434439
_ if left.layout.ty.is_any_ptr() => {
435440
// The RHS type must be a `pointer` *or an integer type* (for `Offset`).

compiler/rustc_middle/src/mir/interpret/value.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
300300
}
301301
}
302302

303+
#[inline(always)]
304+
pub fn to_scalar_int(self) -> InterpResult<'tcx, ScalarInt> {
305+
self.try_to_int().map_err(|_| err_unsup!(ReadPointerAsInt(None)).into())
306+
}
307+
303308
#[inline(always)]
304309
#[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
305310
pub fn assert_int(self) -> ScalarInt {
@@ -311,16 +316,13 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> {
311316
#[inline]
312317
pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
313318
assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
314-
self.try_to_int()
315-
.map_err(|_| err_unsup!(ReadPointerAsInt(None)))?
316-
.to_bits(target_size)
317-
.map_err(|size| {
318-
err_ub!(ScalarSizeMismatch(ScalarSizeMismatch {
319-
target_size: target_size.bytes(),
320-
data_size: size.bytes(),
321-
}))
322-
.into()
323-
})
319+
self.to_scalar_int()?.to_bits(target_size).map_err(|size| {
320+
err_ub!(ScalarSizeMismatch(ScalarSizeMismatch {
321+
target_size: target_size.bytes(),
322+
data_size: size.bytes(),
323+
}))
324+
.into()
325+
})
324326
}
325327

326328
#[inline(always)]

compiler/rustc_middle/src/ty/consts/int.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,12 @@ impl<D: Decoder> Decodable<D> for ScalarInt {
167167

168168
impl ScalarInt {
169169
pub const TRUE: ScalarInt = ScalarInt { data: 1_u128, size: NonZero::new(1).unwrap() };
170-
171170
pub const FALSE: ScalarInt = ScalarInt { data: 0_u128, size: NonZero::new(1).unwrap() };
172171

172+
fn raw(data: u128, size: Size) -> Self {
173+
Self { data, size: NonZero::new(size.bytes() as u8).unwrap() }
174+
}
175+
173176
#[inline]
174177
pub fn size(self) -> Size {
175178
Size::from_bytes(self.size.get())
@@ -196,7 +199,7 @@ impl ScalarInt {
196199

197200
#[inline]
198201
pub fn null(size: Size) -> Self {
199-
Self { data: 0, size: NonZero::new(size.bytes() as u8).unwrap() }
202+
Self::raw(0, size)
200203
}
201204

202205
#[inline]
@@ -207,11 +210,15 @@ impl ScalarInt {
207210
#[inline]
208211
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
209212
let data = i.into();
210-
if size.truncate(data) == data {
211-
Some(Self { data, size: NonZero::new(size.bytes() as u8).unwrap() })
212-
} else {
213-
None
214-
}
213+
if size.truncate(data) == data { Some(Self::raw(data, size)) } else { None }
214+
}
215+
216+
/// Returns the truncated result, and whether truncation changed the value.
217+
#[inline]
218+
pub fn truncate_from_uint(i: impl Into<u128>, size: Size) -> (Self, bool) {
219+
let data = i.into();
220+
let r = Self::raw(size.truncate(data), size);
221+
(r, r.data != data)
215222
}
216223

217224
#[inline]
@@ -220,12 +227,20 @@ impl ScalarInt {
220227
// `into` performed sign extension, we have to truncate
221228
let truncated = size.truncate(i as u128);
222229
if size.sign_extend(truncated) as i128 == i {
223-
Some(Self { data: truncated, size: NonZero::new(size.bytes() as u8).unwrap() })
230+
Some(Self::raw(truncated, size))
224231
} else {
225232
None
226233
}
227234
}
228235

236+
/// Returns the truncated result, and whether truncation changed the value.
237+
#[inline]
238+
pub fn truncate_from_int(i: impl Into<i128>, size: Size) -> (Self, bool) {
239+
let data = i.into();
240+
let r = Self::raw(size.truncate(data as u128), size);
241+
(r, size.sign_extend(r.data) as i128 != data)
242+
}
243+
229244
#[inline]
230245
pub fn try_from_target_usize(i: impl Into<u128>, tcx: TyCtxt<'_>) -> Option<Self> {
231246
Self::try_from_uint(i, tcx.data_layout.pointer_size)

0 commit comments

Comments
 (0)