Skip to content

Commit 13bb102

Browse files
committed
constrain safety preconditions of layout_for_ptr functionality
This commit implements the recommendation of [1] to make the safety preconditions of the raw pointer layout utilities more conservative, to ease the path towards stabilization. In the future, we may (if we choose) remove some of these restrictions without breaking forwards compatibility. [1]: #69835 (comment)
1 parent cf226e9 commit 13bb102

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

library/core/src/alloc/layout.rs

+6
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,16 @@ impl Layout {
181181
/// - a [slice], then the length of the slice tail must be an initialized
182182
/// integer, and the size of the *entire value*
183183
/// (dynamic tail length + statically sized prefix) must fit in `isize`.
184+
/// The pointer address plus the size of the entire value must not
185+
/// overflow the address space. The value one-past-the-end of this range
186+
/// must also be within the address space.
184187
/// - a [trait object], then the vtable part of the pointer must point
185188
/// to a valid vtable for the type `T` acquired by an unsizing coercion,
186189
/// and the size of the *entire value*
187190
/// (dynamic tail length + statically sized prefix) must fit in `isize`.
191+
/// The pointer address plus the size of the entire value must not
192+
/// overflow the address space. The value one-past-the-end of this range
193+
/// must also be within the address space.
188194
/// - an (unstable) [extern type], then this function is always safe to
189195
/// call, but may panic or otherwise return the wrong value, as the
190196
/// extern type's layout is not known. This is the same behavior as

library/core/src/mem/mod.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,15 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
359359
/// - a [slice], then the length of the slice tail must be an initialized
360360
/// integer, and the size of the *entire value*
361361
/// (dynamic tail length + statically sized prefix) must fit in `isize`.
362+
/// The pointer address plus the size of the entire value must not
363+
/// overflow the address space. The value one-past-the-end of this range
364+
/// must also be within the address space.
362365
/// - a [trait object], then the vtable part of the pointer must point
363366
/// to a valid vtable acquired by an unsizing coercion, and the size
364367
/// of the *entire value* (dynamic tail length + statically sized prefix)
365-
/// must fit in `isize`.
368+
/// must fit in `isize`. The pointer address plus the size of the entire
369+
/// value must not overflow the address space. The value one-past-the-end
370+
/// of this range must also be within the address space.
366371
/// - an (unstable) [extern type], then this function is always safe to
367372
/// call, but may panic or otherwise return the wrong value, as the
368373
/// extern type's layout is not known. This is the same behavior as
@@ -506,10 +511,15 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
506511
/// - a [slice], then the length of the slice tail must be an initialized
507512
/// integer, and the size of the *entire value*
508513
/// (dynamic tail length + statically sized prefix) must fit in `isize`.
514+
/// The pointer address plus the size of the entire value must not
515+
/// overflow the address space. The value one-past-the-end of this range
516+
/// must also be within the address space.
509517
/// - a [trait object], then the vtable part of the pointer must point
510518
/// to a valid vtable acquired by an unsizing coercion, and the size
511519
/// of the *entire value* (dynamic tail length + statically sized prefix)
512-
/// must fit in `isize`.
520+
/// must fit in `isize`. The pointer address plus the size of the entire
521+
/// value must not overflow the address space. The value one-past-the-end
522+
/// of this range must also be within the address space.
513523
/// - an (unstable) [extern type], then this function is always safe to
514524
/// call, but may panic or otherwise return the wrong value, as the
515525
/// extern type's layout is not known. This is the same behavior as

0 commit comments

Comments
 (0)