https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107452
Bug ID: 107452 Summary: Failed to catch C++ exception thrown from multiarch-function (x64 CPUs) Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: kim.walisch at gmail dot com Target Milestone: --- Hi, Tested using: GCC 11.2.0, Ubuntu 22.10 x64 Tested using: GCC 9.4.0, Ubuntu 18.04 x64 I am using the GCC multiarch feature (also known as function multiversioning: https://gcc.gnu.org/onlinedocs/gcc/Function-Multiversioning.html) in my primesieve C++ project to take advantage of the latest supported CPU instruction set e.g. AVX, AVX2, AVX512 (on x64 CPUs). Today I found out that if I throw a C++ exception from a multiarch-function and I try to catch that exception outside of the originating multiarch-function but within the same translation unit, then catching the exception fails and my program simply aborts. My exception is thrown from here: https://github.com/kimwalisch/primesieve/blob/776c102f92905401613a83508d60744d41df7c73/src/PrimeGenerator.cpp#L332 It should be caught here: https://github.com/kimwalisch/primesieve/blob/776c102f92905401613a83508d60744d41df7c73/src/iterator-c.cpp#L151 My bug can be reproduced using these steps: git clone https://github.com/kimwalisch/primesieve.git cd primesieve && mkdir build && cd build git checkout 776c102f92905401613a83508d60744d41df7c73 CXXFLAGS="-O2 -Wall -Wextra -pedantic" cmake .. -DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug -DWITH_MULTIARCH=ON && make -j8 test/next_prime2 The test/next_prime2 will fail with the following error message: terminate called after throwing an instance of 'primesieve::primesieve_error' what(): cannot generate primes > 2^64 Aborted If I recompile without function multiversioning (-DWITH_MULTIARCH=OFF) the same exception is caught successfully: rm -rf * CXXFLAGS="-O2 -Wall -Wextra -pedantic" cmake .. -DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug -DWITH_MULTIARCH=OFF && make -j8 test/next_prime2 The test/next_prime2 completes successfully: ... primesieve_iterator: cannot generate primes > 2^64 next_prime(18446744073709551615) = PRIMESIEVE_ERROR: OK primesieve_iterator: cannot generate primes > 2^64 next_prime(18446744073709551615) = PRIMESIEVE_ERROR: OK All tests passed successfully! Clang also supports function multiversioning on Linux & x64 CPUs. And with Clang this issue is not present, with Clang catching C++ exceptions thrown from a multiarch-function works flawlessly (tested using Clang 14.0.0 on Ubuntu 22.10 x64): rm -rf * CXX=clang++ CC=clang CXXFLAGS="-O2 -Wall -Wextra -pedantic" cmake .. -DBUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug -DWITH_MULTIARCH=ON && make -j8 test/next_prime2 The test/next_prime2 completes successfully: ... primesieve_iterator: cannot generate primes > 2^64 next_prime(18446744073709551615) = PRIMESIEVE_ERROR: OK primesieve_iterator: cannot generate primes > 2^64 next_prime(18446744073709551615) = PRIMESIEVE_ERROR: OK All tests passed successfully! Is this a known GCC issue? If needed I could also try to write a minimal test that reproduces this issue.