'Make assembly function exception safe
My program is written in C++. The program generates assembly functions at runtime (with asmjit library). The assembly functions may call other C++ functions which my throw exceptions.
My question is how to make C++ exception safely propagate through my function. The platform is x86_64/Linux.
I did some test:
C++ file:
#include <stdexcept>
#include <iostream>
extern "C" void asm_function();
extern "C" void throws_exception() {
throw std::runtime_error("exception incoming.");
}
int main() {
try {
asm_function();
std::cout << "Whoops, asm_function returned unexpectedly\n";
} catch (std::exception& e) {
std::cout << "Got exception: " << e.what() << std::endl;
}
return 0;
}
If the assembly function is like this:
.global asm_function
asm_function:
push %rbp
mov %rsp, %rbp
call throws_exception
mov %rbp, %rsp
pop %rbp
ret
The program crashes, with error message like this:
terminate called after throwing an instance of 'std::runtime_error'
what(): exception incoming.
Aborted (core dumped)
If the assembly function is as simple as a "jmp" instruction, the test function works, but my program cannot be so simple.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
