Skip to content

Commit a64e5d9

Browse files
committed
update UB
1 parent 2b29a56 commit a64e5d9

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

src/behavior-considered-undefined.md

+34-22
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,56 @@ code.
2323
</div>
2424

2525
* Data races.
26-
* Dereferencing a null or dangling raw pointer.
27-
* Unaligned pointer reading and writing outside of [`read_unaligned`]
28-
and [`write_unaligned`].
29-
* Reads of [undef] \(uninitialized) memory.
30-
* Breaking the [pointer aliasing rules] on accesses through raw pointers;
31-
a subset of the rules used by C.
32-
* `&mut T` and `&T` follow LLVM’s scoped [noalias] model, except if the `&T`
33-
contains an [`UnsafeCell<U>`].
34-
* Mutating non-mutable data &mdash; that is, data reached through a shared
26+
* Dereferencing (using the `*` operator on) a dangling or unaligned raw pointer.
27+
* Breaking the [pointer aliasing rules]. `&mut T` and `&T` follow LLVM’s scoped
28+
[noalias] model, except if the `&T` contains an [`UnsafeCell<U>`].
29+
* Mutating non-mutable data (that is, data reached through a shared
3530
reference or data owned by a `let` binding), unless that data is contained
3631
within an [`UnsafeCell<U>`].
37-
* Invoking undefined behavior via compiler intrinsics:
38-
* Indexing outside of the bounds of an object with [`offset`] with
39-
the exception of one byte past the end of the object.
40-
* Using [`std::ptr::copy_nonoverlapping_memory`], a.k.a. the `memcpy32`and
41-
`memcpy64` intrinsics, on overlapping buffers.
42-
* Invalid values in primitive types, even in private fields and locals:
43-
* Dangling or null references and boxes.
32+
* Invoking undefined behavior via compiler intrinsics.
33+
* Executing code compiled with platform features that the current platform
34+
does not support (see [`target_feature`]).
35+
* Unwinding into another language.
36+
* Producing an invalid value, even in private fields and locals. "Producing" a
37+
value happens any time a value is assigned, passed to a function/primitive
38+
operation or returned from a function/primitive operation.
39+
The following values are invalid (at their respective type):
4440
* A value other than `false` (`0`) or `true` (`1`) in a `bool`.
4541
* A discriminant in an `enum` not included in the type definition.
42+
* A null `fn` pointer.
4643
* A value in a `char` which is a surrogate or above `char::MAX`.
44+
* A `!` (all values are invalid for this type).
45+
* A dangling or unaligned reference or `Box`, or one that points to an invalid value.
46+
* Invalid metadata in a wide reference, `Box` or raw pointer:
47+
* slice metadata is invalid if the slice has a total size larger than
48+
`isize::MAX` bytes in memory.
49+
* `dyn Trait` metadata is invalid if it is not a pointer to a vtable for
50+
`Trait` that matches the actual dynamic trait the reference points to.
4751
* Non-UTF-8 byte sequences in a `str`.
48-
* Executing code compiled with platform features that the current platform
49-
does not support (see [`target_feature`]).
52+
* [Uninitialized memory][undef] in the value of an integer (`i*`/`u*`),
53+
floating point value (`f*`), or raw pointer.
54+
* Invalid values for a type with a custom definition of invalid values, such
55+
as a `NonNull` that is null. (Requesting custom invalid values is an
56+
unstable feature, but some stable libstd types, like `NonNull`, make use of
57+
it.)
5058

5159
> **Note**: Undefined behavior affects the entire program. For example, calling
5260
> a function in C that exhibits undefined behavior of C means your entire
5361
> program contains undefined behaviour that can also affect the Rust code. And
5462
> vice versa, undefined behavior in Rust can cause adverse affects on code
5563
> executed by any FFI calls to other languages.
5664
65+
A reference/pointer is "dangling" if it is null or not all of the bytes it
66+
points to are part of the same allocation (so in particular they all have to be
67+
part of *some* allocation). The span of bytes it points to is determined by the
68+
pointer value and the size of the pointee type. As a consequence, if the span is
69+
empty, "dangling" is the same as "non-null". Note that slices point to their
70+
entire range, so it is very important that the length metadata is never too
71+
large.
72+
5773
[noalias]: http://llvm.org/docs/LangRef.html#noalias
5874
[pointer aliasing rules]: http://llvm.org/docs/LangRef.html#pointer-aliasing-rules
5975
[undef]: http://llvm.org/docs/LangRef.html#undefined-values
60-
[`offset`]: ../std/primitive.pointer.html#method.offset
61-
[`std::ptr::copy_nonoverlapping_memory`]: ../std/ptr/fn.copy_nonoverlapping.html
6276
[`target_feature`]: attributes/codegen.md#the-target_feature-attribute
6377
[`UnsafeCell<U>`]: ../std/cell/struct.UnsafeCell.html
64-
[`read_unaligned`]: ../std/ptr/fn.read_unaligned.html
65-
[`write_unaligned`]: ../std/ptr/fn.write_unaligned.html
6678
[Rustonomicon]: ../nomicon/index.html

0 commit comments

Comments
 (0)