5 ms·
You don't really understand the UB problem if you model this in your head as some sort of simple set of rules that everybody really should have known all along,
by jerf 11y ago
You don't really understand the UB problem if you model this in your head as some sort of simple set of rules that everybody really should have known all along, and it's not a big deal to fix the UB in your code. You have to make sure you're modeling it as A: a set of rules so arcane and byzantine that virtually nobody understands them, and to a first approximation, no non-trivial well-defined C program has ever been written and B: that this was a de facto change in C's nature... in a very real way, the language changed out from underneath people, without them expecting it.
You can't just shrug this away with "Well, if you want performance...", because people in fact don't want abstract "performance"... they want the language they were truly writing in to perform well, not for what is de facto a different dialect of the language to suddenly appear and replace the language they were using.
- haberman 11y ago> You don't really understand the UB problem if you model this in your head as some sort of simple set of rules that everybody really should have known all along What does this have to do with my comment, at all? > You can't just shrug this away with Who is shrugging? You don't understand a complex problem until you understand both sides. The article only presents one side. I am presenting the other. It doesn't mean I think the problem is simple or easy.
- ktRolster 11y agobecause people in fact don't want abstract "performance" That's true, if performance were the only consideration, we would all be writing assembly.
- physguy1123 11y agoTo add to this, a lot of what compilers decide is undefined isn't bizarre corner case code that rarely happens, but common and useful techniques that the compiler will happily demolish without warning. The 'defined' workarounds, however, are usually difficult, less safe, harder to understand, and often slower. This is without getting into the nightmare that is LTO finding 'undefined' behavior across program boundaries.
- haberman 11y ago> The 'defined' workarounds, however, are usually difficult, less safe, harder to understand, and often slower. That has not been my experience. Do you have an example of this?
- AlexandrB 11y agoThere were examples in the linked message board post [1]. Note how there is now a paragraph describing why the code is written with 4 casts instead of comparing pointers naively. Multiply this by hundreds of instances of similar code in a large code base. [1] http://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=tools/libxl/libxl_event.c;h=02b39e6da8c65c033c99a22db4784de8d7aeeb7a;hb=HEAD#l458 http://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=tools/libx...
- Kristine1975 11y agoWhy not just write a function is_ptr_less that does all the casts. P.S: In C++ std::less is guaranteed to do the right thing(tm).
- physguy1123 11y agoSure - all sorts of stuff with type-punning in unions because much harder to understand when it's transformed into a whole series of casts to/from void/char , with the loss of checking that one gets from a union. Even without unions, aliasing rules can screw you over in surprising ways. Look at the free list example linked in the post for another one. Slower - the various methods of checking for integer overflow are slower than x = a + b; if (a < x) {/overflow*/}. It can also be easier to just use a slower, or more memory hungry way of doing things than a better but needlessly complicated method. At my current job we've sacrificed memory (and in this case, performance as a consequence) because the complexity cost of keeping some critical code in the defined behavior realm was too risky.
- Kristine1975 11y agoType punning in unions is best done using memcpy. Compilers know what it does and optimize it: http://blog.regehr.org/archives/959 http://blog.regehr.org/archives/959 As for integer overflow at least gcc and clang have built-in functions that generate optimal code by checking the CPU's overflow flag: http://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins http://clang.llvm.org/docs/LanguageExtensions.html#checked-a... and https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins...
- pcwalton 11y ago> You can't just shrug this away with "Well, if you want performance...", because people in fact don't want abstract "performance"... they want the language they were truly writing in to perform well, not for what is de facto a different dialect of the language to suddenly appear and replace the language they were using. Is wanting a for loop setting an array to zero to optimize into memset optimizing the language they were truly writing in? I think it is. But that optimization frequently depends on undefined behavior. UB exploitation usually exists because people filed bugs on compilers complaining that they didn't optimize some case they expected to optimize.
- nkurz 11y agoIs wanting a for loop setting an array to zero to optimize into memset optimizing the language they were truly writing in? I think it is. But that optimization frequently depends on undefined behavior. Yes, this is a good optimization, since it efficiently does what the programmer intended. The bad optimization is removing the security-essential loop altogether when the compiler notices that result appears unused, and sensitive information is left susceptible to later attack. UB exploitation usually exists because people filed bugs on compilers complaining that they didn't optimize some case they expected to optimize. I doubt that anyone has ever filed a bug saying "I explicitly wrote a loop to zero memory, but the compiler failed to optimize it out." If you know of one, please point to it. I think you are throwing out the baby (intentional C) with the bathwater (autogenerated C++).
- pcwalton 11y ago> I doubt that anyone has ever filed a bug saying "I explicitly wrote a loop to zero memory, but the compiler failed to optimize it out." That optimization is a natural consequence of SROA and DCE. If you claim you don't want those optimizations, I don't know what to tell you. Those optimizations are some of the most basic, critical optimizations any modern C/C++ compiler does and throwing them away can easily result in at least a 2x performance loss.
- cwzwarich 11y ago
- jibsen 11y agoTo a large degree the language didn't change (some of the undefined behaviours we are discussing were in C89), rather the compiler vendors started to enforce parts of it.