1
Fork 0

Rollup merge of #136438 - RalfJung:offset_from_ub_errors, r=oli-obk

miri: improve error when offset_from preconditions are violated

Fixes https://github.com/rust-lang/miri/issues/4143
This commit is contained in:
许杰友 Jieyou Xu (Joe) 2025-02-03 19:13:27 +08:00 committed by GitHub
commit 43764db758
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 81 additions and 19 deletions

View file

@ -280,7 +280,9 @@ const_eval_nullary_intrinsic_fail =
could not evaluate nullary intrinsic
const_eval_offset_from_different_allocations =
`{$name}` called on pointers into different allocations
`{$name}` called on two different pointers that are not both derived from the same allocation
const_eval_offset_from_out_of_bounds =
`{$name}` called on two different pointers where the memory range between them is not in-bounds of an allocation
const_eval_offset_from_overflow =
`{$name}` called when first pointer is too far ahead of second
const_eval_offset_from_test =

View file

@ -319,7 +319,25 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
// Check that the memory between them is dereferenceable at all, starting from the
// origin pointer: `dist` is `a - b`, so it is based on `b`.
self.check_ptr_access_signed(b, dist, CheckInAllocMsg::OffsetFromTest)?;
self.check_ptr_access_signed(b, dist, CheckInAllocMsg::OffsetFromTest)
.map_err_kind(|_| {
// This could mean they point to different allocations, or they point to the same allocation
// but not the entire range between the pointers is in-bounds.
if let Ok((a_alloc_id, ..)) = self.ptr_try_get_alloc_id(a, 0)
&& let Ok((b_alloc_id, ..)) = self.ptr_try_get_alloc_id(b, 0)
&& a_alloc_id == b_alloc_id
{
err_ub_custom!(
fluent::const_eval_offset_from_out_of_bounds,
name = intrinsic_name,
)
} else {
err_ub_custom!(
fluent::const_eval_offset_from_different_allocations,
name = intrinsic_name,
)
}
})?;
// Then check that this is also dereferenceable from `a`. This ensures that they are
// derived from the same allocation.
self.check_ptr_access_signed(