29 ms·
Undefined behavior in C is a reading error
- pjmlp 5y agoDuplicate, https://news.ycombinator.com/item?id=27217981 https://news.ycombinator.com/item?id=27217981
- yesenadam 5y agoThat only got 1 comment.
- pjmlp 5y agoJust like this one, when I posted mine here
- setr 5y agoLinking a failed discussion from another (possibly) failed discussion seems kind of pointless.
- pjmlp 5y agoJust like this thread.
- yesenadam 5y agoI assumed your labelling it a dupe and giving a link meant there was a more interesting discussion on that page. Nope. So I left my comment to warn others not to make the same mistake I did. You didn't seem to understand, so someone tried explaining, then you left another obtuse, mean-spirited comment.
- goalieca 5y agoIf C is just a portable assembler then what if the assembly itself has undefined behaviour. :)
- kmeisthax 5y agoThis exists, but the effect of undefined behavior in CPU architectures is a little bit more forgiving than the interpretation of UB in C to mean "literally the entire program has no meaning". Instead, usually the program will execute correctly up to the invalid instruction, and then something happens, and then the CPU will continue executing from that state. It's actually fairly difficult to build an instruction with undefined behavior that contaminates unrelated parts of the program. Though it HAS happened: notably, brucedawson explains here [1] that the 360 has an instruction so badly thought out that merely having it in an executable page is enough to make your program otherwise meaningless due to speculative execution. [1] https://randomascii.wordpress.com/2018/01/07/finding-a-cpu-design-bug-in-the-xbox-360/ https://randomascii.wordpress.com/2018/01/07/finding-a-cpu-d...
- infogulch 5y agoWow! Interesting to see hints that meltdown exists years before it was officially published.
- mhh__ 5y agoSkimmed the article and didn't see a reference to it, you may be interested to know that our good friends and protectors at the NSA may have stumbled on to Meltdown-like issues in the mid 90s https://en.wikipedia.org/wiki/Meltdown_(security_vulnerability)#History https://en.wikipedia.org/wiki/Meltdown_(security_vulnerabili...
- infogulch 5y agoI see the NSA strategy for 'securing' the nation against technology threats in their 'unique' way was going strong back in 1995.
- cperciva 5y agoThe author seems to be missing this essential text: "the implementor may augment the language by providing a definition of the officially undefined behavior." Making a system call is undefined behavior in the C standard, but it's not undefined behavior in clang-on-FreeBSD, because the implementors of clang on FreeBSD have defined what those system calls do. Ditto for "asm" (UD unless/until you're running on a compiler which defines what that does), all of the tricks which make "malloc" work, and all of his other examples of acceptable uses of code which the C standard does not define.
- bitwize 5y agoThe thing about UB is that it tends to happen when the C standard refuses to specify when a program segment is erroneous or valid. Some C environments treat memory as a large array of undifferentiated bytes or words, by design. Other C environments have tagged, bounds-checked regions of memory, again by design. (For example, the C compiler for the Lisp machine.) Usually, indirecting through a null pointer or walking off the end of an array are erroneous, but sometimes you want to read from memory location 0, or scan through all of available memory. The C standard allows for both kinds of environments by stating that these behaviors are undefined, allowing the implementation to error out or do something sensible, depending on the environment. The idea that UB is carte blanche for implementations to do whatever is an unintended consequence of the vague language of the standard. Maybe a future C standard should use "safe" and "unsafe" instead of UB for some of these operations, and clarify that unsafe code will be erroneous in a safe environment and do something sensible but potentially dangerous in an unsafe environment so you must really know what you're doing.
- deleted 5y ago[deleted]
- Rusky 5y ago> The idea that UB is carte blanche for implementations to do whatever is an unintended consequence of the vague language of the standard. Whether or not this was originally intended, it's certainly become the way the standard is written and used today, so that's kind of beside the point. Further, this is not some new idea that arose from the C standard. It's a basic, core idea in both software engineering and computer science! You define some meaning for your input, which may or may not cover all possible inputs, so that you can go on to process it without considering inputs that don't make sense. Now, to be fair, the "guardrail-free" approach where UB is silent is a bit out of the ordinary. A lot of software that makes assumptions about its input will at least try to validate them first, and a lot of programming language research will avoid UB by construction. But C is in a unique place where neither of those approaches fully work. > The C standard allows for both kinds of environments by stating that these behaviors are undefined, allowing the implementation to error out or do something sensible, depending on the environment. This is true, but it doesn't mean that "something sensible" is actually something the programmer should rely on! That's just asking too much of UB- programmers need to work with the semantics implemented by their toolchain, not make up an intuitive/"sensible" meaning for their undefined program and then get mad when it doesn't work. For example, if you want to scan through a bunch of memory, tell the language that's what you're doing. Is that memory at a fixed address? Tell the linker about it so it can show up as a normal global object in the program. Is it dynamic? Memory allocators fabricate new objects in the abstract machine all the time, perhaps your compiler supports an attribute that means "this function returns a pointer to a new object." The solution is not just to shrug and say "do something sensible but potentially dangerous." It's to precisely define the operations available to the programmer, and then provide tools to help them avoid misuse. If an operation isn't in the language, we can add it! If it's too easy to mess up, we can implement sanitizers and static analyzers, or provide alternatives! Yelling about a supposed misreading of "undefined behavior" is never going to be anywhere near as effective.
- whatshisface 5y agoI don't see any utility in inventing a new reading of the standard. Getting everyone to agree on a new interpretation of a sentence can't possibly be easier than getting everyone to agree on a more clearly worded sentence. The actual thing you'd have to convince everyone of (the utility of the new consensus) and the people you'd have to convince (compiler writers, documentation authors) are the same in both cases.
- setr 5y agoThe difference is that, once decided and written, no party (both old and new) can’t chime in with a new interpretation.
- JadeNB 5y ago> The difference is that, once decided and written, no party (both old and new) can’t chime in with a new interpretation. That double negative ("no party … can't") was accidental, right?
- david2ndaccount 5y agoCompilers have become more powerful (opening up new ways to exploit undefined behavior) and the primary C compilers are free software with corporate sponsors, not programmer customers (or else perhaps Andrew Pinski would not have been so blithe about ignoring his customer Felix-gcc in the GCC bug report cited above). This is the real problem. We have reached a situation where a small number of compilers dominate the space, but yet do not charge the users and so do not treat them as customers. The C standard is a product of an era where you would pay for your tools and so would demand a refund from any compiler vendor that would treat undefined behavior in an absurd manner.
- mhh__ 5y agoWouldn't the largest customers (by far) still be the companies funding development of the compilers already?
- swiley 5y agoAre you arguing that e.g. Turbo C and friends from the 80s were higher quality than modern C compilers?
- setr 5y agoI believe he’s arguing that they were more sane/pragmatic compilers — they would be inherently less comfortable exploiting UB to do anything other than what people expected or were used to, because there is more real possibility of retribution (GCC could get away with making demons fly out your nose and just lose marketshare [that it doesn’t directly depend on anyways] where a commercial compiler would go out of business)
- msbarnett 5y ago> I believe he’s arguing that they were more sane/pragmatic compilers I can’t imagine they have any actual experience with Borland or Symantec’s C or C++ compilers, then. These things had notoriously shakey standards compliance and their own loopy implementations of certain things, along with legions of bugs – it’s not hard to find older C++ libs with long conditional compilation workarounds for Borland’s brain damage. Microsoft’s C++ compiler was for years out of step with the standard in multiple ways. Part of the reason GCC ate these compilers’ markets for lunch was the much more rigorous and reliable standards adherence, not just the lack of cost. This reads like nostalgia for an age that never was.
- Spivak 5y ago> So, “i << 32” buried in a million lines of database code makes the entire program junk – supposedly as an “optimization”! I think the author has it wrong here because they’re assuming lines and order has any meaning when the program is compiled all together. Imagine a compiler where (I know that this is not actually possible to do) undefined behavior was a compiler error. You would sound crazy for complaining that “a syntax error in like 4325 make the whole program uncompileable.” It’s not like running C programs have any idea that they are hitting undefined behavior. It’s just that the generated assembly is allowed to assume that you know what you’re doing. i << runtime_val Can just generate the shift without worry that it will blow up and the propagate the knowledge up that runtime_val’s domain is [0,31] for the optimizer.
- ho_schi 5y agoThis is interesting. Humans interpret standards and compilers interpret code, too :) We have the benefits of a standard, portability and multiple compilers. But it also comes with duties. BTW. The C++ people seem to tackle some of the issues around widespread 'undefined behavior' and defined a lot of behavior: 1) If a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined. i = ++i + 2; // undefined behavior until C++11 i = i++ + 2; // undefined behavior until C++17 f(i = -2, i = -2); // undefined behavior until C++17 f(++i, ++i); // undefined behavior until C++17, unspecified after C++17 i = ++i + i++; // undefined behavior 2) If a side effect on a scalar object is unsequenced relative to a value computation using the value of the same scalar object, the behavior is undefined. cout << i << i++; // undefined behavior until C++17 a[i] = i++; // undefined behavior until C++17 n = ++i + i; // undefined behavior Source: https://en.cppreference.com/w/cpp/language/eval_order https://en.cppreference.com/w/cpp/language/eval_order They replaced the entires "Sequence Point Rules" by "Sequenced-before rules". Compiler implementers cannot choose what to do (implementation defined) or neglect the issue (undefined behavior) - the must act well defined now in many situations.
- jcranmer 5y agoIt's easy to pick on undefined behavior in C when you focus on the more gratuitous undefined behaviors such as signed overflow or oversized shifts. I'm not certain why these are undefined behavior instead of implementation-defined, but my suspicion is that these caused traps on some processors, and traps are inherently undefined behavior. Instead, if you dislike undefined behavior, I challenge you to come up with workable semantics for non-gratuitous scenarios, of which I'll lay out three. Remember, if it's not undefined, then there are some defined semantics that need to be preserved, even if implementation-defined, so you should be able to explain those semantics. If you can't find such semantics, then maybe undefined behavior isn't such a bad thing after all. The first case is the compiler hint. A function marked _Noreturn returns. Or you alias two pointers marked restrict. Remember that the entire point of these hints is to permit the compiler to not generate code to check for these scenarios. The second case is uninitialized memory. You've probably already come up with such semantics, so I'll point out an optimization scenario that you probably don't object to that your semantics didn't cover: static int y; /* uninitialized */ _Bool cond = f(); int x = cond ? 2 : y; /* Is it legal to fold this to int x = 2; ? */ Hopefully, you'll agree that that is a reasonable optimization. Now consider this code: static int y; /* uninitialized */ _Bool cond1 = f(), cond2 = g(); int x1 = cond1 ? 2 : y; /* So int x1 = 2; */ int x2 = cond2 ? 3 : y; /* So int x2 = 3; */ if (!cond1 && !cond2) { assert(x1 == x2); /* Uh... */ } This is just scraping the tip of the surface of uninitialized values. Developing sane semantics around these sorts of values that also allow reasonable optimizations is challenging. You can look at the very lengthy saga that is undef, poison, and freeze in LLVM (still ongoing!) to see what it looks like in practice. The third category is traps. Let's pick an easy example: what happens if you dereference a null pointer? Now let's consider the consequences of those semantics on some more code examples: int *x = NULL; int *y = &*x; /* Can this be lowered to int *y = x; ? */ size_t offset = (size_t)&((struct foo *)NULL)->field; /* Remember, x->a is actually (*x).a */ int *z = get_pointer(); *z; /* No one uses the result of the load, can I delete it? */ for (int i = 0; i < N; i++) { foo(*z); /* Can I hoist the load out of the loop? */ } Note that all of the optimizations I'm alluding to here are ones that would have existed all the way back in the 1980s when C was being standardized, and these are pretty basic, pedestrian optimizations that you will cover in Compilers 101.
- 5y ago
- mpweiher 5y agohttps://news.ycombinator.com/item?id=17454467 https://news.ycombinator.com/item?id=17454467 made very much the same argument.
- ChrisSD 5y agoI think the real problem stems from the mismatch between modern processors and the processors C was originally designed for. C programmers want their code to be fast. Vanilla C no longer gives them the tools to do that on a modern processor. Either the language needs to be extended or the compiler needs to get more creative in interpreting the existing language. The latter is the least disruptive and it doesn't stop judicious use of the former. So, in short, UB is what gives room for the compiler to be more creative without the programmer having to change their code. It wasn't a reading error, it was an opportunity the compiler devs enthusiastically embraced.
- rectang 5y agoWhen compiler writers have get "creative" with C undefined behavior, programming C no longer produces predictable results. > least disruptive Like starting to optimize away loop checks that can "never happen" because signed integer overflow is UB, suddenly changing the behavior of programs that were fine for years? I wish I could just fence off this insanity by never starting another project in C. Unfortunately, C is ubiquitous in the ecosystem so all of us are stuck cleaning it up.
- msbarnett 5y ago> Like starting to optimize away loop checks that can "never happen" because signed integer overflow is UB, suddenly changing the behavior of programs that were fine for years? Yeah. Not doing that on modern processors is actually quite disruptive. Here: for(i = offset; i < (offset + 16); i++) { arr[i] = i + 32; } What C compilers currently do is, in line with the standard, ignore the case that offset + 16 might overflow. This makes this eligible for loop unrolling, and depending on the specifics of the math inside the loop, the compiler can do a lot to pre-calculate things because it knows this is happening 16 times exactly. If, instead, we force compilers to think about the fact that offset + 16 could have some implementation-defined meaning like wrapping, then all bets are off & we have to throw a bunch of optimization opportunities out the window. Lots and lots of hot & tight loops which are currently able to be compiled into something suitable for the preferences of modern CPUs instead has to be to be naively compiled because of the need to hold back due to the possibility of something that largely wasn’t happening, happening. Most people write most loops this way, never expecting or intending to overflow anything. Most loops are benefitting from this optimization. A lot of code would get slower, and programmers would have to do a lot more fragile hand-unrolling of operations to get that performance back. And they’d need to update that more often, as whatever the optimal “stride” of unrolling changes with the evolution of CPU pipelines. It’s slower code and more work more often for more people, to satisfy a minority use-case that should really just have its own separate “please wrap this” construct.
- superjan 5y agoI think the best point in this article is that C, a language invented to write OS code, is currently difficult to use for that purpose due to the current handling of undefined behavior. If you write low level OS code, you are keenly aware of the behavior of the machine you target. There is no point trying to define a language like C in such a way that the only valid programs are those that would run identically on any hypothetically allowed hardware. For instance, on x64, there are no alignment restrictions, why should I be punished for making use of that fact? And what about all this C code with SSE intrinsics? should we ban that too?
- _kst_ 5y agoThe author suggests that the text following the definition of "undefined behavior", listing the permitted or possible range of undefined behavior, should be read to restrict the consequences. But the first possibility listed is "ignoring the situation completely with unpredictable results". Surely that covers any possible consequences. The author also says: > Returning a pointer to indeterminate value data, surely a “use”, is not undefined behavior because the standard mandates that malloc will do that. Returning a pointer to data is not a use of that data. The fact that its value is indeterminate isn't relevant until you attempt to read it (without first writing it). It may be worthwhile to reduce the number of constructs whose behavior is undefined, making them implementation-defined or unspecified instead. For example, if signed integer overflow yielded an unspecified result rather than causing undefined behavior, I wonder if any implementations would be adversely affected. (But it would remove the possibility of aborting a program that computes INT_MAX+1.) I don't think reinterpreting "undefined behavior" as anything other than "the Standard imposes no requirements" is practical. If a program writes through a dangling pointer and, for example, clobbers a function's return address, what constraints could be imposed on what the program might do next?
- anarazel 5y ago> For example, if signed integer overflow yielded an unspecified result rather than causing undefined behavior, I wonder if any implementations would be adversely affected. I suspect so - makes it harder to reason about loop counts because the compiler can't necessarily guarantee that an incremented loop counter won't become negative and thus the loop needs to iterate more. E.g. something like for (int i=param; i < param + 16; i++) has a guaranteed loop count with the current rules, but not with yours? That's not an excuse for but having any way to do proper overflowing operations on signed integers though.
- Asooka 5y ago> I suspect so - makes it harder to reason about loop counts because the compiler can't necessarily guarantee that an incremented loop counter won't become negative and thus the loop needs to iterate more. This is a favourite example that gets thrown around, but for all practical loops GCC and clang seem to have no problem even when you compile with -fwrapv
- jart 5y ago> license for the kinds of dramatic and unintuitive transformations we’ve seen from the compilers, and any indication that undefined behavior should be a vehicle for permitting optimizations. Does anyone have an example of a time where Clang or GCC actually did something bad upon witnessing undefined behavior, rather than simply doing nothing, as the standard proposes? I ask because every time I've seen people get mad about UB it's always because they envisioned that the compiler would do something, but instead the compiler did nothing.
- SloopJon 5y agoThe types of surprises that I've seen have to do with inferences that the compiler draws: this program would be undefined if foo was negative, therefore foo is not negative, therefore I can optimize away this condition.
- anarazel 5y agoWhat do you define as "bad"? Arguably several. I've seen cases of overflow checks that were implemented assuming signed overflow (which all relevant platforms implement!) getting optimized away. Correct, given "signed overflow is UB" and thus can be assumed to not happen. Problematic given for widespread such checks are, and given that there's no easy portable alternative. Entire checks getting optimized away because of a presumed strict aliasing violation. IIRC between structure with a compatible layout. Pretty code, no. UB yes. Reasonable, IDK.
- bhk 5y agoI've seen a real-world example something like this: int a[32] = {...}; int flag = 1 << index; if (index < ARRAYSIZE) { a[index] = x; return flag; } else { return 0; } The "1 << index" operation is undefined behavior (!) when index is greater than 32 (on a platform with 32-bit integers), even if the result is never used! The compiler inferred that index must always be less than 32, which allowed it to optimize out the array bounds check, which turns the code into a write-anywhere gadget. Note that if the standard had not declared "n << 32" to be all-bets-are-off UB, but instead had said something like, "it results in some implementation-specific value, or maybe traps" -- as a rational person would presume -- then this would not turn into a security problem.
- tom_mellior 5y agoI'm not convinced. The argument seems to hinge to a very large extent on the sentence: > Permissible undefined behavior ranges from A, to B, to C. The observation that "Permissible" has a specific meaning is important and interesting. But what about "ranges from ... to ..."? The author reads this as "Permissible undefined behavior is either A or B or C.", but that seems like a stretch to me. (Unless ISO defines "ranges" to mean just this.) Also, in the above, A = "ignoring the situation completely with unpredictable results". Both "ignoring" and "unpredictable" do a lot of heavy lifting here. In the signed integer overflow case discussed in the linked GCC bug report (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475), one could very well argue that "ignoring" a case where signed arithmetic overflows is exactly GCC is doing. It "ignores" the possibility that a + 100 might overflow, hence obviously a + 100 > a is true, leading to results that the reporter considers "unpredictable". Somehow the author seems to think that this is not what the wording intended, but they also fail to explain what they think should happen here. Sounds to me like the wording has always been very vague. Also: > Returning a pointer to indeterminate value data, surely a “use”, is not undefined behavior because the standard mandates that malloc will do that. Yes, malloc will do that, but using a pointer to indeterminate data is not the same as using the indeterminate data itself. The author is doing themselves a disservice by misreading this.
- hedora 5y agoThe original intent was for signed overflow to be architecture-specific (not everything was 2s complement back then). On x86, the correct behavior was previously (and obviously) “perform a 2s complement wraparound”. To “ignore” the situation used to mean “delegate to a lower level”. It now means “silently generate code that violates the semantics of the target microarchitecture” As the article argues, I think this has gone too far. For example, people are starting to claim that asm blocks are undefined behavior. They’re clearly implementation specific (so undefined by the spec), but also well defined by each implementation. In current readings of the spec, compilers are free to ignore them completely. Doing so would break all known operating systems, and many user space programs, so they have not managed to do so yet. Edit: for signed overflow, other architecture-specific behavior (such as optionally trapping floating point exceptions) would also have been permissible, assuming the architecture supported it.
- kmm01 5y agoI'm afraid the author themselves is misreading the definition of undefined behavior. Undefined behavior is not "behavior upon use of a nonportable or erroneous program construct". That rephrasing completely changed the meaning. Undefined behavior is, as the C standard states, "behavior [, ...,] for which the Standard imposes no requirements". The whole ", upon use of...," part is just exemplifying situations in which undefined behavior can occur. The standard will sometimes say that a certain construct results in undefined behavior but more importantly any construct for which the standard does not specify a certain behavior has (what else?) undefined behavior.
- warkdarrior 5y agoTrue. I think the point of the author is that the C standards group is not doing their job by leaving so much room for compilers to interpret undefined behavior.
- mumblemumble 5y ago> C can accommodate significant optimization while regaining semantic coherence – if only the standard and the compilers stop a lazy reliance on a mistaken reading of “impose no requirements”. That wouldn't be enough, because, sadly for us, rewriting history is only an option in Git repositories and speculative fiction. On this timeline, it doesn't much matter how the standard should have been interpreted (I'll refrain from opining on that), what matters is how the standard was interpreted, and its influence on the behavior of major compilers. Given the current situation, it seems to me that reaping the benefits of such an enterprise would require getting everyone on board, a mode that's based on the new interpretation, leaving it turned off by default, and then basically never actually using it for fear of breaking compatibility with toolchains that have to use an older version of the compiler for a good decade or two while we wait on them to age out of existence. I'm not sure the world actually has much practical use for an unapproachable ivory tower dialect of C. Personally, I'd much rather have a go at a language like Zig that's taken upon itself to dream even bigger.
- curtisf 5y agoI think the reasoning here is flowing backwards. The writer wants to believe that C is a well-designed language suitable for writing large programs (because programmers understandably use it that way; there's not really an alternative to C), and so people reading the spec and finding a minefield _must_ be reading the spec wrong. So many important programs are written in C, and so many of them, with a very strict reading of the C standard, can hit cases where their behavior is "undefined". This _is_ scary, if the C-language-lawyers are right! The C language was originally largely descriptive, rather than prescriptive. Early "C" compilers disagreed on what to do in strange cases (e.g., one might wrap integer overflow, one might saturate, one might have wider ints). Even when using the less-chaotic "implementation defined behavior", behavior can still diverge wildly: `x == x + 1` is definitely `false` under some of those interpretations and maybe `true` in some of those interpretations. However, the C spec clearly says that the compiler may "ignore the situation" that "the result is ... not in the range of representable values for its type"; it is "permissible" that `x == x + 1` is replaced with `false` despite the "actual" possibility that adding 1 to x produces the same value, if `+` was compiled to be a saturating add. This has significant practical consequences, even without the "poisoning" result commonly understood of undefined behavior. Since the value is known statically to be `false`, that might be inlined into a call into a function. That function may _dynamically_ re-check `x == x + 1` and find that it is `true`; obviously that function doesn't have a `if (true && false) {` case, so it results in the function misbehaving arbitrarily (maybe it causes a buffer overrun to the argument of a syscall!). 'Intuition' does not make a programming-language semantics. You need to write down all the rules. If you want to have a language without undefined behavior, you need to write down the rules for what must happen, keeping in mind that many examples of undefined behavior, like dereferencing out-of-bounds pointers, _cannot_ be detected dynamically in C without massive performance costs. To detect if a pointer is out-of-bounds, you need to always pair it with information about its provenance; you need to track whether or not the object has been freed, or the stack-frame it came from has expired. Is replacing all pointers with fat-pointers indicating their provenance and doing multiple comparisons before every dereference the "right" way to compile C?
- rodgerd 5y ago> The writer wants to believe that C is a well-designed language suitable for writing large programs [...], and so people reading the spec and finding a minefield _must_ be reading the spec wrong. I think this is the core of the problem. There are languages that provide much more detailed, predictable behaviors, and the author wants to avoid moving to one.
- muth02446 5y agoAlso relevant: What every compiler writer should know aboutprogrammers or “Optimization” based on undefined behaviour hurts performance by M. Anton Ertl https://www.complang.tuwien.ac.at/kps2015/proceedings/KPS_2015_submission_29.pdf https://www.complang.tuwien.ac.at/kps2015/proceedings/KPS_20...
- ghoward 5y agoThank you for linking this paper! I was trying to find it and link it myself. I have found that it is true, actually. I had to write [1] to get portable sane behavior with signed arithmetic, and it obviously will slow my code down. Exploiting UB hurts conscientious programmers who actually try to avoid UB. [1]: https://git.yzena.com/Yzena/Yc/src/branch/master/include/yc/arith.h https://git.yzena.com/Yzena/Yc/src/branch/master/include/yc/...
- mjburgess 5y agoCommenters here seem to be missing the core thesis of this article. It's not about what the standard literally means; it's about it's spirit -- and the reason for its spirit. The issue is "undefined behaviour" should never have been interpreted this extremely. The standard may be silent on how extreme, but it is implausible to suggest that the standard was actually written to enable this. Compiler writers for C! dismiss severe issues which occur in compiling any program with undefined behaviour; issues which would render any modern language a bad joke. Compiler writers are using this as a perverse shield to simply fail to optimize for correctness, or provide means to; and to enable them to optimize only for performance. Are we really saying the standard supports this sleight-of-hand? It seems more like using the Second Amendment to murder someone.
- steveklabnik 5y agoPersonally, I think that arguing that those who define and implement a standard don’t understand one of the most fundamental aspects of said standard is going to be an uphill battle. You could argue that they’ve lost their way, and the article flirts with this, but the path forward is the hard part, and IMHO rings a bit hollow: it’s asserted that these rules aren’t needed for performance, but no evidence is given, and what similar evidence we do have (compiling on lower optimization levels) doesn’t seem to support this thesis. You could argue that, the kernel, which turns off aliasing, is plenty performant without it, and that’s a decent argument, but it’s not clear that it wouldn’t be even faster with it, and it’s much harder to empirically test this than removing the flag, since it will miscompile.
- bonzini 5y agoDifferent code depends on different optimizations. A loop on an int** might benefit a lot from aliasing optimizations, because the compiler will assume that a[i] will remain the same after writing to a[i][j]. Other code may not benefit at all. Likewise that loop may not benefit from signed overflow; instead an initialization loop that, by way of multiple level of macros, ends up doing a[i]=b[i]*1000/100, might become twice as fast if signed overflow rules let the compiler rewrite the assignment as a[i]=b[i]*10.
- 5y ago
- dfabulich 5y agoI think the problem is cultural in the C community. C programmers have Stockholm Syndrome around UB optimizations. As TFA notes, "There is No Reliable Way to Determine if a Large Codebase Contains Undefined Behavior" https://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html https://blog.llvm.org/2011/05/what-every-c-programmer-should... That's because UB is a bug that occurs as your program runs (e.g. dereferencing a null pointer). You'd have to prove that your program is free of bugs to prove that you won't encounter UB at runtime, which is not possible in general. But C programmers believe they deserve to be abused this way. "Sure, the C compiler is allowed to nuke your entire program whenever there's a bug at runtime, but all you have to do is prove that your program is bug free! Why, any real C programmer can write bug-free code, so if you have any UB bugs, you're probably not good enough to write C. Sure, I've been bruised by UB bugs in the past, we all have, but when I made those mistakes, I deserved to be bruised, and those bruises made me a better programmer."
- anarazel 5y agoThe situation has gotten at least somewhat better since then. Ubsan and friends are not a guarantee, but make it much more realistic to find UB issues.
- optymizer 5y agoAnecdotally, I find about as many bugs in my Java code at work, as I do in my hobby C code, including "dereferencing null pointers" aka NullPointerExceptions.
- anarazel 5y agoNPE isn't really a relevant comparison point - it raises an exception of sorts in both languages. Accessing an array past its end, accessing freed memory, reading uninitialized memory seem more apt.
- optymizer 5y agoThese are all issues a compiler could insert checks for, saving you the time to do it manually, while trading performance for security or correctness. C doesn't trade performance away, so if you'd like to pay the price of these checks, it's on you, the programmer to add them in. C programmers have to put in extra effort to make the executables safer and output correct results. Other languages trade off performance for safety and/or correctness. The programmers using them have to put in extra effort to make the executables run as fast as they do in C. Ultimately, programmers tend to make rational choices, and the stockholm syndrome mentioned above is really just C programmers dealing with the consequences of the trade-off they made by using C.
- klodolph 5y agoI think one of the best attempts to solve this is the attempt to classify undefined behavior into "bounded" and "critical" UB, which is a distinction that hasn't gained as much traction as I'd like. Something like 1<<32 is "bounded" UB, and can't result in anything happening to your program... it is permitted to result indeterminate values, it is permitted to trap, but that's basically it. Critical UB is stuff like writing to a const object, calling a function pointer after casting it to an incompatible type, dereferencing an invalid pointer, etc. Basically, anything goes. This is part of the "analyzability" optional extension which I wish would gain more traction.
- MauranKilom 5y ago> Something like 1<<32 is "bounded" UB, and can't result in anything happening to your program... it is permitted to result indeterminate values, it is permitted to trap, but that's basically it. That is what implementation-defined behavior is for. Feel free to advocate for changing the standard accordingly.
- klodolph 5y ago> Feel free to advocate for changing the standard accordingly. I am, in fact, describing the existing C standard, not advocating for changes. Please refer to Annex L "Analyzability", which defines the terms I used: "bounded undefined behavior" and "critical undefined behavior". Note that this section is conditional... it is not widely adopted. I am advocating for increased adoption of this part of the standard, or barring that, revisions that would make adoption more palatable. And in general, "If you don't like it, advocate changing the standard" is not a useful or insightful response. It is completely reasonable and normal to complain about something without trying to fix it.
- MauranKilom 5y agoYou're right, apologies.
- anarazel 5y agoFirst: I do dislike how hard it is to avoid some UB / how impractical some of the rules are. But I also think a lot of discussions of this topic caricaturize compiler writers to a ridiculous degree. Almost describing them to write optimization passes looking for UB so they can over-optimize something, while cackling loudly in glee about all the programs they can break. The set of people doing so overlaps with the set of people complaining that the compiler doesn't optimize their code sufficiently to a significant degree. Lots of compiler optimizations need to know the ranges of values, hence logic to infer ranges. One of the sources for that is "can't happen" style logic - which nearly all of the time are things the code author would agree with if they thought long and hard. Not just about the code as written, but also good the code looks like after inlining (across TUs with LTO).
- rectang 5y ago> But I also think a lot of discussions of this topic caricaturize compiler writers to a ridiculous degree. The inflamed backlash should tell you just how damaging it is to impose silent failure on meticulously written, previously fine programs.
- jcranmer 5y ago> meticulously written, previously fine programs With relatively few exceptions, if your program hits undefined behavior, then your program was already doing something pretty wrong to begin with. Signed overflow is a poignant example: in how many contexts is INT_MAX + 1 overflowing to INT_MIN actually sane semantics? Unless you're immediately attempting to check the result to see if it overflowed (which is extremely rare in the code I see), this overflow is almost certain to be unexpected, and a program which would have permitted this was not "previously fine" nor "meticulously written." I feel compelled right now to point out that software development is a field where it is routine to tell users that it's their fault for expecting our products to work (that's what the big all-caps block of every software license and EULA says, translated into simple English).
- anarazel 5y ago
- zokier 5y agoReading error or not, the ship has sailed long ago. As the article notes, C99 formalized the current UB interpretation. What C89 or K&R said is more just historical curiosity than of any real relevance today. I guess you could construct an argument that gcc should disable some optimizations when invoked with -std=c89, but I doubt anyone really cares at this point enough to justify the maintenance burden. C is a minefield today, and that is the reality we must live with. You can't turn back time 25 years and change what has happened.
- anarazel 5y ago> C is a minefield today, and that is the reality we must live with. You can't turn back time 25 years and change what has happened. I think it's doable to make some of the UB issues considerably less painful. E.g. define facilities to check for overflow safely, add facilities for explicit wrapping operations.
- DangitBobby 5y agoIt truly boggles my mind that this discussion (linked in TFA) played out the way it did. In my mind it's 100% not okay to (by default) optimize away a check for integer overflow. I've never really written in C (or any unsafe language) before so I had little context for the types of traps C sets for developers. Based on the responses of the person who presumably implemented the optimization, it comes as no surprise that C is so dangerous. After reading this I hope I never have to use gcc. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475
- tom_mellior 5y ago> In my mind it's 100% not okay to (by default) optimize away a check for integer overflow. But a + 100 > a is not a check for overflow. If a >= INT_MAX - 100, it is an overflow. The "will this operation overflow" check would be a >= INT_MAX - 100, and GCC would not optimize that away.
- DangitBobby 5y agoWhat you've written doesn't demonstrate the issue outlined in the big report. The issue is that real-world code defends against certain hostile inputs like so, and that the optimization breaks those defenses: int a,b,c; a=INT_MAX; /\* statement ONE */ b=a+2; /* statement TWO */ c=(b>a); /* statement THREE \*/ Whether or not you think this is technically permissible by a sufficiently self-serving (from the perspective of a person whose ONLY goal is speed optimization) reading of spec is irrelevant. Any reading of the spec should err on the side of protection against real-world consequences. I don't want lawyering and technically correct-ing, I want pragmatism.
- tom_mellior 5y agoIf you want pragmatism, check whether something is legal before you do it.
- DangitBobby 5y ago
- olliej 5y agoI dislike how UB is used where unspecified would work. For instance overflowing arithmetic is well specified on every architecture - the behaviour may be different on say sparc vs an hc12 vs x86, but on any of those architectures it will always be the same. Yet compiler devs have instead decided that it is undefined and so can be treated however they like. There are so many of these UB that could be unspecified that it’s absurd - UB should be reserved for when there outcome is not consistent - UaF, invalid pointers, out of range accesses, etc
- MauranKilom 5y agoThe C++ committee recently (for the C++20 standard) voted against making signed overflow defined, opting to keep it undefined. Primarily for performance reasons (and because it would usually be a bug anyway). www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0907r4.html#r0r1 You're of course free to disagree with the reasoning. But you'd probably agree that a new standard revision that forces the average application to become, say, 5% slower, just so that broken programs would be just as broken, would not be very well-received.
- olliej 5y agoI’m not willing to accept a “say” for performance here. I’d also want performance impact on a non-benchmark perf sensitive code base (browser, database, etc rather than spec). The problem with the claim that the code is incorrect is that it is only* incorrect due to the imo bogus interpretation of what should be UB. If I write If (a+b<a) On any modern architecture that has a well defined and understood meaning. But compilers have repeatedly overridden I well understood behavior in the name of perf benefits that only turn up in Spec-style benchmarks
- 1MachineElf 5y agoThis was posted by a user with a name closely matching the domain (perhaps the original author?) 16 hours prior, and flagged: https://news.ycombinator.com/item?id=27215697 https://news.ycombinator.com/item?id=27215697 When I stumbled across it last night, I couldn't understand why that would be. The content seemed good enough for readers on here, and this one's placement on the 2nd page of HN seems to confirm that. What's going on?
- vyodaiken 5y agoGood question. I'd love to know. Is there any way to find out?
- layer8 5y agoBecause there seems to be some confusion in this thread: - "Implementation-defined behavior" means that the C standard specifies the allowable behaviors that a C implementation must choose from, and the implementation must document its particular choice. - "Unspecified behavior" means that the C standard places no particular restrictions on the behavior, but a C implementation must pick a behavior and document its choice. - "Undefined behavior" means that C implementations are allowed to assume that the respective runtime condition does not ever occur, and for example can generate optimized code based on that assumption. In particular, it is free to not decide any behavior for the condition (let alone document it). As a consequence, if the runtime condition actually does occur, this can affect the behavior of any part of the program, even the behavior of code executed before the condition would occur. This is because from a false assumption the truth of any statement can be logically derived (principle of explosion [0]). And that is why the C standard does not restrict the behavior of the whole program if it contains undefined behavior. [0] https://en.m.wikipedia.org/wiki/Principle_of_explosion https://en.m.wikipedia.org/wiki/Principle_of_explosion
- cygx 5y agoTo my knowledge, in case of unspecified behaviour, it's not required to pick and document a particular choice. Behaviour may even vary on a case by case basis where it makes sense (eg order of evaluation of function arguments, whether an inline or external definition of an inline function gets used, etc). The need to properly document the choice is the defining characteristic of implementation-defined behaviour.
- tsimionescu 5y agoI would note that the article is explicitly contesting the definition of UB that you are giving here (though you are absolutely right that this is the de facto definition used by all major compilers, and the commtitee). Basically the article is arguing that UB should be similar to Unspecified behavior - behavior that the implementation leaves up to the hardware and/or OS. I'm not sure where I fall to this issue, though I would note that the definition of UB in the standard needs quite a bit of interpretation to arrive at the commonly used definition you are quoting. That is, while I think the definition you give is compatible with the one in the standard, I don't think it is the definition of the standard, which is much softer about how UB should impact the semantics of a C program. In particular, nothing in the wording of the standard expliclty says that an implementation is expected to assume UB doesn't happen, or that a standard-conforming program can't have UB.
- wallstprog 5y agoTrue story -- someone (not me) decided to initialize a C++ virtual class by defining a default ctor which does a "memset(this, 0, sizeof(*this))", then uses placement new to re-create the vptr. Newer compilers complained more and more until gcc 8 which just silently ignored this awful hack -- no diagnostic, no error, just crickets. Strictly speaking silently ignoring this atrocity is allowed by the standard, but it sure took a while to figure out. So be careful with code that "just works" even if it shouldn't.
- saagarjha 5y agoPerhaps you should file a bug to make them bring back the warning.
- rocqua 5y agoQuoting from the same passage in the standard as the article does: > Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to ... Ignoring the situation completely with unpredictable results seems like it covers the current compiler behavior. The author does not like how current compilers work. But his argument against it mixes "it would be better if it worked differently" with "A specific pedantic reading of the standard says they are wrong". The second kind of argument seems to undercut his wider point. For his wider point is "Compilers should be reasonable rather than turning on pedantry". At least, that is what I think his point is, and it seems like the much stronger argument to me. Trying to "trip up" the proponents of current behavior by pointing to a possible miss-reading of a comma is not going to do much. Arguing instead that their practice is harmful seems like a much more likely to work approach. That said, such an argument should probably be civil. The article links to this [1] discussion. The link is supposed to show the peculiar arguments used by proponents of current behavior. What I read there is someone lashing out, calling names, and grand-standing. Convincing compilers to be more reasonable is probably going to require a very different tone. Not one of "how dare you be so stupid" but one of "perhaps you could consider this side-effect of current behavior" and "have you considered this approach". [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=30475
- tsimionescu 5y ago> The article links to this [1] discussion. The link is supposed to show the peculiar arguments used by proponents of current behavior. What I read there is someone lashing out, calling names, and grand-standing. Even worse, the person who raised that issue is mostly just wrong: the code they wrote had not worked with any optimizations turned on for about 13 years at the time they raised that bug. Teh fact that it worked in debug builds seems irrelevant, so the whole bug is complaining about a change that had no impact on the 99% of released code which is compiled with -O2 or -O3.
- cryptonector 5y ago> ignoring the situation completely with unpredictable results That needn't mean "delete the code". TFA is certainly right that this situation is killing C. But then, so is C's old type model. Rust is clearly better, so it's just as well. That said, I think the C99 UB stance is a disaster for any language that might adopt it. Perhaps the standard should not define `1<<32`, or `(char )0`, or signed integer overflow. But it's easy enough for a compiler to implement shift overflow as 0 (or maybe -1 if the shifted number is signed and negative), it's easy enough to perform signed arithmetic and let the emitted instructions do what they will, and it's easy enough to allow NULL dereferences (maybe the app mapped a page at address 0 for some reason, or maybe you want to catch a segfault). > But his argument against it mixes "it would be better if it worked differently" with "A specific pedantic reading of the standard says they are wrong". I mean, if it works to shame compiler teams into serving their communities better, I'm all for it. > The second kind of argument seems to undercut his wider point. No, it's a plausible argument. It's like arguing about what the Founding Fathers meant by some specific clause in the Constitution. > pointing to a possible miss-reading of a comma is not going to do much That happens all the time in statutory and constitutional interpretation by courts. It's the same here. We're arguing the spec. > Convincing compilers to be more reasonable is probably going to require a very different tone. No, it's just not possible at all, tone or no tone. The right answers were always: 1. write your own compiler that does it right (ETOOHARD now that clang didn't) 2. start over with a new language (Hello Rust) Unsurprisingly we've ended up with (2).
- jacinabox 5y agoSort of a second amendment for footguns.
- aw1621107 5y agoA related post with a similar claim: "How One Word Broke C" (https://news.quelsolaar.com/2020/03/16/how-one-word-broke-c/ https://news.quelsolaar.com/2020/03/16/how-one-word-broke-c/). HN comments at https://news.ycombinator.com/item?id=22589657 https://news.ycombinator.com/item?id=22589657
- joelkevinjones 5y agoTwo points: The notion that compilers that encounter undefined behavior are allowed to generate any code they want is a new interpretation, for some value of "new". I can't remember the first time I encountered such an interpretation being used by compiler writers to justify something they wanted to do until sometime after 2000. The notion that John Regehr has (quoted in the article) that undefined behavior implies the whole execution is meaningless is not supported by the language of either the C89 or C99 standard, at least by my reading. The C89 standard has a notion of sequence points. Wouldn’t all sequence points executed before undefined behavior is encountered be required to occur as if the undefined behavior wasn’t there? It would seem so: From the C89 standard: 2.1.2.3 Program execution The semantic descriptions in this Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant. Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place. The C99 standard has nearly identical language: 5.1.2.3 Program execution 1 The semantic descriptions in this International Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant. 2 Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects,11) which are changes in the state of the execution environment. Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.
- AlotOfReading 5y agoNo, sequence points aren't relevant because they occur at runtime. "Time traveling UB" happens in the compiler, typically in optimization passes and can cause otherwise valid code to exhibit completely different behavior than it would if the UB didn't exist.
- zlynx 5y ago> Wouldn’t all sequence points executed before undefined behavior is encountered be required to occur as if the undefined behavior wasn’t there? It would seem so: No. Code optimization is a series of logic proofs. It is like playing Minesweeper. If a revealed square has 1 neighboring mine and a count of 1, then you know that all 7 other squares are safe. In other Minesweeper situations you make a proof that is much more complex and allows you to clear squares many steps away from a revealed mine. If you make a false assumption of where a mine is, via a faulty proof, then you explode. The compiler is exactly like that. "If there is only one possible code path through this function, then I can assume the range of inputs to this function, then I can assume which function generated those inputs..." You can see how the compiler's optimization proof goes "back in time" proving further facts about the program's valid behavior. If the only valid array indexes are 0 and 1 then the only valid values used to compute those indexes are those values that produce 0 and 1. This isn't even program execution. In many cases the code is collapsed into precomputed results which is why code benchmarking is complicated and not for beginners. Many naive benchmark programs collapse 500 lines of code and loops into "xor eax,eax; ret;" A series of putchar, printf and puts calls can be reduced to a single fwrite and a malloc/free pair can be replaced with an implicit stack alloca because all Standard Library functions are known and defined and there is no need to actually call them as written.
- gameswithgo 5y agoLots of us use languages that have no/almost no undefined behavior, it is really weird to see people trying to come up with reasons why this is hard. Perhaps there are still computing environments so slow that undefined behavior still makes sense in C, and C should remain that way. If so, use something with less of this for anything else.
- Animats 5y agoMost of this discussion revolves around integer overflow. Part of the problem is that most of the computer hardware is now twos-complement arithmetic. Programmers think of that as part of the language. It's not, for C. C has run, in the past, on - 36 bit ones complement machines (DEC and UNIVAC) - Machines with 7-bit "char" (DEC) - Machines with 9-bit "char" (UNIVAC, DEC) - Machines where integer overflow yields a promotion to float (Burroughs) - Many GPUs provide saturating arithmetic, where INT_MAX + 1 == INT_MAX. Go, Java and Rust have explicit byte-oriented models with defined overflow semantics, but C does not. C has undefined overflow semantics. Many years ago, around the time Ada was being defined, I wrote a note titled "Type Integer Considered Harmful". I was pushing the idea that integer variables should all have explicit range bounds, not type names. As in Ada, overflow would be checked. Intermediate variables in expressions would have bounds such that the intermediate value could not overflow without the final result overflowing and being caught. Intermediate values often have to be bigger than the operands for this to work. This never caught on, partly because long arithmetic hardware was rare back then, but it illustrates the problem. Numeric typing in programming languages addresses a numeric problem with a linguistic solution. Hence, trouble. Bounds are the right answer numerically, but force people to think too hard about the limits of their values.
- PeterisP 5y agoExplicit bounds everywhere mean that those bounds need to be automatically checked every time instead of only where explicitly specified by the programmer. This leads to safer code at a nontrivial performance tradeoff, one which would not be acceptable for C.
- Too 5y agoYou could imagine bounds being baked into the types and checked at compile time. int{0..10} foo = 4 int{0..10} bar = 5 int{0..10} buzz = foo+bar // ERR: 10+10 potentially > 10 int{0..10} boom = wrapping_add(foo, bar) // OK int{0..100} sum = foo+bar // OK There are tools which can do this, for example Code Contracts in C#. It becomes rather tedious and verbose so is something usually only left for very safety critical code.
- GuB-42 5y agoThe C standard is not the Bible, it is not written by an almighty god. As respectable K&R are, they are humans who wrote a standard for their own needs, based on the state of the art of that time. Sadly C is the work of mortals... Reading the standard trying to understand the way of god like religious scholars do is a pointless exercise. Modern compiler developers found that exploiting undefined behavior the way we do new leads to interesting optimization, others found it reasonable so now it is the standard. I think the issue most people have now is that compilers use advanced solvers that are able to infer a lot from undefined behavior and other things, so UB is no longer just "it works or it crashes".
- alerighi 5y agoTo me the real problem is that compilers these days do too much. Nobody asked for these optimizations, if you want optimized code, use C++, or Rust, or whatever other "modern" language that has higher level constructs and thus can optimize the code better. The reason to use C is to have an "high level assembler", and there is no reason to use C if I can't rely on the output of the compiler, and if the compiler eliminates some code that I write. We all know that happens when a signed integer overflows, we all know that most architectures are little endian, etc. Even if something is "undefined behaviour" for the standard, it usually has some well defined behaviour on the platform that you are targeting. Unfortunately, for these reasons gcc is dangerous to use, it's not a problem of desktop application (if it crashes, who cares), but I talk about critical systems. It would be unacceptable if a machine ends up killing someone because at gcc they though that they could remove a check that they thought it was useless. And so you have to spend thousand of $ just to get reliable compilers that doesn't do silly optimizations. I think that they should make a sort of "gcc lite" that does exactly what (at least to me) a C compiler has to do: translate the code from C to assembly, without changing the semantic of the code. If there is an undefined behaviour that behaviour will be translated to the assembly code and you will let the processor to deal with it. Also, we talk about optimizations that could be made by exploiting undefined behaviour, fine, but also the programmer usually optimizes exploiting the same undefined behaviours by hand.
- djoldman 5y agoSpecifying that anything can be done in the presence of UB is a poor specification. The word "specify" is pretty much the opposite of "anything." Perhaps compilers should delete all scopes with UB: much more UB would be purged from code as a result (programmers would be forced to enable compiler errors on UB).
- PeterisP 5y agoCompilers generally do delete all scopes where they can detect that UB is certain (assuming that they can't happen and thus those sections are unreachable code) - but they can't detect and delete all scopes where UB is possible given certain input data, that would contradict the halting theorem and all that.
- legulere 5y agoLet‘s just agree that C is not a good programming language according to modern standards: basic integer types are a complete mess with varying sizes and confusing implicit casts. The advantages of null terminated strings do not matter anymore, but the disadvantages matter more and more. The compilation model with headers is a mess compared to module systems. The C Preprocessor is an abomination. The complexity of the language is surprising.
- markslicker 5y agoThe main problem with undefined behavior is that the compiler does not produce any warning to that effect (in my case gcc). I've been caught by bugs that were truly puzzling until I understood undefined behavior, and the licensee it gives to the makers of compilers. I think undefined behavior is giving C a reputation worse than it deserves.
- ezekiel68 5y agoI find this article to be so refreshing. The whole "theater of security" circus around the over-blown interpretations of this term is nothing less than breathtaking in some of its applications. Though not about c, the saga of how the author of the actix web server (written in rust) was literally driven from his own project by virtual pitchfork-wielding issue reporters and redditors was a true disgrace. He dared to explore the optimization space in ways they did not approve of. Happy ending: he recovered his composure and created a new, competing project, named ntex, on his own terms.
- zzo38computer 5y agoThere is such flags as -fwrapv which I use in programs that I think may need it. Maybe, there should be another flag which defines or partially defines many more things. For example, shift amounts out of range might produce any numerical answer (without other side effects), or it might trap once that operation is reached and not give any answer at all, but not demons in your nose or anything like that; either way, the compiler might or might not display a warning message. This is similar to what is mentioned in the linked document. Another alternative flag might be used to guarantee terminating with an error message in such a case, making the program execute more slowly (because the compiler will insert such a check), for testing purposes perhaps. The document linked mentions such a case, but I think that it should not be the default case. If you have: int x[3]; int*y=x+1; int*z=x-1; Then, what I think is how it should work, should be: - The program itself is valid. - The pointers y and z are unequal; if you write y!=z then it is guaranteed to be true. - Reading or writing through z should be considered as undefined behaviour and not allowed. The compiler can optimize based on the assumption that it doesn't happen, unless that optimization is disabled, or if the program is changed to specify it as volatile (in which case it remains undefined behaviour, but the compiler can no longer assume this when optimizing). - Since z is out of range, comparing if it is less or greater than y is not predictable. - Since z is out of range, there is no guarantee that it is unequal to any other pointer that isn't based on x, including null; it might or might not be null. - If you write z+=2 then z is now in range and z==y is true and you can read/write through z now. If you write: static int f(void) { int a; return a; } int main(int argc,char**argv) { int x=42; x=f(); printf("%d\n",x); printf("%d\n",x); printf("%d\n",x); return 0; } Then it should be: The return value of f() will be some value for each call (not necessarily the same every time), and can optimize it. Since the function does nothing, and the return value is not defined, it is allowed to optimize out the reassignment to x and always print 42, or it can set x to some other value, which is unpredictable and not necessarily the same every time, but not print three different numbers, trap, or do other stuff. If standard library optimizations are enabled (which they might be by default, depending on the compiler), it may optimize out the entire program and replace it with a constant string and printing it, as long as it is a number and line feed, repeated three times with the same number, which must be in range of the int type. It may also compile the program like it is and when the program is executed multiple times, print different numbers different times, but always three same numbers. Printing nothing is not allowed, since the %d format specifier can never result in nothing; it always results in at least one digit.