6 ms·
Personally, I find it painful that the compiler detects this kind of undefined behavior, and silently uses it for optimization, rather then stopping and emittin
by JoshTriplett 12y ago
Personally, I find it painful that the compiler detects this kind of undefined behavior, and silently uses it for optimization, rather then stopping and emitting an error. In the printf example, the compiler could trivially emit an error saying "NULL check on p after dereference of p", and that would catch a large class of bugs. (Some static analysis tools check for exactly that.) Similarly, a loop access statically determined to fall outside the bounds of the array should produce an error.
- bnegreve 12y ago> In the printf example, the compiler could trivially emit an error saying "NULL check on p after dereference of p" If you know that p is not NULL, the code is just fine. The compiler has to compile it.
- infogulch 12y agoIf you know that p is not NULL then why are you checking for it? Either way, something is wrong here. Either you made a mistake with where you check for NULL, or you are performing extraneous operations for no reason.
- pcwalton 12y agoUsually this sort of thing comes up in code that's the result of several rounds of function inlining. Nobody would write that kind of code by hand, but it arises from the indirect results of several chains of function calls. In this regard, it's a very important optimization to be able to perform. Kent Dybvig's classic response to "Who writes that kind of code?" is "Macros do." Inlining has much the same effect as macros.
- infogulch 12y agoI can see how macros can throw a wrench into this, but couldn't compilers tell the difference between when someone wrote code that might have a bug and when it inlines a function that is a bit paranoid for that situation?
- pcwalton 12y agoYes, by allowing the optimizer the freedom to exploit undefined behavior, but coupling that with static analysis (or a stricter language semantics) that can catch bugs before they get to the optimizer.
- bnegreve 12y ago> If you know that p is not NULL then why are you checking for it? These dummy tests can happen if you have a lot of macros for example. It still valid C code as long as p is not NULL. A compiler generating an error on this particular example wouldn't be a correct C compiler. The compiler can reject this code if it can prove that p can be NULL, but in many case that's impossible at compile time. I agree that a warning would be nice though. edit: clarity.
- pascal_cuoq 12y agoExcuses are provided in http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-... . But they are just that, excuses. To summarize at least one of them, the compiler doesn't really see it as “detecting undefined behavior and optimizing accordingly”. It sees it as doing the right thing for all defined behaviors. The sort of imprecise analysis it does lead it to consider plenty of possible undefined behaviors, many of which cannot happen in real executions. It ignores these as a matter of fact, but reporting them would not tell the programmer anything it doesn't know, and would be perceived as noise. On the example for (int i=1; i==0; i++) …, the compiler does not infer that i eventually overflows (undefined behavior). It infers that i is always positive, and thus that the condition is always false.
- bdamm 12y agoOverflows are not undefined. They are overflows. Maybe I want to overflow on purpose. Your for loop (int is signed) will complete assuming the body of the loop doesn't manipulate i, and given enough time.
- dreish 12y agopascal_cuoq is correct. Here's an excellent introduction to the subject: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
- marvy 12y agounsigned overflows are well defined, but signed ones are not
- mtdewcmu 12y agoThat makes sense (sort of). Better to use unsigned if you are trying to do modular arithmetic. Signed integers have some weirdness attached. The number that's one followed by all zeroes in binary (INT_MIN in limits.h) is defined as negative, because the sign bit is set. But, the rules for 2's complement arithmetic predict that -INT_MIN == INT_MIN. So it's not a normal number.
- deleted 12y ago[deleted]
- mikeash 12y agoYou can come up with specific cases and decide that they should be handled a different way. You're absolutely right that this NULL check elimination should generate a warning. But it's really really hard to come up with a general algorithm that correctly differentiates between "your code implies that this check can never be true, watch out!" and "your code implies that this check can never be true, so we correctly removed it during optimization". For a trivial NULL-related example, the standard C free() function does a NULL check on its parameter. free(NULL) is legal and does nothing. A naive "does this check for NULL after dereferencing the pointer?" checker would therefore warn for this code: printf("the pointer's value is %d", *p); free(p); To a human, this obviously shouldn't be warned about, while the other example should be. But how does the computer tell them apart? It's hard.
- scott_s 12y agoSuper, super pedantic point: that probably wouldn't happen with your example because most of the time, free is defined in a shared library somewhere else, and the compiler wouldn't be able to inspect its code. Even if it's in the same source file, most compilers don't optimize across non-inlined function boundaries. But! You made a good point, and it would apply to a function that did a null-check which was inlined. It's easy for us to imagine a function which 1) does a null-check, 2) gets inlined, and 3) is used in places in the code which dereference the pointer before calling the function.
- mikeash 12y agoThe standard library thing is interesting. Modern compilers actually understand that stuff a lot. I don't think they'll reach into the implementation, but they understand the guaranteed semantics. For example, they know that this is undefined behavior: free(ptr); printf("after free, it contains %d\n", *ptr); And the nasal demons shall flow freely. The compiler will certainly know that there's a NULL check in there. However, the smarts are different enough that it will also know not to warn you about it. So, yes, this is an example that won't happen in reality, although there's no reason it couldn't.
- ygra 12y ago
- robryk 12y agoLLVM does something similar if you pass -fsanitize=undefined: it tries to insert code that will crash the program when it invokes something that has undefined behaviour. It cannot emit a compiler error, because it's perfectly fine to have a function with undefined behaviour in your program, as long as you don't call it.
- throwaway25173 12y agoI don't understand why you say "it cannot emit a compiler error" -- just because something is allowed doesn't mean it's impossible to emit a compiler error for it (c.f. -Werror). Why can't there be a different option to emit a compiler error whenever -fsanitize=undefined would cause the compiler to add program-crashing code? Personally I would definitely use such an option as I can't imagine a purpose for having undefined behavior anywhere in my code. If I have a function that's never being called then I either forgot to call that function somewhere, or I have a useless function that I should remove from my code. Edit: or perhaps I'm implementing a library -- regardless, I can't imagine why I would want to compile successfully with undefined behavior in my code.
- mikeash 12y agoDo you want this to produce an error? int Successor(int x) { return x + 1; } Because that is undefined behavior for x == INT_MAX, and if the compiler is detecting undefined behavior and emitting errors, this would be an obvious candidate. How about this simple function? int Divide(int x, int y) { return x / y; } This is undefined behavior for y == 0, or for x == -INT_MAX - 1 and y == -1. Shall it produce an error? (Never mind why you're writing such simple functions. Imagine they do something more complex and just do the division or addition or whatever as part of their work.) Here's a fun one: void StrClear(char *str) { memset(str, 0, strlen(str)); } This will invoke undefined behavior if passed a string constant as its parameter, or if passed an array that doesn't have a 0 in it. There is no portable (i.e. without invoking undefined behavior) way to check whether the parameter is a string constant or doesn't have a 0, so it is impossible to assert away the undefined behavior for this. Many real, practical, production-worthy C functions will invoke undefined behavior with some inputs. Turning undefined behavior into a compile-time error will cause virtually all C code to not compile.
- DannyBee 12y ago""NULL check on p after dereference of p"," Issuing an error or warning about this would flood stuff with warnings due to inlining/macros, you name it. This happens all the time. Basically, distinguishing between the things that are accidents, and things that are on purpose and expected to be optimized away, is very very very hard.
- nitrogen 12y agoThe warning could be "NULL check after unchecked dereference" with a pragma to disable the warning for macros within an annotated method
- DannyBee 12y agoThe unchecked doesn't add anything that makes it easier, and debugging functions and others rarely check things. /* Assumes you have a valid foo */ int printfoo(struct foo *bar) { /* Print the main part of our foo */ printf("First field: %d\n", bar->first); /* Get the substructure value */ int foosub = get_foosub(bar); printf("Second field: %d\n", bar->second); } /* Doesn't assume you have a valid foo */ int get_foobsub(struct foo *bar) { if (bar != NULL) return bar->second; assert(); } In any case, people have spent a long amount of time trying to make warnings like this work without massive false positive rates. It's just not easy.