5

I've been trying to track down an intermittent crashing bug in my code (which uses setjmp), and narrowed it down to: shows up when compiling with /O2, goes away with /O2 /Oy-, i.e. only shows up with omit frame pointer.

http://msdn.microsoft.com/en-us/library/2kxx5t2c(v=vs.80).aspx suggests setjmp requires a frame pointer. Thus:

  1. It seems that when a program that uses setjmp is compiled with /O2, Visual C++ silently generates code that causes intermittent stack corruption. Is this true, or am I missing something?

  2. It seems to me that only the function that invokes setjmp should need to be compiled with frame pointer, the rest of the program - even the functions that call longjmp - should be okay to omit frame pointer. Is this true?

Edit: I've narrowed it down a little further.

Enabling frame pointer on the function that was calling setjmp made no difference, but that's because the compiler was doing that already, just as it should, obviously noticing it needed to be done, and doing it automatically.

What did make a difference was enabling frame pointer on main. That's not as bizarre as it sounds, since the crash was manifesting in return from main. Now that I think of it, all the examples I can find in a quick google search of setjmp usage, do it in main. Perhaps it happened that the Microsoft compiler team only tested it that way.

That being the idiomatic way to use it, perhaps the best workaround would be for me to just inline the setjmp-using function into main.

5
  • 4
    This sounds like a bug in MSVC. I can't fathom how you would make an implementation of setjmp/longjmp that fails when there's no frame pointer. It seems like you'd have to go out of your way to make them this broken. In any case I'd just enable the frame pointer globally; it doesn't make much of a difference to performance. Commented May 4, 2011 at 17:46
  • What is the callstack of the crash? Commented May 6, 2011 at 3:45
  • 1
    Hi, I would like to say that this bug is still exists. see the discussion on gcc maillist: gcc.gnu.org/ml/gcc/2011-10/msg00253.html
    – ollydbg23
    Commented Oct 22, 2011 at 14:36
  • Can you post the code that caused this? I think I'm seeing this same issue in MSVC10, and turning off Omit Frame Pointers solved it, but I'd like to analyze the assembly to be certain.
    – syplex
    Commented Jul 20, 2012 at 20:46
  • @syplex I can certainly give you the code but it's a little tricky to post because it needs to be in more than one file to stop the compiler breaking the test by inlining functions, if you contact me at russell.wallace (at) gmail (dot) com I can send it to you.
    – rwallace
    Commented Jul 22, 2012 at 17:55

3 Answers 3

3

Okay, I've posted a bug report with a self-contained test case, so hopefully a fix is in the pipeline: http://connect.microsoft.com/VisualStudio/feedback/details/666704/visual-c-generates-incorrect-code-with-omit-frame-pointer-and-setjmp

In the meantime, the workaround is either don't use omit frame pointer, or put the code that calls setjmp in main, or put the function that calls longjmp in the same source file as the call to setjmp.

0
0

Can you please specify more info about the crash itself? I mean, which code the compiler generated for the longjmp, does the crash happen immediately after the longjmp, or when you try to access automatic variables, or when you try to exit the function?

I guess that the target function must be compiled with standard stack frame, there seems to be no reasonable limitation to the function that uses longjmp.

1
  • The crash happens on return from main, in a case where the setjmp-ing function has a frame pointer but main does not. Let me know if you want a copy of the test case, and if so by what medium; or if you want to create one yourself, the key thing to bear in mind is that the setjmp-ing and longjmp-ing functions must be in separate translation units.
    – rwallace
    Commented May 12, 2011 at 19:34
0

setjmp is implemented in a lot of different ways, but this probably has something to do with the assembly implementation for your particular OS.

When a function is compiled using __stdcall, arguments are stored relative to the frame pointer. Your implementation may be accessing the arguments to setjmp relative to said pointer so it doesn't have to trash multiple registers saving the context to them (since this would ruin much of the point of setjmp); I seem to remember setjmp is implemented this way in the linux kernel.

Of course if msvc doesn't generate instructions to set ebp, then this won't work and will surely cause a crash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.