Skip to content

Commit e17be95

Browse files
committed
interpret: add missing alignment check in raw_eq
1 parent 600edc9 commit e17be95

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Diff for: compiler/rustc_const_eval/src/interpret/intrinsics.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -684,19 +684,19 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
684684
assert!(layout.is_sized());
685685

686686
let get_bytes = |this: &InterpCx<'tcx, M>,
687-
op: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>,
688-
size|
687+
op: &OpTy<'tcx, <M as Machine<'tcx>>::Provenance>|
689688
-> InterpResult<'tcx, &[u8]> {
690689
let ptr = this.read_pointer(op)?;
691-
let Some(alloc_ref) = self.get_ptr_alloc(ptr, size)? else {
690+
this.check_ptr_align(ptr, layout.align.abi)?;
691+
let Some(alloc_ref) = self.get_ptr_alloc(ptr, layout.size)? else {
692692
// zero-sized access
693693
return Ok(&[]);
694694
};
695695
alloc_ref.get_bytes_strip_provenance()
696696
};
697697

698-
let lhs_bytes = get_bytes(self, lhs, layout.size)?;
699-
let rhs_bytes = get_bytes(self, rhs, layout.size)?;
698+
let lhs_bytes = get_bytes(self, lhs)?;
699+
let rhs_bytes = get_bytes(self, rhs)?;
700700
Ok(Scalar::from_bool(lhs_bytes == rhs_bytes))
701701
}
702702
}

Diff for: tests/ui/intrinsics/intrinsic-raw_eq-const-bad.rs

+8
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,13 @@ const RAW_EQ_PTR: bool = unsafe {
1313
//~| unable to turn pointer into integer
1414
};
1515

16+
const RAW_EQ_NOT_ALIGNED: bool = unsafe {
17+
let arr = [0u8; 4];
18+
let aref = &*arr.as_ptr().cast::<i32>();
19+
std::intrinsics::raw_eq(aref, aref)
20+
//~^ ERROR evaluation of constant value failed
21+
//~| alignment
22+
};
23+
1624
pub fn main() {
1725
}

Diff for: tests/ui/intrinsics/intrinsic-raw_eq-const-bad.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ LL | std::intrinsics::raw_eq(&(&0), &(&1))
1313
= help: this code performed an operation that depends on the underlying bytes representing a pointer
1414
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
1515

16-
error: aborting due to 2 previous errors
16+
error[E0080]: evaluation of constant value failed
17+
--> $DIR/intrinsic-raw_eq-const-bad.rs:19:5
18+
|
19+
LL | std::intrinsics::raw_eq(aref, aref)
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ accessing memory with alignment 1, but alignment 4 is required
21+
22+
error: aborting due to 3 previous errors
1723

1824
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)