New submission from David Heffernan <[email protected]>:
Starting with Python 3.8 certain ctypes callbacks fail to restore the stack
pointer.
In the repo below, when the DLL is compiled with MSVC under default debug
settings, running the Python script leads to a debug error dialog which says:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a
function call. This is usually a result of calling a function declared with
one calling convention with a function pointer declared with a different
calling convention.
It appears that when the C code calls the callback function, the value of ESP
is 4 greater than it should be.
This problem does not occur with older versions of Python.
**DLL code**
#include <Windows.h>
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID
lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
typedef void (__stdcall *MYCALLBACK)(int, double);
extern "C"
{
__declspec(dllexport) void __stdcall foo(MYCALLBACK callback)
{
callback(1, 11);
callback(2, 21);
callback(3, 31);
}
}
**Python code**
import ctypes
import ctypes.wintypes
def CallbackType(restype, *argtypes):
def from_param(cls, obj):
if obj is None:
return obj
return ctypes._CFuncPtr.from_param(obj)
result = ctypes.WINFUNCTYPE(restype, *argtypes)
result.from_param = classmethod(from_param)
return result
MYCALLBACK = CallbackType(
None,
ctypes.c_int,
ctypes.c_double
)
def callback(handle, time):
print(handle, time)
mycallback = MYCALLBACK(callback)
lib = ctypes.WinDLL(r'path\to\dll\foo.dll')
func = getattr(lib, '_foo@4')
func.restype = None
func.argtypes = MYCALLBACK,
func(mycallback)
----------
components: ctypes
messages: 356249
nosy: David Heffernan
priority: normal
severity: normal
status: open
title: 32 bit ctypes stdcall callback fails to restore stack pointer
type: crash
versions: Python 3.8
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue38748>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com