offset_from: "the difference must fit in an isize" is a corollary
also, isize::MIN is an impossible distance
This commit is contained in:
parent
7d97c59438
commit
273d253ce6
@ -301,9 +301,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
|
||||
}
|
||||
// The signed form of the intrinsic allows this. If we interpret the
|
||||
// difference as isize, we'll get the proper signed difference. If that
|
||||
// seems *positive*, they were more than isize::MAX apart.
|
||||
// seems *positive* or equal to isize::MIN, they were more than isize::MAX apart.
|
||||
let dist = val.to_target_isize(self)?;
|
||||
if dist >= 0 {
|
||||
if dist >= 0 || i128::from(dist) == self.pointer_size().signed_int_min() {
|
||||
throw_ub_custom!(
|
||||
fluent::const_eval_offset_from_underflow,
|
||||
name = intrinsic_name,
|
||||
|
@ -611,8 +611,7 @@ impl<T: ?Sized> *const T {
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// If any of the following conditions are violated, the result is Undefined
|
||||
/// Behavior:
|
||||
/// If any of the following conditions are violated, the result is Undefined Behavior:
|
||||
///
|
||||
/// * `self` and `origin` must either
|
||||
///
|
||||
@ -623,26 +622,10 @@ impl<T: ?Sized> *const T {
|
||||
/// * The distance between the pointers, in bytes, must be an exact multiple
|
||||
/// of the size of `T`.
|
||||
///
|
||||
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
|
||||
///
|
||||
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
|
||||
///
|
||||
/// Rust types are never larger than `isize::MAX` and Rust allocations never wrap around the
|
||||
/// address space, so two pointers within some value of any Rust type `T` will always satisfy
|
||||
/// the last two conditions. The standard library also generally ensures that allocations
|
||||
/// never reach a size where an offset is a concern. For instance, `Vec` and `Box` ensure they
|
||||
/// never allocate more than `isize::MAX` bytes, so `ptr_into_vec.offset_from(vec.as_ptr())`
|
||||
/// always satisfies the last two conditions.
|
||||
///
|
||||
/// Most platforms fundamentally can't even construct such a large allocation.
|
||||
/// For instance, no known 64-bit platform can ever serve a request
|
||||
/// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
|
||||
/// However, some 32-bit and 16-bit platforms may successfully serve a request for
|
||||
/// more than `isize::MAX` bytes with things like Physical Address
|
||||
/// Extension. As such, memory acquired directly from allocators or memory
|
||||
/// mapped files *may* be too large to handle with this function.
|
||||
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
|
||||
/// such large allocations either.)
|
||||
/// As a consequence, the absolute distance between the pointers, **in bytes**, computed on
|
||||
/// mathematical integers (without "wrapping around"), cannot overflow an `isize`. This is
|
||||
/// implied by the in-bounds requirement, and the fact that no allocated object can be larger
|
||||
/// than `isize::MAX` bytes.
|
||||
///
|
||||
/// The requirement for pointers to be derived from the same allocated object is primarily
|
||||
/// needed for `const`-compatibility: the distance between pointers into *different* allocated
|
||||
|
@ -836,8 +836,7 @@ impl<T: ?Sized> *mut T {
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// If any of the following conditions are violated, the result is Undefined
|
||||
/// Behavior:
|
||||
/// If any of the following conditions are violated, the result is Undefined Behavior:
|
||||
///
|
||||
/// * `self` and `origin` must either
|
||||
///
|
||||
@ -848,26 +847,10 @@ impl<T: ?Sized> *mut T {
|
||||
/// * The distance between the pointers, in bytes, must be an exact multiple
|
||||
/// of the size of `T`.
|
||||
///
|
||||
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
|
||||
///
|
||||
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
|
||||
///
|
||||
/// Rust types are never larger than `isize::MAX` and Rust allocations never wrap around the
|
||||
/// address space, so two pointers within some value of any Rust type `T` will always satisfy
|
||||
/// the last two conditions. The standard library also generally ensures that allocations
|
||||
/// never reach a size where an offset is a concern. For instance, `Vec` and `Box` ensure they
|
||||
/// never allocate more than `isize::MAX` bytes, so `ptr_into_vec.offset_from(vec.as_ptr())`
|
||||
/// always satisfies the last two conditions.
|
||||
///
|
||||
/// Most platforms fundamentally can't even construct such a large allocation.
|
||||
/// For instance, no known 64-bit platform can ever serve a request
|
||||
/// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
|
||||
/// However, some 32-bit and 16-bit platforms may successfully serve a request for
|
||||
/// more than `isize::MAX` bytes with things like Physical Address
|
||||
/// Extension. As such, memory acquired directly from allocators or memory
|
||||
/// mapped files *may* be too large to handle with this function.
|
||||
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
|
||||
/// such large allocations either.)
|
||||
/// As a consequence, the absolute distance between the pointers, in bytes, computed on
|
||||
/// mathematical integers (without "wrapping around"), cannot overflow an `isize`. This is
|
||||
/// implied by the in-bounds requirement, and the fact that no allocated object can be larger
|
||||
/// than `isize::MAX` bytes.
|
||||
///
|
||||
/// The requirement for pointers to be derived from the same allocated object is primarily
|
||||
/// needed for `const`-compatibility: the distance between pointers into *different* allocated
|
||||
|
@ -761,38 +761,21 @@ impl<T: ?Sized> NonNull<T> {
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// If any of the following conditions are violated, the result is Undefined
|
||||
/// Behavior:
|
||||
/// If any of the following conditions are violated, the result is Undefined Behavior:
|
||||
///
|
||||
/// * Both `self` and `origin` must be either in bounds or one
|
||||
/// byte past the end of the same [allocated object].
|
||||
/// * `self` and `origin` must either
|
||||
///
|
||||
/// * Both pointers must be *derived from* a pointer to the same object.
|
||||
/// (See below for an example.)
|
||||
/// * both be *derived from* a pointer to the same [allocated object], and the memory range between
|
||||
/// the two pointers must be either empty or in bounds of that object. (See below for an example.)
|
||||
/// * or both be derived from an integer literal/constant, and point to the same address.
|
||||
///
|
||||
/// * The distance between the pointers, in bytes, must be an exact multiple
|
||||
/// of the size of `T`.
|
||||
///
|
||||
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
|
||||
///
|
||||
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
|
||||
///
|
||||
/// Rust types are never larger than `isize::MAX` and Rust allocations never wrap around the
|
||||
/// address space, so two pointers within some value of any Rust type `T` will always satisfy
|
||||
/// the last two conditions. The standard library also generally ensures that allocations
|
||||
/// never reach a size where an offset is a concern. For instance, `Vec` and `Box` ensure they
|
||||
/// never allocate more than `isize::MAX` bytes, so `ptr_into_vec.offset_from(vec.as_ptr())`
|
||||
/// always satisfies the last two conditions.
|
||||
///
|
||||
/// Most platforms fundamentally can't even construct such a large allocation.
|
||||
/// For instance, no known 64-bit platform can ever serve a request
|
||||
/// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
|
||||
/// However, some 32-bit and 16-bit platforms may successfully serve a request for
|
||||
/// more than `isize::MAX` bytes with things like Physical Address
|
||||
/// Extension. As such, memory acquired directly from allocators or memory
|
||||
/// mapped files *may* be too large to handle with this function.
|
||||
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
|
||||
/// such large allocations either.)
|
||||
/// As a consequence, the absolute distance between the pointers, in bytes, computed on
|
||||
/// mathematical integers (without "wrapping around"), cannot overflow an `isize`. This is
|
||||
/// implied by the in-bounds requirement, and the fact that no allocated object can be larger
|
||||
/// than `isize::MAX` bytes.
|
||||
///
|
||||
/// The requirement for pointers to be derived from the same allocated object is primarily
|
||||
/// needed for `const`-compatibility: the distance between pointers into *different* allocated
|
||||
|
@ -92,6 +92,14 @@ pub const TOO_FAR_APART2: isize = {
|
||||
unsafe { ptr_offset_from(ptr1, ptr2) } //~ERROR evaluation of constant value failed
|
||||
//~| too far before
|
||||
};
|
||||
pub const TOO_FAR_APART3: isize = {
|
||||
let ptr1 = &0u8 as *const u8;
|
||||
let ptr2 = ptr1.wrapping_offset(isize::MIN);
|
||||
// The result of this would be `isize::MIN`, which *does* fit in an `isize`, but its
|
||||
// absolute value does not. (Also anyway there cannot be an allocation of that size.)
|
||||
unsafe { ptr_offset_from(ptr1, ptr2) } //~ERROR evaluation of constant value failed
|
||||
//~| too far before
|
||||
};
|
||||
|
||||
const WRONG_ORDER_UNSIGNED: usize = {
|
||||
let a = ['a', 'b', 'c'];
|
||||
|
@ -60,13 +60,19 @@ LL | unsafe { ptr_offset_from(ptr1, ptr2) }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called when first pointer is too far before second
|
||||
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $DIR/offset_from_ub.rs:99:14
|
||||
--> $DIR/offset_from_ub.rs:100:14
|
||||
|
|
||||
LL | unsafe { ptr_offset_from(ptr1, ptr2) }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from` called when first pointer is too far before second
|
||||
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $DIR/offset_from_ub.rs:107:14
|
||||
|
|
||||
LL | unsafe { ptr_offset_from_unsigned(p, p.add(2) ) }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer has smaller offset than second: 0 < 8
|
||||
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $DIR/offset_from_ub.rs:106:14
|
||||
--> $DIR/offset_from_ub.rs:114:14
|
||||
|
|
||||
LL | unsafe { ptr_offset_from_unsigned(ptr2, ptr1) }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ptr_offset_from_unsigned` called when first pointer is too far ahead of second
|
||||
@ -79,7 +85,7 @@ error[E0080]: evaluation of constant value failed
|
||||
note: inside `std::ptr::const_ptr::<impl *const u8>::offset_from`
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
note: inside `OFFSET_VERY_FAR1`
|
||||
--> $DIR/offset_from_ub.rs:115:14
|
||||
--> $DIR/offset_from_ub.rs:123:14
|
||||
|
|
||||
LL | unsafe { ptr2.offset_from(ptr1) }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -92,11 +98,11 @@ error[E0080]: evaluation of constant value failed
|
||||
note: inside `std::ptr::const_ptr::<impl *const u8>::offset_from`
|
||||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
||||
note: inside `OFFSET_VERY_FAR2`
|
||||
--> $DIR/offset_from_ub.rs:121:14
|
||||
--> $DIR/offset_from_ub.rs:129:14
|
||||
|
|
||||
LL | unsafe { ptr1.offset_from(ptr2.wrapping_offset(1)) }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 13 previous errors
|
||||
error: aborting due to 14 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0080`.
|
||||
|
Loading…
x
Reference in New Issue
Block a user