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 on Windows: error calling C function that returns a struct containing 3 bools #78784

Closed
mattneri mannequin opened this issue Sep 7, 2018 · 11 comments
Closed

ctypes on Windows: error calling C function that returns a struct containing 3 bools #78784

mattneri mannequin opened this issue Sep 7, 2018 · 11 comments
Labels
3.7 (EOL) end of life topic-ctypes type-bug An unexpected behavior, bug, or error

Comments

@mattneri
Copy link
Mannequin

mattneri mannequin commented Sep 7, 2018

BPO 34603
Nosy @amauryfa, @abalkin, @vstinner, @meadori, @serhiy-storchaka, @miss-islington, @vladima
PRs
  • bpo-34603: Fix returning structs from functions #9258
  • [3.7] bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258) #9340
  • [3.6] bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258) #9341
  • [2.7] bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258) #9342
  • [2.7] bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258) #9425
  • Use assertEqual() instead of assertEquals(). #9721
  • [3.7] Use assertEqual() instead of assertEquals(). (GH-9721) #9725
  • [3.6] Use assertEqual() instead of assertEquals(). (GH-9721) (GH-9725) #9727
  • Files
  • MarshallingTest.zip
  • 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 2018-09-19.20:49:26.494>
    created_at = <Date 2018-09-07.11:15:17.826>
    labels = ['ctypes', 'type-bug', '3.7']
    title = 'ctypes on Windows: error calling C function that returns a struct containing 3 bools'
    updated_at = <Date 2018-10-05.19:11:26.672>
    user = 'https://bugs.python.org/mattneri'

    bugs.python.org fields:

    activity = <Date 2018-10-05.19:11:26.672>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-09-19.20:49:26.494>
    closer = 'vstinner'
    components = ['ctypes']
    creation = <Date 2018-09-07.11:15:17.826>
    creator = 'mattneri'
    dependencies = []
    files = ['47789']
    hgrepos = []
    issue_num = 34603
    keywords = ['patch']
    message_count = 11.0
    messages = ['324732', '325207', '325471', '325472', '325473', '325800', '325801', '327159', '327161', '327170', '327175']
    nosy_count = 8.0
    nosy_names = ['amaury.forgeotdarc', 'belopolsky', 'vstinner', 'meador.inge', 'serhiy.storchaka', 'miss-islington', 'v2m', 'mattneri']
    pr_nums = ['9258', '9340', '9341', '9342', '9425', '9721', '9725', '9727']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue34603'
    versions = ['Python 3.4', 'Python 3.5', 'Python 3.6', 'Python 3.7']

    @mattneri
    Copy link
    Mannequin Author

    mattneri mannequin commented Sep 7, 2018

    Marshalling the following C structure with ctypes in Windows (compiled with VS2015) is not working correctly.
    As you can see, in the simple demo program I attached, the integer I pass as a parameter gets somehow corrupted. I use cdecl calling convention.

    The corruption happens only if the returned structure contains 3 booleans, if I add a fourth bool to the structure than I get expected results.

    In linux (compiling with Gcc) the same code (with 3 booleans) work as expected.

    typedef uint64_t CustomHandle;

    typedef struct
    {
    bool first;
    bool second;
    bool third;
    } Flags;

    CustomHandle GetHandle();
    Flags GetFlags(CustomHandle handle);

    As already mention I attached all the files to reproduce the issue with the related cmake file

    @mattneri mattneri mannequin added 3.7 (EOL) end of life topic-ctypes type-bug An unexpected behavior, bug, or error labels Sep 7, 2018
    @mattneri mattneri mannequin changed the title ctypes On Windows: error calling C function that returns a struc containing 3 bools ctypes on Windows: error calling C function that returns a struct containing 3 bools Sep 7, 2018
    @vladima
    Copy link
    Mannequin

    vladima mannequin commented Sep 12, 2018

    I think the problem is that FFI layer assumes that MSVC compiler will try to pass any structure less than 8 bytes in registers whereis it is not always true:
    To be returned by value in RAX, user-defined types must have a length of 1, 2, 4, 8, 16, 32, or 64 bits (from https://msdn.microsoft.com/en-us/library/7572ztz4.aspx).
    I have a fix, now adding tests.

    @vstinner
    Copy link
    Member

    New changeset 7843cae by Victor Stinner (Vladimir Matveev) in branch 'master':
    bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258)
    7843cae

    @miss-islington
    Copy link
    Contributor

    New changeset e3f6aa7 by Miss Islington (bot) in branch '3.7':
    bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258)
    e3f6aa7

    @miss-islington
    Copy link
    Contributor

    New changeset e536320 by Miss Islington (bot) in branch '3.6':
    bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258)
    e536320

    @vstinner
    Copy link
    Member

    New changeset b63a16f by Victor Stinner (Vladimir Matveev) in branch '2.7':
    [2.7] bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258) (GH-9425)
    b63a16f

    @vstinner
    Copy link
    Member

    Thanks MatteoL for the bug report, it has been fixed in 2.7, 3.6, 3.7 and master. Thanks Vladimir Matveev for the fix!

    @serhiy-storchaka
    Copy link
    Member

    assertEquals() is deprecated, use assertEqual() instead.

    @serhiy-storchaka
    Copy link
    Member

    New changeset 4642d5f by Serhiy Storchaka in branch 'master':
    Use assertEqual() instead of assertEquals(). (GH-9721)
    4642d5f

    @serhiy-storchaka
    Copy link
    Member

    New changeset 6bffe50 by Serhiy Storchaka in branch '3.7':
    Use assertEqual() instead of assertEquals(). (GH-9721) (GH-9725)
    6bffe50

    @serhiy-storchaka
    Copy link
    Member

    New changeset d02490a by Serhiy Storchaka (Miss Islington (bot)) in branch '3.6':
    Use assertEqual() instead of assertEquals(). (GH-9721) (GH-9725) (GH-9727)
    d02490a

    @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
    3.7 (EOL) end of life topic-ctypes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants