New submission from Mordechai Botrashvily <moti....@gmail.com>:
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. ---------- components: ctypes files: test_ctypes_shmem.py messages: 330503 nosy: Mordechai Botrashvily priority: normal severity: normal status: open title: ctypes cast and from_address cause crash on Windows 10 type: crash versions: Python 3.5, Python 3.6, Python 3.7 Added file: https://bugs.python.org/file47951/test_ctypes_shmem.py _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue35326> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com