Skip to content

Commit 4151039

Browse files
committed
pass clippy::integer_arithmetic in our shims
1 parent 92d29ed commit 4151039

File tree

9 files changed

+35
-17
lines changed

9 files changed

+35
-17
lines changed

src/shims/backtrace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
175175
// file would have more than 2^32 lines or columns, but whatever, just default to 0.
176176
let lineno: u32 = u32::try_from(lo.line).unwrap_or(0);
177177
// `lo.col` is 0-based - add 1 to make it 1-based for the caller.
178-
let colno: u32 = u32::try_from(lo.col.0 + 1).unwrap_or(0);
178+
let colno: u32 = u32::try_from(lo.col.0.saturating_add(1)).unwrap_or(0);
179179

180180
let dest = this.force_allocation(dest)?;
181181
if let ty::Adt(adt, _) = dest.layout.ty.kind() {

src/shims/foreign_items.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
164164
.expect("interpreting a non-executable crate");
165165
for cnum in iter::once(LOCAL_CRATE).chain(
166166
dependency_format.1.iter().enumerate().filter_map(|(num, &linkage)| {
167+
// We add 1 to the number because that's what rustc also does everywhere it
168+
// calls `CrateNum::new`...
169+
#[allow(clippy::integer_arithmetic)]
167170
(linkage != Linkage::NotLinked).then_some(CrateNum::new(num + 1))
168171
}),
169172
) {
@@ -542,7 +545,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
542545
.rev()
543546
.position(|&c| c == val)
544547
{
545-
let new_ptr = ptr.offset(Size::from_bytes(num - idx as u64 - 1), this)?;
548+
let idx = u64::try_from(idx).unwrap();
549+
#[allow(clippy::integer_arithmetic)] // idx < num, so this never wraps
550+
let new_ptr = ptr.offset(Size::from_bytes(num - idx - 1), this)?;
546551
this.write_pointer(new_ptr, dest)?;
547552
} else {
548553
this.write_null(dest)?;
@@ -690,8 +695,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
690695
let a = this.read_scalar(a)?.to_u64()?;
691696
let b = this.read_scalar(b)?.to_u64()?;
692697

698+
#[allow(clippy::integer_arithmetic)] // adding two u64 and a u8 cannot wrap in a u128
693699
let wide_sum = u128::from(c_in) + u128::from(a) + u128::from(b);
694-
let (c_out, sum) = ((wide_sum >> 64).truncate::<u8>(), wide_sum.truncate::<u64>());
700+
let (c_out, sum) = ((wide_sum.wrapping_shr(64)).truncate::<u8>(), wide_sum.truncate::<u64>());
695701

696702
let c_out_field = this.place_field(dest, 0)?;
697703
this.write_scalar(Scalar::from_u8(c_out), &c_out_field)?;

src/shims/intrinsics/simd.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
382382
assert_eq!(bitmask_len, mask.layout.size.bits());
383383
assert_eq!(dest_len, yes_len);
384384
assert_eq!(dest_len, no_len);
385+
let dest_len = u32::try_from(dest_len).unwrap();
386+
let bitmask_len = u32::try_from(bitmask_len).unwrap();
385387

386388
let mask: u64 = this
387389
.read_scalar(mask)?
@@ -390,18 +392,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
390392
.try_into()
391393
.unwrap();
392394
for i in 0..dest_len {
393-
let mask =
394-
mask & (1 << simd_bitmask_index(i, dest_len, this.data_layout().endian));
395-
let yes = this.read_immediate(&this.mplace_index(&yes, i)?.into())?;
396-
let no = this.read_immediate(&this.mplace_index(&no, i)?.into())?;
397-
let dest = this.mplace_index(&dest, i)?;
395+
let mask = mask
396+
& 1u64
397+
.checked_shl(simd_bitmask_index(i, dest_len, this.data_layout().endian))
398+
.unwrap();
399+
let yes = this.read_immediate(&this.mplace_index(&yes, i.into())?.into())?;
400+
let no = this.read_immediate(&this.mplace_index(&no, i.into())?.into())?;
401+
let dest = this.mplace_index(&dest, i.into())?;
398402

399403
let val = if mask != 0 { yes } else { no };
400404
this.write_immediate(*val, &dest.into())?;
401405
}
402406
for i in dest_len..bitmask_len {
403407
// If the mask is "padded", ensure that padding is all-zero.
404-
let mask = mask & (1 << i);
408+
let mask = mask & 1u64.checked_shl(i).unwrap();
405409
if mask != 0 {
406410
throw_ub_format!(
407411
"a SIMD bitmask less than 8 bits long must be filled with 0s for the remaining bits"
@@ -474,9 +478,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
474478
let val = if src_index < left_len {
475479
this.read_immediate(&this.mplace_index(&left, src_index)?.into())?
476480
} else if src_index < left_len.checked_add(right_len).unwrap() {
477-
this.read_immediate(
478-
&this.mplace_index(&right, src_index - left_len)?.into(),
479-
)?
481+
let right_idx = src_index.checked_sub(left_len).unwrap();
482+
this.read_immediate(&this.mplace_index(&right, right_idx)?.into())?
480483
} else {
481484
span_bug!(
482485
this.cur_span(),
@@ -540,12 +543,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
540543
assert!(dest.layout.ty.is_integral());
541544
assert!(bitmask_len <= 64);
542545
assert_eq!(bitmask_len, dest.layout.size.bits());
546+
let op_len = u32::try_from(op_len).unwrap();
543547

544548
let mut res = 0u64;
545549
for i in 0..op_len {
546-
let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
550+
let op = this.read_immediate(&this.mplace_index(&op, i.into())?.into())?;
547551
if simd_element_to_bool(op)? {
548-
res |= 1 << simd_bitmask_index(i, op_len, this.data_layout().endian);
552+
res |= 1u64
553+
.checked_shl(simd_bitmask_index(i, op_len, this.data_layout().endian))
554+
.unwrap();
549555
}
550556
}
551557
this.write_int(res, dest)?;
@@ -572,10 +578,11 @@ fn simd_element_to_bool(elem: ImmTy<'_, Provenance>) -> InterpResult<'_, bool> {
572578
})
573579
}
574580

575-
fn simd_bitmask_index(idx: u64, vec_len: u64, endianess: Endian) -> u64 {
581+
fn simd_bitmask_index(idx: u32, vec_len: u32, endianess: Endian) -> u32 {
576582
assert!(idx < vec_len);
577583
match endianess {
578584
Endian::Little => idx,
585+
#[allow(clippy::integer_arithmetic)] // idx < vec_len
579586
Endian::Big => vec_len - 1 - idx, // reverse order of bits
580587
}
581588
}

src/shims/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![warn(clippy::integer_arithmetic)]
2+
13
mod backtrace;
24
pub mod foreign_items;
35
pub mod intrinsics;

src/shims/os_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
157157
alloc
158158
.write_scalar(alloc_range(size2 * offset, size2), Scalar::from_u16(wchar).into())?;
159159
}
160-
Ok((true, string_length - 1))
160+
Ok((true, string_length.checked_sub(1).unwrap()))
161161
}
162162

163163
/// Allocate enough memory to store the given `OsStr` as a null-terminated sequence of bytes.

src/shims/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8484
Ok(0)
8585
}
8686

87-
#[allow(non_snake_case)]
87+
#[allow(non_snake_case, clippy::integer_arithmetic)]
8888
fn GetSystemTimeAsFileTime(
8989
&mut self,
9090
LPFILETIME_op: &OpTy<'tcx, Provenance>,

src/shims/tls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl<'tcx> Default for TlsData<'tcx> {
6363
impl<'tcx> TlsData<'tcx> {
6464
/// Generate a new TLS key with the given destructor.
6565
/// `max_size` determines the integer size the key has to fit in.
66+
#[allow(clippy::integer_arithmetic)]
6667
pub fn create_tls_key(
6768
&mut self,
6869
dtor: Option<ty::Instance<'tcx>>,

src/shims/unix/fs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ pub struct DirHandler {
520520
}
521521

522522
impl DirHandler {
523+
#[allow(clippy::integer_arithmetic)]
523524
fn insert_new(&mut self, read_dir: ReadDir) -> u64 {
524525
let id = self.next_id;
525526
self.next_id += 1;

src/shims/unix/linux/sync.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ pub fn futex<'tcx>(
242242
// before doing the syscall.
243243
this.validate_atomic_fence(AtomicFenceOrd::SeqCst)?;
244244
let mut n = 0;
245+
#[allow(clippy::integer_arithmetic)]
245246
for _ in 0..val {
246247
if let Some(thread) = this.futex_wake(addr_usize, bitset) {
247248
this.unblock_thread(thread);

0 commit comments

Comments
 (0)