# When relative pointers survive relocation

A relative reference is useful only while its coordinate system still identifies the intended object. Moving a complete layout and copying a single pointer field are different operations.

## Worked byte-address cases

All addresses below are synthetic integers. No memory is dereferenced.

| Operation | Stored displacement or word | Decoded address | Intended address |
| --- | --- | --- | --- |
| Holder 0x1000 and target 0x1080 both move by 0x2000 | +0x80 | 0x3080 | 0x3080 |
| Holder moves 0x1000 to 0x3000; target moves 0x5080 to 0x9080 | +0x4080 | 0x7080 | 0x9080 |
| Encoded displacement is copied from holder 0x1000 to 0x1100 | +0x80 | 0x1180 | 0x1080 |
| Same target is assigned at the new holder 0x1100 | -0x80 | 0x1080 | 0x1080 |
| Region 7 moves 0x5000 to 0x9000; target offset stays 0x80 | 0x700000080 | 0x9080 | 0x9080 |
| Target moves inside remapped region 7 to offset 0x100; reference is unchanged | 0x700000080 | 0x9080 | 0x9100 |

For holder H and target T, store D = T - H. After moving them by dH and dT, the unchanged reference decodes to H + dH + D = T + dH. Its error relative to the intended T + dT is dH - dT. Thus, without address wraparound, unchanged displacement is correct exactly when dH = dT.

Rebasing a reference to the same target at new holder H2 instead stores D2 = H + D - H2. This is why copying an encoded integer alone is not equivalent to assigning the same pointer target.

## Regions and identity

The RIV illustration stores an ID in the upper 32 bits and an offset in the lower 32 bits. A current lookup table maps the ID to a region base. This declared teaching format is not a reconstruction of the paper's specialized virtual-address tables.

Changing a region's mapping can preserve its objects' offsets. Moving an object within the region cannot. Reusing an ID for different contents is another failure: the same numeric address can refer to an unrelated object. Bounds checks cannot establish identity or lifetime.

Self-relative displacements here are mathematical signed integers, not a specified fixed-width bit pattern. This model has no null encoding. In a real format, the null sentinel, integer width, byte order, layout and pointer-copy semantics are additional compatibility requirements. Successful address resolution is not evidence that an update reached persistent storage.

## Reproduce

Use Node.js 22 or later. Download these three files into one directory:
[model.mjs](model.mjs), [model.test.mjs](model.test.mjs), [results.json](results.json).

```sh
node model.mjs --check results.json
node --test model.test.mjs
```

The finite grid checks 3969 combinations of holder, target and displacement changes. Unit tests also cover backward/self references, values above floating-point precision, field overflow, invalid mappings, rebasing and ID reuse. These are arithmetic checks, not 3969 hardware observations.

## Pointer arithmetic is not the recovery contract

The member addresses below intentionally test the pointer header, not heap
tracing. Ralloc's [ISMM 2020 paper, section 4.5, p. 66](https://www.cs.rochester.edu/u/scott/papers/2020_ISMM_Ralloc.pdf)
excludes pointers into allocation fields from its recovery design. A successful
load through a member pointer therefore does not establish that the containing
allocation would remain reachable after recovery.

## A C++ struct copy is not a relocated image

The native [aggregate-copy.cpp](aggregate-copy.cpp) uses the unmodified Ralloc
`pptr` header at revision `6b9d7a1`. A struct contains integer members on
both sides of its pointer. For each of 16 member locations, default copy
construction, copy assignment and move construction preserve the pointer's
original target while copying the integer members themselves.

The distinction is observable: changing the source integer changes the value
read through the copied pointer, while the copy's own integer stays unchanged.
Explicitly assigning the copied pointer to the copy's corresponding member
changes that behavior. The probe keeps all objects alive; it never dereferences
a dangling pointer. Code that destroys the source still has to address lifetime.

This is expected shallow pointer behavior, not an allocator defect. In this
header, `pptr` declares a copy constructor and no move constructor; memberwise
move of the enclosing struct can use that copy constructor. Neither operation
clones a graph. A graph-copy operation needs an old-object-to-new-object mapping
and a policy for external references; blindly rebasing every target is not enough.

[Recorded results](native-results.json) contain 227 checks in each of
`-O0`, `-O2` and `-O2 -DNDEBUG`, plus one intentionally wrong
"copy points into the new object" expectation rejected in each configuration.
The checker uses exceptions, so disabling assertions cannot remove the checks.
These are observed GCC/x86-64 source-level results, not portability,
concurrency, remapping, recovery or power-loss evidence.

### Run the native check

Keep `relative-pointers/` beside `ralloc/`. Download
[aggregate-copy.cpp](aggregate-copy.cpp), [atomic-aba.cpp](atomic-aba.cpp) and
[verify-native.mjs](verify-native.mjs),
and preserve the existing [Ralloc upstream files and MIT license](../ralloc/README.md).
The runner verifies all four upstream hashes before compiling. It needs Node.js
and g++ with C++11 support, installs nothing, and removes its temporary binaries.

```sh
node relative-pointers/verify-native.mjs
```

The probe and runner are original Hesela test code under the MIT terms in the
linked license; upstream files are unchanged. The recorded result includes
compiler, architecture, source hashes and generation time.

## Atomic equality can miss intervening changes

The [atomic-aba.cpp](atomic-aba.cpp) check uses the same unmodified header and
prescribes a single-thread sequence. Every target stays alive.

| Slot history before compare-exchange | Stale expected value | Desired value | Observed result |
| --- | --- | --- | --- |
| A, then B | A | C | Failure; expected becomes B |
| A, then B, then A | A | C | Success; slot becomes C |
| Null | A | A | Failure; expected becomes null |
| Null | Null | A | Success; slot becomes A |

The second row restores both the same pointee and the same encoded offset.
Compare-exchange compares the present value, not its history. This is expected
behavior of the primitive, not a discovered allocator failure. The test executes
16 prescribed histories, checks 166 conditions per compiler configuration, and
rejects an intentionally wrong "history must be detected" expectation. The
[recorded results](native-results.json) bind both probe sources and the runner
to their hashes. There are no concurrent threads, freed nodes or crash tests.

This example makes no claim about Ralloc's internal list-head protocol: the
[ISMM paper, section 4.2, p. 65](https://www.cs.rochester.edu/u/scott/papers/2020_ISMM_Ralloc.pdf)
separately describes 34-bit counters in its partial-list and superblock-free-list
heads. Ordinary `atomic_pptr` and those counted list heads are not interchangeable.

Michael's [Hazard Pointers, section 2.3, p. 493](https://www.eecg.toronto.edu/~amza/ece1747h/papers/hazard_pointers.pdf)
distinguishes safe reclamation from logical ABA: moving still-live nodes between
lists can produce ABA even with garbage collection. Do not treat a relative
offset, region ID or a reclamation mechanism alone as a history check.
[ABA corpus record](https://hesela.dev/terms/aba-problem/).

To reproduce both native checks, include `atomic-aba.cpp` beside
`aggregate-copy.cpp` and run the existing `verify-native.mjs` command.
No additional dependency or allocator setup is required.

## Check the recorded evidence offline

Download [validate-native.mjs](validate-native.mjs) and its
[mutation tests](validate-native.test.mjs) alongside the native files above,
including [native-results.json](native-results.json).

```sh
node relative-pointers/validate-native.mjs
node --test relative-pointers/validate-native.test.mjs
```

This does not compile or execute the experiment. It checks that the report
contains the complete pinned upstream inventory, matches the supplied probe and
runner bytes, and records all three compiler configurations and negative controls.
An empty hash map is a missing evidence set, not a successful check of zero files.
Compiler, architecture, timestamp and experiment scope must also be present.

The checker tests consistency, not authenticity: someone able to replace both
the checker and evidence can forge an internally consistent bundle. It does not
validate the compiler binary or prove an experiment happened. Use the separate
native runner to obtain a new execution, and review its environment.

## Source context

Chen, Zhang, Budhiraja, Shen and Wu, *Efficient Support of Position Independence on Non-Volatile Memory*, MICRO 2017, pp. 191-203, [DOI](https://doi.org/10.1145/3123939.3124543). Sections 4.2-4.4 explain off-holder and RIV; section 6 describes PMEP-based evaluation. The full paper was reviewed, but no compiler, PMEP or crash experiment was reproduced.

The region access pattern is a separate comparison variable: section 6.3 places
successive data-structure elements round-robin across ten regions. A cache of
only the most recent region ID/base then sees little reuse. In the single-region
experiment that optimization is effective. Neither case establishes the cost of
all possible mapping caches or current hardware; our tests measure no throughput.

[Boost.Interprocess 1.85 offset_ptr](https://www.boost.org/doc/libs/1_85_0/doc/html/interprocess/offset_ptr.html) documents holder-relative addressing and a null sentinel of 1. This is a pinned historical documentation version, not a claim about the latest release.

The [Boost 1.85 implementation](https://raw.githubusercontent.com/boostorg/interprocess/boost-1.85.0/include/boost/interprocess/offset_ptr.hpp) also recomputes offsets on semantic copy and assignment. This is source inspection, not a native Boost execution.

The [Ralloc native-header probe](../ralloc/README.md) separately executes real implementation code. The present example adds independently moved regions and the algebraic boundary, without reclassifying that earlier probe as a durability test.

Read the corpus: [self-relative pointer](https://hesela.dev/terms/self-relative-pointer/), [Region ID in Value](https://hesela.dev/terms/region-id-in-value/).
