Hacker News new | ask | show | jobs
by afdbcreid 12 days ago
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.
2 comments

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.

> Nope that's incorrect.

https://rust.godbolt.org/z/nxnazs7Y6

c and d are both integers. Do you agree?

When you follow this link they'll have arbitrary values so I can't predict what those values will be but I can tell you that they'll be identical because the optimising compiler put both those unused temporary locals in the same spot on the stack.

One and three are booleans which ask whether the pointers to those temporaries are the same, LLVM says they are not even though Rust's policy is that they are. That's not great, but we could choose to imagine that's a Rust bug, except...

Two is another boolean asking whether the integers c and d are the same. LLVM says that those aren't the same either. It has given provenance to integers, that's the bug.

Oh, it seems you're right. I wasn't aware of this. Interesting.
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.