Unanswered Exceptions and context switching

  • Freitag, 17. August 2012 09:34
     
      Enthält Code

    Hello!

    I have my own "microthread" mechanism written in x86 ASM for Visual Studio, so I can do context switches inside a function. I have been using intensively this system during some years with perfect working and now i detect a problem when using C++ Exceptions "near" a context switch point. A simple example as:

    void f()
    
    {
    
    	switchProcess(); //do context switch and go to next task in scheduler
    
    	try{
                throw 1;
             }
     	catch( int ){}
    
    }

    doesn't work because exception is not catched. The message from Windows is very clear: it can find the handler for "int" exception, so it's the same message as if I had thrown a different type than the catched one.

    The "switchProcess" function works as usual in a multithread scheduler: save stack and registers and all will be restored when sheduler decides to re-schedule the function. It seems that the handler for the catch(int) is lost after returning from switchProcess but I have looking at the stack before and after context switch and all looks ok.

    If I call some other function after switchProcess in which there is a try-catch block it works fine( exception handler are set on function prolog, so it has sense). The problem comes when using try-catch in the same function after the context switch.

    The main problem is that I don't know how exception handlers work in deep, and I'm trying to resolve it through reverse-enginering, so maybe the exception handler mechanism use some kind of "static data" and I'm not saving/restoring it in the correct way.

    Anybody could explain me, in deep, how the exception handlers are managed?

    Thanks in advance

    P.D. 1- in GCC on Mac all works fine

    P.D. 2- similar problem arise when using iOS (ARM assembler code)

    P.D. 3- sorry for my english!!

Alle Antworten

  • Freitag, 17. August 2012 14:28
     
     

    Be aware that the actual implementation details of C++ exception handling will vary by compiler and processor architecture and even compiler version.

    In general, if you are doing anything at a low level that interferes with the compiler's control of code flow, you run the risk of breaking things.  This is why you cannot mix __try/__catch or setjmp/longjmp with try/catch.

    While I have no idea what your process switcher is doing, there does seem to be a good chance that it is interfering with exception semantics.

  • Montag, 20. August 2012 10:58
     
     

    Thanks for the answer. I suppose that exception handling depends on compiler version,etc, but I would like to see internal implementacion details for the current or past compiler versions.

    What makes me wonder is why, although the stack is correctly preserved along "switchProcess" call (and so the handler for the exception, which is stored in stack in the function prolog), the hanlder doesn't work after returning from context switch.

  • Dienstag, 21. August 2012 11:10
     
     

    dani, this article http://msdn.microsoft.com/en-us/library/x057540h.aspx tells about exception handling in visual c++.

    hope it can help you.