MikeMB
MikeMB

Reputation: 21136

Behavior of c++ exceptions escaping into c program

Lets say I have a shared library that is implemented in c++ but exposes a pure c interface. That library is then used in a c program.

Does gcc make any guarantees about what happens, if an exception escapes from the c++ library into the c application?
Will it e.g. always terminate the program?

I'm mainly interested in an answer for gcc on Linux on x64 and ARMv7-R, but answers about other operating systems, compilers and architectures are also welcome.

EDIT:
Just to make this clear: I'm not talking about letting exceptions pass through a c-function and then be caught in a calling c++ function or interaction with c or c++ callbacks. The application code itself is pure c. At some point it will call a function of the shared library (which internally is pure c++) and no application code will be called until that function returns. Also let's assume that I have no control over what flags are used to compile the application code.

Upvotes: 11

Views: 649

Answers (2)

nonsensickle
nonsensickle

Reputation: 4528

From what I see about C++ exceptions, in this example which I took from MSDN, GCC seems to include the following assembly in the catch statement:

    call    __cxa_end_catch
    jmp     .L37
    movq    %rax, %rbx
    call    __cxa_end_catch
    movq    %rbx, %rax
    movq    %rax, %rdi
    call    _Unwind_Resume

Which makes use of what I can only assume are C++ library calls to functions that deal with exceptions (e.g. _Unwind_resume). So if the C code links against your library it will have to provide these symbols/functions which means that the code is going to be entering the C++ library to deal with the exceptions.

However, I don't yet know what the C++ library requires in order to do its job. I would expect it to be self contained but I'm not certain of it.

Edit: The answer to this question likely lies in the answers to the following two existing questions (and their interpretation):

  1. How is the C++ exception handling runtime implemented?
  2. How are exceptions implemented under the hood?
  3. How do exceptions work (behind the scenes) in c++

Edit 2: From this answer, it seems that since __cxa_throw uses a table for keeping track of available handlers. I would assume that when the table is exhausted, which in our case occurs when we enter C code, the function would call std::terminate. Hence, the C++ runtime (against which you must have linked) should take care of this for you without you needing to put up a catch all clause.

Since I'm still uncertain I will write up a test of this theory and update the answer with the results.

Upvotes: 1

Potatoswatter
Potatoswatter

Reputation: 137770

If a C++ exception escapes from the outermost try block (which can only be within the outermost C++ function call), then the exception cannot be caught.

An uncaught exception always results in termination by std::terminate, according to the C++ standard, §15.3/9.

Upvotes: 1

Related Questions