This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Mordechai Botrashvily
Recipients Mordechai Botrashvily
Date 2018-11-27.11:00:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1543316403.16.0.788709270274.issue35326@psf.upfronthosting.co.za>
In-reply-to
Content
Hi,

Using cast() or from_address() to convert from c_void_p (or integer) address to POINTER(c_ubyte) causes the interpreter to crash, when accessing the contents of the array.

The problem seems to happen when running the following combination:
Windows 10 @ 64 bit, version 1803 (OS build 17134.407) and python 3 64 bit, starting with 3.5 until 3.7 (the last that was tested).

The following code works fine on the same system using python 2.7.15 or python 3 until 3.4 (inclusive), 64 bit.
In addition, it works well also under Windows 7 64 bit and python 3.5/3.6/3.7 etc.

How to reproduce?
```
from ctypes import wintypes, windll, c_void_p, c_size_t, POINTER, c_ubyte, cast
# Define constants.
FILE_MAP_ALL_ACCESS = 983071
PAGE_READWRITE = 4
# Configure function arguments.
windll.kernel32.CreateFileMappingA.argtypes = [wintypes.HANDLE, c_void_p, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD, wintypes.LPCSTR]
windll.kernel32.CreateFileMappingA.restype = wintypes.HANDLE
windll.kernel32.MapViewOfFile.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD, c_size_t]
windll.kernel32.MapViewOfFile.restypes = wintypes.LPVOID
# Open shared-memory.
handle = windll.kernel32.CreateFileMappingA(-1, None, PAGE_READWRITE, 0, 1024 * 1024, b'TestSHMEM')
# Obtain pointer to SHMEM buffer.
ptr = windll.kernel32.MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, 1024 * 1024)
arr = cast(ptr, POINTER(c_ubyte))
# or
#arr = POINTER(c_ubyte).from_address(ptr)
arr[0]
# now crashes
```

Also note that changing the return type of MapViewOfFile as follows:
windll.kernel32.MapViewOfFile.restypes = wintypes.LPVOID
is changed to:
windll.kernel32.MapViewOfFile.restypes = POINTER(c_ubyte)
The contents of the buffer can be directly accessed without a problem.

Just in case I'm not using it properly with new python releases?

Thanks!
Moti.
History
Date User Action Args
2018-11-27 11:00:03Mordechai Botrashvilysetrecipients: + Mordechai Botrashvily
2018-11-27 11:00:03Mordechai Botrashvilysetmessageid: <1543316403.16.0.788709270274.issue35326@psf.upfronthosting.co.za>
2018-11-27 11:00:03Mordechai Botrashvilylinkissue35326 messages
2018-11-27 11:00:03Mordechai Botrashvilycreate