News for hackers. Stuff that matters.
Re:Depressing and faintly terrifying days for the C standard... by armitron on Wednesday May 30, 2018 @01:13PM (#17189502)
I can't be the only one that 99.9% of the time doesn't care _one iota_ about these mythical optimizations that compilers can introduce by exploiting undefined behavior. I just want to write straightforward, predictable code that tries its best to be safe but for one reason or another have to stick with C.

Is there a guide/reference on how to disable these optimizations in modern compilers? A list of GCC/Clang arguments that disable as much of this as possible would be greatly appreciated. I've seen a lot of posts and articles discussing C undefined behavior but almost nothing describing how to counter it.

Re:Depressing and faintly terrifying days for the C standard... by pcwalton on Wednesday May 30, 2018 @01:18PM (#17189553)
Sure. Compile at -O0.

A huge number of seemingly-trivial optimizations depend on assuming that undefined behavior will never happen. The number of optimizations that don't depend on UB in any way is quite small. For example, if you want to get pedantic about it, even automatic promotion of local variables to registers is exploiting undefined behavior—who's to say you didn't have a pointer that just happened to point to one of them?

Re:Depressing and faintly terrifying days for the C standard... by loup-vaillant on Wednesday May 30, 2018 @05:25PM (#17191972)
> Sure. Compile at -O0.

Hmm, nope. Sure, it will save you from much UB. But GCC and Clang have idiotic front ends. On purpose. They subscribe to the idea that the optimizer phase should clean up the front end mess. They're not entirely wrong, since it does simplify the front end, and does not really make the rest any more complex. It does make it slower, though.

Long story short, those compilers don't generate reasonable code by default. You need at least -O1.

Re:Depressing and faintly terrifying days for the C standard... by glandium on Wednesday May 30, 2018 @06:52PM (#17192589)
I was recently surprised to find that GCC, at -O0, does some level of optimization. At least it does for multiplications. https://godbolt.org/g/ekTkBm

Edit: the irony is that if you do compile with optimizations, it does a different optimization.

Re:Depressing and faintly terrifying days for the C standard... by umanwizard on Wednesday May 30, 2018 @11:18PM (#17193712)
What do you mean by "reasonable" ? Can you give an example of code that is compiled in a more obvious, straightforward way with O1 than O0 ?
Re:Depressing and faintly terrifying days for the C standard... by loup-vaillant on Thursday May 31, 2018 @02:31AM (#17194361)
My knowledge of this is only second hand, so I don't have any specific example. I believe Chandler Carruth explains this well on one of his LLVM talks (that is, how and why the front end emits crappy intermediate code, before the optmiser kicks in).
Re:Depressing and faintly terrifying days for the C standard... by kazinator on Wednesday May 30, 2018 @01:29PM (#17189659)
Of course you can have a pointer to a local variable, and that variable can still spend portions of its lifetime in a register.

What won't necessarily work as expected is a shadily obtained pointer to a local variable, like by displacing &i to try to point to the same location as &j.

If &j doesn't occur anywhere (or does occur, but in a way that is optimized away), j may not even have a location.

Re:Depressing and faintly terrifying days for the C standard... by jcranmer on Wednesday May 30, 2018 @01:43PM (#17189796)
How do you reflect the fact that pulling a pointer to a value "out of thin air" may not make any sense without either banning it entirely or defining some step of the process to be undefined behavior?
Re:Depressing and faintly terrifying days for the C standard... by esrauch on Wednesday May 30, 2018 @01:29PM (#17189666)
Most languages have no concept of undefined behavior and they still have compilers and runtimes that can do a huge amount of optimizations.
Re:Depressing and faintly terrifying days for the C standard... by xenadu02 on Wednesday May 30, 2018 @02:05PM (#17190022)
Because they define-away the scenarios where UB is an issue for most code. Sometimes they do this by having a GC, but Rust and Swift prove that isn't necessary.

Such languages typically do provide some form of "no promises, here there be dragons" in the form of unsafe blocks or functions. Restricting this region of unsafety is a huge benefit for programmers and compilers. Programmers because relatively few pieces of code need to be carefully reviewed. Compilers because most of the program contains little or no UB.

As a typical example, in C you can alias and type pun. Therefore to avoid all UB the compiler would need to be extremely careful in any function containing a pointer. You could return a reference to a local through a separate opaque function call, or receive multiple pointers all aliasing the same memory. To completely avoid all UB means inserting checks after every potentially mutable machine instruction, or emitting duplicate function bodies that take different paths when the pointers alias; that's assuming the compiler even has enough type information to answer the question!

With typical C#, Rust, Swift, etc you just can't cause those kinds of problems without deliberate subterfuge and use of Unsafe types or blocks.

Re:Depressing and faintly terrifying days for the C standard... by eddyb on Wednesday May 30, 2018 @02:32PM (#17190264)
Note that `unsafe` code blocks (or having some unsafe primitives) fundamentally results in some kind of UB in the language as a whole, and you totally can create tons of problems from it, since the rest of the language has invariants it can't itself violate, but the unsafe code can (potentially much easier than C, if there are more invariants) - that is the "UB", and the invariants that can be violated are what the compiler optimizes based on.

Lest we forget, any language with a C FFI capability must have some notion of UB, because the FFI effectively includes the C code in its own semantics (unless fully sandboxed, which may be too expensive to be done).

Re:Depressing and faintly terrifying days for the C standard... by pjmlp on Wednesday May 30, 2018 @02:58PM (#17190594)
True, but they are quite easy to track down.

In systems like Unisys ClearPath, you can even configure the system such that only admins can execute applications with unsafe blocks.

In C any line of code, if care is not taken to use the correct compiler flags, can be a possible source of UB.

Re:Depressing and faintly terrifying days for the C standard... by eddyb on Wednesday May 30, 2018 @03:59PM (#17191217)
Indeed. Only wanted to point out that their optimizations also rely on UB, so they're bad examples for specifically "optimizing without relying on UB".
Re:Depressing and faintly terrifying days for the C standard... by umanwizard on Wednesday May 30, 2018 @11:21PM (#17193723)
> In C any line of code, if care is not taken to use the correct compiler flags, can be a possible source of UB.

What do you mean?

Re:Depressing and faintly terrifying days for the C standard... by pjmlp on Thursday May 31, 2018 @04:21AM (#17194814)
You need to turn on all warnings as errors, static analysers and pedantic modes depending how each compiler allows them.

ANSI C11 has 200 documented cases of UB, and each compiler might have additional cases, are you sure you can know all of them by heart while looking at a random line of C code?

Re:Depressing and faintly terrifying days for the C standard... by pjmlp on Wednesday May 30, 2018 @02:09PM (#17190047)
Languages like Ada and Modula-2 were already proving it long time ago.

Or to go even further in the past, NEWP, still alive on Unisys ClearPath mainframes.

Re:Depressing and faintly terrifying days for the C standard... by eddyb on Wednesday May 30, 2018 @01:35PM (#17189728)
The languages that you describe must look nothing like C (other than, ironically, syntax), and must have no untyped direct memory access pointer feature at all, which usually means they rely on a GC instead for memory safety.
Re:Depressing and faintly terrifying days for the C standard... by kazinator on Wednesday May 30, 2018 @05:10PM (#17191823)
We can easily imagine a language that is exactly like C in every regard, but free of some gratuitous behaviors. All standard-conforming C programs work in this dialect, and a good many others also work and are portable.

For instance, we could have a dialect of C in which this is required to print "0123":

   int i = 0;
   printf("%d%d%d%d\n", i++, i++, i++, i++);
A behavior is gratuitously undefined if it is left that way for no good reason, such that programming language constructs can be undefined simply for having the wrong form. That is to say, the input values are well-defined (i has a good initial value, which we can increment four times), and the individual operations are defined also (i++ is fine by itself). But for no possible value of i is the above printf call correct.

An example of undefined behavior which is not gratuitous is overflow on integer addition. Th expression i + j, where i and j are int, is not ipso facto undefined because of its form. Only for certain combinations of values of its operands is it undefined. We cannot simply banish that without banishing addition, and various ways of making overflow defined have drawbacks, like being expensive (e.g. target machine has no native support for the particular behavior, so extra instructions have to be generated) or super-expensive, with complicated representation and memory management (switching to bignums).

Re:Depressing and faintly terrifying days for the C standard... by eddyb on Wednesday May 30, 2018 @06:31PM (#17192472)
It's interesting to see which things Rust has defined where C refused to, such as fixing a strict evaluation order (mostly post-order on the AST), requiring signed integers to be 2's complement or masking shift amounts (`x << n` being `x << (n % bitwidth)`).

Where does C actually gain anything nowadays? Signed integer UB is mostly useful for optimizing misuses of `int` for unsigned values (https://news.ycombinator.com/item?id=17191295), and is evaluation order even relevant anymore? (I don't think clang can pass that information down to LLVM, at all)

The only example I gave which has known drawbacks is the shift one, where modern platforms differ in the behavior, and LLVM will only optimize out the masking of the shift amount on the platforms that have that same behavior in their shift instructions (I think x86, but not ARM).

However, even if you make all of these changes to C, there's still a lot of UB left, in the form of memory accesses, which is much harder to get rid of (see the other comments, some of which mention Rust as well).

Re:Depressing and faintly terrifying days for the C standard... by kazinator on Wednesday May 30, 2018 @07:10PM (#17192690)
> I don't think clang can pass that information down to LLVM, at all

I.e. you mean that the compiler front end has to choose an order without knowing which order will be good for LLVM, and there is no information to say "I don't actually need this specific order; pick another one that is better".

Re:Depressing and faintly terrifying days for the C standard... by pcwalton on Wednesday May 30, 2018 @09:11PM (#17193226)
I can't think of a case in which changing the evaluation order between sequence points in a way that LLVM can't prove is safe on its own actually improves codegen.
Re:Depressing and faintly terrifying days for the C standard... by eddyb on Thursday May 31, 2018 @07:01AM (#17195409)
Wasn't the ordering thing in C for stack push order in calling conventions? At least that's one theory I've heard.
Re:Depressing and faintly terrifying days for the C standard... by kazinator on Thursday May 31, 2018 @08:42AM (#17195834)
Unspecified evaluation order helps dumb compilers produce better code. Whenever a particular evaluation order is convenient in order fit some canned code generation pattern, like a function call or whatever, that desired evaluation order can just be blindly used, rather than following forced evaluation order (using temporaries to hold the results).

Compilers for languages with strict evaluation order can still perturb evaluation order when it is safe to do so: like when expressions do not have side effects, or have effects that don't mutually interfere and are not external. They can thus avoid or eliminate the temporaries.

C is still being specified like it's 1982 and compilers have to run in a few kilobytes of RAM, in a single pass, and go straight from source to target machine code.

Re:Depressing and faintly terrifying days for the C standard... by zokier on Wednesday May 30, 2018 @01:53PM (#17189903)
> and must have no untyped direct memory access pointer feature at all

Can you expand on why you feel this must be the case?

I'm thinking for example deference of pointer could be defined to be equivalent of platform-specific memory load instruction. It wouldn't be memory safe and would segfault like C, but it still wouldn't bring up nasal demons like C UB.

Re:Depressing and faintly terrifying days for the C standard... by pcwalton on Wednesday May 30, 2018 @02:04PM (#17190009)
If every source-language load must result in a memory load, then you at the very least killed scalar replacement of aggregates (a critical optimization), and, depending on how you define it, you might have killed register allocation too.
Re:Depressing and faintly terrifying days for the C standard... by eddyb on Wednesday May 30, 2018 @02:33PM (#17190280)
You get register allocation back if you deny taking the address of variables declared with the `register` keyword - and we've gone full circle!
Re:Depressing and faintly terrifying days for the C standard... by kazinator on Wednesday May 30, 2018 @05:13PM (#17191853)
Taking the addresses of variables defined register is denied; that's the only modern meaning of the specifier.
Re:Depressing and faintly terrifying days for the C standard... by kazinator on Wednesday May 30, 2018 @05:12PM (#17191847)
Give me the rule "every source language load of a structure member, array or global variable, or anything loaded indirectly through a pointer results in an actual load" and I will still crank out fast code, by explicitly using local variables for caching the values coming from those places.
Re:Depressing and faintly terrifying days for the C standard... by pcwalton on Wednesday May 30, 2018 @06:13PM (#17192367)
You'll also write unmaintainable code. To a large degree, optimizations exist to allow programmers to write maintainable code without sacrificing performance.
Re:Depressing and faintly terrifying days for the C standard... by kazinator on Wednesday May 30, 2018 @06:17PM (#17192391)
I will write absolutely clear code this way. I use that style anyway, quite often.

Optimization can work backwards in this regard.

For instance, let's consider CSE (common subexpression elimination). That allows some repetitive code to produce the same results as code that adheres to DRY.

E.g. stupid way to insert into a circular list:

   node->prev = prev;
   node->next = prev->next;
   prev->next->prev = node;
   prev->next = node;
The compiler has no idea whether node and prev are aliased or not. The assignment to node->next might be the same as prev->next, and so prev->next has to be reloaded even though it was just loaded in the previous line.

smart way: get a local variable for prev->next!

   node *next = prev->next;

   node->prev = prev;
   node->next = next;
   next->prev = node;
   prev->next = node;
Bonus: much more readable, lining up in neat columns. Just one arrow in each line. If this doesn't generate at most one load and four stores, the compiler is garbage.

Also note that we can reorder these four assignments in any of the 4! permutations and they produce the same result (unless there is aliasing, which would be unintentional and wrong regardless of the order).

Not the case in the original. For instance, it's important that prev->next is stored in node->next before the assignment to prev->next.

You can shoot yourself in the foot with local variables though. Caching is susceptible to staleness. You have to know when it is legitimate to keep using the cached value and when it must be reloaded or disused.

After our assignment prev->next = node, the next variable no longer represents the value of prev->next. In this case, since we are done, we don't care. If more code followed which still assumed that next is the original successor of prev (now the successor of node), that would be wrong.

Re:Depressing and faintly terrifying days for the C standard... by pcwalton on Wednesday May 30, 2018 @08:13PM (#17192993)
Do you also, for example, look up the magic multiplication number in Hacker's Delight every time you want to divide by a constant?
Re:Depressing and faintly terrifying days for the C standard... by kazinator on Wednesday May 30, 2018 @09:08PM (#17193212)
That silliness is not comparable to effective use of local variables to both streamline the source code and get better output from the compiler.

By the way, the magic multiplication number can be worked out, because it's just fixed point math. We take the 32:32 fixed point representation of 1 and divide by 17 to get an approximation of 1/17 in 32:32 fixed: 0x100000000 / 17 = 0xF0F0F0F. That's our magic number for dividing 32 bits by 17 by doing a 64 bit multiplication. For instance 90/17 is 90 * 0xF0F0F0F = 0x54B4B4B46. The integer part of this 32:32 fixed point value is in the upper 32 bits which is 5.

There are some subtleties there, plus considerations of whether we want signed or unsigned division. Better let the compiler deal with it. Arithmetic reductions are safe optimizations. It's hard to imagine what you could do wrong so that an arithmetic reduction breaks your code, given that it produces the same result and doesn't interact with some some memory aliasing where the compiler isn't informed about what you're doing.

And by the way, given this code:

  #include <stdio.h>

  struct node {
    struct node *next, *prev;
  };

  void ins_after_a(struct node *prev, struct node *node)
  {
    node->prev = prev;
    node->next = prev->next;
    prev->next->prev = node;
    prev->next = node;
  }

  void ins_after_b(struct node *prev, struct node *node)
  {
    struct node *next = prev->next;
    node->prev = prev;
    node->next = next;
    prev->next = node;
    next->prev = node;
  }
gcc 7.2.0 on Ubuntu 17 generates better code for the cleaner, streamlined second one with the local variable, for exactly the reason I gave. ins_after_a yields 6 movq instructions; in_after_b yields 5.

Better source that is easier to reason about; better machine code: all round win.

  ins_after_a:
  .LFB23:
          .cfi_startproc
          movq  (%rdi), %rax
          movq  %rdi, 8(%rsi)
          movq  %rax, (%rsi)
          movq  (%rdi), %rax  <-- wasteful re-load of (%rdi) due to aliasing suspicion
          movq  %rsi, 8(%rax)
          movq  %rsi, (%rdi)
          ret   
          .cfi_endproc
  .LFE23:
          .size ins_after_a, .-ins_after_a
          .p2align 4,,15
          .globl        ins_after_b
          .type ins_after_b, @function
  ins_after_b:
  .LFB24:
          .cfi_startproc
          movq  (%rdi), %rax
          movq  %rdi, 8(%rsi)
          movq  %rax, (%rsi)
          movq  %rsi, (%rdi)
          movq  %rsi, 8(%rax)
          ret   
          .cfi_endproc
The language could be specified that way (accesses to structs are memory loads) for all I care and that could be helpful.
Re:Depressing and faintly terrifying days for the C standard... by comex on Wednesday May 30, 2018 @09:54PM (#17193395)
In other words, TBAA is fundamentally limited in its ability to capture the programmer's expectations about aliasing, because it must assume that any two pointers that happen to have the same type could alias – even if a person reading the code would consider that obviously unreasonable.

In this case, you could avoid the wasteful re-load by marking the function arguments as `restrict`. One of the reasons Rust's memory model is interesting is that the language statically prevents mutable aliasing in most cases, so "restrict" comes for free… well, kind of. (It's actually rather difficult to nail down the precise guarantees, especially when your compiler's backend was originally designed for C.)

Re:Depressing and faintly terrifying days for the C standard... by kazinator on Wednesday May 30, 2018 @10:04PM (#17193432)
Ah, but by using restrict I'm saying, "please screw my C program with C99 stuff that potentially introduces undefined behavior".

That's what restrict does: it makes some behaviors undefined and otherwise doesn't change the semantics of the program. It's completely against the grain of moving to a safer language.

Why would I do that, if I can instead beautify the source and machine code while sticking to what was available in C90.

I think restrict is mainly intended as a way of competing against Fortran. If you're processing arrays referenced by pointers, and can get the compiler to believe that they do not overlap, then the compiler can unroll the loops and rearrange the accesses and calculations in the unrolled body. Like it can load four elements from a source arrays, do four calculations, and then store four elements into a target, rather than interleaving. That can be done by vectorized instructions.

Here is where our technique falls short: if we write the loop body with our load-calculate-store style, it cannot be unrolled and vectorized. We have pinned down an exact behavior for all possible cases of aliasing, like self-overlap with a displacement. Vectorizing unrolls do not preserve that behavior.

Manual unrolling isn't attractive because it's a guessing game. A good amount of unrolling on one machine may give the instruction cache indigestion on another machine.

Re:Depressing and faintly terrifying days for the C standard... by kazinator on Wednesday May 30, 2018 @05:16PM (#17191889)
Pointers are a red herring; misuses of pointer dereferences are the manageable form of UB that programmers understand well, in reference to memory models that are straightforward. It's all the nasty gratuitous UB that wrecks the C language: behavior that could be well-defined without changing the character of the C language, or making incorrect any existing programs that are correct.

Like UB in the preprocessor. WTF? Processing a bunch of tokens at compile time should be totally safe. But no: if the ## token pasting operator glues together two tokens which do not look like one token, the behavior is undefined.

It worked differently on different compilers 35 years ago and was coded as undefined. It being undefined gave the compiler writers no incentive to fix their implementations to some common behavior (like diagnosis of an invalid token paste).

Re:Depressing and faintly terrifying days for the C standard... by pcwalton on Wednesday May 30, 2018 @01:45PM (#17189831)
Right, because those languages are type- and memory-safe. C is not.
Re:Depressing and faintly terrifying days for the C standard... by vyodaiken on Wednesday May 30, 2018 @03:28PM (#17190907)
I'd like to see some actual evidence of some significant optimization that depends on UB.
Re:Depressing and faintly terrifying days for the C standard... by eddyb on Wednesday May 30, 2018 @04:01PM (#17191240)
See this sibling thread https://news.ycombinator.com/item?id=17189666 - typically anything touching memory needs invariants to be optimized, which in languages that can directly manipulate memory means there's also UB (code that can't be statically proven not to break those invariants).
Re:Depressing and faintly terrifying days for the C standard... by vyodaiken on Wednesday May 30, 2018 @05:11PM (#17191836)
you don't need UB for invariant optimization. What UB permits the compiler to do is INCORRECTLY assume invariance. This permits the compiler to "optimize" badly written C code in a way that silently changes its function - which I don't call an optimization.
Re:Depressing and faintly terrifying days for the C standard... by xenadu02 on Wednesday May 30, 2018 @05:26PM (#17191978)
I’m not sure you’ve though this through.

Any C function that manipulates pointers (or arrays) can alias those pointers, type pun, and otherwise touch the same block of memory through different paths and/or treating the bits as different types.

Vast areas of optimization are completely closed to you if you want code to be resilient in the face of aliasing.

You should sit down with pen and paper to figure out how to optimize a simple function while preserving invariants but without UB. It would be very illuminating.

Re:Depressing and faintly terrifying days for the C standard... by vyodaiken on Wednesday May 30, 2018 @06:02PM (#17192284)
It's really irritating that people take this condescending approach. C is harder to optimize than language that e.g. do not have pointers. That is a tradeoff the language designers chose because they wanted C programmers to be able to type pun, for example. This is why C89's only limit on type punning was memory alignment. The intent of "restrict" was to enable some aliasing optimizations via opt-in. So, sure, things that are not invariant in C are not available for invariant optimization. C permits aliasing. So it calls for smarter compiler techniques. If you want a language that forbids aliasing, use one that does and then call out to C libraries. But breaking C to allow CS220 "clever optimizing tricks" to be easily used is bad engineering. Essentially, what you are saying is, "elimination of invariants is a good optimization, so pretending C has more invariants than it does is a good thing. "

So maybe come up with a real example and stop hand waving.

Re:Depressing and faintly terrifying days for the C standard... by eddyb on Wednesday May 30, 2018 @06:20PM (#17192410)
The UB is the result of operations which assume invariants, when they are not met. Invariants are useless if you can't assume them. Having no UB in C would require having no way to break those assumptions but C is memory/type-unsafe so that's outright impossible.

Recently I've been trying to imagine what Rust's safe abstractions that use `unsafe` code internally would look like on top of some advanced mix of dependent type theory and proofs about state-manipulating imperative programs.

At that point, the optimizer would have proofs of the invariants it can rely on and it could even potentially emit proofs that every single transformation it performed preserves the (safe) semantics of the code being optimized.

But we're not there yet. Today, we need at least a small subset of the libraries of a systems language to prevent UB "by hand". And in C (or C++, although not necessarily for the same reasons), that's even harder, as there is no notion of a "safe abstraction" (one which you cannot misuse to produce UB).

Re:Depressing and faintly terrifying days for the C standard... by vyodaiken on Wednesday May 30, 2018 @06:45PM (#17192555)
>Invariants are useless if you can't assume them.

I would think that what we want is invariants that have been validated, not assumed - especially not assumed incorrectly.

Re:Depressing and faintly terrifying days for the C standard... by eddyb on Wednesday May 30, 2018 @07:06PM (#17192665)
You won't be able to entirely validate yourself most memory-related invariants in a language which can allow breaking memory safety, that's why I mentioned the hypothetical language which can encode proofs for invariants.

What you want is effectively automated proof-search (a hard problem) for an entirely-safe systems language (which doesn't even exist yet, AFAIK. at least not what I described).

Re:Depressing and faintly terrifying days for the C standard... by vyodaiken on Wednesday May 30, 2018 @09:26PM (#17193284)
Right. C is not designed to be memory safe. So certain invariants are harder to prove (or just not true). One problem with UB is that it allows compilers to assume false things.
Re:Depressing and faintly terrifying days for the C standard... by umanwizard on Wednesday May 30, 2018 @11:24PM (#17193735)
Conversely, I'd like to see some actual evidence of some significant amount of UB that you could make defined without losing much optimization ability.

Everyone always rails against the bogeyman of asshole compile writers maliciously rewriting your program to eke out 1ms on benchmarks, but nobody every names which clang or llvm optimization pass, specifically, they think should be turned off even in -O2 or -O3.

Re:Depressing and faintly terrifying days for the C standard... by vyodaiken on Thursday May 31, 2018 @06:04AM (#17195181)
That's a false choice. Obviously Linux code is still optimized significantly despite their extensive disabling of UB based "optmizations".
Re:Depressing and faintly terrifying days for the C standard... by umanwizard on Thursday May 31, 2018 @12:58PM (#17197942)
Okay, so, specifically, which passes do you think clang/llvm should not run that it's running now in -O3 ?
Re:Depressing and faintly terrifying days for the C standard... by lyricaljoke on Wednesday May 30, 2018 @04:02PM (#17191250)
Chandler Carruth's talk on the subject was really useful for my own understanding of the reasoning behind UB:

https://www.youtube.com/watch?v=yG1OZ69H_-o

Re:Depressing and faintly terrifying days for the C standard... by eddyb on Wednesday May 30, 2018 @04:09PM (#17191295)
Some of that is sadly C having bad defaults - like people ending up using signed 32-bit integers (i.e. `int`) to index arrays on 64-bit platforms, which keeps signed integer overflow UB relevant, for optimizing typical indexing C code.

Both C++ and Rust use pointer ranges for iteration, and Rust even forces indexing/counting to use pointer-sized unsigned integers. So Rust turned off the LLVM bit which says "signed overflow is UB" and did not really lose much from it (AFAIK, anyway).

Re:Depressing and faintly terrifying days for the C standard... by vyodaiken on Wednesday May 30, 2018 @06:21PM (#17192413)
It's an interesting talk, but, just for example: architecture dependent behavior is not something anyone really wants to prohibit in C. It's hard to believe, that DJB, who writes sophisticated C code that calls out directly to Intel vector operations thinks that should be machine independent. And his API analogy is wrong. If you have an API that responds to out-of-contract parameters by, e.g. rewriting code it is a bad API. People do fuzzing tests specifically to find badly implemented APIs that don't reject bad parameters. In particular, imagine an API that does one thing, the thing you expect, when called without optimizations, and breaks your program when "optimized"! I got to the node example and gave up. The compiler is not asked to check program correctness.
Re:Depressing and faintly terrifying days for the C standard... by xorblurb on Wednesday May 30, 2018 @07:44PM (#17192862)
That's, given the question, is a really moronic answer.

There are means to apply the modern approach of optimizations beyond O0 on C without importing all kind of UB from the language level. You just have to actually proove the properties you want to rely on, instead of relying on wishful "UB => authorized by the standard => programmer fault if anything goes bad" thinking.

And promoting local variable to registers CERTAINLY NOT depends on language level UB. It would be permitted by the as-if general rule even if anything prevented it to happen in the first place, which is not the case. You don't have to have an address if nobody wants it and random pointers haver never been required to allow access to all objects, especially those who might never have an address at all. Plus nobody ever expected that anyway. People expect 2s complement. Or at least something that can not result in nasal daemons, and given C history, something that matches what the processor does. So 2s complements is at least not utterly stupid. So conflating the two is dishonest to the highest point -- except maybe if the only intended audience of the C language is now experts who e.g. write compilers. What a bright future this would be.

Hell, we dropped the hypothetical flat memory model even without strict aliasing for maybe 20 years (and probably 30, to be honest), and this had NEVER caused the kind of issues we are talking about. So don't pretend it did, just to dismiss the real issues. So ok even then it was actually probably informal as hell and in some ways worse for experts, but the amount of exploited UB was also WAY smaller. Quantity matters in this area. And context too. Do you want secure OR fast embedded systems? I would prefer reasonably secure and reasonably fast. Certainly NOT fast to execute and exploit, or more probably fast to crash pathetically.

You know very well that compiling in O0 is not going to happen in prod on tons on projects.

Don't dismiss real concerns with false "solutions", especially when mixed with proofs of your misunderstanding of the situation.

Re:Depressing and faintly terrifying days for the C standard... by comex on Wednesday May 30, 2018 @09:36PM (#17193319)
> You don't have to have an address if nobody wants it

The spec says otherwise:

> An object exists, has a constant address, and retains its last-stored value throughout its lifetime.

[C11 6.2.4.2]

But that's more of a pedantic detail. More pragmatically, you do have a point. It is indeed possible to build a C compiler that has no "undefined behavior", but only unspecified behavior, as long as the program doesn't actually violate memory safety by, say, writing to some random address it can't prove it has permission to write. For example, guessing the stack slot used for a variable and overwriting it pretty much has to be undefined behavior – it's hard to optimize anything if variables can randomly change their values without being referenced. But that's okay, because overwriting random memory is inherently unsafe without obtaining a guarantee of what that memory will be used for. On the other hand, reading from random stack memory could be unspecified. A particular stack slot might be used for a variable, a temporary expression, or nothing at all, so it's unspecified what you might find there. But the compiler will always generate a single, real load instruction, without making any assumptions about aliasing that it can't prove; thus, you'll never get logical impossibilities like "x + 1 > x".

And such a compiler could definitely produce code that's better optimized than -O0 – because -O0 is a very, very low bar (it doesn't even do register allocation, in the compilers I've seen). But I expect it would do substantially worse than a modern compiler's -O2 even on average code, with a lot of little missed optimizations that add up. (Though if you're using -fno-strict-aliasing, you're probably already eating a decent percentage of that penalty.) And in the worst cases, like tight loops that can be autovectorized only by taking advantage of undefined behavior, it might be only a fraction of the speed of the better-optimized version.

Still, it might be an interesting project, especially if you could formalize the "no undefined behavior" guarantee.

Re:Depressing and faintly terrifying days for the C standard... by xamuel on Wednesday May 30, 2018 @04:26PM (#17191426)
Here's an example where these optimizations are useful. Suppose I create a macro DEREF_AND_FREE(x) which expands to if(x)free(*x). Often times, I'll lazily use that in places where I know x isn't null. It's more readable and maintainble than splitting the macro up into two separate macros, DEREF_AND_FREE_IF_NONNULL and DEREF_AND_FREE_I_KNOW_ITS_NONNULL. The UB-based optimizations mean that I don't take the performance hit for my laziness.
Re:Depressing and faintly terrifying days for the C standard... by vyodaiken on Wednesday May 30, 2018 @05:22PM (#17191952)
There is nothing to prevent the compiler from deducing that x is non-null without UB. What UB permits is for the the compiler to deduce that x is not null in the absence of information.

so : if(x)f(x) ...code ... if(x)g(x) doesn't need UB but y = x-> n ..... code if(x)g(x) - the compiler uses UB to remover the check. This will be especially horrible when LTO becomes more prevalent.

Re:Depressing and faintly terrifying days for the C standard... by umanwizard on Wednesday May 30, 2018 @01:26PM (#17189636)
Disabling them is trivial: compile with -O0 in clang or gcc and your code will be compiled in as straightforward a way as possible.
Re:Depressing and faintly terrifying days for the C standard... by rwj on Wednesday May 30, 2018 @01:55PM (#17189924)
UB is also a source of portability errors...
Re:Depressing and faintly terrifying days for the C standard... by bokglobule on Wednesday May 30, 2018 @01:22PM (#17189586)
Amen. IMO the root of all evil is compiler "optimization" of code. If you really, really need the wee bit of extra performance through optimization, hire an expert at ass'y language and optimize the bit that matters. Rarely is this the case, especially with business apps.

I've seen my fair share of bugs from the compiler incorrectly hoisting variables from loops, mis-using registers, etc. I prefer to turn off compiler optimization as one of the first steps in a new project, but that's me.

Re:Depressing and faintly terrifying days for the C standard... by adwn on Wednesday May 30, 2018 @01:40PM (#17189769)
> If you really, really need the wee bit of extra performance through optimization, hire an expert at ass'y language and optimize the bit that matters. Rarely is this the case, especially with business apps.

If execution efficiency is not a concern and you're using C, then you're most likely using the wrong language. Especially for "business apps".

Re:Depressing and faintly terrifying days for the C standard... by Gibbon1 on Thursday May 31, 2018 @03:35AM (#17194624)
> If execution efficiency is not a concern and you're using C

Two things.

Thing one: Execution efficient on super scalar processors is very unlike what you assume it to be. For instance you assume alignment makes you program fast, when in fact it makes it slower due to cache pressure.

Thing two: On lower end processors what's important is not speed but code size.

Thing three: (off by one error natch) The amount of critical C code is tiny tiny tiny compared to the amount of C code where execution speed just does not matter because the code is almost never executed. The bleeding hot sections are often rewritten in assembly (encryption cough decryption)

Re:Depressing and faintly terrifying days for the C standard... by adwn on Thursday May 31, 2018 @08:37AM (#17195812)
> Execution efficient on super scalar processors is very unlike what you assume it to be.

It's not. What do you think I "assume execution efficiency to be"?

> On lower end processors what's important is not speed but code size.

That's why I said "most likely", not "always". Besides, those tiny microprocessors typically don't run the "business apps" which bokglobule was talking about.

> The amount of critical C code is tiny tiny tiny compared to the amount of C code where execution speed just does not matter because the code is almost never executed.

Not true for most software. Sure, most applications spend most of their execution time in a small part of the code, but it's typically not so small that you could easily rewrite it in assembly.

> The bleeding hot sections are often rewritten in assembly (encryption cough decryption)

Cryptography kernels are written in assembly to ensure that they have a constant execution time to prevent timing attacks. Not so much for performance.

Re:Depressing and faintly terrifying days for the C standard... by Gibbon1 on Thursday May 31, 2018 @08:42PM (#17201602)
> I "assume execution efficiency to be"?

You assume that modern high performance processors execute instructions. They do not, they analyze, optimize and emit their an internal instruction stream and then execute that. That extra step is what makes fiddly UB type 'optimizations' worthless. And since you don't know what processor is going to execute the code and how it's optimized, the speed gains are basically 'noise'

> Besides, those tiny microprocessors typically don't run the "business apps" which bokglobule was talking about.

"Business apps" is usually network and io bound more than anything.

> Not true for most software.

Dan Bernstein says you're wrong.

> Cryptography kernels are written in assembly to ensure that they have a constant execution time to prevent timing attacks. Not so much for performance.

Dan Bernstein says you're wrong here too. Performance is everything with encryption. Consider video streaming over and encrypted connection. Yeah that.

Re:Depressing and faintly terrifying days for the C standard... by adwn on Friday June 01, 2018 @12:48AM (#17202670)
> You assume that modern high performance processors execute instructions.

Yeah, stop making stuff up about me.

> they analyze, optimize and emit their an internal instruction stream and then execute that.

I know how an out-of-order, superscalar processor works. I also know that they can't do magic, because their optimizations are severely limited by time and scope constraints (although they do have access to some information that isn't available at compile time).

> That extra step is what makes fiddly UB type 'optimizations' worthless.

I'm not arguing for optimizations based on UB. This subthread is about compiler optimizations in general, which bokglobule claimed to be nearly always unnecessary.

> Dan Bernstein says you're wrong.

Do you have a study you can cite? Do you think just dropping a name will convince anyone?

> Performance is everything with encryption.

I never claimed it wasn't. I claimed that performance isn't the reason why cryptography kernels are rewritten in assembly, and that's because C + optimizing compiler is already fast enough and the small performance gain alone doesn't justify the switch to assembly.

Re:Depressing and faintly terrifying days for the C standard... by AnimalMuppet on Wednesday May 30, 2018 @01:50PM (#17189885)
You can care enough about efficiency to use C, and still not care about the last 0.1% of efficiency. You can especially not care about the last 0.1% of efficiency in 99% of your code.
Re:Depressing and faintly terrifying days for the C standard... by jcranmer on Wednesday May 30, 2018 @01:57PM (#17189949)
If you're compiling C with -O0 (as OP implies)... it's not just the last 0.1% of efficiency you're missing out on. Modern C compilers generate really, really crappy code at -O0, and you're looking at a 10x, 20x slowdown by forgoing optimizations. At those slowdowns, using an interpreted language that lacks a JIT starts to look competitive in performance.
Re:Depressing and faintly terrifying days for the C standard... by armitron on Wednesday May 30, 2018 @02:24PM (#17190186)
Which is why it's so important to have -- ultra-clear -- (have you looked at the clang documentation or GCC manpages?) guidelines on how to disable optimizations that can be dangerous/unpredictable (strict-aliasing comes to mind) "within" -O1 or -O2 or -Os

One shouldn't need to throw the baby out with the bathwater (-O0) in order to get some semblance of semantics that won't pull the rug under your feet when you're not looking.

Re:Depressing and faintly terrifying days for the C standard... by jcranmer on Wednesday May 30, 2018 @04:40PM (#17191562)
In practice, what these requests tend to boil down to is requests for the compiler to read the programmer's mind (strict aliasing is pretty much the biggest exception). What optimizations do you think are "dangerous/unpredictable"? Demanding that things like traps happen predictably means that you heavily constrain the ability to do dead-code elimination (can't eliminate code that could trap!) or loop-invariant code motion, two of the biggest performance wins, especially for things that could trap such as memory loads and stores, which are the code you most want to avoid whenever possible for performance.

Undefined behavior essentially says that compilers don't have to care about what happens in the cases that would constitute undefined behavior. This doesn't manifest in the compiler as if (undefined_behavior()) { destroy_users_code(); }, contrary to popular opinion. It instead tends to manifest as logic like "along this control-flow path, this condition is true, so we can now thread the jump from block A to block C since you're redundantly checking a known-true condition" and only after unwrapping several layers of computed assumptions do you find the "we assumed overflow cannot occur" at the bottom.

Re:Depressing and faintly terrifying days for the C standard... by BeeOnRope on Wednesday May 30, 2018 @05:52PM (#17192208)
Exactly this.

There seems to be some idea that there is some "evil pass" in the compilers which looks for undefined behavior and then maliciously optimizes surrounding code to do something the programmer didn't expect.

Not at all. Even all the examples of UB leading to unexpected optimizations usually involve a chain of events, with very straightforward and necessary optimizations being involved, like value propagation, inlining, dead-code elimination, etc - and these aren't inherently related to "exploiting UB". You could (in some compilers) disable one of the things in the chain and perhaps avoid the problem for that example, but you'd also hurt a lot of other code that relied on the optimization.

That's one of the problems with people asking for specific small snippets of code where the UB-related transformation produced a big gain: you can certainly find these (but they'll often be picked apart) - but the bigger problem is not with the specific examples: it's that whatever optimization optimization you disable to make the small example work as you'd expect might then produce worse code across your application.

So people who want to disable the optimization that does "that" are often incorrectly assuming there is a small simple optimization which leads to "that" in the first place.

Still, I definitely agree that the situation regarding UB is depressing in many respects. Many of the decisions made by the C committee in the past haven't aged well. If you take a look at the low-level optimizations afforded by the largely-deterministic Java specification, they are largely at the same level as C - but Java had the benefit of coming along a couple of decades later where many open questions in C's time, such as integer overflow behavior, integer sizes, shift behavior, pointer models, etc, had largely been resolved. Platforms that don't conform to the JVM's model of an ideal machine will just have to generate slow code in some cases.

Re:Depressing and faintly terrifying days for the C standard... by armitron on Wednesday May 30, 2018 @06:09PM (#17192332)
I'm not requesting the compiler to read my mind, I'm just asking for dead obvious and simple guidelines that allow me to perform a cost-benefit analysis and tune the compiler's behavior to what I consider acceptable.

Examples:

I don't care at all about optimizations that are a result of treating signed integer overflow as undefined behavior. I'll go for predictable, deterministic behavior every single time.

Same for strict aliasing rules. I find it absolutely insane and mind boggling that -O2 enables strict aliasing amongst who knows what else (50+ other flags). Why can't the impact of these optimization flags be easier to deduce? Why do I feel like I need to be a compiler developer just to get some measure of confidence in what the optimizers are doing? It's insane that there are people who treat this sort of unwarranted, dangerous complexity as a rite of passage and don't push for something that's better. Most importantly, it's terrifying that a significant chunk of C programmers _are not even aware of these issues_.

C will stick around for decades and will continue to be picked up by newcomers. It doesn't have to stay as dangerous and uncompromising as it is now.

EDIT: There is some progress with things like ubsan and asan but alas they're fairly limited platform-wise. What's worrying is that there hasn't been a clear shift in the mentality of those who control the language as the OP indicates in his report.

Re:Depressing and faintly terrifying days for the C standard... by jcranmer on Wednesday May 30, 2018 @07:32PM (#17192796)
> I'm not requesting the compiler to read my mind, I'm just asking for dead obvious and simple guidelines that allow me to perform a cost-benefit analysis and tune the compiler's behavior to what I consider acceptable.

And how can I, as a compiler writer, know what you, the compiler user, consider acceptable without reading your mind?

It would be nice if we had some sort of contract. I, the compiler writer, will say that, so long as you write code that conforms to this contract, I will faithfully execute that code. And you, the programmer, will say that you will only write code that conforms to this contract, and that I don't need to worry about you writing code that fails to conform to this contract. Well, somebody already wrote this contract: it's the C specification.

What you're really saying is that you don't like the contract you have, which is a totally valid position to take (I myself would love to see strict aliasing go die in a fire). But almost no one who objects to the C contract is willing to actually negotiate a new contract. Instead, it's almost invariably a complaint that amounts to "you fucking compiler writers are ruining my code with your stupid optimizations." Well, no, your code was already broken per the contract; we were just too dumb to realize it earlier. And for the big undefined behaviors where well-behaved semantics are feasible (such as strict aliasing or signed integer overflow), we provide options to let you say "I don't agree to the C contract, I want to agree to a not-quite-C contract instead."

Many people right now don't realize that undefined behavior is merely a contract that the programmers not to create these statements, true. But that's why we, as compiler writers, try to educate people about why undefined behavior exists, what it means for programmers, and providing tools to make it much easier for programmers to find where it exists (hence things like asan and ubsan).

Re:Depressing and faintly terrifying days for the C standard... by pjmlp on Wednesday May 30, 2018 @06:30PM (#17192464)
> C will stick around for decades and will continue to be picked up by newcomers. It doesn't have to stay as dangerous and uncompromising as it is now.

Exactly, even if most of us don't touch it directly, we need to use systems that make an heavy use of C (including Objective-C and C++), so it would already be an improvement to our computing stack if C could be improved as such.

Re:Depressing and faintly terrifying days for the C standard... by anyfoo on Wednesday May 30, 2018 @01:59PM (#17189967)
Every once in a while I have to compile a kernel without optimizations. The performance penalty you pay is far away from 0.1%, it is definitely very noticeable on first glance. There might be projects where even that does not matter, but I would not think those are the majority.
Re:Depressing and faintly terrifying days for the C standard... by BeeOnRope on Wednesday May 30, 2018 @05:54PM (#17192227)
An order of magnitude slower on non-trivial computation-heavy code (i.e,. not just doing a lot of IO or calls into libraries/the kernel) is a pretty good rule of thumb. Sometimes better, sometimes much worse.

The more layers of abstraction, the worse the penalty for not optimizing, so C++ is usually more heavily affected than C, for example: many of the so called "zero-cost" abstractions in C++ rely on a good optimizer.

Re:Depressing and faintly terrifying days for the C standard... by anyfoo on Wednesday May 30, 2018 @01:42PM (#17189788)
C is nowadays foremost a system programming language. Try compiling your operating system with -O0 and see if you still don’t need those optimizations. You’d be surprised.
Re:Depressing and faintly terrifying days for the C standard... by convolvatron on Wednesday May 30, 2018 @01:54PM (#17189914)
there's tenuous and difficult to reason about optimizations and then there's removing all of the completely useless loads and stores of intermediate results that occurs in the default simplistic translation.

i might be with you on the former, but that first pass really cuts down on executable (icache) size, nets a huge performance win and actually makes the generated code alot easier to read.

some of those analyses can get pretty involved. but i dont think we should be discouraging people from investigating them - or from providing nice safe defaults and flags to turn them on.

one other speed-independent factor here is that like you I usually start without any optimizations. and usually when I turn it on I find a few bugs in my own code right away. thats pretty helpful.

Re:Depressing and faintly terrifying days for the C standard... by kazinator on Wednesday May 30, 2018 @01:31PM (#17189689)
If the project will eventually be compiled with optimizations in production, it's probably better to hit those problems earlier than later.