about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-08-10 10:08:06 +0000
committerbors <bors@rust-lang.org>2024-08-10 10:08:06 +0000
commitbb04eab4f192d8f84028fcb8e7aaf23041b45015 (patch)
tree3434916264899672086659227d0d3695cd9c1f9c /src
parentd36e1571a55e3c36c026855220ad94df75e977a4 (diff)
parent6552a82166aa20c48103e846c0d2387159fe6d12 (diff)
downloadrust-bb04eab4f192d8f84028fcb8e7aaf23041b45015.tar.gz
rust-bb04eab4f192d8f84028fcb8e7aaf23041b45015.zip
Auto merge of #3795 - tiif:ice-layout-limit, r=RalfJung
throw_unsup_format for alignment greater than 2^29

Fixes #3687
Diffstat (limited to 'src')
-rw-r--r--src/tools/miri/src/shims/foreign_items.rs17
-rw-r--r--src/tools/miri/tests/fail/alloc/unsupported_big_alignment.rs14
-rw-r--r--src/tools/miri/tests/fail/alloc/unsupported_big_alignment.stderr14
-rw-r--r--src/tools/miri/tests/fail/alloc/unsupported_non_power_two_alignment.rs11
-rw-r--r--src/tools/miri/tests/fail/alloc/unsupported_non_power_two_alignment.stderr15
5 files changed, 68 insertions, 3 deletions
diff --git a/src/tools/miri/src/shims/foreign_items.rs b/src/tools/miri/src/shims/foreign_items.rs
index 98e095447c3..d40dbdba80f 100644
--- a/src/tools/miri/src/shims/foreign_items.rs
+++ b/src/tools/miri/src/shims/foreign_items.rs
@@ -8,7 +8,7 @@ use rustc_middle::mir;
 use rustc_middle::ty;
 use rustc_span::Symbol;
 use rustc_target::{
-    abi::{Align, Size},
+    abi::{Align, AlignFromBytesError, Size},
     spec::abi::Abi,
 };
 
@@ -199,9 +199,20 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
         if i128::from(size) > this.tcx.data_layout.pointer_size.signed_int_max() {
             throw_ub_format!("creating an allocation larger than half the address space");
         }
-        if !align.is_power_of_two() {
-            throw_ub_format!("creating allocation with non-power-of-two alignment {}", align);
+        if let Err(e) = Align::from_bytes(align) {
+            match e {
+                AlignFromBytesError::TooLarge(_) => {
+                    throw_unsup_format!(
+                        "creating allocation with alignment {align} exceeding rustc's maximum \
+                         supported value"
+                    );
+                }
+                AlignFromBytesError::NotPowerOfTwo(_) => {
+                    throw_ub_format!("creating allocation with non-power-of-two alignment {align}");
+                }
+            }
         }
+
         Ok(())
     }
 
diff --git a/src/tools/miri/tests/fail/alloc/unsupported_big_alignment.rs b/src/tools/miri/tests/fail/alloc/unsupported_big_alignment.rs
new file mode 100644
index 00000000000..08d84c461bf
--- /dev/null
+++ b/src/tools/miri/tests/fail/alloc/unsupported_big_alignment.rs
@@ -0,0 +1,14 @@
+// Previously, attempting to allocate with an alignment greater than 2^29 would cause miri to ICE
+// because rustc does not support alignments that large.
+// https://github.com/rust-lang/miri/issues/3687
+
+extern "Rust" {
+    fn __rust_alloc(size: usize, align: usize) -> *mut u8;
+}
+
+fn main() {
+    unsafe {
+        __rust_alloc(1, 1 << 30);
+        //~^ERROR: exceeding rustc's maximum supported value
+    }
+}
diff --git a/src/tools/miri/tests/fail/alloc/unsupported_big_alignment.stderr b/src/tools/miri/tests/fail/alloc/unsupported_big_alignment.stderr
new file mode 100644
index 00000000000..4c783b866c8
--- /dev/null
+++ b/src/tools/miri/tests/fail/alloc/unsupported_big_alignment.stderr
@@ -0,0 +1,14 @@
+error: unsupported operation: creating allocation with alignment ALIGN exceeding rustc's maximum supported value
+  --> $DIR/unsupported_big_alignment.rs:LL:CC
+   |
+LL |         __rust_alloc(1, 1 << 30);
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^ creating allocation with alignment ALIGN exceeding rustc's maximum supported value
+   |
+   = help: this is likely not a bug in the program; it indicates that the program performed an operation that Miri does not support
+   = note: BACKTRACE:
+   = note: inside `main` at $DIR/unsupported_big_alignment.rs:LL:CC
+
+note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
+
+error: aborting due to 1 previous error
+
diff --git a/src/tools/miri/tests/fail/alloc/unsupported_non_power_two_alignment.rs b/src/tools/miri/tests/fail/alloc/unsupported_non_power_two_alignment.rs
new file mode 100644
index 00000000000..a4ab8094bf4
--- /dev/null
+++ b/src/tools/miri/tests/fail/alloc/unsupported_non_power_two_alignment.rs
@@ -0,0 +1,11 @@
+// Test non-power-of-two alignment.
+extern "Rust" {
+    fn __rust_alloc(size: usize, align: usize) -> *mut u8;
+}
+
+fn main() {
+    unsafe {
+        __rust_alloc(1, 3);
+        //~^ERROR: creating allocation with non-power-of-two alignment
+    }
+}
diff --git a/src/tools/miri/tests/fail/alloc/unsupported_non_power_two_alignment.stderr b/src/tools/miri/tests/fail/alloc/unsupported_non_power_two_alignment.stderr
new file mode 100644
index 00000000000..69a6c375f47
--- /dev/null
+++ b/src/tools/miri/tests/fail/alloc/unsupported_non_power_two_alignment.stderr
@@ -0,0 +1,15 @@
+error: Undefined Behavior: creating allocation with non-power-of-two alignment ALIGN
+  --> $DIR/unsupported_non_power_two_alignment.rs:LL:CC
+   |
+LL |         __rust_alloc(1, 3);
+   |         ^^^^^^^^^^^^^^^^^^ creating allocation with non-power-of-two alignment ALIGN
+   |
+   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
+   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
+   = note: BACKTRACE:
+   = note: inside `main` at $DIR/unsupported_non_power_two_alignment.rs:LL:CC
+
+note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
+
+error: aborting due to 1 previous error
+