Skip to content

Commit a403e9f

Browse files
committed
expand pointer provenance example
1 parent 41cc668 commit a403e9f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

reference/src/glossary.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,24 @@ Abstract Rust Machine is intended to operate according to the definition here.
5757

5858
#### (Pointer) Provenance
5959

60-
The *provenance* of a pointer can be used, in the Rust Abstract Machine, to distinguish pointers that point to the same memory address (i.e., pointers that, when cast to `usize`, will compare equal).
60+
The *provenance* of a pointer is used, in the Rust Abstract Machine, to distinguish pointers that point to the same memory address (i.e., pointers that, when cast to `usize`, will compare equal).
6161

6262
For example, we have to distinguish pointers to the same location if they originated from different allocations.
63-
A pointer "remembers" the original allocation to which it pointed.
64-
This is necessary to make it impossible for pointer arithmetic to cross allocation boundaries:
63+
After all, cross-allocation pointer arithmetic does not lead to usable pointers, so the Rust Abstract Machine *somehow* has to remember the original allocation to which a pointer pointed.
64+
It uses provenance to achieve this:
6565

6666
```rust
67+
// Let's assume the two allocations here have base addresses 0x100 and 0x200.
68+
// We write pointer provenance as `@N` where `N` is some kind of ID uniquely
69+
// identifying the allocation.
6770
let raw1 = Box::into_raw(Box::new(13u8));
6871
let raw2 = Box::into_raw(Box::new(42u8));
6972
let raw2_wrong = raw1.wrapping_add(raw2.wrapping_sub(raw1 as usize) as usize);
70-
// Now raw2 and raw2_wrong have same *address*...
73+
// These pointers now have the following values:
74+
// raw1 points to address 0x100 and has provenance @1.
75+
// raw2 points to address 0x200 and has provenance @2.
76+
// raw2_wrong points to address 0x200 and has provenance @1.
77+
// In other words, raw2 and raw2_wrong have same *address*...
7178
assert_eq!(raw2 as usize, raw2_wrong as usize);
7279
// ...but it would be UB to use raw2_wrong, as it was obtained by
7380
// cross-allocation arithmetic. raw2_wrong has the wrong *provenance*.

0 commit comments

Comments
 (0)