简体   繁体   中英

Barebones SWIG python C interface segfaulting on OSX+clang, not in Linux+gcc

I've noticed a couple of similar questions here in stackoverflow.com, but unfortunately they don't solve my problem at hand:

SWIG & C/C++ Python API connected - SEGFAULT

how to investigate python2 segfault on imp.load_module

I'm trying to build a barebones C-python SWIG interface that works on both OSX (w/ clang) and Linux (w/ gcc) but I'm failing at doing so on OSX.

Please, clone the following repo and test locally:

https://github.com/brainstorm/swig-python-skel

Here's what happens when running make on my Macbook Air with latest developer tools and clang:

$ uname -a
Darwin mba 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun  3 21:27:35 PDT 2014; root:xnu-          2422.110.17~1/RELEASE_X86_64 x86_64

$ cc -v
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix

$ make
cc -O3 -Wall -g -fPIC  -c 'example'.c -o _'example'.so
swig -Wall -python -builtin -module 'example' -o 'example'_wrap.c 'example'.i
cc -O3 -Wall -g -fPIC -I/Users/roman/.anaconda/anaconda/include/python2.7 -I/Users/roman/.anaconda/anaconda/include/python2.7 -c 'example'_wrap.c
cc -shared -undefined dynamic_lookup -O3 -Wall -g -fPIC -I/Users/roman/.anaconda/anaconda/include/python2.7 -I/Users/roman/.anaconda/anaconda/include/python2.7 -o _'example'.so 'example'_wrap.o -lpython
python -c 'import 'example'' && echo "Python build is ok!"
/bin/sh: line 1: 11917 Segmentation fault: 11  (core dumped) python -c 'import 'example''
make: *** [test-python] Error 139

The resulting backtrace from the generate core file in OSX (after the obligatory ulimit -c unlimited to be able to generate core files):

$ lldb -c /cores/core.11917
(lldb) bt
* thread #1: tid = 0x0000, 0x0000000000000000, stop reason = signal SIGSTOP
  * frame #0: 0x0000000000000000
    frame #1: 0x000000010068637d Python`PyEval_GetGlobals + 23
    frame #2: 0x000000010064b9ed Python`___lldb_unnamed_function1014$$Python + 954
    frame #3: 0x000000010064b0f9 Python`___lldb_unnamed_function1007$$Python + 50
    frame #4: 0x00000001002f496b _example.so`init_example + 75 at example_wrap.c:4117
    frame #5: 0x00000001000e5082 libpython2.7.dylib`_PyImport_LoadDynamicModule + 178
    frame #6: 0x00000001000e499a libpython2.7.dylib`imp_load_module + 186
    frame #7: 0x00000001000c8f49 libpython2.7.dylib`PyEval_EvalFrameEx + 24313
    frame #8: 0x00000001000c983b libpython2.7.dylib`PyEval_EvalFrameEx + 26603
    frame #9: 0x00000001000cad35 libpython2.7.dylib`PyEval_EvalCodeEx + 1733
    frame #10: 0x00000001000cb036 libpython2.7.dylib`PyEval_EvalCode + 54
    frame #11: 0x00000001000e17b1 libpython2.7.dylib`PyImport_ExecCodeModuleEx + 209
    frame #12: 0x00000001000e2502 libpython2.7.dylib`load_source_module + 626
    frame #13: 0x00000001000e2f66 libpython2.7.dylib`import_submodule + 310
    frame #14: 0x00000001000e31ba libpython2.7.dylib`load_next + 234
    frame #15: 0x00000001000e3c45 libpython2.7.dylib`PyImport_ImportModuleLevel + 581
    frame #16: 0x00000001000bc573 libpython2.7.dylib`builtin___import__ + 131
    frame #17: 0x000000010000d382 libpython2.7.dylib`PyObject_Call + 98
    frame #18: 0x00000001000c1fd7 libpython2.7.dylib`PyEval_CallObjectWithKeywords + 87
    frame #19: 0x00000001000c556e libpython2.7.dylib`PyEval_EvalFrameEx + 9502
    frame #20: 0x00000001000cad35 libpython2.7.dylib`PyEval_EvalCodeEx + 1733
    frame #21: 0x00000001000cb036 libpython2.7.dylib`PyEval_EvalCode + 54
    frame #22: 0x00000001000f0db8 libpython2.7.dylib`PyRun_StringFlags + 280
    frame #23: 0x00000001000f0ea1 libpython2.7.dylib`PyRun_SimpleStringFlags + 65
    frame #24: 0x0000000100107dc2 libpython2.7.dylib`Py_Main + 1586
    frame #25: 0x0000000100000f54 python`start + 52
(lldb)

On the other hand, on a regular x86_64 Linux machine, same code and no problems at all:

$ uname -a
Linux milou 2.6.32-431.20.3.el6.x86_64 #1 SMP Thu Jun 19 14:01:59 CDT 2014    x86_64 x86_64 x86_64 GNU/Linux

$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

$ make
cc -O3 -Wall -g -fPIC  -c 'example'.c -o _'example'.so
swig -Wall -python -builtin -module 'example' -o 'example'_wrap.c 'example'.i
cc -O3 -Wall -g -fPIC -I/home/roman/.pyenv/versions/2.7.6/include/python2.7 -I/home/roman/.pyenv/versions/2.7.6/include/python2.7 -c 'example'_wrap.c
cc -pthread -shared -Wl,-O1 -O3 -Wall -g -fPIC -o _'example'.so 'example'_wrap.o
python -c 'import 'example'' && echo "Python build is ok!"
Python build is ok!

I'm running conda 3.4.1 on both systems as a virtualenv system.

Interestingly, the following SWIG-backed projects also segfault in my OSX machine:

https://github.com/ilveroluca/rapi/blob/master/pyrapi/Makefile

https://github.com/brainstorm/facs/blob/swig/facs/Makefile

But not this one (works both on OSX and Linux), for instance:

https://github.com/PacificBiosciences/ConsensusCore

Any ideas? Can you reproduce this on your machines?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM