public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* C++: Letting compiler know asm block can call function that can throw?
@ 2012-03-29  7:05 Stephan Bergmann
  2012-03-29  7:44 ` Jakub Jelinek
  2012-03-29 16:14 ` Richard Henderson
  0 siblings, 2 replies; 22+ messages in thread
From: Stephan Bergmann @ 2012-03-29  7:05 UTC (permalink / raw)
  To: gcc

Hi all,

In LibreOffice's ever-beloved low-level code to synthesize calls to C++ 
virtual functions, I'm having the following problem (on Linux x86_64). 
The function callVirtualMethod at 
<http://cgit.freedesktop.org/libreoffice/core/tree/bridges/source/cpp_uno/gcc3_linux_x86-64/uno2cpp.cxx?id=571876c1234ae55aab0198c7e2caf9049fcd230e#n61> 
effectively does the following:

First, call dummy_can_throw_anything that can potentially throw (see 
below for why that's there).  Second, in an asm block, call some virtual 
function (that can potentially throw).  Third, call x86_64::fill_struct 
that can potentially throw (it doesn't, but nobody bothered to annotate 
it as "throw ()").

Now, at least GCC 4.7.0 with -O0 produces a .gcc_except_table section 
for callVirtualMethod, with two call-site table entries each spanning 
the first (dummy_can_throw_anything) and third (x86_64::fill_struct) 
calls, resp., but none spanning the second (asm block) call.  These 
entries are effectively nop, simply calling back into _Unwind_Resume, 
and compiling at higher optimization levels leaves them out anyway 
(leading to callVirtualMethod having no corresponding .gcc_except_table 
section).

The problem is that if the virtual function called through the asm block 
throws an exception, that then immediately leads to std::terminate.  My 
understanding is that because the ip is at the call instruction in the 
asm block that is between the two call-site table entries, the unwind 
machinery thinks this cannot happen and bails out.  (When compiled -O2, 
the code happens to work fine, as there is no .gcc_except_table section 
for this frame at all, so unwinding simply passes through it without 
calling the personality function.)

Making sure that there are no calls to (compiler-visible) functions that 
can throw within callVirtualMethod would happen to make the code also 
work with -O0.  But that would remain a fragile solution.

Is there a way to let the compiler know that the asm block potentially 
calls functions that can throw?  So that it could emit correct code, 
regardless of whether callVirtualMethod happens to have a corresponding 
.gcc_except_table section or not.

(The call to dummy_can_throw_anything, copied from the corresponding 
older code for 32-bit x86, is there for the following reason:  At least 
with some compiler version and some optimization level, on x86 it was 
discovered that the compiler did not emit the .eh_frame data necessary 
for unwinding to successfully pass through this frame at all.  As the 
corresponding x86 code does not have a call to x86_64::fill_struct, the 
compiler apparently considered callVirtualMethod a leaf function and 
optimized accordingly.  The dummy_can_throw_anything hack happened to 
make it do the right thing again, but again this is a fragile solution, 
anyway, that could be replaced with something robust if there were a way 
to annotate the asm block as "calls functions that can throw.")

Stephan

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

end of thread, other threads:[~2012-04-16 20:01 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-29  7:05 C++: Letting compiler know asm block can call function that can throw? Stephan Bergmann
2012-03-29  7:44 ` Jakub Jelinek
2012-03-29  8:48   ` Stephan Bergmann
2012-03-29  9:16     ` Richard Guenther
2012-03-29 12:51       ` Stephan Bergmann
2012-03-29 13:59         ` Michael Matz
2012-03-29 14:12           ` Andrew Haley
2012-03-29 15:10             ` Stephan Bergmann
2012-03-29 15:43             ` Michael Matz
2012-03-29 20:38               ` Eric Botcazou
2012-03-30  8:23           ` Richard Guenther
2012-03-29 16:14 ` Richard Henderson
2012-03-29 17:16   ` Jan Hubicka
2012-03-29 18:34     ` Richard Henderson
2012-03-30  8:19       ` Richard Guenther
2012-03-30 12:21         ` Jan Hubicka
2012-03-30 12:23           ` Richard Guenther
2012-03-30 15:46             ` Jan Hubicka
2012-04-02 14:08               ` Michael Matz
2012-04-02 14:17                 ` Jakub Jelinek
2012-04-02 16:04                   ` Michael Matz
2012-04-16 20:01                     ` Hans-Peter Nilsson

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).