Rust values can be up to isize::MAX in size

This commit is contained in:
Ralf Jung 2022-03-27 20:01:04 -04:00
parent 6e1ed17295
commit aa04dc1eeb
3 changed files with 12 additions and 6 deletions

View File

@ -83,6 +83,8 @@ fn call_intrinsic(
let val_byte = this.read_scalar(val_byte)?.to_u8()?;
let ptr = this.read_pointer(ptr)?;
let count = this.read_scalar(count)?.to_machine_usize(this)?;
// `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max),
// but no actual allocation can be big enough for the difference to be noticeable.
let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| {
err_ub_format!("overflow computing total size of `{}`", intrinsic_name)
})?;

View File

@ -6,13 +6,8 @@ struct MySlice {
tail: [u8],
}
#[cfg(target_pointer_width = "64")]
const TOO_BIG: usize = 1usize << 47;
#[cfg(target_pointer_width = "32")]
const TOO_BIG: usize = 1usize << 31;
fn main() { unsafe {
let ptr = Box::into_raw(Box::new(0u8));
// The slice part is actually not "too big", but together with the `prefix` field it is.
let _x: &MySlice = mem::transmute((ptr, TOO_BIG-1)); //~ ERROR: invalid reference metadata: total size is bigger than largest supported object
let _x: &MySlice = mem::transmute((ptr, isize::MAX as usize)); //~ ERROR: invalid reference metadata: total size is bigger than largest supported object
} }

View File

@ -2,6 +2,7 @@
#![feature(new_uninit)]
#![feature(slice_as_chunks)]
#![feature(slice_partition_dedup)]
#![feature(layout_for_ptr)]
use std::slice;
@ -250,9 +251,17 @@ fn test_for_invalidated_pointers() {
buffer.copy_within(1.., 0);
}
fn large_raw_slice() {
let size = isize::MAX as usize;
// Creating a raw slice of size isize::MAX and asking for its size is okay.
let s = std::ptr::slice_from_raw_parts(1usize as *const u8, size);
assert_eq!(size, unsafe { std::mem::size_of_val_raw(s) });
}
fn main() {
slice_of_zst();
test_iter_ref_consistency();
uninit_slice();
test_for_invalidated_pointers();
large_raw_slice();
}