Help needed with SEH exception handler (not really Win32 specific)

Andrew Haley aph@redhat.com
Mon Mar 17 10:37:00 GMT 2003


Ranjit Mathew writes:
 > Ranjit Mathew wrote:
 > > 
 > 
 > I just found out about the "&&" operator that can be 
 > used in GCC to find the address corresponding to a
 > label and using this I have managed to achieve the
 > effect needed by at least the GC. 
 > 
 > For those curious about it, I'm attaching a simple 
 > program that demonstrates the technique.

Interesting.

 > BTW, the exception registration works only when 
 > declared with a local variable simply because the
 > kernel in Windows (9x and NT) does not trust an 
 > exception registration found outside of a thread's 
 > stack bounds!

 > ----------------------------- 8< -----------------------------
 > #define WIN32_LEAN_AND_MEAN
 > #include <windows.h>
 > #include <excpt.h>
 > 
 > #include <stdio.h>
 > #include <stdlib.h>
 > 
 > 
 > int foo( int x)
 > {
 >   MyExRec er;
 >   int retVal = 0;
 > 
 >   /* Install the handler */
 >   er.hlr_addr = &&ex_hlr;
 >   er.ex_reg.handler = my_handler;
 >   asm volatile ("movl %%fs:0, %0" : "=r" (er.ex_reg.prev));
 >   asm volatile ("movl %0, %%fs:0" : : "r" (&er));
 > 
 >   
 >   retVal = inner_foo( x);
 > 
 >   printf( "Normal Execution!\n");
 > 
 >   /* Without this redundant check, GCC considers the following code
 >    * unreachable and eliminates it! 
 >    */
 >   if (er.hlr_addr != 0)
 >     goto rm_hlr;
 > 
 > ex_hlr:

There's a potential problem here.  gcc doesn't know that the printf()
above has not happened.  For example, it is possible that gcc will
generate this:

      move    #printf, r1
      move    #string1, r0
      call    r1
      cmp     er.hlr_addr, #0
      bne     rm_hlr

ex_hlr:
      move    #string2, r0
      call    r1

Bad -- the address of printf isn't in r1.  The compiler needs to know
that there is an abnormal edge to ex_hlr in the control flow graph.

I don't know the right way to tell gcc that this might happen.  There
may be a way that uses __builtin_setjmp.

Andrew.



More information about the Java mailing list