You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have tried to document everything I believe we have consensus on. I've
left some things open that I possibly could have closed, but because
this PR is very big, I would like to focus on getting it in as quickly
as possible and worrying about whatever's left aftwards.
I strongly encourage others to submit follow up PRs to close out the
other open issues.
Closesrust-lang#156.
Closesrust-lang#298.
Closesrust-lang#352.
Copy file name to clipboardexpand all lines: reference/src/glossary.md
+28-7
Original file line number
Diff line number
Diff line change
@@ -190,8 +190,9 @@ guarantee that `Option<&mut T>` has the same size as `&mut T`.
190
190
191
191
While all niches are invalid bit-patterns, not all invalid bit-patterns are
192
192
niches. For example, the "all bits uninitialized" is an invalid bit-pattern for
193
-
`&mut T`, but this bit-pattern cannot be used by layout optimizations, and is not a
194
-
niche.
193
+
`&mut T`, but this bit-pattern cannot be used by layout optimizations, and is not a niche.
194
+
195
+
It is a surprisingly common misconception that niches can occur in [padding] bytes. They cannot: A niche representation must be invalid for `T`. But a padding byte must be irrelevant to the value of `T`. It follows that if you take a niche representation of `T`, and change any of the padding bytes to any other values, then the result must still be a niche representation of `T`. If a niche were contained entirely in padding, that would mean that `T` was entirely niches and, consequently, uninhabited.
195
196
196
197
#### Zero-sized type / ZST
197
198
@@ -207,6 +208,8 @@ requirement of 2.
207
208
208
209
*Padding* (of a type `T`) refers to the space that the compiler leaves between fields of a struct or enum variant to satisfy alignment requirements, and before/after variants of a union or enum to make all variants equally sized.
209
210
211
+
Padding for a type is either [interior padding], which is part of one or more fields, or [exterior padding], which is before, between, or after the fields.
212
+
210
213
Padding can be though of as `[Pad; N]` for some hypothetical type `Pad` (of size 1) with the following properties:
211
214
*`Pad` is valid for any byte, i.e., it has the same validity invariant as `MaybeUninit<u8>`.
212
215
* Copying `Pad` ignores the source byte, and writes *any* value to the target byte. Or, equivalently (in terms of Abstract Machine behavior), copying `Pad` marks the target byte as uninitialized.
@@ -217,8 +220,26 @@ for all values `v` and lists of bytes `b` such that `v` and `b` are related at `
217
220
changing `b` at index `i` to any other byte yields a `b'` such `v` and `b'` are related (`Vrel_T(v, b')`).
218
221
In other words, the byte at index `i` is entirely ignored by `Vrel_T` (the value relation for `T`), and two lists of bytes that only differ in padding bytes relate to the same value(s), if any.
219
222
220
-
This definition works fine for product types (structs, tuples, arrays, ...).
221
-
The desired notion of "padding byte" for enums and unions is still unclear.
223
+
This definition works fine for product types (structs, tuples, arrays, ...) and for unions. The desired notion of "padding byte" for enums is still unclear.
224
+
225
+
#### Padding (exterior)
226
+
[exterior padding]: #exterior-padding
227
+
228
+
Exterior padding bytes are [padding] bytes that are not part of one or more fields. They are exactly the padding bytes that are not [interior padding], and therefore must be before, between, or after the fields of the type. Padding that comes after all fields is called [tail padding].
229
+
230
+
#### Padding (interior)
231
+
[interior padding]: #interior-padding
232
+
233
+
Interior padding bytes are [padding] bytes that are part of one or more fields of a type.
234
+
235
+
We can say that a field `f: F`*contains* the byte at index `i` in the type `T` if the layout of `T` places `f` at offset `j` and we have `j <= i < j + size_of::<F>()`. Then a padding byte is interior padding if and only if there exists a field `f` that contains it.
236
+
237
+
It follows that, provided `T` is not an enum, for any such `f`, the byte at index `i - j` in `F` is a padding byte of `F`. This is because all values of `f` give rise to distinct values of `T`.
238
+
239
+
#### Padding (tail)
240
+
[tail padding]: #tail-padding
241
+
242
+
Tail padding is [exterior padding] that comes after all fields of a type.
222
243
223
244
#### Place
224
245
@@ -254,8 +275,8 @@ The relation should be functional for a fixed list of bytes (i.e., every list of
254
275
It is partial in both directions: not all values have a representation (e.g. the mathematical integer `300` has no representation at type `u8`), and not all lists of bytes correspond to a value of a specific type (e.g. lists of the wrong size correspond to no value, and the list consisting of the single byte `0x10` corresponds to no value of type `bool`).
255
276
For a fixed value, there can be many representations (e.g., when considering type `#[repr(C)] Pair(u8, u16)`, the second byte is a [padding byte][padding] so changing it does not affect the value represented by a list of bytes).
256
277
257
-
See the [value domain][value-domain] for an example how values and representation relations can be made more precise.
278
+
See the [MiniRust page on values][minirust-values] for an example how values and representation relations can be made more precise.
@@ -29,8 +29,17 @@ largest field, and the offset of each union field within its variant. How these
29
29
are picked depends on certain constraints like, for example, the alignment
30
30
requirements of the fields, the `#[repr]` attribute of the `union`, etc.
31
31
32
-
[padding]: ../glossary.md#padding
33
-
[layout]: ../glossary.md#layout
32
+
Unions may contain both [exterior][exterior padding] and [interior padding]. In the below diagram, exterior padding is marked by `EXT`, interior padding by `INT`, and bytes that are padding bytes for a particular field but not padding for union as a whole are marked `NON`:
33
+
34
+
```text
35
+
[ EXT [ field0_0_ty | INT | field0_1_ty | INT ] EXT ]
36
+
[ EXT [ field1_0_ty | INT | NON NON NON | INT ] EXT ]
37
+
[ EXT | NON NON NON | INT [ field2_0_ty ] INT | EXT ]
38
+
```
39
+
40
+
It is necessarily the case that any byte that is a non-padding byte for any field is also a non-padding byte for the union. It is, in general, **unspecified** whether the converse is true. Specific reprs may specify whether or not bytes are padding bytes.
41
+
42
+
Padding bytes in unions has subtle implications; see the union [value model].
34
43
35
44
### Unions with default layout ("`repr(Rust)`")
36
45
@@ -40,6 +49,10 @@ layout of Rust unions is, _in general_, **unspecified**.
40
49
That is, there are no _general_ guarantees about the offset of the fields,
41
50
whether all fields have the same offset, what the call ABI of the union is, etc.
42
51
52
+
**Major footgun:** The layout of `#[repr(Rust)]` enums allows for the [interior padding footgun] to also exist with `#[repr(Rust)]`, and this behaviour *is* extant in Rustc as of this writing. It is [**TBD**][#354] whether it will be removed.
> **Note**: Fields are overlapped instead of laid out sequentially, so
142
-
> unlike structs there is no "between the fields" that could be filled
143
-
> with padding.
154
+
#### Interior Padding Footgun
155
+
156
+
**Major footgun:** On some platform ABIs, such as the obscure ARM64, C unions may also have [interior padding]*within* fields, where a byte is padding in every variant:
This is, surprisingly, undefined behaviour, because it appears that the union is fully initialized and therefore ought to be castable to a slice.
169
+
170
+
However, because byte 1 is a padding byte in both variants, it can be a padding byte in the union type as well. Fortunately, this counterintuitive behaviour is limited to obscure platforms like amd64.
171
+
172
+
**C/C++ compatibility hazard:** This footgun exists for compatibility with the C/C++ platform ABI, and it is not well-known in C/C++ communities. So whenever dealing with a union that might have internal padding, you should assume that C/C++ code may be handing you a loaded footguns.
144
173
145
174
#### Zero-sized fields
146
175
@@ -172,4 +201,48 @@ translation of that code into Rust will not produce a compatible result. Refer
172
201
to the [struct chapter](structs-and-tuples.md#c-compatible-layout-repr-c) for
173
202
further details.
174
203
204
+
<details><summary><b>Rationale</b></summary>
205
+
206
+
Look. It wasn't our idea.
207
+
208
+
We could try to limit the blast radius to `extern "C"` functions, but really, that's just sawing off the end of the footgun.
209
+
210
+
</details>
211
+
212
+
### Transparent layout (`#[repr(transparent)]`)
213
+
214
+
`#[repr(transparent)]` is currently unstable for unions, but [RFC 2645] documents most of its semantics. Notably, it causes unions to be passed using the same ABI as the non-1-ZST field.
215
+
216
+
**Major footgun:** Matching the interior ABI means that all padding bytes of the non-1-ZST field will also be padding bytes of the union, so the [interior mutability footgun] exists with `#[repr(transparent)]` unions.
217
+
218
+
**Note:** If `U` is a transparent union wrapping a `T`, `U` may not inherit `T`'s niches, and therefore `Option<U>` and `Option<T>`, for instance, will not necessarily have the same layout or even the same size.
219
+
220
+
This is because, if `U` contains any zero-sized fields in addition to the `T` field, the [value model] forces `U` to support uninitialized bytes, and that in turn prevents `T`'s niches from being present in `U`. Currently, `U` also supports uninitialized bytes if it does not contain any additional fields, but it is [**TBD**][#364] if single-field transparent unions might support niches.
There are applications where it is desirable that unions behave simply as a buffer of abstract bytes, with no constraints on validity and no interior padding bytes that can [get surprisingly reset to uninit][interior mutability footgun].
227
+
228
+
Thus, we propose that Rust support a repr, which we are tentatively calling the Raw-repr, which gives these semantics to unions. The Raw-repr may be `#[repr(Rust)]` or it may be a new repr, say `#[repr(Raw)`]. The Raw-repr will have the following properties:
229
+
230
+
* All fields are laid out at offset 0.
231
+
* The alignment of the union is the greatest alignment among fields.
232
+
* The only padding bytes are tail padding bytes, if any.
233
+
234
+
<details><summary><b>Rationale</b></summary>
235
+
236
+
We need at least one repr without the [interior mutability footgun]. This layout is extremely constrained, so it would generally be against the philosophy of `#[repr(Rust)]` to impose these constraints on the default layout instead of introducing a new one. However, without such constraints, `#[repr(Rust)]` is a just a giant, largely useless footgun, which is a rationale to simply constrain it and leave any potential relaxations, e.g. for safe transmutes and niches, to other reprs.
0 commit comments