From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29969 invoked by alias); 20 Mar 2015 08:07:57 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 29955 invoked by uid 48); 20 Mar 2015 08:07:53 -0000 From: "thomas.izard at silkan dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/65485] New: Use openmp in dynamic library but not in calling program causes segfault at the end of execution Date: Fri, 20 Mar 2015 08:58:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 4.9.2 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: thomas.izard at silkan dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-03/txt/msg02055.txt.bz2 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