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

zooba webhook-mailer at python.org
Mon Sep 26 12:28:06 EDT 2022


https://github.com/python/cpython/commit/d79dd929accac13832bc13ed12be34466584ca81
commit: d79dd929accac13832bc13ed12be34466584ca81
branch: main
author: Michael Curran <mick at nvaccess.org>
committer: zooba <steve.dower at microsoft.com>
date: 2022-09-26T17:27:44+01:00
summary:

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

files:
M Lib/test/test_ctypes/test_callbacks.py
M Modules/_ctypes/_ctypes_test.c

diff --git a/Lib/test/test_ctypes/test_callbacks.py b/Lib/test/test_ctypes/test_callbacks.py
index b5bef04e14d2..e8fa3e6f7aca 100644
--- a/Lib/test/test_ctypes/test_callbacks.py
+++ b/Lib/test/test_ctypes/test_callbacks.py
@@ -1,3 +1,4 @@
+import sys
 import functools
 import unittest
 from test import support
@@ -150,6 +151,18 @@ def __del__(self):
                 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):
diff --git a/Modules/_ctypes/_ctypes_test.c b/Modules/_ctypes/_ctypes_test.c
index 770c96c60d1f..e1f91b476a49 100644
--- a/Modules/_ctypes/_ctypes_test.c
+++ b/Modules/_ctypes/_ctypes_test.c
@@ -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}
 };



More information about the Python-checkins mailing list