Skip to content

Commit

Permalink
explain place alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 23, 2023
1 parent f4876d6 commit 518e335
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/behavior-considered-undefined.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ code.
</div>

* Data races.
* Accessing (loading from or storing to) a place that is [dangling] or unaligned.
* Accessing (loading from or storing to) a place that is [dangling] or [based on
an unaligned pointer].
* Performing a place projection that violates the requirements of [in-bounds
pointer arithmetic][offset]. A place projection is a [field
expression][project-field], a [tuple index expression][project-tuple], or an
Expand Down Expand Up @@ -100,6 +101,20 @@ reading uninitialized memory is permitted are inside `union`s and in "padding"

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`).

### Places based on unaligned pointers
[based on an unaligned pointer]: #places-based-on-unaligned-pointers

A place is said to be "based on an unaligned pointer" if the last `*` projection
during place computation was performed on a pointer that was not aligned for its
type. (If there is no `*` projection in the place expression, then this is
accessing the field of a local and rustc will guarantee proper alignment.)

For instance, if `ptr` has struct type `S` with a field `f` of type `u8`, but
the alignment of `S` is 8, then `ptr` must be 8-aligned or else `(*ptr).f` is
"based on an unaligned pointer". In other words, the alignment requirement
derives from the type of the pointer that was dereferenced, *not* the type of
the field that is being accessed.

### Dangling pointers
[dangling]: #dangling-pointers

Expand Down

0 comments on commit 518e335

Please sign in to comment.