31 points · by signa11 · 8y ago · 111 comments
There is, of course: performance.
If you want a compiler that generates code which behaves intuitively even under UB, that requirement is at odds with compiler optimisation. Compiler optimisation generally means doing strange and unexpected things to your code, and the reason C has undefined behaviour in the first place (unlike, say, Java) is to maximally enable compiler optimisation.
In other words, C values performance over safety, and that's part of the point.
If you want to catch undefined behaviour at runtime and treat it as an error, the tooling for doing that is better than ever: Clang's 'UBSan', for instance.
Edit
Aside: I have a personal favourite 'incident' pertaining to exactly this tension (between performance and intuitive behavior). Torvalds vents his frustrations on the GCC mailing list, regarding legal-but-counterintuitive behaviour of code generated for the Alpha architecture: https://gcc.gnu.org/ml/gcc/2012-02/msg00038.html
My understanding is that C has undefined behaviour where there is no universally sensible implementation across all architectures.
> Compiler optimisation generally means doing strange and unexpected things
In the first place it should make code faster. I'm no one to judge, but the general reception seems to be that exploiting undefined behaviour (by assuming it will never happen) rarely brings obvious speed advantages. "Strange and unexpected things" can be a bad tradeoff for a tiny speed advantage.
For example, I'll boldly state that optimizing out a "if (p == NULL)" only because p was dereferenced earlier never makes sense. Code containing such a check is never performance sensitive enough to justify the tradeoff (or it's really bad code).
And without being a compiler engineer I assume that it should be easy to change compilers to NOT do this optimization (I assume the not-NULL inference is a special case in the code that can simply be removed).
> In other words, C values performance over safety, and that's part of the point.
Many people value C because of control and allowing one to not paint oneself in a corner. Performance is only a side effect.
I’ll play devil’s advocate here, even though I strongly dislike the current direction of the major C compilers.
This aggressive interpretation of undefined behavior apparently can enable some important optimizations. Here are a couple of examples:
First, say you have an index variable where you increment it (i += n) and check it’s within bounds (i >= start && I < end). It could be useful to assume that i won’t overflow and wrap, so some of the bounds checks can be optimized out. (The downside is that if you want to explicitly check for overflow, it’s tricky to do so correctly.)
Second, say you have a function that takes several pointers and copies data between them. It can be useful to assume those pointers aren’t aliased, so we can assume that memory isn’t changing in unexpected ways, and therefore it’s safe to optimize out some repeated reads.
I do feel like those cases could be handled in different ways, though. In the second case, perhaps the compiler could insert a runtime check for aliasing, and branch to either a fast path or a slow path as appropriate.
I’m also not sure that C really needs to try to be the fastest and most highly optimized language, even though it’s widely regarded as being very fast. As far as I know C isn’t the most popular language for scientific supercomputing, for example, where the users really do care about performance.
Sure it does. That's the whole point: performance and low-level access over safety and convenient features. These are the defining attributes of the C language (along with its minimalism).
If you want a safe and convenient language, why are you using C?
If you don't think C should be as fast as possible, which language should? How would that language differ from C?
It's not C's fault that people use it when they shouldn't.
> In the second case, perhaps the compiler could insert a runtime check for aliasing, and branch to either a fast path or a slow path as appropriate.
I don't get you. Are you referring to the dangers of the 'restrict' keyword? Otherwise, it's quite legal to pass aliasing pointers, and the compiler is free to introduce a check for aliasing.
If you're thinking of the strict aliasing rule, I think you're misunderstand it. It has to do with misusing types. To quote Wikipedia:
> pointer arguments in a function are assumed not to alias if they point to fundamentally different types, except for char* and void*, which may alias to any other type
This is a very common example, but how hard is it for the compiler to prove that it won't overflow? I know in the general case it's the halting problem, but for specific cases (especially where a human inspecting the code would easily see) it should be easy --- especially those cases which warrant optimisation.
There are two less drastic options used by the Standard for those cases.
- Unspecified behavior: behavior that must be "sane" in some sense but may differ between implementations. Vendors need not document the exact behavior.
- Implementation-defined behavior: behavior that must be sane and may differ between implementations. Vendors are required to document the behavior.
That's somewhat true for things like signed integer overflow, but it's more than that. For instance, if there wasn't UB around pointers being "in bounds", then either there would have to be expensive checking of every pointer dereference ever (both read and write), or other performance drains like values never ending up in registers (both literally and conceptually) for extended periods, they'd have to be loaded from memory every time they're used, effectively treated as `volatile`. (And, this inhibits other optimizations like breaking up structs via SROA.)
For instance,
int f(int *p) {
int x = 0;
*p = 10;
if (x != 0) { ... }
}
The compiler can't optimize out the branch unless it can assume (or dynamically ensure) that the dereference of the pointer p won't point to an unrelated object (x). Arbitrary transformations on pointers (including constructing them directly from integers, and one might guess the stack address of x) are "only" undefined behaviour in C, not prohibited statically.Sure, that's what it means to have full pointer arithmetic. It's very much an aim away from face feature of the language.
This kind of thing really comes to the fore when the compiler is doing a lot of inlining. Say you have some kind of utility function:
int frob(struct foo *f)
{
int r;
if (f != NULL)
{
/* things */
}
else
{
/* other things */
}
/* more things */
return r;
}
..and you call this function all over the place. If some of those callsites end up with `frob()` being inlined, and the compiler can see that the pointer passed is never NULL at that particular site, it can usefully eliminate the dead code. This despite the fact that the code isn't dead in general - just at some of the callers.No I would think it happens naturally as a result of more generally giving abstract values stamps, narrowing the stamps based on expressions, and then simplifying expressions using those stamps. I would guess you wouldn't want stamps and optimisations based on them removed entirely.
> My understanding is that C has undefined behaviour where there is no universally sensible implementation across all architectures.
These are two ways of looking at the same thing. A naive translation of C statements to what seems like the most straightforward way to do them on some target architecture is an optimization over emulating some other architecture’s behavior. Consider the problem of translating one arch’s assembly to another’s. Essentially, C needs to be defined at a certain level of abstraction for all of this to work, but it also happens that detecting holes in this abstraction is undecideable.
Optimisation refers to code transformations which result in output code which has better performance (whether in terms of space or time).
Changing the definition of the source language, isn't optimisation.
Whether the source language can cleanly be mapped to the target machine, may be significant to performance, but it's not optimisation.
But that's really the same thing.
Java takes the opposite approach: it lays down the law, and if your CPU does things differently, the JIT compiler will have to generate however many extra instructions are necessary to bend things to Java's approach.
For instance, Java mandates 2's complement, where C does not. Java mandates the size of an 'int', where C does not. Java specifies exactly what happens if you attempt to dereference null, where C does not.
(Too late I see that oddity just made the same points.)
> In the first place it should make code faster
Well of course, or it's not optimisation, it's obfuscation.
> the general reception seems to be that exploiting undefined behaviour (by assuming it will never happen) rarely brings obvious speed advantages
Depends what you mean by 'obvious' I suppose. For instance, Java pays a non-zero performance penalty for insisting on range-checks for array-element accesses.
> I'll boldly state that optimizing out a "if (p == NULL)" only because p was dereferenced earlier never makes sense.
No need for boldness: Apple says that UBSan's performance penalty is around 20%. https://developer.apple.com/documentation/code_diagnostics/u...
> Code containing such a check is never performance sensitive enough to justify the tradeoff
C programmers tend to hold some disdain for the attitude that they should be made to trade off performance for safety, and not without reason. Claiming they should 'never' care about the overhead of some check, isn't the C way.
If you want a safe language, don't use C. (Well, there's MISRA C, I suppose.)
> it should be easy to change compilers to NOT do this optimization
I don't know exactly what change you're proposing. If you manually check for null, the compiler can't just eliminate your check. Or do you mean C should always check for null at every dereference? The overhead there would be considerable.
> Performance is only a side effect
No, performance is central to the appeal of C.
It surely didn't felt like that using 8 and 16 bit compilers up to early 90's.
Their post was predicated on the assumption that the compiler can in fact do this if 'p' was dereferenced before the check. Is that not the case?
The thing that were and were not codified as requirements in 1989 ANSI C were a historic accident of the time; now that historic accident is just being preserved, more or less.
E.g. we must have unspecified evaluation order of function arguments because we've always had unspecified evaluation order of function arguments.
The original intention was to enable optimisations, after all. If you do
make_coffee( get_barista(), get_beans() );
then if your target platform uses a stack-based calling convention, you'll want to do those two 'inner' calls in a certain order (depending on whether it's a 'reversed' stack-based calling convention or not).If you're passing by registers, it will presumably make no difference.
It's a pretty wacky micro-optimisation by anyone's standards, and allows strange platform-specific bugs to arise, but I presume the refusal to specify evaluation order is just the standards body exhibiting a healthy resistance to changing the very stable C language. They don't want to cause a downside for anyone, even the strangest half-baked compilers.
The programmer almost certainly didn't put in the dereference on purpose, as a way of encouraging p == NULL to be optimized away!
It's very irresponsible to treat situations that are coding mistakes as "let's assume there is no mistake and just optimize".
Well, that's the C philosophy, and it's part of the reason it's so fast. As I've said repeatedly throughout this thread, if you want a safe language, why are you using C?
If the compiler detects undefined behavior, it should of course issue a loud warning to the programmer, but that's not the issue. The issue is that the compiler assumes absence of UB and optimises accordingly, spending relatively little effort investigating whether UB is actually present.
If that's what you want, use a proper static-analysis tool. (They really deserve more use!)
The example I should have given was "is dereferenced LATER". Because only in this setting the optimization actually has an effect on most platforms (where dereferencing NULL segfaults).
The criticism arose because this optimisation had been applied in a kernel environment, where the dereference happened before the test, but in some circumstances might not trap. It isn't something that concerns normal applications, though.
> There is, of course: performance.
It is a reason. But it is not a good one.
Especially when you're not ignoring the possible outcomes and just ""optimizing"" code away (and even disregarding the behaviour of their own platform)
If C took that approach, it would lose many of its unique advantages.
Whether performance matters, depends wholly on what you're doing. There can be no one-size-fits-all programming language.
Such as?
> Whether performance matters, depends wholly on what you're doing. There can be no one-size-fits-all programming language.
Correct. But I think the performance side is overrated because C does not have the needed expressiveness to use most modern performance enhancements (like SIMD, auto vectorizing requires a lot of work and checking of conditions, and is not very efficient) and what were important optimizations 40 yrs ago today are reckless (like no boundary checks)
(This discussion doesn't apply to embedded systems for the most part, those today a lot of them are faster than 15y.o. PCs)
Sure they have reasons. And for them, these reasons are "good". The problem is that what is good for the compiler engineers has ceased to align with what is good for most compiler users.
The reasons for this are complex, but one huge one is that we all generally don't pay for compilers any longer, at least not directly. If you have paying customers, you don't pull stunts like that.
Michael Stonebraker talked about a very similar phenomenon in database systems and research:
https://www.youtube.com/watch?v=DJFKl_5JTnA
The observation I found particularly interesting is that the few commercial companies that still interact with research, and I think the same goes for compiler/optimizer engineering, are the "whales", companies like Microsoft, Amazon, Google and Facebook. Their needs are very different from most everyone else who doesn't run datacenter where a 0.1% performance increase can mean millions of dollars of cost savings and therefore be worth quite a bit of pain. Particularly when it is pain for others.
> C values performance over safety
No. Well, yes, because it doesn't value safety much at all. Certainly not when I started, which was with K&R C, so no type-checking at all for function calls, for example. What C values is DWITYTD, "Do What I Tell You To Do". C makes it possible for programmers to write code that performs well, it doesn't try to make code perform well. That's a crucial difference.
Seibel: Do you think C is a reasonable language if they had restricted its use to operating-system kernels?
Allen: Oh, yeah. That would have been fine. And, in fact, you need to have something like that, something where experts can really fine-tune without big bottlenecks because those are key problems to solve. By 1960, we had a long list of amazing languages: Lisp, APL, Fortran, COBOL, Algol 60. These are higher-level than C. We have seriously regressed, since C developed. C has destroyed our ability to advance the state of the art in automatic optimization, automatic parallelization, automatic mapping of a high-level language to the machine. This is one of the reasons compilers are ... basically not taught much anymore in the colleges and universities."
-- Fran Allen interview, Excerpted from: Peter Seibel. Coders at Work: Reflections on the Craft of Programming
1) Faster CPUs and more memory allowing compilers to step beyond mostly just peephole-optimization to do optimizations that were once prohibitively expensive.
2) The general convergence of mainstream processors on more or less identical characteristics (16/32/64 bit registers, MMUs giving the appearance of a flat memory space, code exists in memory, 2s complement, etc...) causing programmers to forget how truly diverse computers can be.
In effect, as a collective, we’ve forgotten why these parts of C were never defined and now that compilers are smart enough to optimize code in magical ways but not smart enough to perfectly verify that the programmer didn’t make a mistake, we have this situation of terrible UX and programming pitfalls.
I’m conflicted, and it feels wrong to say this, but maybe it is time to accept that all the world’s more or less an x86 and spin off a more well defined C that’s perf-optimal under some compilation settings for x86-likes. Everyone else not in the sanctioned-land would write code in their own dialect of C with well defined characteristics or limitations for their now-obscure architecture. There are a couple other issues that would need to be resolved, but in some sense, this is already the world we live in.
Except, what about this new web architecture? Do we consider the problem of compiling C to it impossible until we’ve extended it to the point that the semantics are similar to x86?
That out of bounds write could go anywhere. Into any other variable. Changing the code of the program (if it isn't write protected). So the most common C bug can lead to absolutely any change (hypothetically) to the running program.
That's one of the reasons writing text to "explain" what undefined behaviour can do is so hard.
you just write to that address
What happens then is hard to predict, and therefore in a sense the behavior after that becomes "undefined". This is fine. What's not fine is the compiler taking some action other than
1. just writing to that address (ignoring), or
2. emitting a compiler error and terminating, or
3. doing something else that is "characteristic" of the environment, and documenting it
Current optimizing compilers don't do any of these things, and also not something that in any way resembles any of these options.
Saying that writing past the end of an array could modify any other variable is one thing. Saying that writing past the end of an array could make your entire program do nothing is quite another.
(n << i) | (n >> (sizeof(n) - i))
So, does it fall under "demons out of your nose"?You can find a correct implementation in a blog post by John Regehr [1].
If that disaster really does boil down to one instruction, I'd hope the compiler would expose an intrinsic.
Doing that kind of thing isn't so evil if you're careful to include a compile-time assert that will break the build (or perhaps 'failover' to safe code) if you try to compile on anything but your exact compiler and for anything but your exact target platform, but even so, I've never seen an appropriate time to deliberately invoke UB like that.
Undefined behavior simply means that the standard doesn't have requirements for what is being expressed in the program.
The possible range of actual behaviors doesn't define what UB is; that wording just clarifies that UB doesn't necessarily mean that there is a software defect: it is possible to behave in a documented manner characteristic of an implementation. This refers to documented extensions.
The way to fix C isn't to monkey with the definition of UB, but to target specific cases where there aren't requirements and, like, put requirements there.
Hmm...you might want to actually read an article before declaring it an "ignorant diatribe".
The current standards do say "possible". The first ANSI/ISO standard said "permissible". It also wasn't a "NOTE" back then.
If the standard currently said what I would like it to say, why on earth would I advocate changing it?!?
And once again, the wording I want is the way it was in ANSIC-C '89. So I am advocating changing the wording back to what it was...and lo-and-behold, C compilers were more sensible back then.
So please inform yourself.
"Permissible" is a poor choice of word; "possible" is a legitimate clarification.
The reason "permissible" is poor because it wrongly hints at the possibility that some behaviors in response to UB may be "impermissible". Which, in turn, wrongly suggests that UB is something other than the absence of requirements.
Compilers were more sensible, but not because of squabbling over some minor wording over the definition of undefined behavior.
It was never the case that undefined behavior was intended to be constrained in any way.
Personally I'm OK with the more aggressive optimisations, but they are still very controversial -- I think more so in the C than the C++ community (I'm in the C++ one).
Though one could argue that, I don't really think this is the case.
Certainly "ignoring" it isn't the case when you record the fact that there is UB and then optimize based on it.
They also aren't issuing a diagnostic and terminating, either at compile or runtime.
So the remaining option is "behave in a manner characteristic of the environment". Which is fairly loose/rubbery, and I'd be happy to tighten it further. I still don't think it allows the current behavior even as written though, because I don't see how that is "characteristic of the environment". But the stinger is that the behavior has to be documented, which very little of the UB exploitation currently is.
If he kept that word intact his argument would collapse.
However, it can be perfectly predictable what code will be emitted: a store.