Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix promising a very large alignment #3211

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,23 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {

// Promises that a pointer has a given symbolic alignment.
"miri_promise_symbolic_alignment" => {
use rustc_target::abi::AlignFromBytesError;

let [ptr, align] = this.check_shim(abi, Abi::Rust, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let align = this.read_target_usize(align)?;
let Ok(align) = Align::from_bytes(align) else {
if !align.is_power_of_two() {
throw_unsup_format!(
"`miri_promise_symbolic_alignment`: alignment must be a power of 2"
"`miri_promise_symbolic_alignment`: alignment must be a power of 2, got {align}"
);
};
}
let align = Align::from_bytes(align).unwrap_or_else(|err| {
match err {
AlignFromBytesError::NotPowerOfTwo(_) => unreachable!(),
// When the alignment is a power of 2 but too big, clamp it to MAX.
AlignFromBytesError::TooLarge(_) => Align::MAX,
}
});
let (_, addr) = ptr.into_parts(); // we know the offset is absolute
// Cannot panic since `align` is a power of 2 and hence non-zero.
if addr.bytes().checked_rem(align.bytes()).unwrap() != 0 {
Expand Down
13 changes: 1 addition & 12 deletions src/shims/intrinsics/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
}
}
Op::Numeric(name) => {
assert!(op.layout.ty.is_integral());
let size = op.layout.size;
let bits = op.to_scalar().to_bits(size).unwrap();
let extra = 128u128.checked_sub(u128::from(size.bits())).unwrap();
let bits_out = match name {
sym::ctlz => u128::from(bits.leading_zeros()).checked_sub(extra).unwrap(),
sym::cttz => u128::from((bits << extra).trailing_zeros()).checked_sub(extra).unwrap(),
sym::bswap => (bits << extra).swap_bytes(),
sym::bitreverse => (bits << extra).reverse_bits(),
_ => unreachable!(),
};
Scalar::from_uint(bits_out, size)
this.numeric_intrinsic(name, op.to_scalar(), op.layout)?
}
};
this.write_scalar(val, &dest)?;
Expand Down
8 changes: 8 additions & 0 deletions tests/fail/unaligned_pointers/promise_alignment_zero.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[path = "../../utils/mod.rs"]
mod utils;

fn main() {
let buffer = [0u32; 128];
unsafe { utils::miri_promise_symbolic_alignment(buffer.as_ptr().cast(), 0) };
//~^ERROR: alignment must be a power of 2
}
14 changes: 14 additions & 0 deletions tests/fail/unaligned_pointers/promise_alignment_zero.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: unsupported operation: `miri_promise_symbolic_alignment`: alignment must be a power of 2, got 0
--> $DIR/promise_alignment_zero.rs:LL:CC
|
LL | unsafe { utils::miri_promise_symbolic_alignment(buffer.as_ptr().cast(), 0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `miri_promise_symbolic_alignment`: alignment must be a power of 2, got 0
|
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
= note: BACKTRACE:
= note: inside `main` at $DIR/promise_alignment_zero.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

12 changes: 12 additions & 0 deletions tests/pass/align_offset_symbolic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,21 @@ fn test_cstr() {
std::ffi::CStr::from_bytes_with_nul(b"this is a test that is longer than 16 bytes\0").unwrap();
}

fn huge_align() {
#[cfg(target_pointer_width = "64")]
const SIZE: usize = 1 << 47;
#[cfg(target_pointer_width = "32")]
const SIZE: usize = 1 << 30;
#[cfg(target_pointer_width = "16")]
const SIZE: usize = 1 << 13;
struct HugeSize([u8; SIZE - 1]);
let _ = std::ptr::invalid::<HugeSize>(SIZE).align_offset(SIZE);
}

fn main() {
test_align_to();
test_from_utf8();
test_u64_array();
test_cstr();
huge_align();
}
2 changes: 1 addition & 1 deletion tests/pass/issues/issue-3200-packed2-field-offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {
unsafe {
let p = PackedSized { f: 0, d: [1, 2, 3, 4] };
let p = p.unsize() as *const PackedUnsized;
// Make sure the size computation correctly adds exact 1 byte of padding
// Make sure the size computation correctly adds exactly 1 byte of padding
// in front of the `d` field.
assert_eq!(mem::size_of_val_raw(p), 1 + 1 + 4 * 4);
// And likewise for the offset computation.
Expand Down