Hacker News new | ask | show | jobs
by tialaramex 10 days ago
Quite some time after this blog was written Aria blogged about the "strict provenance experiment" for Rust, and today Aria's API (more or less) is sanctioned and Rust is defined to deliver the behaviour Aria described in the Tower of Weakenings.

Here's the Rust documentation: https://doc.rust-lang.org/std/ptr/index.html#strict-provenan...

So in Rust much of this is spelled out today, although of course the LLVM backend still miscompiles some programs (in Rust or indeed in C or C++) because it can be tricked into giving integers provenance even though that's clearly not what anybody wanted. Oops.

However in C, which most of the examples are written in, the status is a bit less clear. Martin Uecker (who can often be found here in HN's comments) and colleagues wrote a document for ISO but that document is not (yet?) part of the ISO C standard itself. Of course in practice compilers like LLVM, GCC and MSVC need to do what the document says instead of making it up as they go, or else this matters very little.

One thing which cannot be in doubt in any of these close-to-the-metal languages today, almost certainly including obscure ones you've maybe taken a shine to like Odin or Hare, is that provenance is a thing, if you're sure you don't have pointer provenance in your languages but you do have actual pointers, check with a stop watch. If your programs in that language are very slow you might be right.

Because that's why this became a thing. Without provenance, pointers strangle many "obvious" optimisations. Add provenance and now these optimisations are available and your programs go much faster. It's a bargain - albeit one you probably never signed up for as a programmer.

2 comments

Hey, that's me. TS 6010 is not currently supported by GCC and Clang. But also for Rust this does not work due to the same compiler issues. (as you point out)
Do note though that strict provenance in Rust does work (at least we're not aware of any miscompilation AFAIK), and pre-strict-provenance semantics, or exposed provenance semantics, are warned against in the docs and clarify that they do not have accepted formal semantics. So the situation is a bit better than C.
It's true that Rust gives confidence to do (some) crazy pointer tricks which would work in the "pointers are just integers" world, yet not pay the performance price such tricks would cost if we really did "pointers are just integers"

My favourite example is ColdString, a value the same size as a thin pointer and which could be pointing at some text on the heap - but for smaller texts it is actually inline UTF-8 encoded text, the author originally wrote this to rely on the scary Exposure rules and I asked them if it couldn't be Strict instead and instead of "If you're so smart why don't you do it then?" they just fixed it. Nice. Huge fan of ColdString.

However, there are extant examples where you can just trick LLVM into asserting that two integers a,b are different and then showing that a - b == 0 which proves it was lying, those are by definition miscompilations and they don't rely on Exposure, they're just getting the LLVM optimiser to apply a pointer provenance optimisation to a thing it was just told is not a pointer.

> However, there are extant examples where you can just trick LLVM into asserting that two integers a,b are different and then showing that a - b == 0 which proves it was lying, those are by definition miscompilations and they don't rely on Exposure, they're just getting the LLVM optimiser to apply a pointer provenance optimisation to a thing it was just told is not a pointer.

If I'm getting what you mean correctly, that's not true. Integers do not have provenance and if `a - b == 0` then `a == b` when a, b are integers. The problem is only when LLVM concludes from this that `a as *const _ == b as *const _`, and that doesn't exist for strict provenance.

> If I'm getting what you mean correctly, that's not true

Well, what I'm doing here is describing the real situation with LLVM. If by "that's not true" you mean "that would be a bug in LLVM" then, uh, yeah, we know that it's a bug in LLVM. The LLVM maintainers know it's a bug in LLVM, have for many years.

> Integers do not have provenance

No, integers shouldn't have provenance. Due to this compiler bug unfortunately we can give integers provenance and blow up the world. Remembering the difference between what is and what should be is important, most especially in Criminal Justice.

So as to not rag specifically on LLVM, here's a ticket with Martin in the comments where somebody blew up GNU's C compiler on essentially the same trick question:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119472

> If by "that's not true" you mean "that would be a bug in LLVM" then, uh, yeah, we know that it's a bug in LLVM.

No that's not what I meant. Of course there is a bug in LLVM, but I said there will be no miscompilations and meant that - because with strict provenance, we don't use the problematic constructs (that LLVM miscompiles).

> No, integers shouldn't have provenance. Due to this compiler bug unfortunately we can give integers provenance and blow up the world.

Nope that's incorrect. In LLVM (and all other compilers I know) integers do not have provenance, and there aren't any optimizations that think they do. The bug (as understood by experts) is in pointer handling and not integers, specifically that ptr2int is not treated as having side-effects and that int2pt is considered to give the same pointers for the same integers.

Perhaps. In C one should avoid any non-local use of one-after pointers and any integer-to-pointers casts. Then I think one is also safe from miscompilation. Alternatively, one can use a compiler that is not broken.
You definitely can be safe in C as well - the difference I noted is that in Rust the safe pattern is officially blessed and the unsafe pattern is officially unblessed.
That is fair, but we are a bit in a different situation. There are many C compilers, and we can not unbless language constructs every time one of them has implementation bugs. Perhaps would could this in this case, but compared to other issues compilers have, it is also a relatively minor issue in terms of practical impact on C code.

Also to be fair, it has to be said that officially blessed safe Rust code is not really what is competing with C.

Do you have a particular suggestion of a "not broken" C compiler? Genuinely interested.
No, not really. There are couple of smaller compiler projects such as tcc, slimcc, cparser, kefir, etc. that I kind of hope will mature a bit more, but I haven't really used them.

Sometimes I am wondering whether I should continue to develop my own compiler with an eye towards correctness, but I do not have enough time at the moment. And fixing bugs in GCC seems more useful.

> However in C, which most of the examples are written in, the status is a bit less clear.

I believe it's a bit of an exaggeration. Formally specifying int2ptr and ptr2int casts in a way that permits both all code found in the wild (or at least a large majority of it) and all optimizations compilers want to perform (ditto), is more than just a bit difficult.