This paper looks closely at the semantics of
poison in LLVM and finds that some of the optimizations in LLVM are making contradicting assumptions about these special values and their interaction, leading to end-to-end miscompilations. They also propose a solution, which has been also suggested to the LLVM community.
I have to say I really like the model they end up with. The model is simple, reducing the number of these “special bad values” from 2 to 1 and also getting rid of the surprising “propagating non-determinism” rules around
undef. (Previously, in LLVM,
x XOR x could be any value, because
x could be
undef. Under the proposed rules,
x XOR x can only be
poison.) Furthermore, the model supports all the optimizations LLVM does; some need some minor modification though precisely to guard against the existing miscompilations.
So, overall, I think it would make a lot of sense for Rust to adapt the same kind of approach for dealing with “bad values”. Incidentally, miri already has
poison, though they call it
Undef for additional confusion.