Segfault when linking cudart statically and using exceptions

Hi!

I encounter a segmentation fault when using exceptions and linking against libcudart_static.a. The problem goes away when linking against libcudart.so. I’m curious to learn why this happens. Below is a description of my setup.

Source code:

#include <stdexcept>
#include <cuda_runtime.h>

void foo()
{
    throw std::runtime_error("Foo");
}

int main()
{
    int* ptr{};
    auto err = cudaMalloc(&ptr, 128);
    if (cudaSuccess != err)
        return 1;

    try {
        foo();
    }
    catch (const std::runtime_error&)
    {
    }
}

Backtrace:

#0  linear_search_fdes (ob=0x7fffffffd580, this_fde=0x0, pc=0x403df7 <foo()+65>) at ../../../gcc/libgcc/unwind-dw2-fde.c:969
#1  0x00007ffff7dc0e11 in find_fde_tail (dbase=18446744073709383432, bases=0x7fffffffd7d8, hdr=0x4aa594, pc=4210167) at ../../../gcc/libgcc/unwind-dw2-fde-dip.c:519
#2  _Unwind_Find_FDE (pc=<optimized out>, bases=bases@entry=0x7fffffffd7d8) at ../../../gcc/libgcc/unwind-dw2-fde-dip.c:573
#3  0x00007ffff7dbc4aa in uw_frame_state_for (context=0x7fffffffd730, fs=0x7fffffffd820) at ../../../gcc/libgcc/unwind-dw2.c:1005
#4  0x00007ffff7dbdefd in _Unwind_RaiseException (exc=0xcfc520) at ../../../gcc/libgcc/unwind.inc:104
#5  0x00007ffff79fdd8a in __cxa_throw ()
   from /path/to/libstdc++.so.6
#6  0x0000000000403df8 in foo() ()
#7  0x0000000000403e44 in main ()

I am running this on the following setup:

  • Ubuntu 20.04.
  • cuda-cudart-11-7_11.7.99-1_amd64.deb.
  • GCC 13.1 built from source.

I am aware that NVCC does not support GCC > 11, but in this case I’m not using NVCC, just plain C++ code and linking external libraries, so I don’t see the reason why it shouldn’t work. We’d prefer to keep linking cudart statically instead of dynamically.

Do you have any feeling for what the problem might be?

Thanks!

I got a very similar problem with a different setup:
Ubuntu 18.04
GCC 7.5.0 from ubuntu-toolchain-r/test
libgcc_s.so 13.1.0 from ubuntu-toolchain-r/test
CUDA 11.4

A fairly complex project I’m working on segfaults when throwing exceptions even at the beginning of main() with the exactly same backtrace as OP’s. And for me it can be fixed by downgrading libgcc_s (I tested 7.5.0 and 8.4.0 from Ubuntu official repo) or dynamically linking cudart
However, I’m unable create a minimal working example and OP’s code runs fine on my system.