4 ms·
This is a great writeup, thanks! > in debug mode, arithmetic ... is checked for overflow > in release mode, overflow is not checked ... > "But what ab
by wyldfire 11y ago
This is a great writeup, thanks!
> in debug mode, arithmetic ... is checked for overflow
> in release mode, overflow is not checked
...
> "But what about performance?" I hear you ask. It is true that undefined behaviour drives optimisations by allowing the compiler to make assumptions
Are there any targets that have instructions like integer-add-but-trap-if-overflow? Or maybe a mode that can be switched? It would be interesting if there could be a high-performing path to leave the check in for release mode.
- deleted 11y ago[deleted]
- masklinn 11y ago> Are there any targets that have instructions like integer-add-but-trap-if-overflow? No "modern" targets. MIPS and Alpha could trap on overflow (on MIPS `add` would trap, `addu` would not, not sure about Alpha) but neither x86_64 nor ARM have hardware support for trapping on overflow. x86 had INTO, which would INT 4 if the overflow flag was set, it still required additional instructions and the OS needed to convert that to an application trap, but there you are. It was removed in x86_64, likely because it wasn't used much.
- GolDDranks 11y agoDo you know if there is any interest to re-introduce the feature? It'd certainly be nice to be able to eliminate overflow bugs without runtime overhead.
- Gankro 11y agoApparently modern Intel CPUs will fuse JO instructions to preceding adds, producing the effect of dedicated "checked add" instructions: https://gist.github.com/regehr/65252e90fb6bad82d533#gistcomment-1733637 https://gist.github.com/regehr/65252e90fb6bad82d533#gistcomm...
- chrisseaton 11y agoIn the examples the article this shows this will not help, as the overflow is in indirect addressing, not add instructions. I'm not a microcode expert but I guess arithmetic instructions, even without a jo, are not as efficient as indirect addressing, so we can't switch to arithmetic instructions with jo.
- dbaupp 11y agoThe efficiency of the add/jo combo does help (i.e. checking overflow isn't a total drag), it just doesn't completely solve the performance problem.
- chrisseaton 11y agoPerhaps we could have a new addressing mode that is the same as normal indirect but falls through and sets o if some part of it overflows. Then you'd do mov, jo which could then be fused. But I don't really know what I'm talking about.
- amelius 11y ago> It was removed in x86_64, likely because it wasn't used much. Well, to be honest (anecdotal data) in my career of programming (with lots of C++), I must say that I can't remember having any actual problems with integer overflow. But for security (buffer overflow protection), it could be useful.
- nightpool 11y agoRust already has bounds checking on buffers though, so that specific issue isn't a huge concern. However, there is a class of security bugs introduced by bad logic, its just a little more difficult to exploit.
- nitrogen 11y agoWhen doing integer math with medium sized numbers on systems without floating point hardware, I have to be careful to arrange operations so they do not overflow, and sometimes it's still necessary to give up and use a 64-bit type on a 32-bit CPU. An example calculation would be projecting a 3D coordinate into a 2D space.
- prodigal_erik 11y agoIf you don't deliberately send an overflow through every function that does any arithmetic, they could quietly give wrong answers sometimes and I don't see how you'd know that.
- spc476 11y agoIn my career of programming (doing a lot of x86 programming in the late 80s/early 90s) I never saw INTO used, and on modern systems, it's very slow and worse yet, at least under Linux, it's mapped to a SIGSEGV (http://boston.conman.org/2015/09/05.2 http://boston.conman.org/2015/09/05.2).
- hbbio 11y agoIt was most likely removed from x86_64 because it is less useful! 32 bit overflows are possible (I have seen them in scientific algorithms for instance). But I have yet to see 64 bits overflows.
- dikaiosune 11y agoOverflows in subtraction remain common for 64-bit unsigned integers, however.
- dspillett 11y agoIt is going to be less common in many fields but I can imagine 64-bit overflow is a concern in a fair few scientific number crunching situations. Also, if you were to use the scaled integer method of representing fractional numbers (like the fixed-point data types seen in many SQL implementations) with a scale factor of 2^32 then a 64-bit overflow is a 32-bit overflow effectively (though I'll admit that is going to be no less rare than genuinely needing the full range of 64-bit integers). And I'm guessing all this applies to underflow as much as it is does to overflow which could be a concern for unsigned integers of any size.
- msbarnett 11y agoIIRC MIPS did. Alpha did as well I think.