Cython GIL SIGSEGV

时间:2014-04-20 01:02:15

标签: cython ld-preload

在尝试通过Cython中的LD_PRELOAD拦截调用时,我遇到了分段违规。我不明白为什么会这样?

"""An experiment in shimming from Cython / Python."""

cdef extern from "dlfcn.h":
    void* dlsym(void*, char*)
    void* RTLD_NEXT

cdef extern int execvp(const char *file, char *const argv[]) with gil:
    print "Intercepted lookup of %r" % file
    libc_execvp = dlsym(RTLD_NEXT, "execvp")
    if libc_execvp:
        with nogil:
            return (<int(*)(const char*, char * const *) nogil>libc_execvp)(file, argv)
    return -1

带有示例测试用例的项目位于https://github.com/CraigJPerry/pyshim/blob/master/pyshim/pyshim.pyx

我相信python运行时可能没有正确初始化,这是我的问题的根源?

[craig@d1 pyshim](master)$ gdb env
..
Reading symbols from /usr/bin/env...Reading symbols from /usr/bin/env...(no debugging symbols found)...done.
(gdb) set environment LD_PRELOAD=pyshim/pyshim.so
(gdb) set args echo
(gdb) run
Starting program: /usr/bin/env echo
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x000000396ee0ddb0 in sem_wait () from /lib64/libpthread.so.0
(gdb) bt
#0  0x000000396ee0ddb0 in sem_wait () from /lib64/libpthread.so.0
#1  0x0000003bbcf0c7b5 in PyThread_acquire_lock ()
   from /lib64/libpython2.7.so.1.0
#2  0x0000003bbcefad80 in ?? () from /lib64/libpython2.7.so.1.0
#3  0x0000003bbcefb62c in PyGILState_Ensure () from /lib64/libpython2.7.so.1.0
#4  0x00007ffff7df9519 in execvp (__pyx_v_file=0x7fffffffe85b "echo", 
    __pyx_v_argv=0x7fffffffe4d0) at pyshim/pyshim.c:681
#5  0x0000000000401a82 in main ()

1 个答案:

答案 0 :(得分:2)

Cython假设您正在运行一个功能性Python解释器(即您正在编写扩展模块)。在这种情况下,您嵌入 Python,而不是扩展它。所以你需要做一些额外的工作来初始化所有东西。

幸运的是,这不是一个具有挑战性的问题:

"""An experiment in shimming from Cython / Python."""

cdef extern from "dlfcn.h":
    void* dlsym(void*, char*)
    void* RTLD_NEXT

cdef extern from "Python.h":
    void Py_Initialize() nogil

cdef extern void initpyshim()

cdef extern int execvp(const char *file, char *const argv[]) nogil: # note nogil here
    Py_Initialize() # initialize Python
    with gil:
        initpyshim() # initialize containing module
        print "Intercepted lookup of %r" % file
        libc_execvp = dlsym(RTLD_NEXT, "execvp")
        if libc_execvp:
            with nogil:
                return (<int(*)(const char*, char * const *) nogil>libc_execvp)(file, argv)
        return -1

如果您的功能可能被多次调用,您可能希望避免重新初始化您的模块(您可以通过选中Py_IsInitialized()来执行此操作)。在离开您的方法之前,您可能还需要致电Py_Finalize()

如果您的目标是Python 3,则init方法称为PyInit_<modname>并返回一个PyObject *引用,您需要保留该引用直到方法结束(至少)。< / p>