Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of c++ exceptions escaping into c program

Tags:

c++

c

exception

gcc

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.

like image 452
MikeMB Avatar asked Feb 06 '17 11:02

MikeMB


People also ask

What are exceptions in C?

Master C and Embedded C Programming- Learn as you go A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another.

Can we handle exception in C?

The C programming language does not support exception handling nor error handling.

Is there a try except in C?

The try-except statement is a Microsoft extension to the C language that enables applications to gain control of a program when events that normally terminate execution occur. Such events are called exceptions, and the mechanism that deals with exceptions is called structured exception handling.

How is try catch implemented in C++?

C++ try and catch: The throw keyword throws an exception when a problem is detected, which lets us create a custom error. The catch statement allows you to define a block of code to be executed if an error occurs in the try block.


2 Answers

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.

like image 200
nonsensickle Avatar answered Oct 10 '22 20:10

nonsensickle


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.

like image 29
Potatoswatter Avatar answered Oct 10 '22 19:10

Potatoswatter