[Python-checkins] bpo-34126: Fix crashes while profiling invalid calls. (GH-8300) (GH-8371)

Serhiy Storchaka webhook-mailer at python.org
Sat Jul 21 12:58:39 EDT 2018


https://github.com/python/cpython/commit/7638eb892a50d101fe06342da180f0ab63a0b334
commit: 7638eb892a50d101fe06342da180f0ab63a0b334
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2018-07-21T19:58:35+03:00
summary:

bpo-34126: Fix crashes while profiling invalid calls. (GH-8300) (GH-8371)

(cherry picked from commit 56868f940e0cc0b35d33c0070107ff3bed2d8766)

Co-authored-by: jdemeyer <jdemeyer at cage.ugent.be>

files:
A Misc/NEWS.d/next/Core and Builtins/2018-07-16-20-55-29.bpo-34126.mBVmgc.rst
M Lib/test/test_sys_setprofile.py
M Python/ceval.c

diff --git a/Lib/test/test_sys_setprofile.py b/Lib/test/test_sys_setprofile.py
index a3e1d31fbebe..16467e7f7184 100644
--- a/Lib/test/test_sys_setprofile.py
+++ b/Lib/test/test_sys_setprofile.py
@@ -334,6 +334,22 @@ def j(p):
                               (1, 'return', j_ident),
                               ])
 
+    # Test an invalid call (bpo-34126)
+    def test_unbound_method_no_args(self):
+        def f(p):
+            dict.get()
+        f_ident = ident(f)
+        self.check_events(f, [(1, 'call', f_ident),
+                              (1, 'return', f_ident)])
+
+    # Test an invalid call (bpo-34126)
+    def test_unbound_method_invalid_args(self):
+        def f(p):
+            dict.get(print, 42)
+        f_ident = ident(f)
+        self.check_events(f, [(1, 'call', f_ident),
+                              (1, 'return', f_ident)])
+
 
 def ident(function):
     if hasattr(function, "f_code"):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-07-16-20-55-29.bpo-34126.mBVmgc.rst b/Misc/NEWS.d/next/Core and Builtins/2018-07-16-20-55-29.bpo-34126.mBVmgc.rst
new file mode 100644
index 000000000000..7cfc439cf887
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-07-16-20-55-29.bpo-34126.mBVmgc.rst	
@@ -0,0 +1,2 @@
+Fix crashes when profiling certain invalid calls of unbound methods.
+Patch by Jeroen Demeyer.
diff --git a/Python/ceval.c b/Python/ceval.c
index 86cb4cd82474..600147b7aaf2 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -4555,10 +4555,16 @@ call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
     }
     else if (Py_TYPE(func) == &PyMethodDescr_Type) {
         PyThreadState *tstate = PyThreadState_GET();
-        if (tstate->use_tracing && tstate->c_profilefunc) {
-            // We need to create PyCFunctionObject for tracing.
-            PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
-            func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
+        if (nargs > 0 && tstate->use_tracing) {
+            /* We need to create a temporary bound method as argument
+               for profiling.
+
+               If nargs == 0, then this cannot work because we have no
+               "self". In any case, the call itself would raise
+               TypeError (foo needs an argument), so we just skip
+               profiling. */
+            PyObject *self = stack[0];
+            func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
             if (func == NULL) {
                 return NULL;
             }



More information about the Python-checkins mailing list