{"article":{"slug":"pointer-provenance-in-rust-vs-c","title":"Pointer Provenance in Rust vs C++","subtitle":null,"summary":"Sofía Belén López Vicens explores pointer provenance, the idea that pointers are more than plain integers, by inspecting LLVM IR and comparing how C++ and Rust approach provenance and the optimisations and undefined behaviour it affects.","content_type":"tutorial","language":"en","canonical_url":"https://sofiabelen.github.io/projects/pointer-provenance-in-rust-vs-cpp/","author":{"name":"Sofía Belén López Vicens","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"Sofía Belén López Vicens","url":"https://sofiabelen.github.io/","listing_slug":null,"listing":null},"topics":[{"name":"Rust","slug":"rust","url":"https://listedarticles.com/topics/rust"},{"name":"C++","slug":"c","url":"https://listedarticles.com/topics/c"},{"name":"Compilers","slug":"compilers","url":"https://listedarticles.com/topics/compilers"},{"name":"Systems Programming","slug":"systems-programming","url":"https://listedarticles.com/topics/systems-programming"}],"about_listings":[],"cover_image_url":null,"license":"CC-BY-NC-SA-4.0","word_count":1976,"reading_minutes":9,"published_at":"2026-11-02T00:00:00.000Z","added_at":"2026-11-09T17:11:06.186Z","updated_at":"2026-11-09T17:11:06.186Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/pointer-provenance-in-rust-vs-c","markdown_url":"https://listedarticles.com/articles/pointer-provenance-in-rust-vs-c.md","example":false,"citation":"Sofía Belén López Vicens, Sofía Belén López Vicens. \"Pointer Provenance in Rust vs C++.\" 2 Nov 2026. https://sofiabelen.github.io/projects/pointer-provenance-in-rust-vs-cpp/ (CC-BY-NC-SA-4.0)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://sofiabelen.github.io/projects/pointer-provenance-in-rust-vs-cpp/"},"body_markdown":"[![Featured image of post Pointer Provenance in Rust vs C++](https://sofiabelen.github.io/projects/pointer-provenance-in-rust-vs-cpp/yves-alarie-MmZet_7mzMY-unsplash_hu_f9705943a9d572c4.jpg)](https://sofiabelen.github.io/projects/pointer-provenance-in-rust-vs-cpp/)\n\n[C++](https://sofiabelen.github.io/categories/c++/)[Rust](https://sofiabelen.github.io/categories/rust/)[Tutorial](https://sofiabelen.github.io/categories/tutorial/)[Pointers](https://sofiabelen.github.io/categories/pointers/)\n\n## [Pointer Provenance in Rust vs C++](https://sofiabelen.github.io/projects/pointer-provenance-in-rust-vs-cpp/)\n\nNov 02, 2026\n\n9 minute read\n\nI’ve been exploring Rust, and one topic that got me intrigued is pointer\nprovenance, particularly because it’s not easy to visualize.\n\nTo get a feel for\n[vtables](https://sofiabelen.github.io/projects/visualizing-rusts-vtables-how-dyn-trait-works-in-memory/),\nwhat I wanted was to look at the actual memory, to see what it really looked\nlike and prove that I could in fact call these functions through their\nrespective function pointers. With provenance, I wanted to take it apart the\nsame way, but it turns out it’s not so straightforward. This only got me more\nintrigued, so let’s see what we can find!\n\nMy first time hearing about provenance, I wondered, why hadn’t I heard about it\nbefore? It seems so crucial, yet a bit of an obscure concept.\n\n## What is Provenance: Pointers Are Not Just Glorified Ints\n\n![](https://sofiabelen.github.io/chuttersnap-Fy7guhZfmAM-unsplash.jpg)\n\nVictoria crowned pigeon: some will say it's just a pigeon.\n\nPhoto by [CHUTTERSNAP](https://unsplash.com/@chuttersnap?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/photos/red-and-blue-bird-with-white-wings-Fy7guhZfmAM?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)\n\nLet’s start by taking a good look at this snippet. It’s short, but our intuition\ncan trick us if it’s our first time coming across this concept. `void opaque(int*)` is forward declared here, so for us it’s a black box. Neither we\nnor the compiler can rule out that this function dereferences `p` and writes to\nmemory.\n\n```\nvoid opaque(int*);\n\nint f(int* p) {\n    int x = 1;\n    opaque(p);\n    return x;\n}\n```\n\nIf we compile this with `-O2`, Clang optimizes this to return 1. Let’s try this\non Godbolt:\n\nThe question is, how does it know that `int* p` doesn’t point to the same memory\naddress where `x` lives (meaning that `opaque(p)` could end up modifying `x`)?\n\nThe compiler assumes `p` can’t equal `&x` because `p` wasn’t *derived* from `x`.\n\nWhen I first heard about this, I thought it was a bit of a strange concept. What\ndoes it mean that something is *derived* from something?\n\nI mean, if pointers were just numbers (memory addresses), `p` could\ntheoretically happen to hold the same memory address as `x`’s allocation on the\nstack. Good thing they’re not just numbers!\n\nLet us follow the white rabbit.\n\n![](https://sofiabelen.github.io/sincerely-media-2yL77QTLCq4-unsplash.jpg)\n\nYes, I've been trying to get more rabbits on my page. Somehow, only cats seemed to make the cut. This one reminds me of my childhood pet, Kaisie (honorary mention).\n\nPhoto by [Sincerely Media](https://unsplash.com/@sincerelymedia?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/photos/white-and-black-rabbit-2yL77QTLCq4?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)\n\nThis brings us to the core of provenance. That “*derived from*” relation\nis **provenance**: for every pointer, the compiler keeps track of which\nallocation it belongs to and may, therefore, touch.\n\nI tried to find a way to print this mysterious provenance, but it turns out that\nit’s purely a compiler abstraction. It does not exist as an actual data field\nstored alongside the memory address. (There are exceptions though: feel free to\nlook up the CHERI architecture to go down another rabbit hole).\n\n## Inspecting Provenance: LLVM IR\n\nSo what can we do to inspect it? If the compiler knows about it then it must\nexist somewhere. If so, there must be a way to *see* it.\n\nThis is how I learned about LLVM IR (intermediate representation), which is\nessentially a low-level programming language that acts as a bridge between\nhigh-level languages (like Rust, C++, etc) and machine code.\n\nLet’s try it for this code instead, where we also pass `&x` to `opaque` to see\nwhat happens when the compiler doesn’t optimize that part away:\n\n```\nvoid opaque(int*);\n\nint f(int* p) {\n    int x = 1;\n    opaque(p);\n    opaque(&x);\n    return x;\n}\n```\n\nWe can use the `-emit-llvm` flag on Godbolt to [see the LLVM\nIR](https://godbolt.org/#g:!%28%28g:!%28%28g:!%28%28h:codeEditor,i:%28filename:%271%27,fontScale:14,fontUsePx:%270%27,j:1,lang:c%2B%2B,selection:%28endColumn:2,endLineNumber:8,positionColumn:2,positionLineNumber:8,selectionStartColumn:2,selectionStartLineNumber:8,startColumn:2,startLineNumber:8%29,source:%27void+opaque%28int*%29%3B%0A%0Aint+f%28int*+p%29+%7B%0A++++int+x+%3D+1%3B%0A++++opaque%28p%29%3B%0A++++opaque%28%26x%29%3B%0A++++return+x%3B%0A%7D%27%29,l:%275%27,n:%270%27,o:%27C%2B%2B+source+%231%27,t:%270%27%29%29,k:44.33887569279493,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27%29,%28g:!%28%28h:compiler,i:%28compiler:clang2310,filters:%28b:%270%27,binary:%271%27,binaryObject:%271%27,commentOnly:%270%27,debugCalls:%271%27,demangle:%270%27,directives:%270%27,execute:%271%27,intel:%270%27,libraryCode:%270%27,trim:%271%27,verboseDemangling:%270%27%29,flagsViewOpen:%271%27,fontScale:14,fontUsePx:%270%27,j:1,lang:c%2B%2B,libs:!%28%29,options:%27-O2++-emit-llvm%27,overrides:!%28%29,selection:%28endColumn:29,endLineNumber:24,positionColumn:1,positionLineNumber:1,selectionStartColumn:29,selectionStartLineNumber:24,startColumn:1,startLineNumber:1%29,source:1%29,l:%275%27,n:%270%27,o:%27+x86-64+clang+23.1.0+%28Editor+%231%29%27,t:%270%27%29%29,k:55.66112430720507,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27%29%29,l:%272%27,n:%270%27,o:%27%27,t:%270%27%29%29,version:4):\n\n```\ndefine dso_local noundef i32 @f(int*)(ptr noundef %0) local_unnamed_addr #0 !dbg !14 {\n  %2 = alloca i32, align 4 // --> root of %2 allocation (x)\n    #dbg_assign(i1 poison, !22, !DIExpression(), !23, ptr %2, !DIExpression(), !24)\n    #dbg_value(ptr %0, !21, !DIExpression(), !24)\n  call void @llvm.lifetime.start.p0(ptr nonnull %2) #3, !dbg !25\n  store i32 1, ptr %2, align 4, !dbg !26\n    #dbg_assign(i32 1, !22, !DIExpression(), !28, ptr %2, !DIExpression(), !24)\n  tail call void @opaque(int*)(ptr noundef %0), !dbg !29\n  call void @opaque(int*)(ptr noundef nonnull %2), !dbg !30\n  %3 = load i32, ptr %2, align 4, !dbg !31\n  call void @llvm.lifetime.end.p0(ptr nonnull %2) #3, !dbg !32\n  ret i32 %3, !dbg !33\n}\n\ndeclare void @llvm.lifetime.start.p0(ptr captures(none)) #1\n\ndeclare !dbg !34 void @opaque(int*)(ptr noundef) local_unnamed_addr #2\n\ndeclare void @llvm.lifetime.end.p0(ptr captures(none)) #1\n\nattributes #0 = { mustprogress uwtable \"min-legal-vector-width\"=\"0\" \"no-trapping-math\"=\"true\" \"stack-protector-buffer-size\"=\"8\" \"target-cpu\"=\"x86-64\" \"target-features\"=\"+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87\" \"tune-cpu\"=\"generic\" }\nattributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }\nattributes #2 = { \"no-trapping-math\"=\"true\" \"stack-protector-buffer-size\"=\"8\" \"target-cpu\"=\"x86-64\" \"target-features\"=\"+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87\" \"tune-cpu\"=\"generic\" }\nattributes #3 = { nounwind }\n```\n\nWhat the compiler does is to conceptually construct a DAG (directed acyclic\ngraph), to answer the question of where a pointer comes from and whether two\npointers can touch the same memory.\n\nIf the root origins don’t match, LLVM can guarantee that writing through `%0`\nwill never alter the value stored at `%2`.\n\n100%\n\nThere is so much more to explore here that it’s hard not to get carried away\nbecause it’s truly fascinating. However, my original goal was to compare how C++\nand Rust handle this, so let’s continue.\n\n## C++: What Happens If We Cast to an Integer?\n\nIf we were to cast `&x` to an integer, then LLVM must assume that anyone could\nrebuild a pointer to `x` from that integer.\n\n```\n#include <cstdint>\n\nvoid g(std::uintptr_t);\nvoid opaque(int*);\n\nint f(int* p) {\n    int x = 1;\n    g(reinterpret_cast<std::uintptr_t>(&x)); // address of x is now 'exposed'\n    opaque(p);\n    return x;\n}\n```\n\nThe result is that the compiler gives up the optimization:\n\nThat’s a pity if we only wanted to read the address and never intended to cast\nit back to a pointer. Wouldn’t it be nice if we could communicate this intent to\nthe compiler?\n\n## Rust: Making Your Intent Explicit\n\nThis is exactly what Rust provides with its powerful provenance APIs.\n\n### Inspecting Without Exposing\n\n```\nlet p: *const u32 = &x;\nlet a: usize = p.addr();\n```\n\n`p.addr()` extracts the raw memory address integer while telling Rust’s abstract\nmachine:\n\n> I am reading this numeric address for inspection, but I guarantee I WILL NOT\n> cast it back to a valid pointer.\n\nProvenance is therefore not exposed.\n\nTo mirror our previous experiments, we can try to see if the optimization was\navoided or not:\n\nWhat we see is that `mov eax, dword ptr [rsp + 12]` reloads `x` after\n`opaque(p)`, meaning that, at least for now, the optimization is given up. Same\nas in the C++ case.\n\nStill, we can benefit from using the more expressive API, and not relying on\nexposure semantics. It gives the compiler room to optimize in the future, and,\nby telling Miri explicitly what we mean, it can better help us catch bugs.\n\n### Preserving Provenance\n\n![](https://sofiabelen.github.io/michael-myers-FvuisLAN-rA-unsplash.jpg)\n\nPointers reproduce like rabbits and someone has to keep track of the family\ntree.\n\nPhoto by [Michael Myers](https://unsplash.com/@myers2021?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/photos/a-group-of-rabbits-sitting-on-top-of-a-pile-of-hay-FvuisLAN-rA?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)\n\nIn lock-free data structures, we sometimes need to store status flags in the\nlower unused bits of a pointer. It’s a very cool trick, which left me with my\neyes wide open for a minute when I first read about it. I talk more about it in\n[this\npost](https://sofiabelen.github.io/projects/visualizing-crossbeam-epoch-based-reclamation/#q2-dissecting-crossbeamepochshared).\nEssentially, due to alignment, the lower bits of pointers are “wasted” (always\n0), so we can use them to store flags.\n\nWhat we can do is work with an intermediate pointer value, which will have the\nflag bit set BUT that also inherits provenance from the original pointer. This\nis exactly the functionality that `with_addr` provides us. After we’re done, we\ncan recreate our valid initial pointer.\n\nThe tagged value is stays a pointer rather than ‘usize’ because an integer is\njust a number and can’t carry provenance.\n\n```\nlet tagged: *const u32 = p.with_addr(p.addr() | 1);\n// ...\nlet revived_ptr: *const u32 = tagged.with_addr(tagged.addr() & !1);\n```\n\nKeep in mind that `tagged` isn’t dereferenceable, given that `p.addr() | 1` is\nmisaligned for `u32`. Dereferencing it would be UB.\n\nIn short:\n\n- `addr()` reads the address without exposing provenance.\n- `with_addr()` builds a new pointer with a new address but the *same*\n  provenance as the original.\n\n### Legacy Rust, Similar To C++\n\nIn legacy code, we’d have to do something like this:\n\n```\nlet addr = p as usize;\nlet tagged_addr = addr | 1;\nlet revived_ptr = (tagged_addr & !1) as *const u32;\nprintln!(\"*revived_ptr: {}\", unsafe { *revived_ptr});\n```\n\nThis code will run, but if we run it with Miri, it emits a warning:\n\n```\nwarning: integer-to-pointer cast\n --> src/main.rs:6:22\n  |\n6 |     let revived_ptr = (tagged_addr & !1) as *const u32;\n  |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ integer-to-pointer cast\n  |\n  = help: this program is using integer-to-pointer casts or (equivalently) `ptr::with_exposed_provenance`, which means that Miri might miss pointer bugs in this program\n  = help: see https://doc.rust-lang.org/nightly/std/ptr/fn.with_exposed_provenance.html for more details on that operation\n  = help: to ensure that Miri does not miss bugs in your program, use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead\n  = help: you can then set `MIRIFLAGS=-Zmiri-strict-provenance` to ensure you are not relying on `with_exposed_provenance` semantics\n  = help: alternatively, `MIRIFLAGS=-Zmiri-permissive-provenance` disables this warning\n\n*revived_ptr: 1\nwarning: 1 warning emitted\n```\n\nActually, the reason that it runs is also interesting. What happens is similar\nto the C++ example. `p as usize` exposes the provenance of `p`, and since the\nreconstructed pointer points to the same allocation, provenance is recovered.\n\nProvenance would be lost if we did some pointer arithmetic and landed in a\ndifferent allocation.\n\n### Casting Without Exposing Provenance\n\n![](https://sofiabelen.github.io/dimitry-b-GNu4g4Z5jN4-unsplash.jpg)\n\nPhoto by [Dimitry B](https://unsplash.com/@dimitry_b?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/photos/brown-monkey-photograph-GNu4g4Z5jN4?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)\n\nTo see what happens when provenance is **not** exposed, we can use\n`ptr::without_provenance`, which creates a bare address with no allocation\nbehind it. (It’s useful for dangling or sentinel pointers, but it can’t be\ndereferenced).\n\n```\nlet bare_ptr = std::ptr::without_provenance::<u32>(p.addr());\nprintln!(\"*tagged_ptr: {}\", unsafe { *bare_ptr});\n```\n\nAs expected, Miri complains about a *dangling pointer* and tells us explicitly that it has no provenance.\n\n```\nerror: Undefined Behavior: memory access failed: attempting to access 4 bytes, but got 0x20b28[noalloc] which is a dangling pointer (it has no provenance)\n  --> src/main.rs:11:42\n   |\n11 |     println!(\"*tagged_ptr: {}\", unsafe { *bare_ptr});\n   |                                          ^^^^^^^^^ Undefined Behavior occurred here\n   |\n   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior\n   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information\n\nnote: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace\n\nerror: aborting due to 1 previous error\n```\n\n## Recap\n\nProvenance, in short, is a hidden piece of information tracking “which\nallocation” a pointer originates from, which LLVM uses for optimizations.\n\nIn C++, `uintptr_t` casts force the optimizer to prepare for the worst case by\ndisabling some optimizations.\n\nIn Rust, `addr()`, `with_addr()` and `without_provenance()` let you state your\nintent explicitly: you only want the number, or you are deriving a new pointer\nfrom an existing one. In principle, that gives the compiler room to optimize\nmore aggressively. In our experiment, however, the Rust version is unoptimized\ntoo: `x` was still reloaded from the stack. Using more expressive\nAPIs is still valuable though, as Miri can enforce the rules and catch bugs that\nstem from unintended usage.\n\nI had planned to compare what Miri and ASan can catch, but I’ll save that for\nanother post. It was enough new info as it is and it takes some time to fully\nwrap my head around it.\n\nI’m currently between jobs and looking for a Rust role, so writing these posts\nkeeps me motivated and has helped me find a sense of community. Thank you for\nreading, and I hope you enjoyed it!\n\n[C++](https://sofiabelen.github.io/tags/c++/)\n[Rust](https://sofiabelen.github.io/tags/rust/)\n[Tutorial](https://sofiabelen.github.io/tags/tutorial/)\n[Pointers](https://sofiabelen.github.io/tags/pointers/)\n\nLicensed under CC BY-NC-SA 4.0\n","body_html":"<p><a href=\"https://sofiabelen.github.io/projects/pointer-provenance-in-rust-vs-cpp/\" rel=\"nofollow ugc noopener\"><img src=\"https://sofiabelen.github.io/projects/pointer-provenance-in-rust-vs-cpp/yves-alarie-MmZet_7mzMY-unsplash_hu_f9705943a9d572c4.jpg\" alt=\"Featured image of post Pointer Provenance in Rust vs C++\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></a></p>\n<p><a href=\"https://sofiabelen.github.io/categories/c++/\" rel=\"nofollow ugc noopener\">C++</a><a href=\"https://sofiabelen.github.io/categories/rust/\" rel=\"nofollow ugc noopener\">Rust</a><a href=\"https://sofiabelen.github.io/categories/tutorial/\" rel=\"nofollow ugc noopener\">Tutorial</a><a href=\"https://sofiabelen.github.io/categories/pointers/\" rel=\"nofollow ugc noopener\">Pointers</a></p>\n<h2 id=\"pointer-provenance-in-rust-vs-c\"><a href=\"https://sofiabelen.github.io/projects/pointer-provenance-in-rust-vs-cpp/\" rel=\"nofollow ugc noopener\">Pointer Provenance in Rust vs C++</a></h2>\n<p>Nov 02, 2026</p>\n<p>9 minute read</p>\n<p>I’ve been exploring Rust, and one topic that got me intrigued is pointer\nprovenance, particularly because it’s not easy to visualize.</p>\n<p>To get a feel for\n<a href=\"https://sofiabelen.github.io/projects/visualizing-rusts-vtables-how-dyn-trait-works-in-memory/\" rel=\"nofollow ugc noopener\">vtables</a>,\nwhat I wanted was to look at the actual memory, to see what it really looked\nlike and prove that I could in fact call these functions through their\nrespective function pointers. With provenance, I wanted to take it apart the\nsame way, but it turns out it’s not so straightforward. This only got me more\nintrigued, so let’s see what we can find!</p>\n<p>My first time hearing about provenance, I wondered, why hadn’t I heard about it\nbefore? It seems so crucial, yet a bit of an obscure concept.</p>\n<h2 id=\"what-is-provenance-pointers-are-not-just-glorified-ints\">What is Provenance: Pointers Are Not Just Glorified Ints</h2>\n<figure><img src=\"https://sofiabelen.github.io/chuttersnap-Fy7guhZfmAM-unsplash.jpg\" alt=\"\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></figure>\n<p>Victoria crowned pigeon: some will say it&#39;s just a pigeon.</p>\n<p>Photo by <a href=\"https://unsplash.com/@chuttersnap?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"nofollow ugc noopener\">CHUTTERSNAP</a> on <a href=\"https://unsplash.com/photos/red-and-blue-bird-with-white-wings-Fy7guhZfmAM?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"nofollow ugc noopener\">Unsplash</a></p>\n<p>Let’s start by taking a good look at this snippet. It’s short, but our intuition\ncan trick us if it’s our first time coming across this concept. <code>void opaque(int*)</code> is forward declared here, so for us it’s a black box. Neither we\nnor the compiler can rule out that this function dereferences <code>p</code> and writes to\nmemory.</p>\n<pre><code>void opaque(int*);\n\nint f(int* p) {\n    int x = 1;\n    opaque(p);\n    return x;\n}</code></pre>\n<p>If we compile this with <code>-O2</code>, Clang optimizes this to return 1. Let’s try this\non Godbolt:</p>\n<p>The question is, how does it know that <code>int* p</code> doesn’t point to the same memory\naddress where <code>x</code> lives (meaning that <code>opaque(p)</code> could end up modifying <code>x</code>)?</p>\n<p>The compiler assumes <code>p</code> can’t equal <code>&amp;x</code> because <code>p</code> wasn’t <em>derived</em> from <code>x</code>.</p>\n<p>When I first heard about this, I thought it was a bit of a strange concept. What\ndoes it mean that something is <em>derived</em> from something?</p>\n<p>I mean, if pointers were just numbers (memory addresses), <code>p</code> could\ntheoretically happen to hold the same memory address as <code>x</code>’s allocation on the\nstack. Good thing they’re not just numbers!</p>\n<p>Let us follow the white rabbit.</p>\n<figure><img src=\"https://sofiabelen.github.io/sincerely-media-2yL77QTLCq4-unsplash.jpg\" alt=\"\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></figure>\n<p>Yes, I&#39;ve been trying to get more rabbits on my page. Somehow, only cats seemed to make the cut. This one reminds me of my childhood pet, Kaisie (honorary mention).</p>\n<p>Photo by <a href=\"https://unsplash.com/@sincerelymedia?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"nofollow ugc noopener\">Sincerely Media</a> on <a href=\"https://unsplash.com/photos/white-and-black-rabbit-2yL77QTLCq4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"nofollow ugc noopener\">Unsplash</a></p>\n<p>This brings us to the core of provenance. That “<em>derived from</em>” relation\nis <strong>provenance</strong>: for every pointer, the compiler keeps track of which\nallocation it belongs to and may, therefore, touch.</p>\n<p>I tried to find a way to print this mysterious provenance, but it turns out that\nit’s purely a compiler abstraction. It does not exist as an actual data field\nstored alongside the memory address. (There are exceptions though: feel free to\nlook up the CHERI architecture to go down another rabbit hole).</p>\n<h2 id=\"inspecting-provenance-llvm-ir\">Inspecting Provenance: LLVM IR</h2>\n<p>So what can we do to inspect it? If the compiler knows about it then it must\nexist somewhere. If so, there must be a way to <em>see</em> it.</p>\n<p>This is how I learned about LLVM IR (intermediate representation), which is\nessentially a low-level programming language that acts as a bridge between\nhigh-level languages (like Rust, C++, etc) and machine code.</p>\n<p>Let’s try it for this code instead, where we also pass <code>&amp;x</code> to <code>opaque</code> to see\nwhat happens when the compiler doesn’t optimize that part away:</p>\n<pre><code>void opaque(int*);\n\nint f(int* p) {\n    int x = 1;\n    opaque(p);\n    opaque(&amp;x);\n    return x;\n}</code></pre>\n<p>We can use the <code>-emit-llvm</code> flag on Godbolt to <a href=\"https://godbolt.org/#g:!%28%28g:!%28%28g:!%28%28h:codeEditor,i:%28filename:%271%27,fontScale:14,fontUsePx:%270%27,j:1,lang:c%2B%2B,selection:%28endColumn:2,endLineNumber:8,positionColumn:2,positionLineNumber:8,selectionStartColumn:2,selectionStartLineNumber:8,startColumn:2,startLineNumber:8%29,source:%27void+opaque%28int*%29%3B%0A%0Aint+f%28int*+p%29+%7B%0A++++int+x+%3D+1%3B%0A++++opaque%28p%29%3B%0A++++opaque%28%26x%29%3B%0A++++return+x%3B%0A%7D%27%29,l:%275%27,n:%270%27,o:%27C%2B%2B+source+%231%27,t:%270%27%29%29,k:44.33887569279493,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27%29,%28g:!%28%28h:compiler,i:%28compiler:clang2310,filters:%28b:%270%27,binary:%271%27,binaryObject:%271%27,commentOnly:%270%27,debugCalls:%271%27,demangle:%270%27,directives:%270%27,execute:%271%27,intel:%270%27,libraryCode:%270%27,trim:%271%27,verboseDemangling:%270%27%29,flagsViewOpen:%271%27,fontScale:14,fontUsePx:%270%27,j:1,lang:c%2B%2B,libs:!%28%29,options:%27-O2++-emit-llvm%27,overrides:!%28%29,selection:%28endColumn:29,endLineNumber:24,positionColumn:1,positionLineNumber:1,selectionStartColumn:29,selectionStartLineNumber:24,startColumn:1,startLineNumber:1%29,source:1%29,l:%275%27,n:%270%27,o:%27+x86-64+clang+23.1.0+%28Editor+%231%29%27,t:%270%27%29%29,k:55.66112430720507,l:%274%27,n:%270%27,o:%27%27,s:0,t:%270%27%29%29,l:%272%27,n:%270%27,o:%27%27,t:%270%27%29%29,version:4\" rel=\"nofollow ugc noopener\">see the LLVM\nIR</a>:</p>\n<pre><code>define dso_local noundef i32 @f(int*)(ptr noundef %0) local_unnamed_addr #0 !dbg !14 {\n  %2 = alloca i32, align 4 // --&gt; root of %2 allocation (x)\n    #dbg_assign(i1 poison, !22, !DIExpression(), !23, ptr %2, !DIExpression(), !24)\n    #dbg_value(ptr %0, !21, !DIExpression(), !24)\n  call void @llvm.lifetime.start.p0(ptr nonnull %2) #3, !dbg !25\n  store i32 1, ptr %2, align 4, !dbg !26\n    #dbg_assign(i32 1, !22, !DIExpression(), !28, ptr %2, !DIExpression(), !24)\n  tail call void @opaque(int*)(ptr noundef %0), !dbg !29\n  call void @opaque(int*)(ptr noundef nonnull %2), !dbg !30\n  %3 = load i32, ptr %2, align 4, !dbg !31\n  call void @llvm.lifetime.end.p0(ptr nonnull %2) #3, !dbg !32\n  ret i32 %3, !dbg !33\n}\n\ndeclare void @llvm.lifetime.start.p0(ptr captures(none)) #1\n\ndeclare !dbg !34 void @opaque(int*)(ptr noundef) local_unnamed_addr #2\n\ndeclare void @llvm.lifetime.end.p0(ptr captures(none)) #1\n\nattributes #0 = { mustprogress uwtable &quot;min-legal-vector-width&quot;=&quot;0&quot; &quot;no-trapping-math&quot;=&quot;true&quot; &quot;stack-protector-buffer-size&quot;=&quot;8&quot; &quot;target-cpu&quot;=&quot;x86-64&quot; &quot;target-features&quot;=&quot;+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87&quot; &quot;tune-cpu&quot;=&quot;generic&quot; }\nattributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }\nattributes #2 = { &quot;no-trapping-math&quot;=&quot;true&quot; &quot;stack-protector-buffer-size&quot;=&quot;8&quot; &quot;target-cpu&quot;=&quot;x86-64&quot; &quot;target-features&quot;=&quot;+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87&quot; &quot;tune-cpu&quot;=&quot;generic&quot; }\nattributes #3 = { nounwind }</code></pre>\n<p>What the compiler does is to conceptually construct a DAG (directed acyclic\ngraph), to answer the question of where a pointer comes from and whether two\npointers can touch the same memory.</p>\n<p>If the root origins don’t match, LLVM can guarantee that writing through <code>%0</code>\nwill never alter the value stored at <code>%2</code>.</p>\n<p>100%</p>\n<p>There is so much more to explore here that it’s hard not to get carried away\nbecause it’s truly fascinating. However, my original goal was to compare how C++\nand Rust handle this, so let’s continue.</p>\n<h2 id=\"c-what-happens-if-we-cast-to-an-integer\">C++: What Happens If We Cast to an Integer?</h2>\n<p>If we were to cast <code>&amp;x</code> to an integer, then LLVM must assume that anyone could\nrebuild a pointer to <code>x</code> from that integer.</p>\n<pre><code>#include &lt;cstdint&gt;\n\nvoid g(std::uintptr_t);\nvoid opaque(int*);\n\nint f(int* p) {\n    int x = 1;\n    g(reinterpret_cast&lt;std::uintptr_t&gt;(&amp;x)); // address of x is now &#39;exposed&#39;\n    opaque(p);\n    return x;\n}</code></pre>\n<p>The result is that the compiler gives up the optimization:</p>\n<p>That’s a pity if we only wanted to read the address and never intended to cast\nit back to a pointer. Wouldn’t it be nice if we could communicate this intent to\nthe compiler?</p>\n<h2 id=\"rust-making-your-intent-explicit\">Rust: Making Your Intent Explicit</h2>\n<p>This is exactly what Rust provides with its powerful provenance APIs.</p>\n<h3 id=\"inspecting-without-exposing\">Inspecting Without Exposing</h3>\n<pre><code>let p: *const u32 = &amp;x;\nlet a: usize = p.addr();</code></pre>\n<p><code>p.addr()</code> extracts the raw memory address integer while telling Rust’s abstract\nmachine:</p>\n<blockquote><p>I am reading this numeric address for inspection, but I guarantee I WILL NOT\ncast it back to a valid pointer.</p></blockquote>\n<p>Provenance is therefore not exposed.</p>\n<p>To mirror our previous experiments, we can try to see if the optimization was\navoided or not:</p>\n<p>What we see is that <code>mov eax, dword ptr [rsp + 12]</code> reloads <code>x</code> after\n<code>opaque(p)</code>, meaning that, at least for now, the optimization is given up. Same\nas in the C++ case.</p>\n<p>Still, we can benefit from using the more expressive API, and not relying on\nexposure semantics. It gives the compiler room to optimize in the future, and,\nby telling Miri explicitly what we mean, it can better help us catch bugs.</p>\n<h3 id=\"preserving-provenance\">Preserving Provenance</h3>\n<figure><img src=\"https://sofiabelen.github.io/michael-myers-FvuisLAN-rA-unsplash.jpg\" alt=\"\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></figure>\n<p>Pointers reproduce like rabbits and someone has to keep track of the family\ntree.</p>\n<p>Photo by <a href=\"https://unsplash.com/@myers2021?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"nofollow ugc noopener\">Michael Myers</a> on <a href=\"https://unsplash.com/photos/a-group-of-rabbits-sitting-on-top-of-a-pile-of-hay-FvuisLAN-rA?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"nofollow ugc noopener\">Unsplash</a></p>\n<p>In lock-free data structures, we sometimes need to store status flags in the\nlower unused bits of a pointer. It’s a very cool trick, which left me with my\neyes wide open for a minute when I first read about it. I talk more about it in\n<a href=\"https://sofiabelen.github.io/projects/visualizing-crossbeam-epoch-based-reclamation/#q2-dissecting-crossbeamepochshared\" rel=\"nofollow ugc noopener\">this\npost</a>.\nEssentially, due to alignment, the lower bits of pointers are “wasted” (always\n0), so we can use them to store flags.</p>\n<p>What we can do is work with an intermediate pointer value, which will have the\nflag bit set BUT that also inherits provenance from the original pointer. This\nis exactly the functionality that <code>with_addr</code> provides us. After we’re done, we\ncan recreate our valid initial pointer.</p>\n<p>The tagged value is stays a pointer rather than ‘usize’ because an integer is\njust a number and can’t carry provenance.</p>\n<pre><code>let tagged: *const u32 = p.with_addr(p.addr() | 1);\n// ...\nlet revived_ptr: *const u32 = tagged.with_addr(tagged.addr() &amp; !1);</code></pre>\n<p>Keep in mind that <code>tagged</code> isn’t dereferenceable, given that <code>p.addr() | 1</code> is\nmisaligned for <code>u32</code>. Dereferencing it would be UB.</p>\n<p>In short:</p>\n<ul><li><code>addr()</code> reads the address without exposing provenance.</li><li><p><code>with_addr()</code> builds a new pointer with a new address but the <em>same</em></p><p>provenance as the original.</p></li></ul>\n<h3 id=\"legacy-rust-similar-to-c\">Legacy Rust, Similar To C++</h3>\n<p>In legacy code, we’d have to do something like this:</p>\n<pre><code>let addr = p as usize;\nlet tagged_addr = addr | 1;\nlet revived_ptr = (tagged_addr &amp; !1) as *const u32;\nprintln!(&quot;*revived_ptr: {}&quot;, unsafe { *revived_ptr});</code></pre>\n<p>This code will run, but if we run it with Miri, it emits a warning:</p>\n<pre><code>warning: integer-to-pointer cast\n --&gt; src/main.rs:6:22\n  |\n6 |     let revived_ptr = (tagged_addr &amp; !1) as *const u32;\n  |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ integer-to-pointer cast\n  |\n  = help: this program is using integer-to-pointer casts or (equivalently) `ptr::with_exposed_provenance`, which means that Miri might miss pointer bugs in this program\n  = help: see https://doc.rust-lang.org/nightly/std/ptr/fn.with_exposed_provenance.html for more details on that operation\n  = help: to ensure that Miri does not miss bugs in your program, use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead\n  = help: you can then set `MIRIFLAGS=-Zmiri-strict-provenance` to ensure you are not relying on `with_exposed_provenance` semantics\n  = help: alternatively, `MIRIFLAGS=-Zmiri-permissive-provenance` disables this warning\n\n*revived_ptr: 1\nwarning: 1 warning emitted</code></pre>\n<p>Actually, the reason that it runs is also interesting. What happens is similar\nto the C++ example. <code>p as usize</code> exposes the provenance of <code>p</code>, and since the\nreconstructed pointer points to the same allocation, provenance is recovered.</p>\n<p>Provenance would be lost if we did some pointer arithmetic and landed in a\ndifferent allocation.</p>\n<h3 id=\"casting-without-exposing-provenance\">Casting Without Exposing Provenance</h3>\n<figure><img src=\"https://sofiabelen.github.io/dimitry-b-GNu4g4Z5jN4-unsplash.jpg\" alt=\"\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></figure>\n<p>Photo by <a href=\"https://unsplash.com/@dimitry_b?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"nofollow ugc noopener\">Dimitry B</a> on <a href=\"https://unsplash.com/photos/brown-monkey-photograph-GNu4g4Z5jN4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"nofollow ugc noopener\">Unsplash</a></p>\n<p>To see what happens when provenance is <strong>not</strong> exposed, we can use\n<code>ptr::without_provenance</code>, which creates a bare address with no allocation\nbehind it. (It’s useful for dangling or sentinel pointers, but it can’t be\ndereferenced).</p>\n<pre><code>let bare_ptr = std::ptr::without_provenance::&lt;u32&gt;(p.addr());\nprintln!(&quot;*tagged_ptr: {}&quot;, unsafe { *bare_ptr});</code></pre>\n<p>As expected, Miri complains about a <em>dangling pointer</em> and tells us explicitly that it has no provenance.</p>\n<pre><code>error: Undefined Behavior: memory access failed: attempting to access 4 bytes, but got 0x20b28[noalloc] which is a dangling pointer (it has no provenance)\n  --&gt; src/main.rs:11:42\n   |\n11 |     println!(&quot;*tagged_ptr: {}&quot;, unsafe { *bare_ptr});\n   |                                          ^^^^^^^^^ Undefined Behavior occurred here\n   |\n   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior\n   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information\n\nnote: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace\n\nerror: aborting due to 1 previous error</code></pre>\n<h2 id=\"recap\">Recap</h2>\n<p>Provenance, in short, is a hidden piece of information tracking “which\nallocation” a pointer originates from, which LLVM uses for optimizations.</p>\n<p>In C++, <code>uintptr_t</code> casts force the optimizer to prepare for the worst case by\ndisabling some optimizations.</p>\n<p>In Rust, <code>addr()</code>, <code>with_addr()</code> and <code>without_provenance()</code> let you state your\nintent explicitly: you only want the number, or you are deriving a new pointer\nfrom an existing one. In principle, that gives the compiler room to optimize\nmore aggressively. In our experiment, however, the Rust version is unoptimized\ntoo: <code>x</code> was still reloaded from the stack. Using more expressive\nAPIs is still valuable though, as Miri can enforce the rules and catch bugs that\nstem from unintended usage.</p>\n<p>I had planned to compare what Miri and ASan can catch, but I’ll save that for\nanother post. It was enough new info as it is and it takes some time to fully\nwrap my head around it.</p>\n<p>I’m currently between jobs and looking for a Rust role, so writing these posts\nkeeps me motivated and has helped me find a sense of community. Thank you for\nreading, and I hope you enjoyed it!</p>\n<p><a href=\"https://sofiabelen.github.io/tags/c++/\" rel=\"nofollow ugc noopener\">C++</a>\n<a href=\"https://sofiabelen.github.io/tags/rust/\" rel=\"nofollow ugc noopener\">Rust</a>\n<a href=\"https://sofiabelen.github.io/tags/tutorial/\" rel=\"nofollow ugc noopener\">Tutorial</a>\n<a href=\"https://sofiabelen.github.io/tags/pointers/\" rel=\"nofollow ugc noopener\">Pointers</a></p>\n<p>Licensed under CC BY-NC-SA 4.0</p>","headings":[{"level":2,"text":"Pointer Provenance in Rust vs C++","id":"pointer-provenance-in-rust-vs-c"},{"level":2,"text":"What is Provenance: Pointers Are Not Just Glorified Ints","id":"what-is-provenance-pointers-are-not-just-glorified-ints"},{"level":2,"text":"Inspecting Provenance: LLVM IR","id":"inspecting-provenance-llvm-ir"},{"level":2,"text":"C++: What Happens If We Cast to an Integer?","id":"c-what-happens-if-we-cast-to-an-integer"},{"level":2,"text":"Rust: Making Your Intent Explicit","id":"rust-making-your-intent-explicit"},{"level":3,"text":"Inspecting Without Exposing","id":"inspecting-without-exposing"},{"level":3,"text":"Preserving Provenance","id":"preserving-provenance"},{"level":3,"text":"Legacy Rust, Similar To C++","id":"legacy-rust-similar-to-c"},{"level":3,"text":"Casting Without Exposing Provenance","id":"casting-without-exposing-provenance"},{"level":2,"text":"Recap","id":"recap"}]}}