Making something like this into a panic is not a good fit for Rust as-is. Because enums are proven to have only correct values, not only is code written to assume pattern matches cannot panic, but compilers are free to optimize around only having valid values as well. That goes not only for the enum discriminant, but for any associated values being properly initialized values of their respective types.
In a sense, Rust lets you write code as if invalid values never happen, so there's less to check for in your code. It's understandable from the perspective of the abstraction needed for computer code to be "correct" and not just temporarily getting away with Undefined Behavior. There are simpler ways to violate it than just rowhammer, write straight to process memory for example, which can also violate invariants that compilers assumed while optimizing.
If you wanted to compile Rust (or anything else) with a hardening mode that does check what should be redundant values, it would be a lot slower and code that never panicked before would now panic, but it would probably be a worthwhile tradeoff for some programs to opt into. After all, if you built for CHERI or arm64e and got a machine exception from an unauthenticated pointer, you'd be thrilled you mitigated a vulnerability even if it violated your higher-level language model. Defense in depth and all that.
Maybe someone feels motivated enough to write an RFC and prototype for this. It just wouldn't stop at enum values, it should mean all sorts of other things too, such as not eliding any other checks that appear redundant given assumptions like immutability. That's what makes it slow and hard to reason about.
I wonder, is choosing random enum values at runtime more secure against Rowhammer than just having fixed values that were chosen randomly once and compiled in, since presumably the attacking code now has no way to know which bits it needs to flip? If so, it might even be desirable to implement this as a "secure enum" in a compiled language.
It would be neat to see an algorithm that generates suitable values.
Although I'm not sure if it's optimal, the many case seems to be the same as the 2-case but repeated for every 2 items. I expect they double checked that the amount of bitflips is still pretty high.
Maybe a better algorithm for the many case would be something like the popcnt parallel patterns:
* 0b0101010101010101
* 0b0011001100110011
* 0b0000111100001111
* 0b0000000011111111
Since they would all have equal hamming distance between each of the entries.
And indeed, the two values ate bitwise complements.
__attribute__((rand)) enum e { FOO, BAR, ... };
which randomizes the values, as an extension.You only need this in specific places, like setuid programs.
Randomization can be bad because it wrecks build reproducibility; it would have to be tied to the GNU Build ID.
If such an enum is used in any interface between files, the randomization has to be the same in every translation unit.
Maybe the syntax could specify a seed: rand(42).
Which is why only a couple of the values are random, with the others being those values, XOR'd with 0xff*