Skip to content

Commit 7cbdc9c

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 7cbdc9c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Diff for: library/core/src/alloc/layout.rs

+4
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,14 @@ 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.
184186
/// - a [trait object], then the vtable part of the pointer must point
185187
/// to a valid vtable for the type `T` acquired by an unsizing coercion,
186188
/// and the size of the *entire value*
187189
/// (dynamic tail length + statically sized prefix) must fit in `isize`.
190+
/// The pointer address plus the size of the entire value must not
191+
/// overflow the address space.
188192
/// - an (unstable) [extern type], then this function is always safe to
189193
/// call, but may panic or otherwise return the wrong value, as the
190194
/// extern type's layout is not known. This is the same behavior as

Diff for: library/core/src/mem/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,13 @@ 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.
362364
/// - a [trait object], then the vtable part of the pointer must point
363365
/// to a valid vtable acquired by an unsizing coercion, and the size
364366
/// of the *entire value* (dynamic tail length + statically sized prefix)
365-
/// must fit in `isize`.
367+
/// must fit in `isize`. The pointer address plus the size of the entire
368+
/// value must not overflow the address space.
366369
/// - an (unstable) [extern type], then this function is always safe to
367370
/// call, but may panic or otherwise return the wrong value, as the
368371
/// extern type's layout is not known. This is the same behavior as
@@ -506,10 +509,15 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
506509
/// - a [slice], then the length of the slice tail must be an initialized
507510
/// integer, and the size of the *entire value*
508511
/// (dynamic tail length + statically sized prefix) must fit in `isize`.
512+
/// The pointer address plus the size of the entire value must not
513+
/// overflow the address space. The value one-past-the-end of this range
514+
/// must also be within the address space.
509515
/// - a [trait object], then the vtable part of the pointer must point
510516
/// to a valid vtable acquired by an unsizing coercion, and the size
511517
/// of the *entire value* (dynamic tail length + statically sized prefix)
512-
/// must fit in `isize`.
518+
/// must fit in `isize`. The pointer address plus the size of the entire
519+
/// value must not overflow the address space. The value one-past-the-end
520+
/// of this range must also be within the address space.
513521
/// - an (unstable) [extern type], then this function is always safe to
514522
/// call, but may panic or otherwise return the wrong value, as the
515523
/// extern type's layout is not known. This is the same behavior as

0 commit comments

Comments
 (0)