[OpenBIOS] Strict aliasing

Pavel Roskin proski at gnu.org
Tue Aug 11 22:55:06 CEST 2009


The reason OpenBIOS violates the strict aliasing rules so often is
because it uses char arrays to represent endian-specific data.

Char arrays may have alignment different from that of the longer types.
That's why casting a pointer to such array to a pointer to a longer type
and dereferencing it is considered unsafe by the compiler.  The compiler
assumes that different types would not occupy the same memory unless we
tell it not to make such assumption.

Char arrays serve as protection against misusing endian-specific data as
host-endian integers.  The alternative it to use sparse annotated types,
such as __le32.  __le32 has the same alignment as u32, but sparse would
recognize attempts to use __le32 in arithmetic operations.

I think switching to sparse annotated types would be a good idea whether
strict aliasing is disabled or not.  It makes the code more readable.
It could also fix some hard to find bugs.

Pavel Roskin

More information about the OpenBIOS mailing list