Skip to content

Commit 88a22cf

Browse files
authored
Merge pull request #190 from RalfJung/soundness
define (un)sound and UB
2 parents 23569a8 + 53aec0b commit 88a22cf

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Diff for: reference/src/glossary.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn main() { unsafe {
129129
The *safety* invariant is an invariant that safe code may assume all data to uphold.
130130
This invariant is used to justify which operations safe code can perform.
131131
The safety invariant can be temporarily violated by unsafe code, but must always be upheld when interfacing with unknown safe code.
132-
It is not relevant when arguing whether some *program* has UB, but it is relevant when arguing whether some code safely encapsulates its unsafety -- IOW, it is relevant when arguing whether some *library* can be used by safe code to *cause* UB.
132+
It is not relevant when arguing whether some *program* has UB, but it is relevant when arguing whether some code safely encapsulates its unsafety -- in other words, it is relevant when arguing whether some *library* is [sound][soundness].
133133

134134
In terms of code, some data computed by `TERM` (possibly constructed from some `arguments` that can be *assumed* to satisfy the safety invariant) is valid at type `T` if and only if the following library function can be safely exposed to arbitrary (safe) code as part of the public library interface:
135135
```rust,ignore
@@ -146,6 +146,30 @@ Moreover, such unsafe code must not return a non-UTF-8 string to the "outside" o
146146
To summarize: *Data must always be valid, but it only must be safe in safe code.*
147147
For some more information, see [this blog post](https://www.ralfj.de/blog/2018/08/22/two-kinds-of-invariants.html).
148148

149+
#### Undefined Behavior
150+
[ub]: #undefined-behavior
151+
152+
*Undefined Behavior* is a concept of the contract between the Rust programmer and the compiler:
153+
The programmer promises that the code exhibits no undefined behavior.
154+
In return, the compiler promises to compile the code in a way that the final program does on the real hardware what the source program does according to the Rust Abstract Machine.
155+
If it turns out the program *does* have undefined behavior, the contract is void, and the program produced by the compiler is essentially garbage (in particular, it is not bound by any specification; the program does not even have to be well-formed executable code).
156+
157+
In Rust, the [Nomicon](https://doc.rust-lang.org/nomicon/what-unsafe-does.html) and the [Reference](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) both have a list of behavior that the language considers undefined.
158+
Rust promises that safe code cannot cause Undefined Behavior---the compiler and authors of unsafe code takes the burden of this contract on themselves.
159+
For unsafe code, however, the burden is still on the programmer.
160+
161+
Also see: [Soundness][soundness].
162+
163+
#### Soundness (of code / of a library)
164+
[soundness]: #soundness-of-code--of-a-library
165+
166+
*Soundness* is a type system concept (actually originating from the study of logics) and means that the type system is "correct" in the sense that well-typed programs actually have the desired properties.
167+
For Rust, this means well-typed programs cannot cause [Undefined Behavior][ub].
168+
This promise only extends to safe code however; for `unsafe` code, it is up to the programmer to uphold this contract.
169+
170+
Accordingly, we say that a library (or an individual function) is *sound* if it is impossible for safe code to cause Undefined Behavior using its public API.
171+
Conversely, the library/function is *unsound* if safe code *can* cause Undefined Behavior.
172+
149173
#### Layout
150174

151175
The *layout* of a type defines its size and alignment as well as the offsets of its subobjects (e.g. fields of structs/unions/enum/... or elements of arrays).

0 commit comments

Comments
 (0)