by afdbcreid · 1w ago · 0 comments
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.
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.
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:
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.
Also to be fair, it has to be said that officially blessed safe Rust code is not really what is competing with C.
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.