[Bug c++/65485] New: Use openmp in dynamic library but not in calling program causes segfault at the end of execution

2015-03-20 Thread thomas.izard at silkan dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65485

Bug ID: 65485
   Summary: Use openmp in dynamic library but not in calling
program causes segfault at the end of execution
   Product: gcc
   Version: 4.9.2
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: thomas.izard at silkan dot com

Consider:
#include "omp.h"
#include 
extern "C" {
int test() { 
  int N = omp_get_max_threads();
#pragma omp parallel num_threads(N)
  {
std::cout << omp_get_thread_num() << std::endl;
  }
  return 0;
}
};

Compiled with g++ -fopenmp -shared -fPIC -Wall -Wextra -o shared.so shared.cpp
(no warnings/no errors).

and :

#include 
#include 
int main() {
  void* handle = dlopen("./shared.so", RTLD_NOW);
  if (!handle) {
std::cerr << "can not open shared.so" << std::endl;
return 1;
  }
  int(*f)() =  (int(*)()) dlsym(handle,"test");
  if (!f) {
std::cerr << "can not find 'test' symbol in shared.so" << std::endl;
return 1;
  }
  (*f)();
  if (dlclose(handle)) {
std::cerr << "can not close shared.so" << std::endl;
return 1;
  }  
  return 0;
}

compiled with:
 g++ -Wall -Wextra -o main main.cpp -ldl (no warnings/no errors).

The main program loads the dynamic library and calls the "test" function. It
does not directly use openmp.

The problem is that a segmentation fault happen at the very end of the program.
According to valgrind, at this point some threads are still active.

Adding a -fopenmp flag while compiling the main program does not fix the
problem. It seems that in this case the compiled code is not linked to GOMP
(confirmed by ldd).

The only workaround is to actually add an OpenMP directive (or a call to an
OpenMP runtime function) in the main.cpp file. 

Same issue with (at least): g++-4.6.4, g++-4.7, g++-4.8.2

Systems used : 
Linux 3.2.0-77-generic #114-Ubuntu SMP (Ubuntu 64 12.04)
Linux 3.13.0-43-generic #72-Ubuntu SMP (Ubuntu 64 server 14.04)

Thomas


[Bug c++/65485] Use openmp in dynamic library but not in calling program causes segfault at the end of execution

2015-03-20 Thread thomas.izard at silkan dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65485

--- Comment #2 from thomas.izard at silkan dot com ---
(In reply to Jakub Jelinek from comment #1)
> Can't reproduce, works just fine here (Fedora 21, x86_64).

Do you launch the program several times ? Some executions are correct (program
does not segfault everytime). I think it depends on the order the threads are
killed.