Undefined behaviour¶
Category: Hazards · Status: stub · Lessons: chapter 02, Shared state
One line: A program the language standard stops describing: once it has one, no requirement is placed on what it does, so a right answer on this build is not evidence of anything.
Also called: UB, undefined behavior.
How it connects¶
flowchart LR
n_dangling_pointer["Dangling pointer"]
n_data_race["Data race"]
n_undefined_behaviour["Undefined behaviour"]
n_dangling_pointer -->|can cause| n_undefined_behaviour
n_data_race -->|can cause| n_undefined_behaviour
classDef center stroke-width:3px
class n_undefined_behaviour center
classDef outside stroke-dasharray: 4 3
class n_dangling_pointer,n_data_race outside
- Can be caused by: Dangling pointer, Data race
- See also: Heisenbug, Weak memory models and reordering
In each language¶
| Rust | Behavior considered undefined ↗ lists what unsafe code must not do; safe code cannot reach any of it |
| Go | There is none of this kind for a racing program: the memory model ↗ defines what a racy read may observe rather than abandoning the program |
| C | Undefined behaviour ↗: the standard imposes no requirements, and a data race or a dead-frame read is one |
| C++ | Undefined behaviour ↗, with the same consequence |
| Java | Also none: JLS 17.4 ↗ gives unsynchronized reads weak guarantees, not undefined ones |
Where to read more¶
- In this library: Data race or race condition?
- In this library: Can a thread borrow a local variable?
- In this library: What does AddressSanitizer find that the race detector cannot?
- Reference: Wikipedia: Undefined behavior ↗