Skip to content

Commit e84a179

Browse files
committed
define provenance
1 parent 3e4c891 commit e84a179

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

reference/src/glossary.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ bytes.
1010
**Note**: a full aliasing model for Rust, defining when aliasing is allowed
1111
and when not, has not yet been defined. The purpose of this definition is to
1212
define when aliasing *happens*, not when it is *allowed*. The most developed
13-
potential aliasing model so far is known as "Stacked Borrows", and can be found
14-
[here](https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md).
13+
potential aliasing model so far is [Stacked Borrows][stacked-borrows].
1514

1615
Consider the following example:
1716

@@ -56,6 +55,24 @@ somewhat differently from this definition. However, that's considered a low
5655
level detail of a particular Rust implementation. When programming Rust, the
5756
Abstract Rust Machine is intended to operate according to the definition here.
5857

58+
#### (Pointer) Provenance
59+
60+
The *provenance* of a pointer can be used to distinguish pointers that point to the same memory location.
61+
For example, doing pointer arithmetic "remembers" the original allocation to which the pointer pointed, so it is impossible to cross allocation boundaries using pointer arithmetic:
62+
63+
```rust
64+
let raw1 = Box::into_raw(Box::new(13u8));
65+
let raw2 = Box::into_raw(Box::new(42u8));
66+
let raw2_wrong = raw1.wrapping_add(raw2.wrapping_sub(raw1 as usize) as usize);
67+
// Now raw2 and raw2_wrong have same *address*...
68+
assert_eq!(raw2 as usize, raw2_wrong as usize);
69+
// ...but it would be UB to use raw2_wrong, as it was obtained by
70+
// cross-allocation arithmetic. raw2_wrong has the wrong *provenance*.
71+
```
72+
73+
Another example of pointer provenance is the "tag" from [Stacked Borrows][stacked-borrows].
74+
For some more information, see [this blog post](https://www.ralfj.de/blog/2018/07/24/pointers-and-bytes.html) and [this document proposing a more precise definition of provenance for C](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2364.pdf).
75+
5976
#### Interior mutability
6077

6178
*Interior Mutation* means mutating memory where there also exists a live shared reference pointing to the same memory; or mutating memory through a pointer derived from a shared reference.
@@ -140,7 +157,8 @@ requirement of 2.
140157

141158
### TODO
142159

143-
* *tag*
144160
* *rvalue*
145161
* *lvalue*
146162
* *representation*
163+
164+
[stacked-borrows]: https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md

0 commit comments

Comments
 (0)