Bug 5780 - OpenMP program linked with static libraries gets a runtime memory fault
Summary: OpenMP program linked with static libraries gets a runtime memory fault
Status: RESOLVED DUPLICATE of bug 5784
Alias: None
Product: glibc
Classification: Unclassified
Component: nptl (show other bugs)
Version: 2.4
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-02-21 00:03 UTC by Geir Johansen
Modified: 2016-02-11 21:14 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Geir Johansen 2008-02-21 00:03:27 UTC
Simple OpenMP program will not work when compiled with '-static' option

$ cat test.f90
      use omp_lib
      implicit none
      integer, parameter :: NT = 4
      integer :: nThreads(NT)

      print *, 'Call omp_set_dynamic'
!$    call omp_set_dynamic(.false.)
      print *, 'Call omp_set_num_threads'
!$    call omp_set_num_threads(NT)
      print *, 'Now enter the parallel region'

!$omp parallel default(none) shared(nThreads)
      nThreads(omp_get_thread_num()+1) = omp_get_num_threads()
!$omp end parallel

      print*, nThreads

      END
$ gfortran -fopenmp test.f90
$ ./a.out
 Call omp_set_dynamic
 Call omp_set_num_threads
 Now enter the parallel region
           4           4           4           4
$ gfortran -static -fopenmp test.f90
$ ./a.out
Memory fault
$

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30471 offers the following workaround:

$ gfortran -Wl,--whole-archive -lpthread -Wl,--no-whole-archive -fopenmp -static
test.f90
/usr/lib/../lib64/libpthread.a(sem_open.o): In function `sem_open':
(.text+0x46d): warning: the use of `mktemp' is dangerous, better use `mkstemp'
$ ./a.out
 Call omp_set_dynamic
 Call omp_set_num_threads
 Now enter the parallel region
           4           4           4           4
$

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31604#c5 provides analysis on where
the problem is occurring.
Comment 1 Carlos O'Donell 2008-02-21 14:11:26 UTC

*** This bug has been marked as a duplicate of 5784 ***
Comment 2 H.J. Lu 2012-09-06 23:25:45 UTC
The issue is GCC uses

1. Mark pthread_foo as weak.
2. Use a weak pthread_xxxx to check if thread is active.
3. It does

if (pthread_xxxx)
  pthread_foo ();

With libpthread.a, there is no guarantee that linking in
archive member for pthread_xxxx will also include archive
member for pthread_foo.  When it happens, static executable
crashes when calling pthread_foo.  We can add

if (pthread_foo)
  pthread_foo ();

But it may lead to bad static executable hehavior.  The
list of pthread_foo is

pthread_attr_init
pthread_attr_setdetachstate
pthread_cond_broadcast
pthread_cond_destroy
pthread_cond_init
pthread_cond_signal
pthread_cond_wait
pthread_create
pthread_detach
pthread_exit
pthread_getschedparam
pthread_getspecific
pthread_join
pthread_key_create
pthread_key_delete
pthread_mutex_destroy
pthread_mutex_init
pthread_mutex_lock
pthread_mutex_trylock
pthread_mutex_unlock
pthread_once
pthread_self
pthread_setschedparam
pthread_setspecific
pthread_sigmask
__pthread_key_create
Comment 3 Jackie Rosen 2014-02-16 18:28:58 UTC Comment hidden (spam)
Comment 4 Carlos O'Donell 2016-02-11 21:14:21 UTC
Closing this issue as duplicate of 5784 (since this issue has spam comment we should use 5784 to discuss).

*** This bug has been marked as a duplicate of bug 5784 ***