Anatomy of a Compiler Bug
mikeash.com
Anatomy of a Compiler Bug
1–10 of 21 posts
Re: Anatomy of a Compiler Bug
#2Letting new compilers loose on existing codebases is always fun and you learn lots of things in the process, I can only recommend it. I debugged a problem once that also had to do with interfacing runtime-generated code with compiletime-generated code. There were differences in the expectations of the ABI, which is described in this bug:
http://llvm.org/bugs/show_bug.cgi?id=12207
It only surfaced when compiling the codebase with clang (previously gcc). Took quite some digging to find the problem.
Re: Anatomy of a Compiler Bug
#3Surprisingly it wasn't as hard to follow as I thought. Maybe I'm starting to get good at this Computer Science thing.
Re: Anatomy of a Compiler Bug
#4Great analysis and tenacity in hunting this one down :) Letting new compilers loose on existing codebases is always fun and you learn lots of things in the process, I can only recommend it. I debugged a problem once that also had to do with interfacing runtime-generated code with compiletime-generated code. There were differences in the expectations of the ABI, which is described in this bug: http://llvm.org/bugs/sho…
Re: Anatomy of a Compiler Bug
#5Haha, it's almost like some detective short story. Surprisingly it wasn't as hard to follow as I thought. Maybe I'm starting to get good at this Computer Science thing.
Seriously though, assembly isn't all that hard, mentally. It's difficult in the same way that unloading a truck full of sand with your bare hands is difficult. Which is to say, it takes a long time, but all it really needs is time, not deep thought.
People get scared away from it because they don't know where to start with it, or because it looks really hard, or because it just takes too much time to understand what's going on, but it can be really rewarding.
Re: Anatomy of a Compiler Bug
#6Great analysis and tenacity in hunting this one down :) Letting new compilers loose on existing codebases is always fun and you learn lots of things in the process, I can only recommend it. I debugged a problem once that also had to do with interfacing runtime-generated code with compiletime-generated code. There were differences in the expectations of the ABI, which is described in this bug: http://llvm.org/bugs/sho…
Great analysis, indeed. I miss one thing though: what was causing the incomplete printing of 'Testing' in the loop?
Re: Anatomy of a Compiler Bug
#7The compiler itself was crashing out while compiling a SPEC2000 test case (perlbmk I think?) with an illegal instruction. This was already quite suspect since it was branching to somewhere WAY outside of where the program usually resides, and the compiler was compiled with a compiler that's known to be nearly rock solid. I got quite lucky in that I managed to find one level of the stack trace, and it pointed me towards sprintf. Using some awesome tools some coworkers and I had developed over the years, I managed to narrow down the test case to about 5 lines of code that involved long doubles. So I grepped the compiler source code for sprintf, set breakpoints on the ones that I thought would get called, and just kept stepping through them until it finally crashed hard. Then I just reran, and stopped at the final breakpoint and started stepping through the assembly. What I saw happen just blew my mind, the code was just a simple:
sprintf(buffer, "fold: %Lf", result);
But what was happening is that the buffer was only 200 characters long, and the long double was roughly 1000 characters long. It was just a buffer overflow, that was so long it ended up overwriting the register save area, and the return address pointer. So the sprintf completed, but when it went to branch back, it loaded some characters instead of the return address. Just hilarious, and good thing I was working on stack mapping and was familiar with the stack layout of this linkage convention.
The solution of course was to just use snprintf instead. No sorry, that's wrong since that platform doesn't have an snprintf (yay mainframes!), and so I had to use %0.6Lg instead of %Lf.
Compilers are fun!
Re: Anatomy of a Compiler Bug
#8Haha, it's almost like some detective short story. Surprisingly it wasn't as hard to follow as I thought. Maybe I'm starting to get good at this Computer Science thing.
Must be my fantastic writing. Seriously though, assembly isn't all that hard, mentally. It's difficult in the same way that unloading a truck full of sand with your bare hands is difficult. Which is to say, it takes a long time, but all it really needs is time, not deep thought. People get scared away from it because they don't know where to start with it, or because it looks really hard, or because it just takes too…
Re: Anatomy of a Compiler Bug
#9Haha, it's almost like some detective short story. Surprisingly it wasn't as hard to follow as I thought. Maybe I'm starting to get good at this Computer Science thing.
Must be my fantastic writing. Seriously though, assembly isn't all that hard, mentally. It's difficult in the same way that unloading a truck full of sand with your bare hands is difficult. Which is to say, it takes a long time, but all it really needs is time, not deep thought. People get scared away from it because they don't know where to start with it, or because it looks really hard, or because it just takes too…
At least to be able to grasp what a piece of code might do, not necessarily to write code.
Re: Anatomy of a Compiler Bug
#10I used to work on compilers, C/C++, COBOL, PL/I, Java, a whole bunch of them. This is actually somewhat similar to my favorite bug I encountered while implementing some stack mapping optimizations. The compiler itself was crashing out while compiling a SPEC2000 test case (perlbmk I think?) with an illegal instruction. This was already quite suspect since it was branching to somewhere WAY outside of where the program…