Skip to content

Commit 8497fd4

Browse files
committed
pass clippy::integer_arithmetic in our shims
1 parent ab88e64 commit 8497fd4

File tree

10 files changed

+42
-18
lines changed

10 files changed

+42
-18
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
clippy::too_many_arguments,
2828
clippy::type_complexity,
2929
clippy::single_element_loop,
30+
clippy::needless_return,
3031
// We are not implementing queries here so it's fine
3132
rustc::potential_query_instability
3233
)]

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 & 1 deletion
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)?;
@@ -708,7 +713,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
708713
let a = this.read_scalar(a)?.to_u64()?;
709714
let b = this.read_scalar(b)?.to_u64()?;
710715

716+
#[allow(clippy::integer_arithmetic)] // adding two u64 and a u8 cannot wrap in a u128
711717
let wide_sum = u128::from(c_in) + u128::from(a) + u128::from(b);
718+
#[allow(clippy::integer_arithmetic)] // it's a u128, we can shift by 64
712719
let (c_out, sum) = ((wide_sum >> 64).truncate::<u8>(), wide_sum.truncate::<u64>());
713720

714721
let c_out_field = this.place_field(dest, 0)?;

src/shims/intrinsics/simd.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
393393
assert_eq!(bitmask_len, mask.layout.size.bits());
394394
assert_eq!(dest_len, yes_len);
395395
assert_eq!(dest_len, no_len);
396+
let dest_len = u32::try_from(dest_len).unwrap();
397+
let bitmask_len = u32::try_from(bitmask_len).unwrap();
396398

397399
let mask: u64 = this
398400
.read_scalar(mask)?
@@ -401,18 +403,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
401403
.try_into()
402404
.unwrap();
403405
for i in 0..dest_len {
404-
let mask =
405-
mask & (1 << simd_bitmask_index(i, dest_len, this.data_layout().endian));
406-
let yes = this.read_immediate(&this.mplace_index(&yes, i)?.into())?;
407-
let no = this.read_immediate(&this.mplace_index(&no, i)?.into())?;
408-
let dest = this.mplace_index(&dest, i)?;
406+
let mask = mask
407+
& 1u64
408+
.checked_shl(simd_bitmask_index(i, dest_len, this.data_layout().endian))
409+
.unwrap();
410+
let yes = this.read_immediate(&this.mplace_index(&yes, i.into())?.into())?;
411+
let no = this.read_immediate(&this.mplace_index(&no, i.into())?.into())?;
412+
let dest = this.mplace_index(&dest, i.into())?;
409413

410414
let val = if mask != 0 { yes } else { no };
411415
this.write_immediate(*val, &dest.into())?;
412416
}
413417
for i in dest_len..bitmask_len {
414418
// If the mask is "padded", ensure that padding is all-zero.
415-
let mask = mask & (1 << i);
419+
let mask = mask & 1u64.checked_shl(i).unwrap();
416420
if mask != 0 {
417421
throw_ub_format!(
418422
"a SIMD bitmask less than 8 bits long must be filled with 0s for the remaining bits"
@@ -485,9 +489,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
485489
let val = if src_index < left_len {
486490
this.read_immediate(&this.mplace_index(&left, src_index)?.into())?
487491
} else if src_index < left_len.checked_add(right_len).unwrap() {
488-
this.read_immediate(
489-
&this.mplace_index(&right, src_index - left_len)?.into(),
490-
)?
492+
let right_idx = src_index.checked_sub(left_len).unwrap();
493+
this.read_immediate(&this.mplace_index(&right, right_idx)?.into())?
491494
} else {
492495
span_bug!(
493496
this.cur_span(),
@@ -551,12 +554,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
551554
assert!(dest.layout.ty.is_integral());
552555
assert!(bitmask_len <= 64);
553556
assert_eq!(bitmask_len, dest.layout.size.bits());
557+
let op_len = u32::try_from(op_len).unwrap();
554558

555559
let mut res = 0u64;
556560
for i in 0..op_len {
557-
let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
561+
let op = this.read_immediate(&this.mplace_index(&op, i.into())?.into())?;
558562
if simd_element_to_bool(op)? {
559-
res |= 1 << simd_bitmask_index(i, op_len, this.data_layout().endian);
563+
res |= 1u64
564+
.checked_shl(simd_bitmask_index(i, op_len, this.data_layout().endian))
565+
.unwrap();
560566
}
561567
}
562568
this.write_int(res, dest)?;
@@ -583,10 +589,11 @@ fn simd_element_to_bool(elem: ImmTy<'_, Provenance>) -> InterpResult<'_, bool> {
583589
})
584590
}
585591

586-
fn simd_bitmask_index(idx: u64, vec_len: u64, endianess: Endian) -> u64 {
592+
fn simd_bitmask_index(idx: u32, vec_len: u32, endianess: Endian) -> u32 {
587593
assert!(idx < vec_len);
588594
match endianess {
589595
Endian::Little => idx,
596+
#[allow(clippy::integer_arithmetic)] // idx < vec_len
590597
Endian::Big => vec_len - 1 - idx, // reverse order of bits
591598
}
592599
}

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/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
@@ -443,6 +443,7 @@ pub struct DirHandler {
443443
}
444444

445445
impl DirHandler {
446+
#[allow(clippy::integer_arithmetic)]
446447
fn insert_new(&mut self, read_dir: ReadDir) -> u64 {
447448
let id = self.next_id;
448449
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.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);

src/shims/windows/handle.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ impl Handle {
6262
let floor_log2 = variant_count.ilog2();
6363

6464
// we need to add one for non powers of two to compensate for the difference
65+
#[allow(clippy::integer_arithmetic)] // cannot overflow
6566
if variant_count.is_power_of_two() { floor_log2 } else { floor_log2 + 1 }
6667
}
6768

@@ -73,7 +74,7 @@ impl Handle {
7374
/// None of this layout is guaranteed to applications by Windows or Miri.
7475
fn to_packed(self) -> u32 {
7576
let disc_size = Self::packed_disc_size();
76-
let data_size = u32::BITS - disc_size;
77+
let data_size = u32::BITS.checked_sub(disc_size).unwrap();
7778

7879
let discriminant = self.discriminant();
7980
let data = self.data();
@@ -86,7 +87,8 @@ impl Handle {
8687

8788
// packs the data into the lower `data_size` bits
8889
// and packs the discriminant right above the data
89-
discriminant << data_size | data
90+
#[allow(clippy::integer_arithmetic)] // cannot overflow
91+
return discriminant << data_size | data;
9092
}
9193

9294
fn new(discriminant: u32, data: u32) -> Option<Self> {
@@ -101,12 +103,14 @@ impl Handle {
101103
/// see docs for `to_packed`
102104
fn from_packed(handle: u32) -> Option<Self> {
103105
let disc_size = Self::packed_disc_size();
104-
let data_size = u32::BITS - disc_size;
106+
let data_size = u32::BITS.checked_sub(disc_size).unwrap();
105107

106108
// the lower `data_size` bits of this mask are 1
109+
#[allow(clippy::integer_arithmetic)] // cannot overflow
107110
let data_mask = 2u32.pow(data_size) - 1;
108111

109112
// the discriminant is stored right above the lower `data_size` bits
113+
#[allow(clippy::integer_arithmetic)] // cannot overflow
110114
let discriminant = handle >> data_size;
111115

112116
// the data is stored in the lower `data_size` bits

0 commit comments

Comments
 (0)