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

ctypes mishandles void return type #91353

Closed
hoodmane mannequin opened this issue Apr 2, 2022 · 3 comments
Closed

ctypes mishandles void return type #91353

hoodmane mannequin opened this issue Apr 2, 2022 · 3 comments
Labels
3.11 only security fixes tests Tests in the Lib/test dir topic-ctypes type-bug An unexpected behavior, bug, or error

Comments

@hoodmane
Copy link
Mannequin

hoodmane mannequin commented Apr 2, 2022

BPO 47197
Nosy @amauryfa, @abalkin, @tiran, @meadori, @hoodmane, @hoodmane
PRs
  • gh-91353: Fix void return type handling in ctypes (GH-32246) #32246
  • 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 = None
    created_at = <Date 2022-04-02.01:14:09.315>
    labels = ['ctypes', 'type-bug', 'tests', '3.11']
    title = 'ctypes mishandles `void` return type'
    updated_at = <Date 2022-04-03.21:02:00.133>
    user = 'https://github.com/hoodmane'

    bugs.python.org fields:

    activity = <Date 2022-04-03.21:02:00.133>
    actor = 'christian.heimes'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Tests', 'ctypes']
    creation = <Date 2022-04-02.01:14:09.315>
    creator = 'hoodchatham'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 47197
    keywords = ['patch']
    message_count = 1.0
    messages = ['416526']
    nosy_count = 6.0
    nosy_names = ['amaury.forgeotdarc', 'belopolsky', 'christian.heimes', 'meador.inge', 'hoodmane', 'hoodchatham']
    pr_nums = ['32246']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue47197'
    versions = ['Python 3.11']

    @hoodmane
    Copy link
    Mannequin Author

    hoodmane mannequin commented Apr 2, 2022

    On wasm targets, test_code fails. It's because of a bad function pointer cast. The problematic code is as follows:

    import ctypes
    
    py = ctypes.pythonapi
    freefunc = ctypes.CFUNCTYPE(None, ctypes.c_voidp)
    
    RequestCodeExtraIndex = py._PyEval_RequestCodeExtraIndex
    RequestCodeExtraIndex.argtypes = (freefunc,)
    RequestCodeExtraIndex.restype = ctypes.c_ssize_t
    
    SetExtra = py._PyCode_SetExtra
    SetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t, ctypes.c_voidp)
    SetExtra.restype = ctypes.c_int
    
    LAST_FREED = None
    def myfree(ptr):
        global LAST_FREED
        LAST_FREED = ptr
    
    FREE_FUNC = freefunc(myfree)
    FREE_INDEX = RequestCodeExtraIndex(FREE_FUNC)
    
    
    # Verify that the provided free function gets invoked
    # when the code object is cleaned up.
    f = eval('lambda:42')
    
    SetExtra(f.__code__, FREE_INDEX, ctypes.c_voidp(100))
    del f # crashes!!

    The problem: freefunc = ctypes.CFUNCTYPE(None, ctypes.c_voidp) declares a function with signature int f(void*). However, the definition of freefunc is void f(void*). These function signatures do not agree, and trying to make the call crashes.

    Due to a bug(?) ctypes can produce closures that have void return type but it can't produce simple functions with void return type.

    Closures code: checks if restype == Py_None and if so gives the callback a void return type
    https://github.com/python/cpython/blob/main/Modules/_ctypes/callbacks.c#L381

    Direct call code: calls _ctypes_get_ffi_type which never returns ffi_type_void:

    rtype = _ctypes_get_ffi_type(restype);

    @hoodmane hoodmane mannequin added topic-ctypes labels Apr 2, 2022
    @tiran tiran added tests Tests in the Lib/test dir 3.11 only security fixes type-bug An unexpected behavior, bug, or error labels Apr 3, 2022
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @corona10
    Copy link
    Member

    I think that we can close this issue. Please feel free to reopen this issue if needed.

    @tiran
    Copy link
    Member

    tiran commented Jul 12, 2022

    Yeah, the issue is fixed in 3.11 and main branches.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes tests Tests in the Lib/test dir topic-ctypes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants