Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importing "ctypes" immediately causes a segmentation fault #79531

Closed
n0s69z mannequin opened this issue Nov 29, 2018 · 4 comments
Closed

importing "ctypes" immediately causes a segmentation fault #79531

n0s69z mannequin opened this issue Nov 29, 2018 · 4 comments
Labels
topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@n0s69z
Copy link
Mannequin

n0s69z mannequin commented Nov 29, 2018

BPO 35350
Nosy @tiran
Files
  • strace_python.log
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-07-10.08:38:07.944>
    created_at = <Date 2018-11-29.15:17:47.708>
    labels = ['ctypes', 'type-crash']
    title = 'importing "ctypes" immediately causes a segmentation fault'
    updated_at = <Date 2020-07-10.08:38:07.943>
    user = 'https://bugs.python.org/n0s69z'

    bugs.python.org fields:

    activity = <Date 2020-07-10.08:38:07.943>
    actor = 'christian.heimes'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-07-10.08:38:07.944>
    closer = 'christian.heimes'
    components = ['ctypes']
    creation = <Date 2018-11-29.15:17:47.708>
    creator = 'n0s69z'
    dependencies = []
    files = ['47954']
    hgrepos = []
    issue_num = 35350
    keywords = []
    message_count = 4.0
    messages = ['330696', '330762', '330786', '373453']
    nosy_count = 2.0
    nosy_names = ['christian.heimes', 'n0s69z']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue35350'
    versions = ['Python 2.7']

    @n0s69z
    Copy link
    Mannequin Author

    n0s69z mannequin commented Nov 29, 2018

    ~Environment

    Cross compiled Python 2.7.15 for ARM Cortex-A7 target, Linux Kernel 4.18

    uname -a: Linux Test-0002 4.18.13 #1 SMP Wed Oct 31 11:20:07 CET 2018 armv7l GNU/Linux

    ~Description of the problem

    Importing the "ctypes" module in order to load shared libraries causes a segmentation fault:

    root [ /tmpfs/root ] $ python
    Python 2.7.15 (default, Nov 29 2018, 13:57:56)
    [GCC 8.2.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import ctypes
    Segmentation fault

    I have found a similiar issue here:
    https://bugs.python.org/issue11048
    But the changes are already applied in 2.7.15.

    Here is the GDB output similiar to the link I posted:

    (gdb) file python2.7
    Reading symbols from python2.7...done.
    (gdb) run -c "import ctypes"
    Starting program: /usr/bin/python2.7 -c "import ctypes"
    warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.

    Program received signal SIGSEGV, Segmentation fault.
    0x76a4fa94 in CThunkObject_dealloc (_self=0x76adb920) at /home/user/ARM_Linux/src/Python-2.7.15/Modules/_ctypes/callbacks.c:25
    25 /home/user/ARM_Linux/src/Python-2.7.15/Modules/_ctypes/callbacks.c: No such file or directory.
    (gdb)

    From what I can see it tries to use the path from the host I cross compiled for the callbacks.c. Is this the cause of the segmentation fault? If yes, how can I correct the path during compilation?

    I also attached the strace log of the command 'python -c "import ctypes"'

    Thank you in advance!

    @n0s69z n0s69z mannequin added topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump labels Nov 29, 2018
    @n0s69z
    Copy link
    Mannequin Author

    n0s69z mannequin commented Nov 30, 2018

    Small update:

    After commenting out Py_XDECREF(self->restype) in function CThunkObject_dealloc(PyObject *_self), I can import ctypes without getting a segmentation fault.

    static void
    CThunkObject_dealloc(PyObject *_self)
    {
        CThunkObject *self = (CThunkObject *)_self;
        PyObject_GC_UnTrack(self);
        Py_XDECREF(self->converters);
        Py_XDECREF(self->callable);
        //Py_XDECREF(self->restype);
        if (self->pcl_write)
            ffi_closure_free(self->pcl_write);
        PyObject_GC_Del(self);
    }

    But I'm afraid I don't know what other effects could result with this change.

    @n0s69z
    Copy link
    Mannequin Author

    n0s69z mannequin commented Nov 30, 2018

    Another small update:

    After I recompiled Python with the commented out statement, I did a small test if loading a shared library works.

    I compiled the following test function to testib.so:

    #include <stdio.h>
    
    void test_func(void);
    
    void test_func(void) {
    	printf("hello world\n");
    }

    After that I used ctypes to load this library and execute the test_func():

    (gdb) file python2.7
    Reading symbols from python2.7...done.
    (gdb) run -c "import ctypes; lib_test = ctypes.cdll.LoadLibrary('/tmp/testlib.so'); lib_test.test_func();"
    Starting program: /usr/bin/python2.7 -c "import ctypes; lib_test = ctypes.cdll.LoadLibrary('/tmp/testlib.so'); lib_test.test_func();"
    warning: Unable to find libthread_db matching inferior's thread library, thread debugging will not be available.
    hello world

    Program received signal SIGSEGV, Segmentation fault.
    PyCFuncPtr_call (self=<optimized out>, inargs=<optimized out>, kwds=<optimized out>) at /home/user/ARM_Linux/src/Python-2.7.15/Modules/_ctypes/_ctypes.c:4108
    4108 /home/user/ARM_Linux/src/Python-2.7.15/Modules/_ctypes/_ctypes.c: No such file or directory.
    (gdb)

    It prints the expected output, but again I get a segmentation fault, this time in PyCFuncPtr_call function.

    @tiran
    Copy link
    Member

    tiran commented Jul 10, 2020

    Python 2.7 has reached its end of lifetime. Please re-open the bug if you can reproduce the issue on Python 3.8 or newer.

    @tiran tiran closed this as completed Jul 10, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant