public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106627] New: Exception from multiversion function cannot be caught
@ 2022-08-15 13:14 gcc.gnu at vvalter dot com
  2022-08-15 15:04 ` [Bug c++/106627] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: gcc.gnu at vvalter dot com @ 2022-08-15 13:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106627

            Bug ID: 106627
           Summary: Exception from multiversion function cannot be caught
           Product: gcc
           Version: 12.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc.gnu at vvalter dot com
  Target Milestone: ---

An exception thrown by a multiversioned function cannot be caught in the called
function. I expect the following program to exit with status code 0, but it
gets terminated because of an uncaught exception. With clang, the program works
as expected. According to godbolt.org it's also broken in gcc trunk and all
other versions that I checked: https://godbolt.org/z/564c6q8xo

__attribute__((target("default")))
void f() {
    throw 1;
}

__attribute__((target("sse4.2,bmi")))
void f() {
    throw 2;
}

int main()
{
    try {
        f();
    }
    catch(... )
    {
        return 0;
    }
    return 1;
}

Output of g++ -v:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/12/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --enable-libstdcxx-backtrace
--with-linker-hash-style=gnu --enable-plugin --enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-12.1.1-20220507/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-offload-defaulted --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
--with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.1.1 20220507 (Red Hat 12.1.1-1) (GCC)
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-dumpdir' 'a-'
 /usr/libexec/gcc/x86_64-redhat-linux/12/cc1plus -E -quiet -v -D_GNU_SOURCE
bug.cpp -mtune=generic -march=x86-64 -fpch-preprocess -o a-bug.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/12/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/12/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/12/../../../../include/c++/12

/usr/lib/gcc/x86_64-redhat-linux/12/../../../../include/c++/12/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/12/../../../../include/c++/12/backward
 /usr/lib/gcc/x86_64-redhat-linux/12/include
 /usr/local/include
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-dumpdir' 'a-'
 /usr/libexec/gcc/x86_64-redhat-linux/12/cc1plus -fpreprocessed a-bug.ii -quiet
-dumpdir a- -dumpbase bug.cpp -dumpbase-ext .cpp -mtune=generic -march=x86-64
-version -o a-bug.s
GNU C++17 (GCC) version 12.1.1 20220507 (Red Hat 12.1.1-1)
(x86_64-redhat-linux)
        compiled by GNU C version 12.1.1 20220507 (Red Hat 12.1.1-1), GMP
version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version
isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++17 (GCC) version 12.1.1 20220507 (Red Hat 12.1.1-1)
(x86_64-redhat-linux)
        compiled by GNU C version 12.1.1 20220507 (Red Hat 12.1.1-1), GMP
version 6.2.1, MPFR version 4.1.0-p13, MPC version 1.2.1, isl version
isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 2b1c93ff5f3b69a44b0e1b07f6437558
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-dumpdir' 'a-'
 as -v --64 -o a-bug.o a-bug.s
GNU assembler version 2.37 (x86_64-redhat-linux) using BFD version version
2.37-27.fc36
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/12/:/usr/libexec/gcc/x86_64-redhat-linux/12/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/12/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/12/:/usr/lib/gcc/x86_64-redhat-linux/12/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/12/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-dumpdir' 'a.'
 /usr/libexec/gcc/x86_64-redhat-linux/12/collect2 -plugin
/usr/libexec/gcc/x86_64-redhat-linux/12/liblto_plugin.so
-plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/12/lto-wrapper
-plugin-opt=-fresolution=a.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id
--no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker
/lib64/ld-linux-x86-64.so.2
/usr/lib/gcc/x86_64-redhat-linux/12/../../../../lib64/crt1.o
/usr/lib/gcc/x86_64-redhat-linux/12/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-redhat-linux/12/crtbegin.o
-L/usr/lib/gcc/x86_64-redhat-linux/12
-L/usr/lib/gcc/x86_64-redhat-linux/12/../../../../lib64 -L/lib/../lib64
-L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/12/../../.. a-bug.o
-lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/x86_64-redhat-linux/12/crtend.o
/usr/lib/gcc/x86_64-redhat-linux/12/../../../../lib64/crtn.o
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-dumpdir' 'a.'

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

* [Bug c++/106627] Exception from multiversion function cannot be caught
  2022-08-15 13:14 [Bug c++/106627] New: Exception from multiversion function cannot be caught gcc.gnu at vvalter dot com
@ 2022-08-15 15:04 ` pinskia at gcc dot gnu.org
  2022-08-28 19:52 ` [Bug ipa/106627] " gcc.gnu at vvalter dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-15 15:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106627

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-08-15
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed, not a regression.

Using a wrapper function does not fully work either, you need to mark the
function as noipa (I suspect the new function for f is being marked as nonthrow
for some reason):
__attribute__((target("default")))
void f() {
    throw 1;
}

__attribute__((target("sse4.2")))
void f() {
    throw 2;
}
[[gnu::noipa]]
void f1()
{
    f();
}

int main()
{
    try {
        f1();
    }
    catch(... )
    {
        return 0;
    }
    return 1;
}

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

* [Bug ipa/106627] Exception from multiversion function cannot be caught
  2022-08-15 13:14 [Bug c++/106627] New: Exception from multiversion function cannot be caught gcc.gnu at vvalter dot com
  2022-08-15 15:04 ` [Bug c++/106627] " pinskia at gcc dot gnu.org
@ 2022-08-28 19:52 ` gcc.gnu at vvalter dot com
  2022-08-31 13:37 ` marxin at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: gcc.gnu at vvalter dot com @ 2022-08-28 19:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106627

--- Comment #2 from Simon Rainer <gcc.gnu at vvalter dot com> ---
Created attachment 53518
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53518&action=edit
Proposed patch: Set TREE_NOTHROW correctly

I think I identified the issue and found a fix.

ix86_get_function_versions_dispatcher creates a new dispatch declaration that
has always TREE_NOTHROW set to 1, because it is created by build_fn_decl.

It seems like the original function declaration has TREE_NOTHROW set correctly,
so it's enough to set TREE_NOTHROW on the newly created dispatch declaration to
this value.

With the attached patch, I was able to catch exceptions from multiversioned
functions as expected. I also checked manually that if none of the function
versions raise an exception, TREE_NOTHROW is correctly set to 1.

The same code is used for RS/6000 and I'm pretty confident that the same patch
works there, but I'm unable to test this.

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

* [Bug ipa/106627] Exception from multiversion function cannot be caught
  2022-08-15 13:14 [Bug c++/106627] New: Exception from multiversion function cannot be caught gcc.gnu at vvalter dot com
  2022-08-15 15:04 ` [Bug c++/106627] " pinskia at gcc dot gnu.org
  2022-08-28 19:52 ` [Bug ipa/106627] " gcc.gnu at vvalter dot com
@ 2022-08-31 13:37 ` marxin at gcc dot gnu.org
  2022-09-02  6:06 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-08-31 13:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106627

--- Comment #3 from Martin Liška <marxin at gcc dot gnu.org> ---
I think the patch is correct, please send it to gcc-patches mailing list.

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

* [Bug ipa/106627] Exception from multiversion function cannot be caught
  2022-08-15 13:14 [Bug c++/106627] New: Exception from multiversion function cannot be caught gcc.gnu at vvalter dot com
                   ` (2 preceding siblings ...)
  2022-08-31 13:37 ` marxin at gcc dot gnu.org
@ 2022-09-02  6:06 ` cvs-commit at gcc dot gnu.org
  2022-10-28 20:12 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-09-02  6:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106627

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:0b0a3cdbff64d97e7de3e0e2c26e965708064193

commit r13-2360-g0b0a3cdbff64d97e7de3e0e2c26e965708064193
Author: Simon Rainer <gcc.gnu@vvalter.com>
Date:   Wed Aug 31 23:00:08 2022 +0200

    ipa: Fix throw in multi-versioned functions [PR106627]

    Any multi-versioned function was implicitly declared as noexcept, which
    leads to an abort if an exception is thrown inside the function.
    The reason for this is that the function declaration is replaced by a
    newly created dispatcher declaration, which has TREE_NOTHROW always set
    to 1. Instead we need to set TREE_NOTHROW to the value of the original
    declaration.

            PR ipa/106627

    gcc/ChangeLog:

            * config/i386/i386-features.cc
(ix86_get_function_versions_dispatcher):
            Set TREE_NOTHROW correctly for dispatcher declaration.
            * config/rs6000/rs6000.cc
(rs6000_get_function_versions_dispatcher):
            Likewise.

    gcc/testsuite/ChangeLog:

            * g++.target/i386/pr106627.C: New test.

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

* [Bug ipa/106627] Exception from multiversion function cannot be caught
  2022-08-15 13:14 [Bug c++/106627] New: Exception from multiversion function cannot be caught gcc.gnu at vvalter dot com
                   ` (3 preceding siblings ...)
  2022-09-02  6:06 ` cvs-commit at gcc dot gnu.org
@ 2022-10-28 20:12 ` pinskia at gcc dot gnu.org
  2023-10-29 15:49 ` pinskia at gcc dot gnu.org
  2023-10-30  5:49 ` xry111 at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-28 20:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106627

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kim.walisch at gmail dot com

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 107452 has been marked as a duplicate of this bug. ***

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

* [Bug ipa/106627] Exception from multiversion function cannot be caught
  2022-08-15 13:14 [Bug c++/106627] New: Exception from multiversion function cannot be caught gcc.gnu at vvalter dot com
                   ` (4 preceding siblings ...)
  2022-10-28 20:12 ` pinskia at gcc dot gnu.org
@ 2023-10-29 15:49 ` pinskia at gcc dot gnu.org
  2023-10-30  5:49 ` xry111 at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-29 15:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106627

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |erosenberger at kinetica dot com

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 112275 has been marked as a duplicate of this bug. ***

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

* [Bug ipa/106627] Exception from multiversion function cannot be caught
  2022-08-15 13:14 [Bug c++/106627] New: Exception from multiversion function cannot be caught gcc.gnu at vvalter dot com
                   ` (5 preceding siblings ...)
  2023-10-29 15:49 ` pinskia at gcc dot gnu.org
@ 2023-10-30  5:49 ` xry111 at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-10-30  5:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106627

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #7 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to CVS Commits from comment #4)
> The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:
> 
> https://gcc.gnu.org/g:0b0a3cdbff64d97e7de3e0e2c26e965708064193
> 
> commit r13-2360-g0b0a3cdbff64d97e7de3e0e2c26e965708064193
> Author: Simon Rainer <gcc.gnu@vvalter.com>
> Date:   Wed Aug 31 23:00:08 2022 +0200
> 
>     ipa: Fix throw in multi-versioned functions [PR106627]

Is this issue fixed then?

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

end of thread, other threads:[~2023-10-30  5:49 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-15 13:14 [Bug c++/106627] New: Exception from multiversion function cannot be caught gcc.gnu at vvalter dot com
2022-08-15 15:04 ` [Bug c++/106627] " pinskia at gcc dot gnu.org
2022-08-28 19:52 ` [Bug ipa/106627] " gcc.gnu at vvalter dot com
2022-08-31 13:37 ` marxin at gcc dot gnu.org
2022-09-02  6:06 ` cvs-commit at gcc dot gnu.org
2022-10-28 20:12 ` pinskia at gcc dot gnu.org
2023-10-29 15:49 ` pinskia at gcc dot gnu.org
2023-10-30  5:49 ` xry111 at gcc dot gnu.org

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