10 ms·
I think what really annoys me is that this looks like actual malice on the part of the standards writers, and less severe malice on the part of the compiler aut
by lelanthran 3y ago
I think what really annoys me is that this looks like actual malice on the part of the standards writers, and less severe malice on the part of the compiler authors.
I know I should attribute it to stupidity instead but ...
The standards writers could have made all UB implementation defined, but they didn't. The compiler authors could have made all Uab implementation defined, and they didn't.
Take uninitialised memory as an example, or integer overflow:
The standard could have said "will have a result as documented by the implementation". They didn't.
The implementation can choose a specific behaviour such as "overflow results depend on the underlying representation" or "uninitialised values will evaluate to an unpredictable result."
But nooooo... The standard keeps adding more instances of UB in each revision, and the compiler authors refuse to take a stand on what overflow or uninitialised values should result in.
Changing the wording so that all UB is now implementation defined does not affect blegacy code at all, except by forcing the implementation to document the behaviour and preventing a compiler from simply optimising out code.
- nottorp 3y ago> less severe malice on the part of the compiler authors. This is optimization run amok and I'd call it malice. If C/C++ are designed to let me shoot myself in the foot, the compiler should let me shoot myself in the foot instead of erasing my code.
- torstenvl 3y agoThe standard does make all definable UB implementation defined. The compiler writers intentionally misread the standard to allow these kinds of optimizations. C17 Draft, § 3.4.3: "Possible undefined behavior ranges from... . . . ignoring the situation completely with unpredictable results, to . . . . . . behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message) . . . . . . to terminating a translation or execution (with the issuance of a diagnostic message)." Compiler writers like to language-lawyer this in two ways. First, they interpret "possible" to mean that this is a non-exhaustive list, but that isn't how interpretation of such a document typically works. Expressio unius est exclusio alterius -- the express inclusion of one is the exclusion of others -- means that any list is exhaustive unless explicitly indicated otherwise. In other words, the reality is that, according to the standard, these are the only possible options. Second, they interpret "ignoring the situation completely" to mean "actively seeking out the situation and deleting whole sections of code based on that." This is quite self-evidently a dishonest interpretation.
- lelanthran 3y agoWhile you have convinced me that the compiler authors are more culpable than the standards committee, I still feel that they are ultimately responsible - the buck stops with them. They're free to, and easily able to, add the word "exhaustively" when listing possible behaviours, but they don't. They can even do away with it entirely and replace it with implementation-defined, which makes compilers non-conformant if they optimise out code.
- cesarb 3y agoThe optimization opportunities do indeed come from the first option, "ignoring the situation completely with unpredictable results". That is: the compiler assumes that the undefined behavior will not happen, and optimizes based on it. They do not "actively seek out the situation", it's in fact the opposite, they assume that the situation simply won't happen. And "deleting whole sections of code" is just the normal "unreachable code" optimization: code which cannot be reached on any feasible execution path will be deleted.
- deleted 3y ago[deleted]
- torstenvl 3y agoThat is an obvious misreading. Assume you have an unsigned char array r[] declared with 100 elements and the compiler encounters the following line of code: r[200] = 0xff; "ignoring the situation completely with unpredictable results" means translating that to the appropriate object code without bounds checking and no guarantees are made about whether that's a segfault or what your environment does. "assum[ing] that the situation simply won't happen" means eliding the code entirely. The first is required by the standard. The second is prohibited by the standard. There is no section of any version of the standard that permits compilers to assume that anything which might be UB or predicated on UB is dead code. Compilers must either ignore the fact that it's UB, or behave in a documented implementation-defined manner, or stop translation.
- pdw 3y agoYou're quoting from a footnote. The footnotes are not part of the normative text, so how you choose to interpret it is irrelevant. The actual definition of undefined behavior given by § 3.4.3 is: "behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements." Since no requirement is imposed, compilers can do as they want.
- kelnos 3y agoI think it's a bit out of line to call this malice. The C standard was written when people wanted a language that was easy to read and write, but could have similar performance to hand-written assembly. Performance, as the article title points out, was a bigger priority than correctness or eliminating foot guns. Most of us here weren't around when many/most programs were written in assembly, and when machines were very constrained in how much memory they had and how many CPU cycles they could spare. Nowadays, performance concerns usually come behind speed and correctness of development. So it's hard to truly feel what trade offs the original designers of C (and the original C89 standards committee) had to make. I'm not convinced implementation-defined behavior is really any better, though. Packagers and end-users should not have to ensure that the code their compiling is "compatible" with the compiler or hardware architecture they want to use. Developers shouldn't have to put a "best compiled with FooC compiler" or "only known to work on x86-64". That would not be an improvement. I do agree that, these days, there's no excuse for writing language specifications that include undefined behavior. But C has a lot of baggage.
- lelanthran 3y ago> I think it's a bit out of line to call this malice. I apologise, but note (warning: weasel words follow) I was careful to say it looks like malice to me, not that I have any indication that it actually was malice. > I'm not convinced implementation-defined behavior is really any better, though. Packagers and end-users should not have to ensure that the code their compiling is "compatible" with the compiler or hardware architecture they want to use. I think it is better, purely because then the resulting code can't simply be omitted when an integer may overflow, the code still has to be emitted by the compiler. Right now all the worst bits of UB has to do with the compiler optimising out code. With IB replacing UB, the implementation will have to pick a behaviour and then stick with it. Much safer.
- pjmlp 3y agoThat is usually a myth. From the people that were there at the time. "Oh, it was quite a while ago. I kind of stopped when C came out. That was a big blow. We were making so much good progress on optimizations and transformations. We were getting rid of just one nice problem after another. When C came out, at one of the SIGPLAN compiler conferences, there was a debate between Steve Johnson from Bell Labs, who was supporting C, and one of our people, Bill Harrison, who was working on a project that I had at that time supporting automatic optimization...The nubbin of the debate was Steve's defense of not having to build optimizers anymore because the programmer would take care of it. That it was really a programmer's issue.... 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
- layer8 3y ago> The standards writers could have made all UB implementation defined, but they didn't. This is not possible without performance impact, because, in the general case, whether a program constitutes UB depends on data and/or previous program flow (halting problem), and thus would require additional runtime checks even for code that happens to never run into UB.