Skip to content

Commit a64394d

Browse files
authored
Merge pull request #1385 from ivanbakel/immutable-data-UB-clarification
Clarify UB around immutability & mutation
2 parents e94fb3d + f12eaec commit a64394d

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/behavior-considered-undefined.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ 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 data inside a [`const`] item is immutable. Moreover, all
46-
data reached through a shared reference or data owned by an immutable binding
47-
is immutable, unless that data is contained within an [`UnsafeCell<U>`].
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.
49+
50+
A mutation is any write of more than 0 bytes which overlaps with any of the relevant bytes (even if that write does not change the memory contents).
4851
* Invoking undefined behavior via compiler intrinsics.
4952
* Executing code compiled with platform features that the current platform
5053
does not support (see [`target_feature`]), *except* if the platform explicitly documents this to be safe.
@@ -91,13 +94,16 @@ reading uninitialized memory is permitted are inside `union`s and in "padding"
9194
> vice versa, undefined behavior in Rust can cause adverse affects on code
9295
> executed by any FFI calls to other languages.
9396
97+
### Pointed-to bytes
98+
99+
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`).
100+
94101
### Dangling pointers
95102
[dangling]: #dangling-pointers
96103

97104
A reference/pointer is "dangling" if it is null or not all of the bytes it
98-
points to are part of the same live allocation (so in particular they all have to be
99-
part of *some* allocation). The span of bytes it points to is determined by the
100-
pointer value and the size of the pointee type (using `size_of_val`).
105+
[points to] are part of the same live allocation (so in particular they all have to be
106+
part of *some* allocation).
101107

102108
If the size is 0, then the pointer must either point inside of a live allocation
103109
(including pointing just after the last byte of the allocation), or it must be
@@ -121,3 +127,5 @@ must never exceed `isize::MAX`.
121127
[dereference expression]: expressions/operator-expr.md#the-dereference-operator
122128
[place expression context]: expressions.md#place-expressions-and-value-expressions
123129
[rules]: inline-assembly.md#rules-for-inline-assembly
130+
[points to]: #pointed-to-bytes
131+
[pointed to]: #pointed-to-bytes

0 commit comments

Comments
 (0)