Skip to content

Commit 79c9c80

Browse files
committed
Use strict ops instead of checked ops
1 parent 66ad792 commit 79c9c80

File tree

14 files changed

+58
-68
lines changed

14 files changed

+58
-68
lines changed

src/tools/miri/src/alloc_addresses/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl GlobalStateInner {
9797
fn align_addr(addr: u64, align: u64) -> u64 {
9898
match addr % align {
9999
0 => addr,
100-
rem => addr.checked_add(align).unwrap() - rem,
100+
rem => addr.strict_add(align) - rem,
101101
}
102102
}
103103

src/tools/miri/src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ pub fn create_ecx<'tcx>(
303303
let mut argvs = Vec::<Immediate<Provenance>>::with_capacity(config.args.len());
304304
for arg in config.args.iter() {
305305
// Make space for `0` terminator.
306-
let size = u64::try_from(arg.len()).unwrap().checked_add(1).unwrap();
306+
let size = u64::try_from(arg.len()).unwrap().strict_add(1);
307307
let arg_type = Ty::new_array(tcx, tcx.types.u8, size);
308308
let arg_place =
309309
ecx.allocate(ecx.layout_of(arg_type)?, MiriMemoryKind::Machine.into())?;

src/tools/miri/src/helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
963963
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
964964
// terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
965965
let string_length = u64::try_from(c_str.len()).unwrap();
966-
let string_length = string_length.checked_add(1).unwrap();
966+
let string_length = string_length.strict_add(1);
967967
if size < string_length {
968968
return Ok((false, string_length));
969969
}
@@ -1027,7 +1027,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
10271027
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required
10281028
// 0x0000 terminator to memory would cause an out-of-bounds access.
10291029
let string_length = u64::try_from(wide_str.len()).unwrap();
1030-
let string_length = string_length.checked_add(1).unwrap();
1030+
let string_length = string_length.strict_add(1);
10311031
if size < string_length {
10321032
return Ok((false, string_length));
10331033
}
@@ -1391,7 +1391,7 @@ pub(crate) fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
13911391
if success {
13921392
// If the function succeeds, the return value is the number of characters stored in the target buffer,
13931393
// not including the terminating null character.
1394-
u32::try_from(len.checked_sub(1).unwrap()).unwrap()
1394+
u32::try_from(len.strict_sub(1)).unwrap()
13951395
} else {
13961396
// If the target buffer was not large enough to hold the data, the return value is the buffer size, in characters,
13971397
// required to hold the string and its terminating null character.

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
402402
});
403403
let (_, addr) = ptr.into_parts(); // we know the offset is absolute
404404
// Cannot panic since `align` is a power of 2 and hence non-zero.
405-
if addr.bytes().checked_rem(align.bytes()).unwrap() != 0 {
405+
if addr.bytes().strict_rem(align.bytes()) != 0 {
406406
throw_unsup_format!(
407407
"`miri_promise_symbolic_alignment`: pointer is not actually aligned"
408408
);
@@ -714,7 +714,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
714714
// That is probably overly cautious, but there also is no fundamental
715715
// reason to have `strcpy` destroy pointer provenance.
716716
// This reads at least 1 byte, so we are already enforcing that this is a valid pointer.
717-
let n = this.read_c_str(ptr_src)?.len().checked_add(1).unwrap();
717+
let n = this.read_c_str(ptr_src)?.len().strict_add(1);
718718
this.mem_copy(ptr_src, ptr_dest, Size::from_bytes(n), true)?;
719719
this.write_pointer(ptr_dest, dest)?;
720720
}

src/tools/miri/src/shims/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
165165
("tm_hour", dt.hour().into()),
166166
("tm_mday", dt.day().into()),
167167
("tm_mon", dt.month0().into()),
168-
("tm_year", dt.year().checked_sub(1900).unwrap().into()),
168+
("tm_year", dt.year().strict_sub(1900).into()),
169169
("tm_wday", dt.weekday().num_days_from_sunday().into()),
170170
("tm_yday", dt.ordinal0().into()),
171171
("tm_isdst", tm_isdst),

src/tools/miri/src/shims/unix/env.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@ impl<'tcx> UnixEnvVars<'tcx> {
8181
return Ok(None);
8282
};
8383
// The offset is used to strip the "{name}=" part of the string.
84-
let var_ptr = var_ptr.offset(
85-
Size::from_bytes(u64::try_from(name.len()).unwrap().checked_add(1).unwrap()),
86-
ecx,
87-
)?;
84+
let var_ptr = var_ptr
85+
.offset(Size::from_bytes(u64::try_from(name.len()).unwrap().strict_add(1)), ecx)?;
8886
Ok(Some(var_ptr))
8987
}
9088

src/tools/miri/src/shims/unix/fd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl FdTable {
240240
let new_fd = candidate_new_fd.unwrap_or_else(|| {
241241
// find_map ran out of BTreeMap entries before finding a free fd, use one plus the
242242
// maximum fd in the map
243-
self.fds.last_key_value().map(|(fd, _)| fd.checked_add(1).unwrap()).unwrap_or(min_fd)
243+
self.fds.last_key_value().map(|(fd, _)| fd.strict_add(1)).unwrap_or(min_fd)
244244
});
245245

246246
self.fds.try_insert(new_fd, file_handle).unwrap();

src/tools/miri/src/shims/unix/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl FileDescription for SocketPair {
116116
};
117117
let mut writebuf = writebuf.borrow_mut();
118118
let data_size = writebuf.buf.len();
119-
let available_space = MAX_SOCKETPAIR_BUFFER_CAPACITY.checked_sub(data_size).unwrap();
119+
let available_space = MAX_SOCKETPAIR_BUFFER_CAPACITY.strict_sub(data_size);
120120
if available_space == 0 {
121121
if self.is_nonblock {
122122
// Non-blocking socketpair with a full buffer.

src/tools/miri/src/shims/windows/foreign_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
647647
// If the function succeeds, the return value is the length of the string that
648648
// is copied to the buffer, in characters, not including the terminating null
649649
// character.
650-
this.write_int(size_needed.checked_sub(1).unwrap(), dest)?;
650+
this.write_int(size_needed.strict_sub(1), dest)?;
651651
} else {
652652
// If the buffer is too small to hold the module name, the string is truncated
653653
// to nSize characters including the terminating null character, the function
@@ -689,7 +689,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
689689
throw_unsup_format!("FormatMessageW: buffer not big enough");
690690
}
691691
// The return value is the number of characters stored *excluding* the null terminator.
692-
this.write_int(length.checked_sub(1).unwrap(), dest)?;
692+
this.write_int(length.strict_sub(1), dest)?;
693693
}
694694

695695
// Incomplete shims that we "stub out" just to get pre-main initialization code to work.

src/tools/miri/src/shims/windows/handle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Handle {
7474
/// None of this layout is guaranteed to applications by Windows or Miri.
7575
fn to_packed(self) -> u32 {
7676
let disc_size = Self::packed_disc_size();
77-
let data_size = u32::BITS.checked_sub(disc_size).unwrap();
77+
let data_size = u32::BITS.strict_sub(disc_size);
7878

7979
let discriminant = self.discriminant();
8080
let data = self.data();
@@ -103,7 +103,7 @@ impl Handle {
103103
/// see docs for `to_packed`
104104
fn from_packed(handle: u32) -> Option<Self> {
105105
let disc_size = Self::packed_disc_size();
106-
let data_size = u32::BITS.checked_sub(disc_size).unwrap();
106+
let data_size = u32::BITS.strict_sub(disc_size);
107107

108108
// the lower `data_size` bits of this mask are 1
109109
#[allow(clippy::arithmetic_side_effects)] // cannot overflow

src/tools/miri/src/shims/x86/avx2.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
7575
assert_eq!(dest_len, mask_len);
7676

7777
let mask_item_size = mask.layout.field(this, 0).size;
78-
let high_bit_offset = mask_item_size.bits().checked_sub(1).unwrap();
78+
let high_bit_offset = mask_item_size.bits().strict_sub(1);
7979

8080
let scale = this.read_scalar(scale)?.to_i8()?;
8181
if !matches!(scale, 1 | 2 | 4 | 8) {
@@ -93,8 +93,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
9393
let offset =
9494
i64::try_from(this.read_scalar(&offset)?.to_int(offset.layout.size)?)
9595
.unwrap();
96-
let ptr = slice
97-
.wrapping_signed_offset(offset.checked_mul(scale).unwrap(), &this.tcx);
96+
let ptr = slice.wrapping_signed_offset(offset.strict_mul(scale), &this.tcx);
9897
// Unaligned copy, which is what we want.
9998
this.mem_copy(
10099
ptr,
@@ -127,19 +126,19 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
127126
assert_eq!(dest_len.checked_mul(2).unwrap(), left_len);
128127

129128
for i in 0..dest_len {
130-
let j1 = i.checked_mul(2).unwrap();
129+
let j1 = i.strict_mul(2);
131130
let left1 = this.read_scalar(&this.project_index(&left, j1)?)?.to_i16()?;
132131
let right1 = this.read_scalar(&this.project_index(&right, j1)?)?.to_i16()?;
133132

134-
let j2 = j1.checked_add(1).unwrap();
133+
let j2 = j1.strict_add(1);
135134
let left2 = this.read_scalar(&this.project_index(&left, j2)?)?.to_i16()?;
136135
let right2 = this.read_scalar(&this.project_index(&right, j2)?)?.to_i16()?;
137136

138137
let dest = this.project_index(&dest, i)?;
139138

140139
// Multiplications are i16*i16->i32, which will not overflow.
141-
let mul1 = i32::from(left1).checked_mul(right1.into()).unwrap();
142-
let mul2 = i32::from(left2).checked_mul(right2.into()).unwrap();
140+
let mul1 = i32::from(left1).strict_mul(right1.into());
141+
let mul2 = i32::from(left2).strict_mul(right2.into());
143142
// However, this addition can overflow in the most extreme case
144143
// (-0x8000)*(-0x8000)+(-0x8000)*(-0x8000) = 0x80000000
145144
let res = mul1.wrapping_add(mul2);
@@ -164,19 +163,19 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
164163
assert_eq!(dest_len.checked_mul(2).unwrap(), left_len);
165164

166165
for i in 0..dest_len {
167-
let j1 = i.checked_mul(2).unwrap();
166+
let j1 = i.strict_mul(2);
168167
let left1 = this.read_scalar(&this.project_index(&left, j1)?)?.to_u8()?;
169168
let right1 = this.read_scalar(&this.project_index(&right, j1)?)?.to_i8()?;
170169

171-
let j2 = j1.checked_add(1).unwrap();
170+
let j2 = j1.strict_add(1);
172171
let left2 = this.read_scalar(&this.project_index(&left, j2)?)?.to_u8()?;
173172
let right2 = this.read_scalar(&this.project_index(&right, j2)?)?.to_i8()?;
174173

175174
let dest = this.project_index(&dest, i)?;
176175

177176
// Multiplication of a u8 and an i8 into an i16 cannot overflow.
178-
let mul1 = i16::from(left1).checked_mul(right1.into()).unwrap();
179-
let mul2 = i16::from(left2).checked_mul(right2.into()).unwrap();
177+
let mul1 = i16::from(left1).strict_mul(right1.into());
178+
let mul2 = i16::from(left2).strict_mul(right2.into());
180179
let res = mul1.saturating_add(mul2);
181180

182181
this.write_scalar(Scalar::from_i16(res), &dest)?;
@@ -309,7 +308,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
309308

310309
for i in 0..2 {
311310
let dest = this.project_index(&dest, i)?;
312-
let src = match (imm >> i.checked_mul(4).unwrap()) & 0b11 {
311+
let src = match (imm >> i.strict_mul(4)) & 0b11 {
313312
0 => this.project_index(&left, 0)?,
314313
1 => this.project_index(&left, 1)?,
315314
2 => this.project_index(&right, 0)?,
@@ -343,15 +342,15 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
343342

344343
let mut acc: u16 = 0;
345344
for j in 0..8 {
346-
let src_index = i.checked_mul(8).unwrap().checked_add(j).unwrap();
345+
let src_index = i.strict_mul(8).strict_add(j);
347346

348347
let left = this.project_index(&left, src_index)?;
349348
let left = this.read_scalar(&left)?.to_u8()?;
350349

351350
let right = this.project_index(&right, src_index)?;
352351
let right = this.read_scalar(&right)?.to_u8()?;
353352

354-
acc = acc.checked_add(left.abs_diff(right).into()).unwrap();
353+
acc = acc.strict_add(left.abs_diff(right).into());
355354
}
356355

357356
this.write_scalar(Scalar::from_u64(acc.into()), &dest)?;
@@ -377,7 +376,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
377376

378377
let res = if right & 0x80 == 0 {
379378
// Shuffle each 128-bit (16-byte) block independently.
380-
let j = u64::from(right % 16).checked_add(i & !15).unwrap();
379+
let j = u64::from(right % 16).strict_add(i & !15);
381380
this.read_scalar(&this.project_index(&left, j)?)?
382381
} else {
383382
// If the highest bit in `right` is 1, write zero.

src/tools/miri/src/shims/x86/mod.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,7 @@ fn apply_random_float_error<F: rustc_apfloat::Float>(
441441
) -> F {
442442
let rng = this.machine.rng.get_mut();
443443
// generates rand(0, 2^64) * 2^(scale - 64) = rand(0, 1) * 2^scale
444-
let err =
445-
F::from_u128(rng.gen::<u64>().into()).value.scalbn(err_scale.checked_sub(64).unwrap());
444+
let err = F::from_u128(rng.gen::<u64>().into()).value.scalbn(err_scale.strict_sub(64));
446445
// give it a random sign
447446
let err = if rng.gen::<bool>() { -err } else { err };
448447
// multiple the value with (1+err)
@@ -793,7 +792,7 @@ fn split_simd_to_128bit_chunks<'tcx, P: Projectable<'tcx, Provenance>>(
793792

794793
assert_eq!(simd_layout.size.bits() % 128, 0);
795794
let num_chunks = simd_layout.size.bits() / 128;
796-
let items_per_chunk = simd_len.checked_div(num_chunks).unwrap();
795+
let items_per_chunk = simd_len.strict_div(num_chunks);
797796

798797
// Transmute to `[[T; items_per_chunk]; num_chunks]`
799798
let chunked_layout = this
@@ -841,13 +840,11 @@ fn horizontal_bin_op<'tcx>(
841840
for j in 0..items_per_chunk {
842841
// `j` is the index in `dest`
843842
// `k` is the index of the 2-item chunk in `src`
844-
let (k, src) =
845-
if j < middle { (j, &left) } else { (j.checked_sub(middle).unwrap(), &right) };
843+
let (k, src) = if j < middle { (j, &left) } else { (j.strict_sub(middle), &right) };
846844
// `base_i` is the index of the first item of the 2-item chunk in `src`
847-
let base_i = k.checked_mul(2).unwrap();
845+
let base_i = k.strict_mul(2);
848846
let lhs = this.read_immediate(&this.project_index(src, base_i)?)?;
849-
let rhs =
850-
this.read_immediate(&this.project_index(src, base_i.checked_add(1).unwrap())?)?;
847+
let rhs = this.read_immediate(&this.project_index(src, base_i.strict_add(1))?)?;
851848

852849
let res = if saturating {
853850
Immediate::from(this.saturating_arith(which, &lhs, &rhs)?)
@@ -900,7 +897,7 @@ fn conditional_dot_product<'tcx>(
900897
// for the initial value because the representation of 0.0 is all zero bits.
901898
let mut sum = ImmTy::from_int(0u8, element_layout);
902899
for j in 0..items_per_chunk {
903-
if imm & (1 << j.checked_add(4).unwrap()) != 0 {
900+
if imm & (1 << j.strict_add(4)) != 0 {
904901
let left = this.read_immediate(&this.project_index(&left, j)?)?;
905902
let right = this.read_immediate(&this.project_index(&right, j)?)?;
906903

@@ -971,7 +968,7 @@ fn test_high_bits_masked<'tcx>(
971968

972969
assert_eq!(op_len, mask_len);
973970

974-
let high_bit_offset = op.layout.field(this, 0).size.bits().checked_sub(1).unwrap();
971+
let high_bit_offset = op.layout.field(this, 0).size.bits().strict_sub(1);
975972

976973
let mut direct = true;
977974
let mut negated = true;
@@ -1002,7 +999,7 @@ fn mask_load<'tcx>(
1002999
assert_eq!(dest_len, mask_len);
10031000

10041001
let mask_item_size = mask.layout.field(this, 0).size;
1005-
let high_bit_offset = mask_item_size.bits().checked_sub(1).unwrap();
1002+
let high_bit_offset = mask_item_size.bits().strict_sub(1);
10061003

10071004
let ptr = this.read_pointer(ptr)?;
10081005
for i in 0..dest_len {
@@ -1035,7 +1032,7 @@ fn mask_store<'tcx>(
10351032
assert_eq!(value_len, mask_len);
10361033

10371034
let mask_item_size = mask.layout.field(this, 0).size;
1038-
let high_bit_offset = mask_item_size.bits().checked_sub(1).unwrap();
1035+
let high_bit_offset = mask_item_size.bits().strict_sub(1);
10391036

10401037
let ptr = this.read_pointer(ptr)?;
10411038
for i in 0..value_len {
@@ -1082,29 +1079,27 @@ fn mpsadbw<'tcx>(
10821079
let imm = this.read_scalar(imm)?.to_uint(imm.layout.size)?;
10831080
// Bit 2 of `imm` specifies the offset for indices of `left`.
10841081
// The offset is 0 when the bit is 0 or 4 when the bit is 1.
1085-
let left_offset = u64::try_from((imm >> 2) & 1).unwrap().checked_mul(4).unwrap();
1082+
let left_offset = u64::try_from((imm >> 2) & 1).unwrap().strict_mul(4);
10861083
// Bits 0..=1 of `imm` specify the offset for indices of
10871084
// `right` in blocks of 4 elements.
1088-
let right_offset = u64::try_from(imm & 0b11).unwrap().checked_mul(4).unwrap();
1085+
let right_offset = u64::try_from(imm & 0b11).unwrap().strict_mul(4);
10891086

10901087
for i in 0..num_chunks {
10911088
let left = this.project_index(&left, i)?;
10921089
let right = this.project_index(&right, i)?;
10931090
let dest = this.project_index(&dest, i)?;
10941091

10951092
for j in 0..dest_items_per_chunk {
1096-
let left_offset = left_offset.checked_add(j).unwrap();
1093+
let left_offset = left_offset.strict_add(j);
10971094
let mut res: u16 = 0;
10981095
for k in 0..4 {
10991096
let left = this
1100-
.read_scalar(&this.project_index(&left, left_offset.checked_add(k).unwrap())?)?
1097+
.read_scalar(&this.project_index(&left, left_offset.strict_add(k))?)?
11011098
.to_u8()?;
11021099
let right = this
1103-
.read_scalar(
1104-
&this.project_index(&right, right_offset.checked_add(k).unwrap())?,
1105-
)?
1100+
.read_scalar(&this.project_index(&right, right_offset.strict_add(k))?)?
11061101
.to_u8()?;
1107-
res = res.checked_add(left.abs_diff(right).into()).unwrap();
1102+
res = res.strict_add(left.abs_diff(right).into());
11081103
}
11091104
this.write_scalar(Scalar::from_u16(res), &this.project_index(&dest, j)?)?;
11101105
}
@@ -1138,8 +1133,7 @@ fn pmulhrsw<'tcx>(
11381133
let right = this.read_scalar(&this.project_index(&right, i)?)?.to_i16()?;
11391134
let dest = this.project_index(&dest, i)?;
11401135

1141-
let res =
1142-
(i32::from(left).checked_mul(right.into()).unwrap() >> 14).checked_add(1).unwrap() >> 1;
1136+
let res = (i32::from(left).strict_mul(right.into()) >> 14).strict_add(1) >> 1;
11431137

11441138
// The result of this operation can overflow a signed 16-bit integer.
11451139
// When `left` and `right` are -0x8000, the result is 0x8000.
@@ -1246,8 +1240,7 @@ fn pack_generic<'tcx>(
12461240
let left = this.read_scalar(&this.project_index(&left, j)?)?;
12471241
let right = this.read_scalar(&this.project_index(&right, j)?)?;
12481242
let left_dest = this.project_index(&dest, j)?;
1249-
let right_dest =
1250-
this.project_index(&dest, j.checked_add(op_items_per_chunk).unwrap())?;
1243+
let right_dest = this.project_index(&dest, j.strict_add(op_items_per_chunk))?;
12511244

12521245
let left_res = f(left)?;
12531246
let right_res = f(right)?;

0 commit comments

Comments
 (0)