Skip to content

Commit 97c9437

Browse files
committed
Auto merge of #61454 - lzutao:ice-rotate_left, r=RalfJung
Fix integer overflow in rotate_left Closes #61406 r? @RalfJung
2 parents acda261 + d392cb5 commit 97c9437

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/librustc_mir/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
194194
let raw_shift_bits = self.read_scalar(args[1])?.to_bits(layout.size)?;
195195
let width_bits = layout.size.bits() as u128;
196196
let shift_bits = raw_shift_bits % width_bits;
197-
let inv_shift_bits = (width_bits - raw_shift_bits) % width_bits;
197+
let inv_shift_bits = (width_bits - shift_bits) % width_bits;
198198
let result_bits = if intrinsic_name == "rotate_left" {
199199
(val_bits << shift_bits) | (val_bits >> inv_shift_bits)
200200
} else {

src/test/run-pass/const-int-rotate.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
11
const LEFT: u32 = 0x10000b3u32.rotate_left(8);
22
const RIGHT: u32 = 0xb301u32.rotate_right(8);
33

4+
// Rotating these should make no difference
5+
//
6+
// We test using 124 bits because to ensure that overlong bit shifts do
7+
// not cause undefined behaviour. See #10183.
8+
const LEFT_OVERFLOW: i16 = 0i16.rotate_left(124);
9+
const RIGHT_OVERFLOW: i16 = 0i16.rotate_right(124);
10+
const ONE_LEFT_OVERFLOW: u16 = 1u16.rotate_left(124);
11+
const ONE_RIGHT_OVERFLOW: u16 = 1u16.rotate_right(124);
12+
13+
const NON_ZERO_LEFT_OVERFLOW: u16 = 0b10u16.rotate_left(124);
14+
const NON_ZERO_RIGHT_OVERFLOW: u16 = 0b10u16.rotate_right(124);
15+
16+
// Rotating by 0 should have no effect
17+
const ZERO_ROTATE_LEFT: i8 = 0b0010_0001i8.rotate_left(0);
18+
const ZERO_ROTATE_RIGHT: i8 = 0b0111_1001i8.rotate_right(0);
19+
20+
// Rotating by a multiple of word size should also have no effect
21+
const MULTIPLE_ROTATE_LEFT: i32 = 0b0010_0001i32.rotate_left(128);
22+
const MULTIPLE_ROTATE_RIGHT: i32 = 0b0010_0001i32.rotate_right(128);
23+
424
fn ident<T>(ident: T) -> T {
525
ident
626
}
727

828
fn main() {
929
assert_eq!(LEFT, ident(0xb301));
10-
assert_eq!(RIGHT, ident(0x10000b3));
30+
assert_eq!(RIGHT, ident(0x0100_00b3));
31+
32+
assert_eq!(LEFT_OVERFLOW, ident(0));
33+
assert_eq!(RIGHT_OVERFLOW, ident(0));
34+
assert_eq!(ONE_LEFT_OVERFLOW, ident(0b0001_0000_0000_0000));
35+
assert_eq!(ONE_RIGHT_OVERFLOW, ident(0b0001_0000));
36+
37+
assert_eq!(NON_ZERO_LEFT_OVERFLOW, ident(0b0010_0000_0000_0000));
38+
assert_eq!(NON_ZERO_RIGHT_OVERFLOW, ident(0b0000_0000_0010_0000));
39+
40+
assert_eq!(ZERO_ROTATE_LEFT, ident(0b0010_0001));
41+
assert_eq!(ZERO_ROTATE_RIGHT, ident(0b0111_1001));
42+
43+
assert_eq!(MULTIPLE_ROTATE_LEFT, ident(0b0010_0001));
44+
assert_eq!(MULTIPLE_ROTATE_RIGHT, ident(0b0010_0001));
1145
}

0 commit comments

Comments
 (0)