Wednesday, September 3, 2025

Pointer Provenance in C and C++

I happened to stumble across some interesting work from some people who are trying to take some of the Rust ideas about tracking memory lifetimes down into C and C++:

  • A Provenance-aware Memory Object Model for C
    In a committee discussion from 2004 concerning DR260, WG14 confirmed the concept of provenance of pointers, introduced as means to track and distinguish pointer values that represent storage instances with same address but non-overlapping lifetimes. Implementations started to use that concept, in optimisations relying on provenance-based alias analysis, without it ever being clearly or formally defined, and without it being integrated consistently with the rest of the C standard.
  • P2434R4: Nondeterministic pointer provenance
    The main alternative that was considered and rejected is the PVI model, which avoids the notion of storage exposure but imposes further restrictions on integer conversions. These restrictions provide further opportunities for optimization but also complicate the execution model in subtle ways that make it difficult for the programmer to determine whether a manipulation preserves the validity of a pointer (yet to be reconstructed). They also interact badly with serialization of pointers where operations on the converted pointer value are entirely invisible; additional annotations might be required to support this use case.
  • What on Earth Does Pointer Provenance Have to do With RCU?
    The results of operations on invalid pointers are not guaranteed, which provides additional opportunities for optimization. This example perhaps seems a bit silly, but modern compilers can use pointer provenance and invalidity to carry out serious points-to and aliasing analysis.

Not very easy reading. And a bunch of it is nearly 20 years old!

Change comes very slowly to the world of system programming in C. But at least C is still, slowly, evolving.

No comments:

Post a Comment