This is the mail archive of the
mailing list for the GCC project.
C++: Letting compiler know asm block can call function that can throw?
- From: Stephan Bergmann <sbergman at redhat dot com>
- To: gcc at gcc dot gnu dot org
- Date: Thu, 29 Mar 2012 09:05:29 +0200
- Subject: C++: Letting compiler know asm block can call function that can throw?
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
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
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.")