|
8 | 8 |
|
9 | 9 | use std::borrow::Cow;
|
10 | 10 | use std::collections::VecDeque;
|
11 |
| -use std::convert::TryFrom; |
| 11 | +use std::convert::{TryFrom, TryInto}; |
12 | 12 | use std::fmt;
|
13 | 13 | use std::ptr;
|
14 | 14 |
|
@@ -380,7 +380,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
|
380 | 380 | // if this is already a `Pointer` we want to do the bounds checks!
|
381 | 381 | sptr
|
382 | 382 | } else {
|
383 |
| - // A "real" access, we must get a pointer. |
| 383 | + // A "real" access, we must get a pointer to be able to check the bounds. |
384 | 384 | Scalar::from(self.force_ptr(sptr)?)
|
385 | 385 | };
|
386 | 386 | Ok(match normalized.to_bits_or_ptr(self.pointer_size(), self) {
|
@@ -411,15 +411,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
|
411 | 411 | // Test align. Check this last; if both bounds and alignment are violated
|
412 | 412 | // we want the error to be about the bounds.
|
413 | 413 | if let Some(align) = align {
|
414 |
| - if alloc_align.bytes() < align.bytes() { |
415 |
| - // The allocation itself is not aligned enough. |
416 |
| - // FIXME: Alignment check is too strict, depending on the base address that |
417 |
| - // got picked we might be aligned even if this check fails. |
418 |
| - // We instead have to fall back to converting to an integer and checking |
419 |
| - // the "real" alignment. |
420 |
| - throw_ub!(AlignmentCheckFailed { has: alloc_align, required: align }); |
| 414 | + if M::force_int_for_alignment_check(&self.extra) { |
| 415 | + let bits = self |
| 416 | + .force_bits(ptr.into(), self.pointer_size()) |
| 417 | + .expect("ptr-to-int cast for align check should never fail"); |
| 418 | + check_offset_align(bits.try_into().unwrap(), align)?; |
| 419 | + } else { |
| 420 | + // Check allocation alignment and offset alignment. |
| 421 | + if alloc_align.bytes() < align.bytes() { |
| 422 | + throw_ub!(AlignmentCheckFailed { has: alloc_align, required: align }); |
| 423 | + } |
| 424 | + check_offset_align(ptr.offset.bytes(), align)?; |
421 | 425 | }
|
422 |
| - check_offset_align(ptr.offset.bytes(), align)?; |
423 | 426 | }
|
424 | 427 |
|
425 | 428 | // We can still be zero-sized in this branch, in which case we have to
|
|
0 commit comments