Padding can create objects larger than isize::MAX bytes #117265
Labels
A-layout
Area: Memory layout of types
C-bug
Category: This is a bug.
I-unsound
Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
rustc generally detects when a static type is larger than
isize::MAX
bytes, giving an error thatvalues of the type `...` are too big for the current architecture
. However, if a type has fields at mostisize::MAX
bytes long, and additional padding that brings it toisize::MAX + 1
bytes, then this error will not be generated. To illustrate, all four of these print0x80000000
(i.e.,isize::MAX + 1
):(The
black_box()
is to ensure that an object of size0x80000000
is actually created on the stack.)This is clearly unsound, since it breaks the size invariant of
Layout
, and since third-party crates may depend on types being no larger thanisize::MAX
for soundness. However, it's nontrivial to observe unexpected behavior from this using only safe APIs in the standard library, since placing the overlarge type within any other type, even arepr(transparent)
wrapper, will result in a compile error as expected, and the standard APIs tend to refer to&[T; 1]
when creating a slice from a reference.The text was updated successfully, but these errors were encountered: