Skip to content

Commit 8c18a31

Browse files
Isaac van BakelRalfJung
Isaac van Bakel
andcommitted
Define immutability UB in terms of bytes
This is part of the feedback on #1385. Ralf made the point that the immutability definition could be restated solely in terms of bytes, which has the added benefit of no longer requiring the note on padding (since it's a natural consequence of the byte version.) The new wording for shared references also clarifies the case of mutable references behind shared ones, and reintroduces some of the transitivity property that I removed in my previous commit. The wording is separate from that for immutable bindings, since those don't have transitive immutability. This also bumps the definition of bytes pointed to by references and pointers into its own subsection, so that it can be linked to by the UB definition, to avoid duplication. Co-authored-by: Ralf Jung <post@ralfj.de>
1 parent 3514a23 commit 8c18a31

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/behavior-considered-undefined.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ code.
4242
All this also applies when values of these
4343
types are passed in a (nested) field of a compound type, but not behind
4444
pointer indirections.
45-
* Mutating immutable data. All bytes inside a [`const`] item are immutable.
46-
Moreover, the bytes of a value pointed to by a shared reference, or bytes owned by an immutable binding are immutable, unless those bytes are part of an [`UnsafeCell<U>`].
47-
Immutability also affects bytes which are not reachable from safe code, such as padding; it also affects uninitialized bytes.
45+
* Mutating immutable bytes. All bytes inside a [`const`] item are immutable.
46+
The bytes owned by an immutable binding are immutable, unless those bytes are part of an [`UnsafeCell<U>`].
47+
48+
Moreover, the bytes [pointed to] by a shared reference, including transitively through other references (both shared and mutable) and `Box`es, are immutable: transitivity includes those references stored in fields of compound types.
4849

4950
A mutation is any write of more than 0 bytes which overlaps with any of the relevant bytes.
50-
Writes which do not modify the byte contents (i.e. writes of a byte's value to that byte) are still mutations.
51+
52+
> **Note**: Writes which do not modify the byte contents (i.e. writes of a byte's value to that byte) are still mutations.
5153
* Invoking undefined behavior via compiler intrinsics.
5254
* Executing code compiled with platform features that the current platform
5355
does not support (see [`target_feature`]), *except* if the platform explicitly documents this to be safe.
@@ -94,13 +96,16 @@ reading uninitialized memory is permitted are inside `union`s and in "padding"
9496
> vice versa, undefined behavior in Rust can cause adverse affects on code
9597
> executed by any FFI calls to other languages.
9698
99+
### Pointed-to bytes
100+
101+
The span of bytes a pointer or reference "points to" is determined by the pointer value and the size of the pointee type (using `size_of_val`).
102+
97103
### Dangling pointers
98104
[dangling]: #dangling-pointers
99105

100106
A reference/pointer is "dangling" if it is null or not all of the bytes it
101-
points to are part of the same live allocation (so in particular they all have to be
102-
part of *some* allocation). The span of bytes it points to is determined by the
103-
pointer value and the size of the pointee type (using `size_of_val`).
107+
[points to] are part of the same live allocation (so in particular they all have to be
108+
part of *some* allocation).
104109

105110
If the size is 0, then the pointer must either point inside of a live allocation
106111
(including pointing just after the last byte of the allocation), or it must be
@@ -124,3 +129,5 @@ must never exceed `isize::MAX`.
124129
[dereference expression]: expressions/operator-expr.md#the-dereference-operator
125130
[place expression context]: expressions.md#place-expressions-and-value-expressions
126131
[rules]: inline-assembly.md#rules-for-inline-assembly
132+
[points to]: #pointed-to-bytes
133+
[pointed to]: #pointed-to-bytes

0 commit comments

Comments
 (0)