11 ms·
A one word change to the C standard to make undefined behavior sane again
- jmmcd 8y ago"Possible" v "Permissible" is not the problem. What seems to be needed (based on reading the post but no other knowledge) is the phrase "including but not limited to".
- KayEss 8y agoI don't think that is what the article is arguing for. There it would be more akin to "absolutely limited to" as they don't want anything outside of the examples. Unfortunately for them the examples are still open enough to allow for what compilers do (things like remove code that the author intended be left in). 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).
- mpweiher 8y ago> Unfortunately for them the examples are still open enough to allow for what compilers do (things like remove code that the author intended be left in). 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.
- mehrdadn 8y agos/but/and/
- MaxBarraclough 8y agoThe author concedes that he isn't a compiler engineer, but doesn't seem to think that perhaps compiler engineers have good reason for having their compilers generate code that behaves bizarrely if there's undefined behavior. 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 https://gcc.gnu.org/ml/gcc/2012-02/msg00038.html
- jstimpfle 8y ago> C has undefined behaviour in the first place (unlike, say, Java) is to maximally enable compiler optimisation. 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.
- oddity 8y agoI propose that, instead of it being a wording change that sparked the current “craziness” of optimizing C compilers it was the combination of two things: 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?
- iainmerrick 8y agoArguably also 3) the desire to look good on benchmarks, and to take the performance crown from FORTRAN.
- pjmlp 8y agoIt surely takes the security exploit crown.
- praptak 8y agoThis could prevent some useful optimizations. The following can be a single rotate instruction but only when relying on undefined behavior of shifts (otherwise the code has to produce zero for i outside the sensible range) (n << i) | (n >> (sizeof(n) - i)) So, does it fall under "demons out of your nose"?
- tomerv 8y agoI'm the article's suggestion, this could still be a single instruction, as long as it's documented behavior.
- mar77i 8y agodidn't you want to multiply your sizeof n by CHAR_BIT?
- pg314 8y agoOf course that code is broken (relies on undefined behavior) when i is zero, which is a totally reasonable input for a rotate function. A nice illustration of the pitfalls of undefined behavior. You can find a correct implementation in a blog post by John Regehr [1]. [1] https://blog.regehr.org/archives/1063 https://blog.regehr.org/archives/1063
- MaxBarraclough 8y agoI don't really buy the idea that invoking undefined behaviour is ok so long as you know it happens to work on your platform. 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.
- CJefferson 8y agoThe biggest problem is, the most classic C bug is writing an array out of bounds. It would be almost impossible to efficiently add bounds checking to all C arrays, so let's assume we are going to leave that as undefined behaviour. 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.
- jzwinck 8y agoWhat many people don't expect is that if the compiler can prove that some function invokes undefined behavior, the compiler can emit no code for it at all--before, during, and after the UB expression. 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.
- mpweiher 8y agoWhat you describe is pretty much exactly the range of "undefined" behavior one would expect, and also in the range of what used to be "permissible": 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.
- zhivago 8y agoThe argument hinges instead on manging drop the word 'unpredictable' from what he derives from the standard. If he kept that word intact his argument would collapse.
- mpweiher 8y agoNot at all. UB is unpredictable because you don't know what will happen just from the source text, for example when you write beyond array bounds. However, it can be perfectly predictable what code will be emitted: a store.
- pif 8y agoOh no! One more post from someone who can't code as well as he thinks and looks for a scapegoat! Please, just stop!
- kazinator 8y agoThis article is a pretty ignorant diatribe. 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.
- mpweiher 8y ago> The possible range of actual behaviors doesn't define what UB 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.
- kazinator 8y agoA lot of things were better in the ANSI C/C90 era; but the definition of undefined behavior is essentially the same. "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.