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
Copy file name to clipboardExpand all lines: reference/src/glossary.md
+20-1
Original file line number
Diff line number
Diff line change
@@ -177,6 +177,24 @@ zero-sized type", to refer to zero-sized types with an alignment requirement of
177
177
For example, `()` is a "1-ZST" but `[u16; 0]` is not because it has an alignment
178
178
requirement of 2.
179
179
180
+
#### Padding
181
+
[padding]: #padding
182
+
183
+
*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.
184
+
185
+
Padding can be though of as `[Pad; N]` for some hypothetical type `Pad` (of size 1) with the following properties:
186
+
*`Pad` is valid for any byte, i.e., it has the same validity invariant as `MaybeUninit<u8>`.
187
+
* 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.
188
+
189
+
We can also define padding in terms of the [representation relation]:
190
+
A byte at index `i` is a padding byte for type `T` if,
191
+
for all values `v` and lists of bytes `b` such that `v` and `b` are related at `T` (let's write this `Vrel_T(v, b)`),
192
+
changing `b` at index `i` to any other byte yields a `b'` such `v` and `b'` are related (`Vrel_T(v, b')`).
193
+
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.
194
+
195
+
This definition works fine for product types (structs, tuples, arrays, ...).
196
+
The desired notion of "padding byte" for enums and unions is still unclear.
197
+
180
198
#### Place
181
199
182
200
A *place* (called "lvalue" in C and "glvalue" in C++) is the result of computing a [*place expression*][place-value-expr].
@@ -200,6 +218,7 @@ Values are ephemeral; they arise during the computation of an instruction but ar
200
218
(This is comparable to how run-time data in a program is ephemeral and is only ever persisted in serialized form.)
A *representation* of a [value](#value) is a list of bytes that is used to store or "represent" that value in memory.
205
224
@@ -208,7 +227,7 @@ The term "relation" here is used in the mathematical sense: the representation r
208
227
209
228
The relation should be functional for a fixed list of bytes (i.e., every list of bytes has at most one associated representation).
210
229
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`).
211
-
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 so changing it does not affect the value represented by a list of bytes).
230
+
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).
212
231
213
232
See the [value domain][value-domain] for an example how values and representation relations can be made more precise.
Copy file name to clipboardExpand all lines: wip/value-domain.md
+7-1
Original file line number
Diff line number
Diff line change
@@ -64,6 +64,12 @@ But this means there are likely many false negatives, and the final Rust spec wi
64
64
65
65
The value relation for the `()` type relates the empty tuple `Tuple([])` (assuming we can use array notation to "match" on `Vec`) with the empty byte list `[]`, and that's it.
66
66
67
+
### `Pad`
68
+
69
+
The [hypothetical `Pad` type][pad] relates the empty tuple `Tuple([])` with any byte list of length 1.
The value relation for the `!` type is empty: nothing is related to anything at this type.
@@ -130,7 +136,7 @@ We can implement `TypedMemory` for any `Memory` as follows:
130
136
In particular, this defines the semantics of a "typed copy": when an assignment is executed in Rust (at some type `T`), or when an argument of type `T` is passed to a function or a return value of type `T` returned from a function, this is called a "typed copy".
131
137
Its semantics is basically to do a `typed_read` at `T` followed by a `typed_write` at `T`.
132
138
In particular, this means doing a "typed copy" at `T` of some memory that has no valid representation at `T` is undefined behavior!
133
-
This also means that for types that have padding, the "typed copy" does not preserve the padding bytes.
139
+
This also means that for `Pad` and more generally for types that have padding, the "typed copy" does not preserve the padding bytes.
0 commit comments