13 ms·
GCC null pointer check removed
- klodolph 7y agoThis one is interesting... it looks like sibling call optimization was a bit too aggressive, or non-nullness is propagating incorrectly. For those who understand UB, the problem is not that 'node' is NULL. The compiler can "prove" that 'node' is non-NULL, but the results are propagated to 'module' from main, which is not correct because 'main' is not the only call site of 'walk'.
- tedunangst 7y agoBut the proof that it's non null is the deref in main? I'm not sure I understand why people say this has nothing to do with the UB already derefed therefore not null optimization. How else is the compiler inferring the pointer could be not null?
- klodolph 7y ago> But the proof that it's non null is the deref in main? This is an incorrect interpretation, it only proves that 'node' is non-null. Recall that 'walk' is called twice—once for 'node', and once for 'node->child'. The compiler bug (and this IS a compiler bug, make no mistake) is that the the proof that 'node != NULL' is incorrectly treated as a proof that 'module != NULL', which is not sound, and the bug manifests as incorrect program behavior during the recursive call 'walk(module->child, ...)'.
- tedunangst 7y agoYeah. The bug is that the compiler propagated not null from main.node to walk.module. But how did the compiler determine main.node is not null?
- klodolph 7y ago> But how did the compiler determine main.node is not null? The test case in the bug happens to use UB to generate the constraint, but this is just an example of how to trigger the bug. The bug itself is not related to UB, and there are other ways to generate the same constraint. Here is an example on godbolt showing what I mean: https://godbolt.org/z/pRW97L https://godbolt.org/z/pRW97L You'll notice that I removed the UB, but the bug is still there. You can see that the 'return;' statement is not emitted because it is colored with a white background. So the bug is not related to UB at all.
- tedunangst 7y agoOk, this is the clarification I was seeking.
- saagarjha 7y ago> You can see that the 'return;' statement is not emitted Uh, what? The code looks correct to me: func: test rdi, rdi je .L7 mov esi, 1 jmp walk .L7: ret There's a conditional branch on $rdi that jumps to .L7 (which returns) when node is NULL.
- klodolph 7y agoYou're looking at 'func', the error is in 'walk'. Here is the top of 'walk': walk: push rbx mov rbx, rdi test esi, esi je .L3 mov rdi, QWORD PTR [rdi] call walk Note that esi = cleanup = 1 so the branch to .L3 is not taken, and the recursive call to 'walk' is taken. The crash will occur on the instruction before the call, mov rdi, QWORD PTR [rdi] Because rdi = 0 on the second call to 'walk'.
- saagarjha 7y agoOh, yes, walk is still broken. I was focusing on func because you had added that and it coincidentally also had a white return statement.
- 7y ago
- caf 7y agoI think the incorrect propagation is actually from one instance of walk() to the next recursive invocation (which has been replaced by a loop).
- chowells 7y agoOuch, this is annoying. But it's a bug. Sometimes those happen, and they get fixed. This is totally unlike the user hostility that results from playing gotcha with UB which has also resulted in removing null pointer checks. Those aren't bugs and don't get fixed.
- Gibbon1 7y agoThis is more than annoying this is unacceptable and the people responsible need to be stopped.
- saagarjha 7y agoRelax, it's an honest bug in open source software and they're working on fixing it (edit: they've already fixed it in trunk). What more do you expect?
- Gibbon1 7y agoI expect them to stop playing with fire with useless UB optimizations.
- saagarjha 7y agoThis bug has nothing to do with undefined behavior, as many of the other commenters (including myself) have pointed out.
- temac 7y agoI'm curious why you think that? The origin is absolutely the usual exploitation of UB in the sense that the compiler supposes that code paths that are past one can be elided (because programmers never write bugs, that kind of interesting reasoning...) Except it has a bug (the irony :p ) in the logic that computes that, and actually elides more code paths than it should. That shows that their architecture to do that kind of dangerous transformation is fragile, and they are not able to guarantee that that kind of transformation is really restricted to buggy source code (which would even then still be a bad idea IMO, but that is another story).
- saagarjha 7y agoAs the other commenters have pointed out, this is a bug in GCC and not an aggressive optimization typical of compilers exploiting undefined behavior. Perhaps the title can reflect this somehow? "Bug in GCC removes null pointer check", etc.?
- a1369209993 7y ago> aggressive ["]optimization[" ...] exploiting undefined behavior That's still a compiler bug. But yes, this is a bug in GCC and the title should show that.
- saagarjha 7y agoExploiting undefined behavior is something compilers can and will do; that's not a bug. (Specifically, if a compiler can choose to omit code where it can prove that going down that branch would lead to undefined behavior. That's not what happened here.)
- paulddraper 7y agoTo be clear, it's a bug for code to exploit undefined behavior. A compiler may exploit the assumption that source code does not invoke undefined behavior.
- Skunkleton 7y agoComparing a pointer to null isn't UB. De-referencing invalid pointers is ub.
- monocasa 7y agoStuff like this is why I really like sel4's approach to verification. They take the compiler out of the TCB by parsing and verifying the underlying binary.
- topspin 7y agoI believe NaCl and PNaCl analyzes machine instructions (NaCl) or 'bitcode' (PNaCl) to sandbox binaries.
- monocasa 7y agoThey do but it's a little different. (P)NaCL still allows memory safety and data verification bugs that don't compromise the integrity of the sandbox. sel4 goes a lot further by verifying that the total behavior of the application matches the machine readable formal specification. So (P)NaCL probably would have allowed the bug in question to get into production, whereas it would have been caught as a build error by sel4.
- singron 7y agoI couldn't reproduce with 9.1.0 on Arch Linux since it's patched: https://git.archlinux.org/svntogit/packages.git/commit/trunk/bz90949.patch?h=packages/gcc&id=7c2f84ac385521ffeb9ce7f34cc919031e717392 https://git.archlinux.org/svntogit/packages.git/commit/trunk... However the gcc 9.1.0 in nix/nixos is still affected (but not the default gcc, which is still gcc7). EDIT: nix just updated to gcc 9.2.0. I was using a slightly outdated release: https://github.com/NixOS/nixpkgs/pull/66836 https://github.com/NixOS/nixpkgs/pull/66836
- saagarjha 7y agoIf anyone wants to try this themselves: https://godbolt.org/z/j4YJ0c https://godbolt.org/z/j4YJ0c
- mjcohen 7y agoReading that thread, I'm really impressed by the knowledge shown by the people that worked on it.
- saagarjha 7y agoCompiler engineers are a smart bunch :)
- bjoli 7y agoI have been following the development of some open source scheme compilers (chez and guile) and I'm always impressed by the work people do there. The chez code is amazing in most places, and I can understand why racket chose it as their base. Guile has been getting some really neat optimizations lately (jit, declarative modules, efficient local recursive bindings and much more) and it is really fun to try to follow the code that gets published. Andy's blog over at wingolog.org is also great fun to read.
- ncmncm 7y agoThings turned out well this time. The last Gcc bug I submitted was a regression that made my program take fully twice as long to run. For that case, they decided making it slow was the correct choice. (Anyway it matched Clang afterward. :-) The program's main loop, where it had spent 90+% of its time, was just four instructions, which Haswell and later can do in one cycle per iteration, provided the instructions are presented right. Occasionally, it would encounter something that would take 50+ cycles to deal with, and then continue looping. The regression was that it re-arranged the instructions to save a cycle on the slow path and add a cycle to the fast path, so it now takes two cycles per iteration instead of one.
- ChuckMcM 7y agoWow, I am in awe of Jeff Law at the moment :-). I am glad they found the issue and are fixing it. I was helping a high school student learn how compilers work and we started by opening up GCC and I was astonished at just how mindbogglingly complex it had become. We closed that down and opened up the delightful Bellard "Tiny C"[1] which was comprehensible at the level where this student was working. We created a new 'bool' type for that compiler. [1] https://bellard.org/tcc/ https://bellard.org/tcc/
- pcwalton 7y agoIf you want a simple modern compiler backend intended for production-readiness, consider Cranelift [1]. It's largely written by Dan Gohman, who is a longtime LLVM contributor (and a colleague of mine). It does a lot of the things LLVM's backend passes do, but it's cleaner and better factored, with the benefit of a decade of hindsight [2]. Cranelift should be great for teaching purposes. [1]: https://github.com/CraneStation/cranelift https://github.com/CraneStation/cranelift [2]: https://cranelift.readthedocs.io/en/latest/compare-llvm.html https://cranelift.readthedocs.io/en/latest/compare-llvm.html
- dhuramas 7y agoI was wondering where I had heard of Cranelift before- seems like it is the default backend for wasmer[1]. [1] https://github.com/wasmerio/wasmer#building https://github.com/wasmerio/wasmer#building
- ChuckMcM 7y agoThat is a pretty neat backend for compilers. Definitely worth using when going to the 'next level' between how do we take text and turn it into binary as level 1, and then level 2, how do we improve the binary we're going to generate by understanding what the ASTs are telling us.
- nickpsecurity 7y agoThanks. I'll check it out. In that vein, what do you think of QBE (open-source) and LCC for understandable compilers? https://c9x.me/compile/ https://c9x.me/compile/ https://en.wikipedia.org/wiki/LCC_(compiler) https://en.wikipedia.org/wiki/LCC_(compiler)
- nullc 7y agoI wonder if they'll get around to fixing this miscompliation bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90348 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90348
- nullc 7y agoIt isn't particularly helpful to downvote a comment to -3 without actually expressing what you found to be incorrect about it. I linked to an issue where all current GCC versions emits incorrect memory corrupting output from a pretty boring correct input which has been sitting idle for almost 4 months. The issue-- a morally similar issue of applying a constraint to the wrong scope-- was found in production code, in the wild-- it's not like its some machine generated test case. Currently the finders are working around it with -fno-stack-reuse but it's unclear how much other software is impacted... Should I be lobbying Linux distros to recompile everything with -fno-stack-reuse ? ... hard to say: there isn't an easy way to instrument the compiler to detect when the bug is being triggered. A rapid and highly knowledgeable response to miscompilation is what I've historically experienced from GCC and thats what's exhibited on the headline issue, but seemingly not in the case I linked to.
- caf 7y agoThat's definitely an interesting issue - I don't think it's too similar to the one in the original post, though. It's not about applying a constraint to the wrong scope, it's a case where the liveness analysis has correctly deduced that a block-scope variable in a loop is not live from one iteration of the loop to the next but then the address of that block-scope variable has been incorrectly kept live across the loop iterations.
- slovenlyrobot 7y agoObligatory Dan Bernstein presentation: https://cr.yp.to/talks/2015.04.16/slides-djb-20150416-a4.pdf https://cr.yp.to/talks/2015.04.16/slides-djb-20150416-a4.pdf (note: portrait slides!)
- sandermvanvliet 7y agoThanks for sharing this
- MaxBarraclough 7y ago> Wikipedia: “An optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program.” > So the algorithm designer (viewed as a machine) is an optimizing compiler? > Nonsense. Compiler designers have narrower focus. Example: “A compiler will not change an implementation of bubble sort to use mergesort.” — Why not? (No answer is given.) An optimising compiler is permitted to replace your bubble-sort by a merge-sort, provided apparent program-behaviour is unchanged. The only reason real compilers aren't likely to do this, is that the transformation is too sophisticated/too specific, for current compiler technology. > compiler designers take responsibility only for “machine-specific optimization”. Outside this bailiwick they freely blame algorithm designers This is wrong. It's wrong in the shallow sense: in a typical modern compiler, most optimisations are performed at the level of an IR, not at the level of the particular target machine language. Common subexpression elimination, for instance. It's also wrong in a deeper sense: compilers may apply optimisations which improve time complexity. That is to say, they may transform the algorithm itself. The target machine has no bearing there. Consider the following unoptimised loop: int found = 0; for (size_t i = 0; i != n; ++i) { if (arr[i]) { found = 1; // break; } } An optimising compiler may be capable of essentially uncommenting the commented-out 'break'. This changes the time-complexity of the scan in an obvious way. This optimisation could even be applied by a source-to-source optimiser, with no awareness of the the target machine. (We can nitpick about possible tradeoffs regarding branch-prediction when adding the 'break', but I think my point is clear.)
- asveikau 7y agoI think I am already not alone in saying this on this thread, but that was a very fast investigation and bug fix. I am impressed. Not at all the expectation I would have for facing a compiler bug. I guess it also helps that the report seemed clear and high quality. (Even had a revision where it was introduced.)
- rurban 7y agoThere is a reason people are still staying with gcc-4.8. This pass in question (ssa-tree-copy) is just horrible, esp. in the latest versions. I had to blacklist gcc-9 because of similar issues with that. I didn't know that it did start with gcc-7 already.
- mjevans 7y agoAgain: There really needs to be a warning (elevate-able to an error like usual) for all removed / "optimized out" code. A programmer added it for a reason, and either that reason is invalid and it can be removed OR the compiler's doing something the programmer did not intend and is thus exhibiting undesired behavior.
- saagarjha 7y agoSo you’re looking for a warning that will always show up in any non-trivial program?
- ogoffart 7y agoThis is not possible. void foo(int x) { if (x) do_something(); else do_something_else(); } void fn1() { foo(0); } void fn2() { foo(1); } When the code is inlined, the compiler can propagate constant and optimize the `if`. Do you really expect two warnings there? These warning would happen all the time. And anyway, this does not apply to this particular bug which is just a compiler bug (which has been fixed). The warning was there all along: This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- tinus_hn 7y agoThis is a bug, not optimization.
- tinus_hn 7y agoScary, does this have a chance of introducing security issues in (mis)compiled code?