Skip to content

Commit 05a2366

Browse files
authored
Rollup merge of #82751 - RalfJung:offset_from, r=dtolnay
improve offset_from docs `@thomcc` pointed out that the current docs leave it kind of unclear how one can satisfy the "no wrapping around `isize` or the address space" requirement of `offset_from`, so make the docs clearer about that. FWIW, I don't think I entirely agree with that second paragraph about large objects (that I left mostly unchanged here). LLVM, to my knowledge, fundamentally assumes that all allocations fit into an `isize::MAX`. So in that sense creating a larger allocation is simply UB. I would expect a guarantee that Rust heap allocation methods will never return allocations larger than `isize::MAX` (or rather, Rust heap allocation methods should require that the `Layout` is no larger than `isize::MAX`). However, I cannot find any such requirement documented currently. Large allocations are not mentioned at all in the allocator docs, which is quite surprising -- even if we say that such allocations are not insta-UB (which I think is incompatible with LLVM), they are still extremely footgunny since `ptr::offset`/`ptr::add` do not support offsetting by more than `isize::MAX` bytes. Furthermore, the allocator docs don't even say anything about allocations wrapping around the address space. But that is certainly something allocators must ensure never happens; we cannot expect clients to defend against this. Cc `@rust-lang/wg-allocators`
2 parents a5a825e + ebe0407 commit 05a2366

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

library/core/src/ptr/const_ptr.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -320,25 +320,31 @@ impl<T: ?Sized> *const T {
320320
/// * Both pointers must be *derived from* a pointer to the same object.
321321
/// (See below for an example.)
322322
///
323-
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
324-
///
325323
/// * The distance between the pointers, in bytes, must be an exact multiple
326324
/// of the size of `T`.
327325
///
326+
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
327+
///
328328
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
329329
///
330-
/// The compiler and standard library generally try to ensure allocations
331-
/// never reach a size where an offset is a concern. For instance, `Vec`
332-
/// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
333-
/// `ptr_into_vec.offset_from(vec.as_ptr())` is always safe.
330+
/// Rust types are never larger than `isize::MAX` and Rust allocations never wrap around the
331+
/// address space, so two pointers within some value of any Rust type `T` will always satisfy
332+
/// the last two conditions. The standard library also generally ensures that allocations
333+
/// never reach a size where an offset is a concern. For instance, `Vec` and `Box` ensure they
334+
/// never allocate more than `isize::MAX` bytes, so `ptr_into_vec.offset_from(vec.as_ptr())`
335+
/// always satisfies the last two conditions.
334336
///
335-
/// Most platforms fundamentally can't even construct such an allocation.
337+
/// Most platforms fundamentally can't even construct such a large allocation.
336338
/// For instance, no known 64-bit platform can ever serve a request
337339
/// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
338340
/// However, some 32-bit and 16-bit platforms may successfully serve a request for
339341
/// more than `isize::MAX` bytes with things like Physical Address
340342
/// Extension. As such, memory acquired directly from allocators or memory
341343
/// mapped files *may* be too large to handle with this function.
344+
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
345+
/// such large allocations either.)
346+
///
347+
/// [`add`]: #method.add
342348
///
343349
/// # Panics
344350
///

library/core/src/ptr/mut_ptr.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -491,25 +491,31 @@ impl<T: ?Sized> *mut T {
491491
/// * Both pointers must be *derived from* a pointer to the same object.
492492
/// (See below for an example.)
493493
///
494-
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
495-
///
496494
/// * The distance between the pointers, in bytes, must be an exact multiple
497495
/// of the size of `T`.
498496
///
497+
/// * The distance between the pointers, **in bytes**, cannot overflow an `isize`.
498+
///
499499
/// * The distance being in bounds cannot rely on "wrapping around" the address space.
500500
///
501-
/// The compiler and standard library generally try to ensure allocations
502-
/// never reach a size where an offset is a concern. For instance, `Vec`
503-
/// and `Box` ensure they never allocate more than `isize::MAX` bytes, so
504-
/// `ptr_into_vec.offset_from(vec.as_ptr())` is always safe.
501+
/// Rust types are never larger than `isize::MAX` and Rust allocations never wrap around the
502+
/// address space, so two pointers within some value of any Rust type `T` will always satisfy
503+
/// the last two conditions. The standard library also generally ensures that allocations
504+
/// never reach a size where an offset is a concern. For instance, `Vec` and `Box` ensure they
505+
/// never allocate more than `isize::MAX` bytes, so `ptr_into_vec.offset_from(vec.as_ptr())`
506+
/// always satisfies the last two conditions.
505507
///
506-
/// Most platforms fundamentally can't even construct such an allocation.
508+
/// Most platforms fundamentally can't even construct such a large allocation.
507509
/// For instance, no known 64-bit platform can ever serve a request
508510
/// for 2<sup>63</sup> bytes due to page-table limitations or splitting the address space.
509511
/// However, some 32-bit and 16-bit platforms may successfully serve a request for
510512
/// more than `isize::MAX` bytes with things like Physical Address
511513
/// Extension. As such, memory acquired directly from allocators or memory
512514
/// mapped files *may* be too large to handle with this function.
515+
/// (Note that [`offset`] and [`add`] also have a similar limitation and hence cannot be used on
516+
/// such large allocations either.)
517+
///
518+
/// [`add`]: #method.add
513519
///
514520
/// # Panics
515521
///

0 commit comments

Comments
 (0)