Skip to content

f2py modules crash if RTLD_GLOBAL in effect (Trac #1148) #1746

Closed

Description

@thouis

Original ticket http://projects.scipy.org/numpy/ticket/1148 on 2009-06-25 by trac user hugh, assigned to unknown.

I was getting seg faults when the l_bfgs_b optimiser (fortran code) in SciPy ran and discovered this ONLY occurred when the library openbabel was loaded. The backtrace seems to suggest that the error is in numpy/core/multiarray.so.

The following code induces the crash:

import openbabel # comment out this line to stop seg fault
import numpy

from scipy.optimize.lbfgsb import fmin_l_bfgs_b

def f(x):
    return x[0]**2 + x[1]**2

def g(x):
    return numpy.array([2*x[0], 2*x[1]])

x0 = numpy.array([3,1])

opt, energy, dict = fmin_l_bfgs_b(f, x0, fprime=g)

print opt

Here is the backtrace:

(gdb) bt
#0  0x00007f93e8635720 in PyArray_API () from /usr/local/lib64/python2.5/site-packages/numpy/core/multiarray.so
#1  0x00007f93e435afa3 in initminpack2 () at /usr/local/lib64/python2.5/site-packages/numpy/core/include/numpy/__multiarray_api.h:983
#2  0x00007f93ea23e2f6 in _PyImport_LoadDynamicModule () from /usr/lib64/libpython2.5.so.1.0
#3  0x00007f93ea23c6e9 in ?? () from /usr/lib64/libpython2.5.so.1.0
#4  0x00007f93ea23cc79 in ?? () from /usr/lib64/libpython2.5.so.1.0
#5  0x00007f93ea23d200 in ?? () from /usr/lib64/libpython2.5.so.1.0
#6  0x00007f93ea23d335 in PyImport_ImportModuleLevel () from /usr/lib64/libpython2.5.so.1.0
#7  0x00007f93ea221c5d in ?? () from /usr/lib64/libpython2.5.so.1.0
#8  0x00007f93ea1ab683 in PyObject_Call () from /usr/lib64/libpython2.5.so.1.0
#9  0x00007f93ea222251 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.5.so.1.0
#10 0x00007f93ea226278 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.5.so.1.0
#11 0x00007f93ea229c4d in PyEval_EvalCodeEx () from /usr/lib64/libpython2.5.so.1.0
#12 0x00007f93ea229e42 in PyEval_EvalCode () from /usr/lib64/libpython2.5.so.1.0
#13 0x00007f93ea23ada0 in PyImport_ExecCodeModuleEx () from /usr/lib64/libpython2.5.so.1.0
#14 0x00007f93ea23b028 in ?? () from /usr/lib64/libpython2.5.so.1.0
#15 0x00007f93ea23c6e9 in ?? () from /usr/lib64/libpython2.5.so.1.0
#16 0x00007f93ea23c982 in ?? () from /usr/lib64/libpython2.5.so.1.0
#17 0x00007f93ea23cfcf in ?? () from /usr/lib64/libpython2.5.so.1.0
#18 0x00007f93ea23d335 in PyImport_ImportModuleLevel () from /usr/lib64/libpython2.5.so.1.0
#19 0x00007f93ea221c5d in ?? () from /usr/lib64/libpython2.5.so.1.0
#20 0x00007f93ea1ab683 in PyObject_Call () from /usr/lib64/libpython2.5.so.1.0
#21 0x00007f93ea222251 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.5.so.1.0
#22 0x00007f93ea226278 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.5.so.1.0
#23 0x00007f93ea229c4d in PyEval_EvalCodeEx () from /usr/lib64/libpython2.5.so.1.0
#24 0x00007f93ea229e42 in PyEval_EvalCode () from /usr/lib64/libpython2.5.so.1.0
#25 0x00007f93ea23ada0 in PyImport_ExecCodeModuleEx () from /usr/lib64/libpython2.5.so.1.0
#26 0x00007f93ea23b028 in ?? () from /usr/lib64/libpython2.5.so.1.0
#27 0x00007f93ea23c6e9 in ?? () from /usr/lib64/libpython2.5.so.1.0
#28 0x00007f93ea23c982 in ?? () from /usr/lib64/libpython2.5.so.1.0
#29 0x00007f93ea23cfcf in ?? () from /usr/lib64/libpython2.5.so.1.0
#30 0x00007f93ea23d335 in PyImport_ImportModuleLevel () from /usr/lib64/libpython2.5.so.1.0
#31 0x00007f93ea221c5d in ?? () from /usr/lib64/libpython2.5.so.1.0
#32 0x00007f93ea1ab683 in PyObject_Call () from /usr/lib64/libpython2.5.so.1.0
#33 0x00007f93ea222251 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.5.so.1.0
#34 0x00007f93ea226278 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.5.so.1.0
#35 0x00007f93ea229c4d in PyEval_EvalCodeEx () from /usr/lib64/libpython2.5.so.1.0
#36 0x00007f93ea229e42 in PyEval_EvalCode () from /usr/lib64/libpython2.5.so.1.0
#37 0x00007f93ea23ada0 in PyImport_ExecCodeModuleEx () from /usr/lib64/libpython2.5.so.1.0
#38 0x00007f93ea23b028 in ?? () from /usr/lib64/libpython2.5.so.1.0
#39 0x00007f93ea23bfa7 in ?? () from /usr/lib64/libpython2.5.so.1.0
#40 0x00007f93ea23c6e9 in ?? () from /usr/lib64/libpython2.5.so.1.0
#41 0x00007f93ea23c982 in ?? () from /usr/lib64/libpython2.5.so.1.0
#42 0x00007f93ea23d016 in ?? () from /usr/lib64/libpython2.5.so.1.0
#43 0x00007f93ea23d335 in PyImport_ImportModuleLevel () from /usr/lib64/libpython2.5.so.1.0
#44 0x00007f93ea221c5d in ?? () from /usr/lib64/libpython2.5.so.1.0
#45 0x00007f93ea1ab683 in PyObject_Call () from /usr/lib64/libpython2.5.so.1.0
#46 0x00007f93ea222251 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.5.so.1.0
#47 0x00007f93ea226278 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.5.so.1.0
#48 0x00007f93ea229c4d in PyEval_EvalCodeEx () from /usr/lib64/libpython2.5.so.1.0
#49 0x00007f93ea229e42 in PyEval_EvalCode () from /usr/lib64/libpython2.5.so.1.0
#50 0x00007f93ea23ada0 in PyImport_ExecCodeModuleEx () from /usr/lib64/libpython2.5.so.1.0
#51 0x00007f93ea23b028 in ?? () from /usr/lib64/libpython2.5.so.1.0
#52 0x00007f93ea23c6e9 in ?? () from /usr/lib64/libpython2.5.so.1.0
#53 0x00007f93ea23c982 in ?? () from /usr/lib64/libpython2.5.so.1.0
#54 0x00007f93ea23cfcf in ?? () from /usr/lib64/libpython2.5.so.1.0
#55 0x00007f93ea23d335 in PyImport_ImportModuleLevel () from /usr/lib64/libpython2.5.so.1.0
#56 0x00007f93ea221c5d in ?? () from /usr/lib64/libpython2.5.so.1.0
#57 0x00007f93ea1ab683 in PyObject_Call () from /usr/lib64/libpython2.5.so.1.0
#58 0x00007f93ea222251 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.5.so.1.0
#59 0x00007f93ea226278 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.5.so.1.0
#60 0x00007f93ea229c4d in PyEval_EvalCodeEx () from /usr/lib64/libpython2.5.so.1.0
---Type <return> to continue, or q <return> to quit---
#61 0x00007f93ea229e42 in PyEval_EvalCode () from /usr/lib64/libpython2.5.so.1.0
#62 0x00007f93ea2447a1 in ?? () from /usr/lib64/libpython2.5.so.1.0
#63 0x00007f93ea24499c in PyRun_StringFlags () from /usr/lib64/libpython2.5.so.1.0
#64 0x00007f93ea245c10 in PyRun_SimpleStringFlags () from /usr/lib64/libpython2.5.so.1.0
#65 0x00007f93ea24f06a in Py_Main () from /usr/lib64/libpython2.5.so.1.0
#66 0x00007f93e95b8436 in __libc_start_main () from /lib64/libc.so.6
#67 0x00000000004006c9 in _start ()

Activity

  1. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @pv wrote on 2009-07-13

    The problem seems to be due to f2py.

    Minimal example:

    $ cat foomodule.c 
    
    #include "Python.h"
    
    /* f2py/src/fortranobject.h  includes arrayobject like this: */
    #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API
    #include "numpy/arrayobject.h"
    
    static PyMethodDef methods[] = {{NULL,NULL}};
    
    PyMODINIT_FUNC initfoo(void) {
        PyObject *m;
        m = Py_InitModule("foo", methods);
        import_array();
    }
    
    $ export PP=/usr/lib/python2.6/dist-packages/numpy
    $ gcc -I$PP/core/include -I/usr/include/python2.6 -I$PP/f2py/src -c -o foomodule.o foomodule.c 
    $ gcc -shared -DFORTRANOBJECT_C foomodule.o -o foo.so
    $ python -c 'import foo, openbabel'
    [no crash]
    $ python -c 'import openbabel, foo'
    Segmentation fault (core dumped)
    

    If the #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API is left out, there is no crash. What it does, is making the PyArray_API symbol public, cf. objdump -T foo.so:

    foo.so:     file format elf32-i386
    
    DYNAMIC SYMBOL TABLE:
    00000000      D  *UND*  00000000              PyExc_ImportError
    00000000  w   D  *UND*  00000000              __gmon_start__
    00000000  w   D  *UND*  00000000              _Jv_RegisterClasses
    00000000      D  *UND*  00000000              PyErr_Format
    00000000      D  *UND*  00000000              PyCObject_AsVoidPtr
    00000000      D  *UND*  00000000              PyExc_RuntimeError
    00000000      D  *UND*  00000000              PyObject_GetAttrString
    00000000      D  *UND*  00000000              PyErr_Print
    00000000      D  *UND*  00000000              Py_InitModule4
    00000000      D  *UND*  00000000              PyImport_ImportModule
    00000000      D  *UND*  00000000              PyErr_SetString
    00000000      D  *UND*  00000000              PyCObject_Type
    00000000  w   DF *UND*  00000000  GLIBC_2.1.3 __cxa_finalize
    000007b6 g    DF .text  0000005a  Base        initfoo
    00002024 g    DO .bss   00000004  Base        PyArray_API    <------------------- !!
    00002028 g    D  *ABS*  00000000  Base        _end
    0000200c g    D  *ABS*  00000000  Base        _edata
    0000200c g    D  *ABS*  00000000  Base        __bss_start
    0000054c g    DF .init  00000000  Base        _init
    00000848 g    DF .fini  00000000  Base        _fini
    

    Apparently, this is needed for fortranobject.o to access the symbols?

    Openbabel Python bindings call

    sys.setdlopenflags(sys.getdlopenflags() | dl.RTLD_GLOBAL)
    

    Obviously, this will causes problems down the stream, as symbols of subsequently loaded libraries are no longer isolated...

    You can also reproduce the crash without Openbabel,

    python -c 'import sys, dl; sys.setdlopenflags(sys.getdlopenflags() | dl.RTLD_GLOBAL); import foo'
    

    This behavior of Openbabel's is highly suspect IMHO. If it needs to enable this flag, it probably ought to also restore its value after loading whatever needs loading.

    I think you should bring this issue up with Openbabel developers, as it doesn't look like this is a real bug in Numpy or Scipy.

  2. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Milestone changed to 1.4.0 by @pv on 2009-07-13

  3. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Title changed from l_bfgs_b optimiser seg faults in numpy/core/multiarray.so if library openbabel is loaded to f2py modules crash if RTLD_GLOBAL in effect by @pv on 2009-07-13

  4. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @pv wrote on 2009-07-13

    Closing as wontfix, as I believe the problem is in Openbabel.

    Reopen if Openbabel developers disagree, or there is strong evidence that we ought not rely on RTLD_GLOBAL not being enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions