Skip to content

Commit 04047d8

Browse files
committed
Strengthen soundness proof for size_of_val_raw
Previously, we needed to rely on the fact that the instance of any valid Rust type with 0 elements has a size (in number of bytes) which is not greater than `isize::MAX`. Providing this as a guarantee turned out to be controversial. [1] This was made possible by rust-lang/rust#126152. [1] rust-lang/unsafe-code-guidelines#465 (comment)
1 parent b43acd9 commit 04047d8

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Diff for: src/macro_util.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,16 @@ macro_rules! trailing_field_offset {
144144
// - If `$ty` is not a slice DST, this pointer conversion will
145145
// fail due to "mismatched vtable kinds", and compilation will
146146
// fail.
147-
// - If `$ty` is a slice DST, the safety requirement is that "the
148-
// length of the slice tail must be an initialized integer, and
149-
// the size of the entire value (dynamic tail length +
150-
// statically sized prefix) must fit in isize." The length is
151-
// initialized to 0 above, and Rust guarantees that no type's
152-
// minimum size may overflow `isize`. [1]
147+
// - If `$ty` is a slice DST, we have constructed `zero_elems` to
148+
// have zero trailing slice elements. Per the `size_of_val_raw`
149+
// docs, "For the special case where the dynamic tail length is
150+
// 0, this function is safe to call." [2]
153151
//
154152
// [1] TODO(#429),
155153
// TODO(https://github.com/rust-lang/unsafe-code-guidelines/issues/465#issuecomment-1782206516):
156154
// Citation for this?
155+
//
156+
// [2] https://doc.rust-lang.org/nightly/std/mem/fn.size_of_val_raw.html
157157
unsafe {
158158
#[allow(clippy::as_conversions)]
159159
$crate::macro_util::core_reexport::mem::size_of_val_raw(zero_elems as *const $ty)

0 commit comments

Comments
 (0)