2

I had a recent problem where a thrown C++ exception was not caught and instead leads to program termination.

My current MWE is:

#include <stdexcept>
#include <cupti.h>
void foo(){
  CUpti_SubscriberHandle subscriber;
  cuptiSubscribe(&subscriber, (CUpti_CallbackFunc) 0, 0);
}
int main(){
  try{
    throw std::runtime_error("Hello");
  }catch(...){
  }
}

Compiling with g++ testRaise.cpp /sw/installed/CUDAcore/11.1.1/extras/CUPTI/lib64/libcupti_static.a -ldl -lpthread -lrt and running leads to terminate called after throwing an instance of 'std::runtime_error' which I can't explain.

What I found so far ("works"="exception is caught"):

As CUPTI is proprietary I don't know how that was build, but I fail to see how linking a static library can break the C++ exception handling/unwinding mechanism

Does anyone know, what could cause this, how it can be checked/verified and avoided?

Flamefire
  • 5,313
  • 3
  • 35
  • 70
  • The static library has objects in global scope whose constructors are buggy and are causing memory corruption even before you land in `main`? Perhaps a static initialization order fiasco, that the gold linker exposes? – Sam Varshavchik May 11 '21 at 11:04
  • Or: The static libraries has objects whose destructors are called *after* `main()` has returned. – tofro May 11 '21 at 11:08
  • Put some output in the `catch` block to check if it's being entered. – Richard Critten May 11 '21 at 11:24
  • It could have ctors or lib initializers, but not sure how they can interfere with unwinding. @tofro Those don't matter as the bug happens at the throw, i.e. before main is left RichardCritten I had that but not needed. It either works, i.e. catch is entered and exit with 0 or it breaks, then catch is not entered and exit with !=0 (terminate called...) – Flamefire May 11 '21 at 14:28
  • It's possible that there is a bug in `ld.gold` What do you get with `ld.lld`? – Employed Russian Jun 22 '21 at 15:57

0 Answers0