by Animats · 9y ago · 0 comments
The "trap values" concept is interesting. They're thinking that some floating point values are signaling NaNs, which will cause an exception if processed.
One way to approach this is to divide types into "fully mapped", where all bit patterns map to a valid value, and "partially mapped", where they don't. All the integer types, "char", "short", "int", "long", etc. are fully mapped on modern machines. "bool" isn't. Floating point isn't. Pointers are not. Enums are not. Structs are fully mapped if all their fields are fully mapped.
Initializing a fully mapped type with junk won't break the C/++ machine model. That's not true of partially mapped types. So this could be enforced in casts. Casting to fully mapped types could be permitted as at present, but casting to a partially mapped type should require something visibly unsafe, like C++'s "reinterpret_cast". In C, turning the raw memory from "malloc" into a partially mapped type should be a visible act. In C++, all partially mapped fields should be required to be initialized in the constructor. It's OK at the language level if fully mapped fields start as junk.
Requiring that pointers be initialized would be good for safety. Failure to initialize a pointer is usually a bug. Sometimes it's an entry point for an exploit.
That's what the article calls them, but the Standard uses the term "trap representations". The point is that a trap representation does not represent a value. (A representation is the bit sequence stored in an object. A value is more abstract; it's a member of the set of possible values of a type. A value can have more than one representation.)
I recall some SPARC arch (uSparc?) circa 199x that would place chars and shorts in a struct as if it were an int (i.e, aligned on boundary of 4). Then, when loading this field from the structure, it would load a full word, then shift it 16 or 24 bits left then right, to propagate sign or 0 and squeeze out undefined bits.
Interestingly enough, for programming it we used Sun Workbench which had some rudimentary equivalent of valgrind. So if in my code I would initialize the struct by initializing every attribute, then copy the entire structure using memcpy, the tool would complain about reads from uninitialised -- exactly because of the paddings of shorter types.
And then there's Itanium: https://blogs.msdn.microsoft.com/oldnewthing/20040119-00/?p=...
Consider that any particular local variable might be allocated to a register. If the local variable has not been initialized, it might have junk from a previous use, and that junk might have the NaT bit set. Reading from that local variable, or even spilling it to the stack, can cause an exception.
Now this would break at least one possible optimization that at least GCC does, namely that if you know that the value is either 1 or 0, then "!x" can be implemented as "x^1" (bitwise xor the value with 1) instead of a branch. I'm not sure that this optimization is particularly significant in real world code, though.