Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integer overflow in rotate_left #61454

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
let raw_shift_bits = self.read_scalar(args[1])?.to_bits(layout.size)?;
let width_bits = layout.size.bits() as u128;
let shift_bits = raw_shift_bits % width_bits;
let inv_shift_bits = (width_bits - raw_shift_bits) % width_bits;
let inv_shift_bits = (width_bits - shift_bits) % width_bits;
let result_bits = if intrinsic_name == "rotate_left" {
(val_bits << shift_bits) | (val_bits >> inv_shift_bits)
} else {
Expand Down
36 changes: 35 additions & 1 deletion src/test/run-pass/const-int-rotate.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
const LEFT: u32 = 0x10000b3u32.rotate_left(8);
const RIGHT: u32 = 0xb301u32.rotate_right(8);

// Rotating these should make no difference
//
// We test using 124 bits because to ensure that overlong bit shifts do
// not cause undefined behaviour. See #10183.
const LEFT_OVERFLOW: i16 = 0i16.rotate_left(124);
const RIGHT_OVERFLOW: i16 = 0i16.rotate_right(124);
const ONE_LEFT_OVERFLOW: u16 = 1u16.rotate_left(124);
const ONE_RIGHT_OVERFLOW: u16 = 1u16.rotate_right(124);

const NON_ZERO_LEFT_OVERFLOW: u16 = 0b10u16.rotate_left(124);
const NON_ZERO_RIGHT_OVERFLOW: u16 = 0b10u16.rotate_right(124);

// Rotating by 0 should have no effect
const ZERO_ROTATE_LEFT: i8 = 0b0010_0001i8.rotate_left(0);
const ZERO_ROTATE_RIGHT: i8 = 0b0111_1001i8.rotate_right(0);

// Rotating by a multiple of word size should also have no effect
const MULTIPLE_ROTATE_LEFT: i32 = 0b0010_0001i32.rotate_left(128);
const MULTIPLE_ROTATE_RIGHT: i32 = 0b0010_0001i32.rotate_right(128);

fn ident<T>(ident: T) -> T {
ident
}

fn main() {
assert_eq!(LEFT, ident(0xb301));
assert_eq!(RIGHT, ident(0x10000b3));
assert_eq!(RIGHT, ident(0x0100_00b3));

assert_eq!(LEFT_OVERFLOW, ident(0));
assert_eq!(RIGHT_OVERFLOW, ident(0));
assert_eq!(ONE_LEFT_OVERFLOW, ident(0b0001_0000_0000_0000));
assert_eq!(ONE_RIGHT_OVERFLOW, ident(0b0001_0000));

assert_eq!(NON_ZERO_LEFT_OVERFLOW, ident(0b0010_0000_0000_0000));
assert_eq!(NON_ZERO_RIGHT_OVERFLOW, ident(0b0000_0000_0010_0000));

assert_eq!(ZERO_ROTATE_LEFT, ident(0b0010_0001));
assert_eq!(ZERO_ROTATE_RIGHT, ident(0b0111_1001));

assert_eq!(MULTIPLE_ROTATE_LEFT, ident(0b0010_0001));
assert_eq!(MULTIPLE_ROTATE_RIGHT, ident(0b0010_0001));
}