Skip to content

Commit 6a37bae

Browse files
authored
Rollup merge of #130342 - RalfJung:slice-idx-overflow, r=saethlin
interpret, miri: fix dealing with overflow during slice indexing and allocation This is mostly to fix rust-lang/rust#130284. I then realized we're using somewhat sketchy arguments for a similar multiplication in `copy`/`copy_nonoverlapping`/`write_bytes`, so I made them all share the same function that checks exactly the right thing. (The intrinsics would previously fail on allocations larger than `1 << 47` bytes... which are theoretically possible maybe? Anyway it seems conceptually wrong to use any other bound than `isize::MAX` here.)
2 parents a785b59 + 1740f8b commit 6a37bae

File tree

5 files changed

+53
-31
lines changed

5 files changed

+53
-31
lines changed

src/concurrency/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
900900
// This allocation will be deallocated when the thread dies, so it is not in read-only memory.
901901
alloc.mutability = Mutability::Mut;
902902
// Create a fresh allocation with this content.
903-
let ptr = this.allocate_raw_ptr(alloc, MiriMemoryKind::Tls.into())?;
903+
let ptr = this.insert_allocation(alloc, MiriMemoryKind::Tls.into())?;
904904
this.machine.threads.set_thread_local_alloc(def_id, ptr);
905905
Ok(ptr)
906906
}

src/intrinsics/mod.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
mod atomic;
44
mod simd;
55

6-
use std::iter;
7-
86
use rand::Rng;
97
use rustc_apfloat::{Float, Round};
10-
use rustc_middle::ty::layout::LayoutOf;
118
use rustc_middle::{
129
mir,
1310
ty::{self, FloatTy},
@@ -119,19 +116,9 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
119116
this.copy_op(dest, &place)?;
120117
}
121118

122-
"write_bytes" | "volatile_set_memory" => {
119+
"volatile_set_memory" => {
123120
let [ptr, val_byte, count] = check_arg_count(args)?;
124-
let ty = ptr.layout.ty.builtin_deref(true).unwrap();
125-
let ty_layout = this.layout_of(ty)?;
126-
let val_byte = this.read_scalar(val_byte)?.to_u8()?;
127-
let ptr = this.read_pointer(ptr)?;
128-
let count = this.read_target_usize(count)?;
129-
// `checked_mul` enforces a too small bound (the correct one would probably be target_isize_max),
130-
// but no actual allocation can be big enough for the difference to be noticeable.
131-
let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| {
132-
err_ub_format!("overflow computing total size of `{intrinsic_name}`")
133-
})?;
134-
this.write_bytes_ptr(ptr, iter::repeat(val_byte).take(byte_count.bytes_usize()))?;
121+
this.write_bytes_intrinsic(ptr, val_byte, count, "volatile_set_memory")?;
135122
}
136123

137124
// Memory model / provenance manipulation

src/shims/foreign_items.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
196196
if size == 0 {
197197
throw_ub_format!("creating allocation with size 0");
198198
}
199-
if i128::from(size) > this.tcx.data_layout.pointer_size.signed_int_max() {
199+
if size > this.max_size_of_val().bytes() {
200200
throw_ub_format!("creating an allocation larger than half the address space");
201201
}
202202
if let Err(e) = Align::from_bytes(align) {
@@ -441,19 +441,34 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
441441
"malloc" => {
442442
let [size] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
443443
let size = this.read_target_usize(size)?;
444-
let res = this.malloc(size, /*zero_init:*/ false)?;
445-
this.write_pointer(res, dest)?;
444+
if size <= this.max_size_of_val().bytes() {
445+
let res = this.malloc(size, /*zero_init:*/ false)?;
446+
this.write_pointer(res, dest)?;
447+
} else {
448+
// If this does not fit in an isize, return null and, on Unix, set errno.
449+
if this.target_os_is_unix() {
450+
let einval = this.eval_libc("ENOMEM");
451+
this.set_last_error(einval)?;
452+
}
453+
this.write_null(dest)?;
454+
}
446455
}
447456
"calloc" => {
448-
let [items, len] =
457+
let [items, elem_size] =
449458
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
450459
let items = this.read_target_usize(items)?;
451-
let len = this.read_target_usize(len)?;
452-
let size = items
453-
.checked_mul(len)
454-
.ok_or_else(|| err_ub_format!("overflow during calloc size computation"))?;
455-
let res = this.malloc(size, /*zero_init:*/ true)?;
456-
this.write_pointer(res, dest)?;
460+
let elem_size = this.read_target_usize(elem_size)?;
461+
if let Some(size) = this.compute_size_in_bytes(Size::from_bytes(elem_size), items) {
462+
let res = this.malloc(size.bytes(), /*zero_init:*/ true)?;
463+
this.write_pointer(res, dest)?;
464+
} else {
465+
// On size overflow, return null and, on Unix, set errno.
466+
if this.target_os_is_unix() {
467+
let einval = this.eval_libc("ENOMEM");
468+
this.set_last_error(einval)?;
469+
}
470+
this.write_null(dest)?;
471+
}
457472
}
458473
"free" => {
459474
let [ptr] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
@@ -465,8 +480,17 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
465480
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
466481
let old_ptr = this.read_pointer(old_ptr)?;
467482
let new_size = this.read_target_usize(new_size)?;
468-
let res = this.realloc(old_ptr, new_size)?;
469-
this.write_pointer(res, dest)?;
483+
if new_size <= this.max_size_of_val().bytes() {
484+
let res = this.realloc(old_ptr, new_size)?;
485+
this.write_pointer(res, dest)?;
486+
} else {
487+
// If this does not fit in an isize, return null and, on Unix, set errno.
488+
if this.target_os_is_unix() {
489+
let einval = this.eval_libc("ENOMEM");
490+
this.set_last_error(einval)?;
491+
}
492+
this.write_null(dest)?;
493+
}
470494
}
471495

472496
// Rust allocation

src/shims/unix/foreign_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,14 +363,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
363363
//
364364
// Linux: https://www.unix.com/man-page/linux/3/reallocarray/
365365
// FreeBSD: https://man.freebsd.org/cgi/man.cgi?query=reallocarray
366-
match nmemb.checked_mul(size) {
366+
match this.compute_size_in_bytes(Size::from_bytes(size), nmemb) {
367367
None => {
368368
let einval = this.eval_libc("ENOMEM");
369369
this.set_last_error(einval)?;
370370
this.write_null(dest)?;
371371
}
372372
Some(len) => {
373-
let res = this.realloc(ptr, len)?;
373+
let res = this.realloc(ptr, len.bytes())?;
374374
this.write_pointer(res, dest)?;
375375
}
376376
}

tests/pass-dep/libc/libc-mem.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ fn test_malloc() {
101101
let slice = slice::from_raw_parts(p3 as *const u8, 20);
102102
assert_eq!(&slice, &[0_u8; 20]);
103103

104+
// new size way too big (so this doesn't actually realloc).
105+
let p_too_big = libc::realloc(p3, usize::MAX);
106+
assert!(p_too_big.is_null());
107+
104108
// old size > new size
105109
let p4 = libc::realloc(p3, 10);
106110
let slice = slice::from_raw_parts(p4 as *const u8, 10);
@@ -119,9 +123,13 @@ fn test_malloc() {
119123
unsafe {
120124
let p1 = libc::realloc(ptr::null_mut(), 20);
121125
assert!(!p1.is_null());
122-
123126
libc::free(p1);
124127
}
128+
129+
unsafe {
130+
let p_too_big = libc::malloc(usize::MAX);
131+
assert!(p_too_big.is_null());
132+
}
125133
}
126134

127135
fn test_calloc() {
@@ -143,6 +151,9 @@ fn test_calloc() {
143151
let slice = slice::from_raw_parts(p4 as *const u8, 4 * 8);
144152
assert_eq!(&slice, &[0_u8; 4 * 8]);
145153
libc::free(p4);
154+
155+
let p_too_big = libc::calloc(usize::MAX / 4, 4);
156+
assert!(p_too_big.is_null());
146157
}
147158
}
148159

0 commit comments

Comments
 (0)