Re: C aggregate passing (Rust kernel policy)
From: David Laight
Date: Wed Feb 26 2025 - 18:08:47 EST
On Wed, 26 Feb 2025 23:28:20 +0100
Ralf Jung <post@xxxxxxxx> wrote:
...
> > Unions in C, C++ and Rust (not Rust "enum"/tagged union) are
> > generally sharp. In Rust, it requires unsafe Rust to read from
> > a union.
>
> Definitely sharp. At least in Rust we have a very clear specification though,
> since we do allow arbitrary type punning -- you "just" reinterpret whatever
> bytes are stored in the union, at whatever type you are reading things. There is
> also no "active variant" or anything like that, you can use any variant at any
> time, as long as the bytes are "valid" for the variant you are using. (So for
> instance if you are trying to read a value 0x03 at type `bool`, that is UB.)
That is actually a big f***ing problem.
The language has to define the exact behaviour when 'bool' doesn't contain
0 or 1.
Much the same as the function call interface defines whether it is the caller
or called code is responsible for masking the high bits of a register that
contains a 'char' type.
Now the answer could be that 'and' is (or may be) a bit-wise operation.
But that isn't UB, just an undefined/unexpected result.
I've actually no idea if/when current gcc 'sanitises' bool values.
A very old version used to generate really crap code (and I mean REALLY)
because it repeatedly sanitised the values.
But IMHO bool just shouldn't exist, it isn't a hardware type and is actually
expensive to get right.
If you use 'int' with zero meaning false there is pretty much no ambiguity.
David