Part of it is that most CPU architectures don’t make overflow checking as cheap as it could be (there’s no option to trap on overflow, so you need a branch after every arithmetic operation, which has some cost). Another part is the compiler: a lot of compiler optimizations assume that arithmetic is a pure operation that can be added and removed and reordered as needed. So right now, adding overflow checks means opting out of a ton of optimizations. With care it may be possible to recover most of those optimizations, but it would require major improvements to LLVM.
Re:Git Security Vulnerabilities Announced
by superjan on Tuesday January 17, 2023 @05:20PM
(#34420278)
This is a great point. But how about if the language would allow the programmer to specify what range of values is expected for function input/output?
Then a compiler could try to reason about the computation and decide that overflow does not happen if all values are within bounds, and just add checks at the function boundaries.
Re:Git Security Vulnerabilities Announced
by saagarjha on Thursday January 19, 2023 @04:28AM
(#34438361)
This would require more checks, not fewer?
Re:Git Security Vulnerabilities Announced
by kelnos on Tuesday January 17, 2023 @04:22PM
(#34419399)
Presumably the program has to check bit in a status register or something like that to tell if the previous instruction caused overflow, no? That means an extra branch after each arithmetic instruction. I imagine that's not cheap?
Re:Git Security Vulnerabilities Announced
by dahfizz on Tuesday January 17, 2023 @04:29PM
(#34419517)
Integer arithmetic is a significant part of ~every program. A single branch that checks the overflow flag is not expensive. But branching on that flag every time you do integer math is death by a billion paper cuts.
Re:Git Security Vulnerabilities Announced
by CorrectHorseBat on Tuesday January 17, 2023 @04:36PM
(#34419641)
Your could use interrupts, no? Basically free when not triggered and when triggered you probably don't care about performance anymore.
Re:Git Security Vulnerabilities Announced
by prbs23 on Tuesday January 17, 2023 @05:05PM
(#34420076)
Most architectures do not provide an interrupt that is generated by an integer overflow. Since this would be a significant architectural change in the hardware, it can't be simply added in.
Additionally, if you are running inside an operating system, handling an interrupt usually incurs a trip through the kernel, which would add extra overhead every time an overflow did happen. Since there's a lot of software which depends on integers overflowing, this overhead on each overflow could significantly impact legacy software.
Re:Git Security Vulnerabilities Announced
by CorrectHorseBat on Wednesday January 18, 2023 @02:17AM
(#34424224)
Using a new instruction none of that would be an issue. And as someone pointed out, on x86 it wouldn't even be a new instruction, INTO already exists. But apparently it didn't make it in x64 because nobody used it :/
Re:Git Security Vulnerabilities Announced
by gwillen on Tuesday January 17, 2023 @05:06PM
(#34420092)
x86 at one time had a single-byte instruction that would trap if the overflow bit was set, INTO. It doesn't exist in 64-bit mode, I believe, and it was never widely used as far as I know. The performance implications of adding even a single additional instruction to every integer operation were probably still prohibitive? (And there's a history in x86 of specialized clever instructions and mechanisms going unused, due to being slower than doing the same thing some other way.)
What I don't understand is, checking for integer overflow is extremely cheap in hardware, so why is there any cost for performing those checks? What am I missing?