Skip to content
Permalink
Browse files

do the variant idx computations on the host (non-overflowing)

  • Loading branch information...
RalfJung committed Sep 16, 2019
1 parent 75fe84f commit 61a28d5c881d81cca9156527851911bb5aa5b818
Showing with 13 additions and 17 deletions.
  1. +6 −8 src/librustc_mir/interpret/operand.rs
  2. +7 −9 src/librustc_mir/interpret/place.rs
@@ -687,24 +687,22 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
(dataful_variant.as_u32() as u128, dataful_variant)
},
Ok(raw_discr) => {
// We need to use machine arithmetic to get the relative variant idx.
let discr_layout = self.layout_of(discr_layout.value.to_int_ty(*self.tcx))?;
let discr_val = ImmTy::from_uint(raw_discr, discr_layout);
// We need to use machine arithmetic.
let niche_start_val = ImmTy::from_uint(niche_start, discr_layout);
let variants_start_val = ImmTy::from_uint(variants_start, discr_layout);
let variant_index_relative_val = self.binary_op(
mir::BinOp::Sub,
discr_val,
niche_start_val,
)?;
let variant_index_val = self.binary_op(
mir::BinOp::Add,
variant_index_relative_val,
variants_start_val,
)?;
let variant_index = variant_index_val
let variant_index_relative = variant_index_relative_val
.to_scalar()?
.assert_bits(discr_val.layout.size);
// Then computing the absolute variant idx should not overflow any more.
let variant_index = variants_start
.checked_add(variant_index_relative)
.expect("oveflow computing absolute variant idx");
// Check if this is in the range that indicates an actual discriminant.
if variants_start <= variant_index && variant_index <= variants_end {
let index = variant_index as usize;
@@ -1060,17 +1060,15 @@ where
variant_index.as_usize() < dest.layout.ty.ty_adt_def().unwrap().variants.len(),
);
if variant_index != dataful_variant {
let discr_layout = self.layout_of(discr_layout.value.to_int_ty(*self.tcx))?;
// We need to use machine arithmetic.
let variants_start = niche_variants.start().as_u32();
let variants_start_val = ImmTy::from_uint(variants_start, discr_layout);
let variant_index_relative = variant_index.as_u32()
.checked_sub(variants_start)
.expect("overflow computing relative variant idx");
// We need to use machine arithmetic when taking into account `niche_start`.
let discr_layout = self.layout_of(discr_layout.value.to_int_ty(*self.tcx))?;
let niche_start_val = ImmTy::from_uint(niche_start, discr_layout);
let variant_index_val = ImmTy::from_uint(variant_index.as_u32(), discr_layout);
let variant_index_relative_val = self.binary_op(
mir::BinOp::Sub,
variant_index_val,
variants_start_val,
)?;
let variant_index_relative_val =
ImmTy::from_uint(variant_index_relative, discr_layout);
let discr_val = self.binary_op(
mir::BinOp::Add,
variant_index_relative_val,

0 comments on commit 61a28d5

Please sign in to comment.
You can’t perform that action at this time.