Skip to content

Commit 5d5d8b1

Browse files
committed
Auto merge of #2043 - RalfJung:max-size, r=RalfJung
Rust values can be up to isize::MAX in size Needs rust-lang/rust#95388
2 parents 6e1ed17 + e136680 commit 5d5d8b1

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
df20355fa9fa5e9fb89be4e4bfee8a643bb7a23e
1+
297a8018b525c28ef10ee6a91d61954839b508b9

src/shims/intrinsics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8383
let val_byte = this.read_scalar(val_byte)?.to_u8()?;
8484
let ptr = this.read_pointer(ptr)?;
8585
let count = this.read_scalar(count)?.to_machine_usize(this)?;
86+
// `checked_mul` enforces a too small bound (the correct one would probably be machine_isize_max),
87+
// but no actual allocation can be big enough for the difference to be noticeable.
8688
let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| {
8789
err_ub_format!("overflow computing total size of `{}`", intrinsic_name)
8890
})?;

tests/compile-fail/too-big-unsized.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ struct MySlice {
66
tail: [u8],
77
}
88

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

tests/run-pass/slices.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![feature(new_uninit)]
33
#![feature(slice_as_chunks)]
44
#![feature(slice_partition_dedup)]
5+
#![feature(layout_for_ptr)]
56

67
use std::slice;
78

@@ -250,9 +251,17 @@ fn test_for_invalidated_pointers() {
250251
buffer.copy_within(1.., 0);
251252
}
252253

254+
fn large_raw_slice() {
255+
let size = isize::MAX as usize;
256+
// Creating a raw slice of size isize::MAX and asking for its size is okay.
257+
let s = std::ptr::slice_from_raw_parts(1usize as *const u8, size);
258+
assert_eq!(size, unsafe { std::mem::size_of_val_raw(s) });
259+
}
260+
253261
fn main() {
254262
slice_of_zst();
255263
test_iter_ref_consistency();
256264
uninit_slice();
257265
test_for_invalidated_pointers();
266+
large_raw_slice();
258267
}

0 commit comments

Comments
 (0)