It is unfortunate that the CPU cannot enforce signedness and size types.
Anybody programming in assembly can bypass any higher level language type
checks you have in mind. This is true even if the users has the best
intentions.
Ernest.
Posted Feb 17, 2008 19:50 UTC (Sun) by giraffedata (subscriber, #1954)
[Link]
It is unfortunate that the CPU cannot enforce signedness and size types.
The unfortunateness is at a lower level than that. It's unfortunate that a CPU can't do ordinary integer math, where 2 + 2 = 4. I understand why the very first CPUs wrapped around integers -- it happens naturally with the simplest implementations. But I don't get why no CPU today provides even the option of trapping on an arithmetic overflow instead of wrapping around silently. They do it for floating point, but not for integers.
Trapping on overflow
Posted Feb 23, 2008 21:45 UTC (Sat) by anton (guest, #25547)
[Link]
But I don't get why no CPU today provides even the option
of trapping on an arithmetic overflow.
MIPS and Alpha have separate arithmetic instructions that trap on
signed overflow (e.g., ADD on MIPS and ADDV on Alpha). IA-32 has INTO
which traps if OF is set. Apparently this instruction was so rarely
used by programmers, that AMD64 removed it in order to free up some
opcode space, and did not even bother to allocate another (multi-byte)
opcode for it; but you can still implement the functionality by
combining JO (or JNO) with INT.
The existence of INTO has not helped against this security hole, though.
Trapping on overflow
Posted Feb 23, 2008 22:24 UTC (Sat) by giraffedata (subscriber, #1954)
[Link]
MIPS and Alpha have separate arithmetic instructions that trap on signed overflow ...
Nice. Do you know if there is any way to make GCC (or any other C compiler) generate such instructions?
I can understand people resisting adding instructions to handle overflow, but if I could declare in my C program "no arithmetic in here is supposed to wrap around" and get signalled to death if it does, I'd do it a lot.
Trapping on overflow
Posted Feb 28, 2008 21:23 UTC (Thu) by anton (guest, #25547)
[Link]
Apart from asm statements and modifying gcc I don't know of a way to
get gcc or other compilers to use the trapping instructions for C
code.
Concerning "no arithmetic in here is supposed to wrap around",
unsigned arithmetic is supposed to wrap around in standard C, only
signed arithmetic is allowed to trap (or do anything else) on
overflow.