r/programming • • May 30 '17

Compiler Optimizations are Awesome

https://blog.regehr.org/archives/1515
146 Upvotes

97 comments sorted by

View all comments

84

u/slavik262 May 31 '17

Do people seriously make arguments that compiler optimizations aren't useful? Have they ever looked at the assembly compilers emit without optimizations enabled? It's incredibly bad.

Also, anybody who quotes Knuth as saying,

We should forget about small efficiencies, say about 97% of the time; premature optimization is the root of all evil.

without the sentence that immediately follows:

Yet we should not pass up our opportunities in that critical 3%.

is either misinformed or disingenuous.

52

u/TNorthover May 31 '17

In general I agree completely, but Daniel Bernstein is working in a very particular niche: cryptography. For him two particular issues some together to make optimizing compilers The Devil.

First, inner cryptographic loops are generally hot enough that people are willing spend days and weeks making them as fast as possible. A compiler won't be able to match that.

Second, crypto is particularly vulnerable to compiler optimizations breaking naive assumptions. Compilers can give code you expect to execute in constant time a variable execution time; on average this is better (we hope), but just that variability can be exploited by attackers. Compilers can also remove checks they decide aren't valid; the original code would have been violating the language standard, but that's of scant comfort if your primary concern is security and someone manages to hijack a bank account because of it.

What cryptographers seem to want most of all is predictability. This doesn't correspond to most people, so in general I strongly agree with John. But I have some kind of sympathy for Daniel.

34

u/slavik262 May 31 '17

crypto is particularly vulnerable to compiler optimizations breaking naive assumptions.

This is true, but the correct solution is not to make those assumptions. Undefined behavior is a tricky problem (and one can easily argue that C and C++ make it trickier than it has to be), but there are good reasons it exists. Chandler Carruth has a great talk on this (UB, troubles crypto authors have with it, why it exists, and what tools are being made to mitigate it).

Compilers can give code you expect to execute in constant time a variable execution time; on average this is better (we hope), but just that variability can be exploited by attackers.

Unfortunately, there's little you can do about this, even if you're writing assembly. Modern desktop CPUs are essentially virtual machines that translate x64 on the fly into the micro-operations they use. Combine this with the effects of store buffers, cache misses, etc., and it's really difficult to get predictable timing.

12

u/loup-vaillant May 31 '17

the correct solution is not to make those assumptions.

This is blaming the victim. Undefined behaviour in C and C++ has gone utterly insane, you know. Signed integer overflow for instance: because some platforms can't handle it, the standard made it undefined for all platforms. Then compiler writers took advantage of this, effectively turning after-the-fact overflow checks into actual vulnerabilities.

C and C++ were supposed to be close to the machine. Can we really blame the programmer from reasoning like an electrical engineer, and assume that because X86 handles integer overflow just fine, the language should do so as well?

Chandler Carruth has a great talk on this

He downplays UB so much that's almost criminal. The hard fact is, UB sometimes does trigger catastrophes such as formatting all your hard drive, or the encryption of all your files. Some UB lead to vulnerabilities, arbitrary code execution, and malware mayhem. Even those who don't right now might cause similar problems later, as optimisers exploit more and more UB from the standard.

I'm not against UB, but the current C and C++ standard have gone way too far.

it's really difficult to get predictable timing.

Fortunately, we rarely need that much. In cryptography for instance, we don't need predictable, or even constant-time timings. We want those timings to be independent from secrets. And on modern processors, this just means no secret-dependent branch, and no secret-dependent indexing. On less modern processors, you also have to be wary of modular multiplication (this only affects public key crypto), and arithmetic shifts (without a barrel shifter, the timings depend on the magnitude of the shift).

And of course, compiler optimisations: if GCC suddenly detects that your constant-time string comparison function could if fact stop early, it is allowed to do so, and throw your security out the window. Fortunately, it doesn't yet do so.

3

u/mcmcc May 31 '17

Signed integer overflow for instance: because some platforms can't handle it, the standard made it undefined for all platforms.

How would you define the outcome of signed integer overflow? How would that definition make code that (inadvertently) induces signed integer overflow better?

1

u/industry7 May 31 '17

How would you define the outcome of signed integer overflow? How would that definition make code that (inadvertently) induces signed integer overflow better?

Make it signal. That makes code better b/c you can now handle the error case consistently across platforms.

Alternatively, a sufficiently advanced type system could enforce compile time restraints in such a way that unmitigated potential overflows won't compile.

There's a lot of different ways that a language could handle this situation.

2

u/mcmcc May 31 '17

Make it signal.

Hardware vendors' collective heads would explode.

a sufficiently advanced type system

... would be unwieldy. Languages have tried this and you find yourself working around the type system. Sometimes (often) numbers are just numbers.

1

u/industry7 Jun 01 '17

... would be unwieldy. Languages have tried this and you find yourself working around the type system.

Maybe you find yourself working around advanced type systems, but I generally find the more advanced the type system the more my productivity is enhanced.

Sometimes (often) numbers are just numbers.

Until they under/over-flow and/or the compiler decides to optimize away important part of your program, and then it's a huge problem in addition to being "just numbers".

0

u/mcmcc Jun 01 '17

I'll ask again, what general problem does defined signed overflow actually solve?

Read the other branches in this thread for context.

1

u/industry7 Jun 01 '17

How would that definition make code that (inadvertently) induces signed integer overflow better?

I'll ask again, what general problem does defined signed overflow actually solve?

The general problem is undefined behavior... Defining the behavior fixes the problem... by defining the behavior.

What's your question again?

→ More replies (0)