Skip to content

Commit b5d3a25

Browse files
committed
detect when unused bits of a SIMD bitmask are non-0
1 parent bfed3c4 commit b5d3a25

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

src/shims/intrinsics.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
669669
let (yes, yes_len) = this.operand_to_simd(yes)?;
670670
let (no, no_len) = this.operand_to_simd(no)?;
671671
let (dest, dest_len) = this.place_to_simd(dest)?;
672+
let bitmask_len = dest_len.max(8);
672673

673674
assert!(mask.layout.ty.is_integral());
674-
assert_eq!(dest_len.max(8), mask.layout.size.bits());
675-
assert!(dest_len <= 64);
675+
assert!(bitmask_len <= 64);
676+
assert_eq!(bitmask_len, mask.layout.size.bits());
676677
assert_eq!(dest_len, yes_len);
677678
assert_eq!(dest_len, no_len);
678679

@@ -684,14 +685,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
684685
.unwrap();
685686
for i in 0..dest_len {
686687
let mask =
687-
mask & (1 << simd_bitmask_index(i, dest_len, this.data_layout().endian));
688+
mask & (1 << simd_bitmask_index(i, bitmask_len, this.data_layout().endian));
688689
let yes = this.read_immediate(&this.mplace_index(&yes, i)?.into())?;
689690
let no = this.read_immediate(&this.mplace_index(&no, i)?.into())?;
690691
let dest = this.mplace_index(&dest, i)?;
691692

692693
let val = if mask != 0 { yes } else { no };
693694
this.write_immediate(*val, &dest.into())?;
694695
}
696+
for i in dest_len..bitmask_len {
697+
// If the mask is "padded", ensure that padding is all-zero.
698+
let mask =
699+
mask & (1 << simd_bitmask_index(i, bitmask_len, this.data_layout().endian));
700+
if mask != 0 {
701+
throw_ub_format!(
702+
"a SIMD bitmask less than 8 bits long must be filled with 0s for the remaining bits"
703+
);
704+
}
705+
}
695706
}
696707
#[rustfmt::skip]
697708
"simd_cast" | "simd_as" => {
@@ -820,16 +831,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
820831
"simd_bitmask" => {
821832
let &[ref op] = check_arg_count(args)?;
822833
let (op, op_len) = this.operand_to_simd(op)?;
834+
let bitmask_len = op_len.max(8);
823835

824836
assert!(dest.layout.ty.is_integral());
825-
assert_eq!(op_len.max(8), dest.layout.size.bits());
826-
assert!(op_len <= 64);
837+
assert!(bitmask_len <= 64);
838+
assert_eq!(bitmask_len, dest.layout.size.bits());
827839

828840
let mut res = 0u64;
829841
for i in 0..op_len {
830842
let op = this.read_immediate(&this.mplace_index(&op, i)?.into())?;
831843
if simd_element_to_bool(op)? {
832-
res |= 1 << simd_bitmask_index(i, op_len, this.data_layout().endian);
844+
res |= 1 << simd_bitmask_index(i, bitmask_len, this.data_layout().endian);
833845
}
834846
}
835847
this.write_int(res, dest)?;
@@ -1370,10 +1382,10 @@ fn simd_element_to_bool<'tcx>(elem: ImmTy<'tcx, Tag>) -> InterpResult<'tcx, bool
13701382
})
13711383
}
13721384

1373-
fn simd_bitmask_index(idx: u64, len: u64, endianess: Endian) -> u64 {
1374-
assert!(idx < len);
1385+
fn simd_bitmask_index(idx: u64, bitmask_len: u64, endianess: Endian) -> u64 {
1386+
assert!(idx < bitmask_len);
13751387
match endianess {
13761388
Endian::Little => idx,
1377-
Endian::Big => len.max(8) - 1 - idx, // reverse order of bits
1389+
Endian::Big => bitmask_len - 1 - idx, // reverse order of bits
13781390
}
13791391
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(platform_intrinsics, repr_simd)]
2+
3+
extern "platform-intrinsic" {
4+
fn simd_select_bitmask<M, T>(m: M, yes: T, no: T) -> T;
5+
}
6+
7+
#[repr(simd)]
8+
#[allow(non_camel_case_types)]
9+
#[derive(Copy, Clone)]
10+
struct i32x2(i32, i32);
11+
12+
fn main() { unsafe {
13+
let x = i32x2(0, 1);
14+
simd_select_bitmask(0b11111111u8, x, x); //~ERROR bitmask less than 8 bits long must be filled with 0s for the remaining bits
15+
} }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(platform_intrinsics, repr_simd)]
2+
3+
extern "platform-intrinsic" {
4+
fn simd_select<M, T>(m: M, yes: T, no: T) -> T;
5+
}
6+
7+
#[repr(simd)]
8+
#[allow(non_camel_case_types)]
9+
#[derive(Copy, Clone)]
10+
struct i32x2(i32, i32);
11+
12+
fn main() { unsafe {
13+
let x = i32x2(0, 1);
14+
simd_select(x, x, x); //~ERROR must be all-0-bits or all-1-bits
15+
} }

0 commit comments

Comments
 (0)