classification
Title: ctypes on Windows: error calling C function that returns a struct containing 3 bools
Type: behavior Stage: patch review
Components: ctypes Versions: Python 3.7, Python 3.6, Python 3.5, Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, belopolsky, mattneri, meador.inge, miss-islington, v2m, vstinner
Priority: normal Keywords: patch

Created on 2018-09-07 11:15 by mattneri, last changed 2018-09-16 05:57 by miss-islington.

Files
File name Uploaded Description Edit
MarshallingTest.zip mattneri, 2018-09-07 11:15
Pull Requests
URL Status Linked Edit
PR 9258 merged v2m, 2018-09-13 15:43
PR 9340 merged miss-islington, 2018-09-16 05:36
PR 9341 merged miss-islington, 2018-09-16 05:36
PR 9342 open miss-islington, 2018-09-16 05:37
Messages (5)
msg324732 - (view) Author: MatteoL (mattneri) Date: 2018-09-07 11:15
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
msg325207 - (view) Author: Vladimir Matveev (v2m) * Date: 2018-09-12 23:03
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.
msg325471 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-09-16 05:36
New changeset 7843caeb909bd907e903606414e238db4082315a by Victor Stinner (Vladimir Matveev) in branch 'master':
bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258)
https://github.com/python/cpython/commit/7843caeb909bd907e903606414e238db4082315a
msg325472 - (view) Author: miss-islington (miss-islington) Date: 2018-09-16 05:53
New changeset e3f6aa7fe48b91f4ff619b2a51d473249d620bcb by Miss Islington (bot) in branch '3.7':
bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258)
https://github.com/python/cpython/commit/e3f6aa7fe48b91f4ff619b2a51d473249d620bcb
msg325473 - (view) Author: miss-islington (miss-islington) Date: 2018-09-16 05:57
New changeset e53632019816749ffd5be0afab2a99d744dbbe35 by Miss Islington (bot) in branch '3.6':
bpo-34603, ctypes/libffi_msvc: Fix returning structs from functions (GH-9258)
https://github.com/python/cpython/commit/e53632019816749ffd5be0afab2a99d744dbbe35
History
Date User Action Args
2018-09-16 05:57:49miss-islingtonsetmessages: + msg325473
2018-09-16 05:53:16miss-islingtonsetnosy: + miss-islington
messages: + msg325472
2018-09-16 05:37:04miss-islingtonsetpull_requests: + pull_request8766
2018-09-16 05:36:53miss-islingtonsetpull_requests: + pull_request8765
2018-09-16 05:36:45miss-islingtonsetpull_requests: + pull_request8764
2018-09-16 05:36:33vstinnersetnosy: + vstinner
messages: + msg325471
2018-09-13 15:43:43v2msetkeywords: + patch
stage: patch review
pull_requests: + pull_request8690
2018-09-12 23:03:18v2msetnosy: + v2m
messages: + msg325207
2018-09-10 07:50:25mattnerisetnosy: + amaury.forgeotdarc, belopolsky, meador.inge
2018-09-07 11:25:52mattnerisettitle: 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
2018-09-07 11:15:17mattnericreate