Thanks in advance for reading this question. Our program encountered a strange issue, the "dlsym" throws a "segment fault" issue sporadically. The background is, our engine loads multiple share libraries into a single process, and stores this libraries's handle which got from "dlopen" into a map,

 void* handler = dlopen(libraryPath, RTLD_NOW);
if (handler != NULL) {
  handlerStore[libraryPath] = handler;
} else {
  throw exception
}

then later call the functions in these library with below code

void* handler = handlerStore[toStr(libraryPath)];
typedef int (*function)(); // the function to be called
dlerror(); /**clear error code*/

function f = (function) dlsym(handler, functionName);
char *error = dlerror();
if (error != NULL || f == NULL) {
  throw exception
}

int state_out = 0;
if (f != NULL) {
  try{
    state_out = (*f)();
  }catch (...){
    throw exception
  }

The strange behavior is, it works most of the time, but occasionally throws an segment false error with below stack trace.

/lib64/libc.so.6(+0x35270)[0x7f807d4c0270]
/lib64/ld-linux-x86-64.so.2(+0x94b0)[0x7f80808fd4b0]
/lib64/ld-linux-x86-64.so.2(+0x9cfb)[0x7f80808fdcfb]
/lib64/ld-linux-x86-64.so.2(+0x9fdf)[0x7f80808fdfdf]
/lib64/libc.so.6(+0x133e29)[0x7f807d5bee29]
/lib64/libdl.so.2(+0x10d4)[0x7f807c3420d4]
/lib64/ld-linux-x86-64.so.2(+0xf314)[0x7f8080903314]
/lib64/libdl.so.2(+0x15bd)[0x7f807c3425bd]
/lib64/libdl.so.2(dlsym+0x48)[0x7f807c342128]

We are currently struggling on it and really appreciated you can give us some direction

Thanks

1 Answers

0
Employed Russian On

"dlsym" throws a "segment fault" issue sporadically.

Since your program uses exceptions, you should use the term throw more carefully: dlsym is implemented in pure-C, and does not throw anything.

But it raises SIGSEGV signal.

The most likely reason for dlsym to raise SIGSEGV is heap corruption, and the best tools for catching such corruption are Address Sanitizer (build with -fsanitize=address if using recent GCC or Clang), or Valgrind.