Skip to content

ctypes mishandles void return type #91353

Closed

Description

@hoodmane
mannequin
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']

    Activity

    1. hoodmane commented on Apr 2, 2022

      hoodmanemannequin
      MannequinAuthor

      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);

    2. added
      testsTests in the Lib/test dir
      3.11only security fixes
      type-bugAn unexpected behavior, bug, or error
      on Apr 3, 2022
    3. transferred this issue fromon Apr 10, 2022
    4. added a commit that references this issue on Apr 14, 2022
    5. corona10 commented on Jul 12, 2022

      @corona10
      Member

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

    6. tiran commented on Jul 12, 2022

      @tiran
      Member

      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

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.11only security fixestestsTests in the Lib/test dirtopic-ctypestype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions