From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29379 invoked by alias); 29 Mar 2012 07:05:53 -0000 Received: (qmail 29369 invoked by uid 22791); 29 Mar 2012 07:05:51 -0000 X-SWARE-Spam-Status: No, hits=-4.4 required=5.0 tests=AWL,BAYES_00,BELOVED_BODY,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 29 Mar 2012 07:05:32 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q2T75VEx010318 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 29 Mar 2012 03:05:31 -0400 Received: from [10.36.7.9] (vpn1-7-9.ams2.redhat.com [10.36.7.9]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q2T75UnC028552 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 29 Mar 2012 03:05:31 -0400 Message-ID: <4F7409B9.2010407@redhat.com> Date: Thu, 29 Mar 2012 07:05:00 -0000 From: Stephan Bergmann User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120316 Thunderbird/11.0 MIME-Version: 1.0 To: gcc@gcc.gnu.org Subject: C++: Letting compiler know asm block can call function that can throw? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2012-03/txt/msg00454.txt.bz2 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 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