bpo-38748: Add ctypes test for stack corruption due to misaligned arguments (GH-26204)

This commit is contained in:
Michael Curran 2022-09-27 02:27:44 +10:00 committed by GitHub
parent 85752decbf
commit d79dd929ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import sys
import functools
import unittest
from test import support
@ -150,6 +151,18 @@ class Callbacks(unittest.TestCase):
gc.collect()
CFUNCTYPE(None)(lambda x=Nasty(): None)
@need_symbol('WINFUNCTYPE')
def test_i38748_stackCorruption(self):
callback_funcType = WINFUNCTYPE(c_long, c_long, c_longlong)
@callback_funcType
def callback(a, b):
c = a + b
print(f"a={a}, b={b}, c={c}")
return c
dll = cdll[_ctypes_test.__file__]
# With no fix for i38748, the next line will raise OSError and cause the test to fail.
self.assertEqual(dll._test_i38748_runCallback(callback, 5, 10), 15)
@need_symbol('WINFUNCTYPE')
class StdcallCallbacks(Callbacks):

View File

@ -1034,6 +1034,19 @@ EXPORT (HRESULT) KeepObject(IUnknown *punk)
#endif
#ifdef MS_WIN32
// i38748: c stub for testing stack corruption
// When executing a Python callback with a long and a long long
typedef long(__stdcall *_test_i38748_funcType)(long, long long);
EXPORT(long) _test_i38748_runCallback(_test_i38748_funcType callback, int a, int b) {
return callback(a, b);
}
#endif
static struct PyModuleDef_Slot _ctypes_test_slots[] = {
{0, NULL}
};