]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-135608: Add a null check for attribute promotion to fix a JIT crash (GH...
authorKen Jin <kenjin@python.org>
Fri, 20 Jun 2025 07:24:25 +0000 (15:24 +0800)
committerGitHub <noreply@github.com>
Fri, 20 Jun 2025 07:24:25 +0000 (15:24 +0800)
gh-135608: Add a null check for attribute promotion to fix a JIT crash (GH-135613)

Co-authored-by: devdanzin <74280297+devdanzin@users.noreply.github.com>
Lib/test/test_capi/test_opt.py
Misc/NEWS.d/next/Core_and_Builtins/2025-06-17-12-50-48.gh-issue-135608.PnHckD.rst [new file with mode: 0644]
Python/optimizer_analysis.c
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index ba7bcb4540a45174466a8378c40d6d129eee2558..c74c8ee0eef8e292ed45fc760a2b5f05b165477a 100644 (file)
@@ -1942,6 +1942,36 @@ class TestUopsOptimization(unittest.TestCase):
         self.assertNotIn("_COMPARE_OP_INT", uops)
         self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
 
+    def test_attr_promotion_failure(self):
+        # We're not testing for any specific uops here, just
+        # testing it doesn't crash.
+        script_helper.assert_python_ok('-c', textwrap.dedent("""
+        import _testinternalcapi
+        import _opcode
+        import email
+
+        def get_first_executor(func):
+            code = func.__code__
+            co_code = code.co_code
+            for i in range(0, len(co_code), 2):
+                try:
+                    return _opcode.get_executor(code, i)
+                except ValueError:
+                    pass
+            return None
+
+        def testfunc(n):
+            for _ in range(n):
+                email.jit_testing = None
+                prompt = email.jit_testing
+                del email.jit_testing
+
+
+        testfunc(_testinternalcapi.TIER2_THRESHOLD)
+        ex = get_first_executor(testfunc)
+        assert ex is not None
+        """))
+
 
 def global_identity(x):
     return x
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-06-17-12-50-48.gh-issue-135608.PnHckD.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-06-17-12-50-48.gh-issue-135608.PnHckD.rst
new file mode 100644 (file)
index 0000000..a65a0c8
--- /dev/null
@@ -0,0 +1 @@
+Fix a crash in the JIT involving attributes of modules.
index 8b0bd1e9518c6ee155616f780f769f13fba12c98..503e6ed47913713ec66788e02818b94b47c77f89 100644 (file)
@@ -103,6 +103,10 @@ convert_global_to_const(_PyUOpInstruction *inst, PyObject *obj, bool pop)
     if ((int)index >= dict->ma_keys->dk_nentries) {
         return NULL;
     }
+    PyDictKeysObject *keys = dict->ma_keys;
+    if (keys->dk_version != inst->operand0) {
+        return NULL;
+    }
     PyObject *res = entries[index].me_value;
     if (res == NULL) {
         return NULL;
index e99421a3aff4baab398537b361c8f6b186af5830..e3927d6c690a8b0292796aa9cf0d27d97ded1f1f 100644 (file)
@@ -580,7 +580,13 @@ dummy_func(void) {
                     PyDict_Watch(GLOBALS_WATCHER_ID, dict);
                     _Py_BloomFilter_Add(dependencies, dict);
                     PyObject *res = convert_global_to_const(this_instr, dict, true);
-                    attr = sym_new_const(ctx, res);
+                    if (res == NULL) {
+                        attr = sym_new_not_null(ctx);
+                    }
+                    else {
+                        attr = sym_new_const(ctx, res);
+                    }
+
                 }
             }
         }
index 56b4b9983fbaaa0abd88004a488681fe93d0bf36..53ebdc3b562fe07bc259e5703ade15c9c74ed30a 100644 (file)
                         PyDict_Watch(GLOBALS_WATCHER_ID, dict);
                         _Py_BloomFilter_Add(dependencies, dict);
                         PyObject *res = convert_global_to_const(this_instr, dict, true);
-                        attr = sym_new_const(ctx, res);
+                        if (res == NULL) {
+                            attr = sym_new_not_null(ctx);
+                        }
+                        else {
+                            attr = sym_new_const(ctx, res);
+                        }
                     }
                 }
             }