3 ms·
Efficient integer overflow should really be implemented in the processor. This problem exists and often leads to security holes. And developer in 99% cases is n
by vbezhenar 11y ago
Efficient integer overflow should really be implemented in the processor. This problem exists and often leads to security holes. And developer in 99% cases is not prepared to deal with overflows, so it shouldn't happen unless developer explicitly used some overflowing operator.
Unfortunately almost all languages use overflow by default. I only know that C language has unspecified behavior for overflowing signed integers, so it's legal to crash here. That's a sad state of things.
- chrisseaton 11y ago> Efficient integer overflow should really be implemented in the processor. But it already is. As a comment the article links to describes, add and then jo is fused by the processor, so the jo causes no extra uops and we can assume it's the same performance as a real add_and_jo instruction would be. The article also says the that the impact on the icache is insignificant so the fact that it's two instructions rather than one until the fusing also doesn't matter. So what else did you want? (Edit: spc476 points out that addressing mode arithmetic can't be used with jo, so there is that)
- caf 11y agoThe fused uop also can't be executed on as many ports as a plain add.
- spc476 11y agoThe sad thing is---there are processors that can do that. The MIPS can trap on overflow (the ADD instruction will do that) but C compilers tend to use the non-trapping versions of said instructions (ADDU---add unsigned). The VAX architecture (to go back some thirty-five years) can trap or not---there's a bit in the flags register that controls overflow traps. On the x86 line, you can trap on overflow, but the check has to happen after each instruction. The obvious method (using INTO) is slow (http://boston.conman.org/2015/09/05.2 http://boston.conman.org/2015/09/05.2) while using a conditional jump (JO) doesn't incur much overhead (http://boston.conman.org/2015/09/07.1 http://boston.conman.org/2015/09/07.1) but it does lead to overall slower code because you have to use instructions that set the overflow flag. LEA (can be used to add and multiply) never sets the overflow flag, so instead of using a single instruction to, say, multiply by 16 and add 7, you have to use multiple instructions.
- Animats 11y agoVAXen did that. Integer overflow checking could be enabled on a per-function basis by setting the entry mask read by the CALL function at function entry.[1] However, you couldn't configure this for C's "unsigned overflow OK, signed overflow is an error" semantics. There was only one bit for enabling integer overflow detection. (You could also enable decimal overflow detection, in the unlikely event you were using the base 10 arithmetic instructions.) Overflow caused an exception at the machine level, which UNIX got as a signal. I once rebuilt the UNIX tools on a VAX with the integer overflow detection bit set. About half of them still worked. [1] http://odl.sysworks.biz/disk$axpdocmar981/opsys/vmsos71/ovms_71_vax_macro_ref.p283.bkb http://odl.sysworks.biz/disk$axpdocmar981/opsys/vmsos71/ovms...
- _chris_ 11y ago> Efficient integer overflow should really be implemented in the processor. I disagree. It greatly adds to the complexity, verification, cost, and power for the HW to do this, which is especially onerous in that almost nobody cares or runs code that exercises it! Instead, the real performance hit comes from the compiler being forced to serialize otherwise independent instruction streams. So why waste all that HW effort when you can pay it all in the SW when and only when you want to use it? The user probably won't even notice the difference between SW and HW overflow support on real workloads.
- WalterBright 11y agoIt's one thing to use a jo (branch on overflow) instruction. But there's another problem: a = b + c + 47; can be implemented as: LEA a, 47[b+c] (a, b and c are assigned to registers) This does not generate any trap or flag when it overflows, it just wraps. It's an important optimization, too.