Skip to content

Commit bb04eab

Browse files
committed
Auto merge of rust-lang#3795 - tiif:ice-layout-limit, r=RalfJung
throw_unsup_format for alignment greater than 2^29 Fixes rust-lang#3687
2 parents d36e157 + 6552a82 commit bb04eab

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::mir;
88
use rustc_middle::ty;
99
use rustc_span::Symbol;
1010
use rustc_target::{
11-
abi::{Align, Size},
11+
abi::{Align, AlignFromBytesError, Size},
1212
spec::abi::Abi,
1313
};
1414

@@ -199,9 +199,20 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
199199
if i128::from(size) > this.tcx.data_layout.pointer_size.signed_int_max() {
200200
throw_ub_format!("creating an allocation larger than half the address space");
201201
}
202-
if !align.is_power_of_two() {
203-
throw_ub_format!("creating allocation with non-power-of-two alignment {}", align);
202+
if let Err(e) = Align::from_bytes(align) {
203+
match e {
204+
AlignFromBytesError::TooLarge(_) => {
205+
throw_unsup_format!(
206+
"creating allocation with alignment {align} exceeding rustc's maximum \
207+
supported value"
208+
);
209+
}
210+
AlignFromBytesError::NotPowerOfTwo(_) => {
211+
throw_ub_format!("creating allocation with non-power-of-two alignment {align}");
212+
}
213+
}
204214
}
215+
205216
Ok(())
206217
}
207218

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Previously, attempting to allocate with an alignment greater than 2^29 would cause miri to ICE
2+
// because rustc does not support alignments that large.
3+
// https://github.com/rust-lang/miri/issues/3687
4+
5+
extern "Rust" {
6+
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
7+
}
8+
9+
fn main() {
10+
unsafe {
11+
__rust_alloc(1, 1 << 30);
12+
//~^ERROR: exceeding rustc's maximum supported value
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unsupported operation: creating allocation with alignment ALIGN exceeding rustc's maximum supported value
2+
--> $DIR/unsupported_big_alignment.rs:LL:CC
3+
|
4+
LL | __rust_alloc(1, 1 << 30);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ creating allocation with alignment ALIGN exceeding rustc's maximum supported value
6+
|
7+
= help: this is likely not a bug in the program; it indicates that the program performed an operation that Miri does not support
8+
= note: BACKTRACE:
9+
= note: inside `main` at $DIR/unsupported_big_alignment.rs:LL:CC
10+
11+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
12+
13+
error: aborting due to 1 previous error
14+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Test non-power-of-two alignment.
2+
extern "Rust" {
3+
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
4+
}
5+
6+
fn main() {
7+
unsafe {
8+
__rust_alloc(1, 3);
9+
//~^ERROR: creating allocation with non-power-of-two alignment
10+
}
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: creating allocation with non-power-of-two alignment ALIGN
2+
--> $DIR/unsupported_non_power_two_alignment.rs:LL:CC
3+
|
4+
LL | __rust_alloc(1, 3);
5+
| ^^^^^^^^^^^^^^^^^^ creating allocation with non-power-of-two alignment ALIGN
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/unsupported_non_power_two_alignment.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)