public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Exception unwinding bug
@ 2002-02-07  8:50 Ed Maste
  2002-04-25  2:55 ` Exception handling problem on x86-64 Bo Thorsen
  0 siblings, 1 reply; 4+ messages in thread
From: Ed Maste @ 2002-02-07  8:50 UTC (permalink / raw)
  To: 'gcc@gcc.gnu.org'

I've discovered what I believe to be a bug in the exception unwinding code
of gcc 3.0.3.  I wrote a short test function with just a try & catch in a 
function, and the unwinder crashes at run time.

I've traced the unwinding through to the call to the C++ personality 
function in eh_personality.cc.  At the end of the personality function,
_Unwind_SetGR is called to set up a pointer to the exceptionObject
for the call to __cxa_begin_catch later on (eh_personality.cc:393):

  _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
		 (_Unwind_Ptr) &xh->unwindHeader);

_Unwind_SetGR takes an _Unwind_Word as its third argument.

I'm using a MIPS processor; sizeof(_Unwind_Ptr) is 32 bits, and 
sizeof(_Unwind_Word) is 64 bits.  _Unwind_Word is an unsigned type
(unwind.h:32):

typedef unsigned _Unwind_Word __attribute__((__mode__(__word__)));

When gcc generates the call to _Unwind_SetGR in the personality 
function, it takes the &xh->unwindHeader and zero-extends it
to 64 bits.  Later on, the generated code for my "catch" calls
__cxa_begin_catch, and the result of the _Unwind_SetGR is in the
a0 register.

__cxa_begin_catch tries to read a value from the exceptionObject.  
The beginning of __cxa_begin_catch looks like this (mips assembly):

__cxa_begin_catch:
addiu           sp,sp,-48
sd              s0,32(sp)
sd              ra,40(sp)
jal             __cxa_get_globals
addiu           s0,a0,-48
lw              v1,20(s0)

So here's the problem: addiu requires its register operand 
to be a valid 64-bit sign extended representation of a 
32-bit value; if it is not, the result is unpredictable[1].  
The zero-extended version that the compiler generates violates 
this rule.  This problem won't show up with user pointers that
end up < 0x80000000; in kernel mode my pointers are 
>= 0x80000000.

It seems that almost all MIPS processors implement addiu as
"do a 32 bit add and then sign extend" so the unpredictable
behaviour produces the expected result.

The processor I'm working with has different unpredictable
behaviour (the result of the addiu still has zeros in bits
63-32), so the "lw" instruction following causes a MIPS 
processor exception.

I can probably work around this by casting the 
&xh->unwindHeader to a signed word in the personality 
function.  I'm not sure how this should be properly
fixed, though, as other architectures might have other 
issues with such a change.

My compiler info:
Configured with: configure --target=mips-wrs-vxworks --enable-threads
Thread model: vxworks
gcc version 3.0.3

[1]
http://www.mips.com/publications/documentation/MD00087-2B-MIPS64BIS-AFP-00.9
5.pdf, page 39

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-04-25 17:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-07  8:50 Exception unwinding bug Ed Maste
2002-04-25  2:55 ` Exception handling problem on x86-64 Bo Thorsen
2002-04-25 10:08   ` David Edelsohn
2002-04-25 10:33     ` Richard Henderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).