Skip to content

Commit be307ca

Browse files
committed
Auto merge of rust-lang#3694 - southball:fix/use_strict_ops_instead_of_checked_ops, r=RalfJung
Use strict ops instead of checked ops ## What Replace `checked_add(...).unwrap()` with `strict_add(...)`, etc. Resolves rust-lang#3668.
2 parents 53f8175 + 1ee4a5a commit be307ca

File tree

14 files changed

+65
-75
lines changed

14 files changed

+65
-75
lines changed

Diff for: src/tools/miri/src/alloc_addresses/mod.rs

+1-1
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

Diff for: src/tools/miri/src/eval.rs

+1-1
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())?;

Diff for: src/tools/miri/src/helpers.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
973973
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
974974
// terminator to memory using the `ptr` pointer would cause an out-of-bounds access.
975975
let string_length = u64::try_from(c_str.len()).unwrap();
976-
let string_length = string_length.checked_add(1).unwrap();
976+
let string_length = string_length.strict_add(1);
977977
if size < string_length {
978978
return Ok((false, string_length));
979979
}
@@ -1037,7 +1037,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
10371037
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required
10381038
// 0x0000 terminator to memory would cause an out-of-bounds access.
10391039
let string_length = u64::try_from(wide_str.len()).unwrap();
1040-
let string_length = string_length.checked_add(1).unwrap();
1040+
let string_length = string_length.strict_add(1);
10411041
if size < string_length {
10421042
return Ok((false, string_length));
10431043
}
@@ -1406,7 +1406,7 @@ pub(crate) fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
14061406
if success {
14071407
// If the function succeeds, the return value is the number of characters stored in the target buffer,
14081408
// not including the terminating null character.
1409-
u32::try_from(len.checked_sub(1).unwrap()).unwrap()
1409+
u32::try_from(len.strict_sub(1)).unwrap()
14101410
} else {
14111411
// If the target buffer was not large enough to hold the data, the return value is the buffer size, in characters,
14121412
// required to hold the string and its terminating null character.

Diff for: src/tools/miri/src/shims/foreign_items.rs

+2-2
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
}

Diff for: src/tools/miri/src/shims/time.rs

+1-1
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),

Diff for: src/tools/miri/src/shims/unix/env.rs

+2-4
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

Diff for: src/tools/miri/src/shims/unix/fd.rs

+1-1
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();

Diff for: src/tools/miri/src/shims/unix/socket.rs

+1-1
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.

Diff for: src/tools/miri/src/shims/windows/foreign_items.rs

+2-2
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.

Diff for: src/tools/miri/src/shims/windows/handle.rs

+2-2
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

Diff for: src/tools/miri/src/shims/x86/avx2.rs

+17-18
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,
@@ -124,22 +123,22 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
124123
let (dest, dest_len) = this.mplace_to_simd(dest)?;
125124

126125
assert_eq!(left_len, right_len);
127-
assert_eq!(dest_len.checked_mul(2).unwrap(), left_len);
126+
assert_eq!(dest_len.strict_mul(2), 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);
@@ -161,22 +160,22 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
161160
let (dest, dest_len) = this.mplace_to_simd(dest)?;
162161

163162
assert_eq!(left_len, right_len);
164-
assert_eq!(dest_len.checked_mul(2).unwrap(), left_len);
163+
assert_eq!(dest_len.strict_mul(2), 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)?,
@@ -336,22 +335,22 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
336335
let (dest, dest_len) = this.mplace_to_simd(dest)?;
337336

338337
assert_eq!(left_len, right_len);
339-
assert_eq!(left_len, dest_len.checked_mul(8).unwrap());
338+
assert_eq!(left_len, dest_len.strict_mul(8));
340339

341340
for i in 0..dest_len {
342341
let dest = this.project_index(&dest, i)?;
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.

0 commit comments

Comments
 (0)