Definition
Temporal memory safety is the property that a program accesses an object only during the object’s lifetime: after the object’s memory has been reserved and before that memory has been released. A violation is an access through a dangling pointer §4.1, one that still holds the address of an object whose lifetime has ended. Examples are use-after-free §4.6, an access to memory after it was freed, and double free §4.7, releasing the same memory twice. Temporal safety concerns when an access happens; spatial memory safety §2.28 concerns where it lands.
The idea
A host keeps a table of records and lets a guest program, code it runs but does not trust, look one up. The lookup returns the record’s address, and the guest stores it. Later an update deletes the record, the allocator, the host code that hands out memory, gives the same memory to a new record, and the guest uses its stored address. The address still lies in memory the guest may touch, so a runtime bounds check §2.27 passes, and the guest reads or overwrites a record that is not the one it looked up.
An address names a place, not an object. Objects come and go at the same place, so the right to access an object begins and ends in time, and a stored address records neither moment. A hotel key card is a fair picture: room 7 still exists after a guest checks out, a new guest soon sleeps there, and the lock must reject cards issued before the newest one. The picture stops being accurate in one respect: a raw address has no lock. It keeps working unless the system adds a check like the hotel’s, or ensures nobody still holds the address when the memory is reused.
How it works
One family of defenses keeps each object alive while anything can still use it, so no pointer dangles. The other lets the object die but makes every later use detectable.
Ownership and borrowing. One owner frees each object, and a borrowed pointer §4.3 may be used only within a scope that ends before the owner frees it. The Rust compiler enforces these rules before the program runs.
Reference counting §9.22. The object is freed when the count of pointers held to it reaches zero. Taking a pointer and incrementing the count must be one step with respect to deletion.
Garbage collection. The runtime frees only objects that no remaining pointer reaches.
Deferred reclamation. Read-copy update (RCU) §9.20 unlinks a deleted object at once but frees it only after a grace period §9.21, a wait until every reader that might hold a pointer to it has finished.
References limited to one run. A host promises that a pointer it gives a guest program stays valid until that run of the program ends, and frees nothing the guest can reach during the run. It clears the guest’s registers and scratch memory §2.24 before the next run and lets no host address into state that outlives a run.
Generation handles. The guest gets a handle instead of an address: a slot number in a host table plus a generation number. The table keeps each slot’s current generation and adds 1 whenever the slot’s object is freed. Each use compares the two generations and is refused if they differ.
Copies. A copy-only helper §2.22 copies the value into the guest’s own memory, so the guest never holds a host address.
Testing tools such as AddressSanitizer §10.22 (ASan) add detection without a guarantee: they hold freed memory back from reuse for a while and report accesses to it.
Worked example
Slot 2 of a host table holds record X at address 0x20000040, with a count field of 5; the slot’s generation is 5. A guest looks up X and keeps either the address 0x20000040 or the handle (slot 2, generation 5). An update deletes X, so the generation becomes , and a new record Y, with count 99, is placed in slot 2 at the same address.
With the raw address, the guest’s next read returns 99. A region check passes, because the address lies in the table’s region; the guest receives Y’s data believing it is X’s, and a write would corrupt Y. With the handle, the lookup compares generation 5 with 6, finds them unequal and refuses. The address is the same in both cases; only the generation tells X and Y apart.
A generation number has a fixed width and wraps around. In 2 bits its values run 0, 1, 2, 3, 0. A handle with generation 1 that survives 4 frees of its slot meets generation , stored as , and is wrongly accepted. In 32 bits, a slot freed once per microsecond, times per second, repeats a generation after seconds, about 71.6 minutes. In 64 bits the same rate takes seconds, about 584,542 years.
Stated precisely
Give each object a lifetime from the time when its memory is reserved to the time when it is released. An access at time through a pointer derived from is temporally safe exactly when . The object is the one the pointer was derived from, not whatever lies at the address: if the memory of now holds a new object , an access through the old pointer after is a violation although is alive there.
For generation handles, let slot have current generation , stored in bits and increased by 1 modulo each time the slot’s object is freed. A handle is accepted exactly when slot holds an object and . Every accepted access is temporally safe under two assumptions.
No repeat: between the creation of a handle and its last use, the slot’s object is freed fewer than times, so cannot return to .
Check and access together: the object cannot be freed between the comparison and the end of the access; otherwise the result is a time-of-check to time-of-use §9.26 (TOCTOU) error.
Limits and common mistakes
A bounds check cannot detect a temporal violation, because freed memory usually stays inside the same permitted region.
Generation numbers are subject to counter wraparound §6.21, as the example shows. A value that changes and returns to its old value, so that a comparison wrongly sees no change, is the ABA problem §9.25, named for the sequence of values A, B, A.
Reference counting has two classic faults. A lookup that reads a pointer and only then increments the count can lose a race with a deletion between those steps. And objects that point to each other keep each other’s counts above zero and are never freed, a memory leak §4.8.
Temporal safety does not make data current or permitted. A live object can hold an outdated value, and a program can hold a valid pointer to a live object it should no longer be allowed to use; withdrawing permission is a separate mechanism.
A double free is also a temporal violation: the second release acts on memory whose lifetime has ended, and it can corrupt the allocator’s own records.
Questions answered
Can a guest retain an address after an update frees its object?
Yes, a guest can retain the address: an address is a number, and freeing the object does not erase copies of it in the guest’s registers or memory. What matters is whether a later use succeeds. If the runtime checks only that each access falls in a permitted region, and the freed memory still lies in one, the use succeeds and reaches whatever object now occupies that memory: a use-after-free.
A safe runtime makes the retained address harmless in one of three ways. It never gives the guest a host address, only a copy of the data or a handle whose generation number is checked at every use. Or it keeps every object the guest can reach alive until the guest’s current run ends, clears the guest’s registers and scratch memory between runs, and lets no host address into state that outlives a run. Or it removes freed memory from the guest’s permitted regions before that memory is reused.
Related entries: Object lifetime (§4.1), Use-after-free (§4.6), BPF map (§2.18), Double free (§4.7), Read-copy update (RCU) (§9.20), Reference counting (§9.22), ABA problem (§9.25), Copy-only helper (§2.22).