From 49ec70a6ffe358fce6c8725298ee6a99c30ad46b Mon Sep 17 00:00:00 2001 From: Matt Page Date: Fri, 8 Nov 2024 12:36:42 -0800 Subject: [PATCH] Double check that keys are still valid --- Python/bytecodes.c | 10 ++++++++++ Python/executor_cases.c.h | 16 ++++++++++++++++ Python/generated_cases.c.h | 10 ++++++++++ Tools/cases_generator/analyzer.py | 1 + 4 files changed, 37 insertions(+) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 64ffa539899499..c3cea0f67b24e4 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1633,6 +1633,11 @@ dummy_func( #if Py_GIL_DISABLED int increfed = _Py_TryIncrefCompare(&entries[index].me_value, res_o); DEOPT_IF(!increfed); + PyDictObject *dict = (PyDictObject*) GLOBALS(); + if (globals_keys != _Py_atomic_load_ptr_acquire(&dict->ma_keys)) { + Py_DECREF(res_o); + DEOPT_IF(true); + } #else Py_INCREF(res_o); #endif @@ -1650,6 +1655,11 @@ dummy_func( #if Py_GIL_DISABLED int increfed = _Py_TryIncrefCompare(&entries[index].me_value, res_o); DEOPT_IF(!increfed); + PyDictObject *dict = (PyDictObject*) BUILTINS(); + if (builtins_keys != _Py_atomic_load_ptr_acquire(&dict->ma_keys)) { + Py_DECREF(res_o); + DEOPT_IF(true); + } #else Py_INCREF(res_o); #endif diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 5dd8d0bb78a1b4..5afa9263fb8dfb 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -1944,6 +1944,14 @@ UOP_STAT_INC(uopcode, miss); JUMP_TO_JUMP_TARGET(); } + PyDictObject *dict = (PyDictObject*) GLOBALS(); + if (globals_keys != _Py_atomic_load_ptr_acquire(&dict->ma_keys)) { + Py_DECREF(res_o); + if (true) { + UOP_STAT_INC(uopcode, miss); + JUMP_TO_JUMP_TARGET(); + } + } #else Py_INCREF(res_o); #endif @@ -1980,6 +1988,14 @@ UOP_STAT_INC(uopcode, miss); JUMP_TO_JUMP_TARGET(); } + PyDictObject *dict = (PyDictObject*) BUILTINS(); + if (builtins_keys != _Py_atomic_load_ptr_acquire(&dict->ma_keys)) { + Py_DECREF(res_o); + if (true) { + UOP_STAT_INC(uopcode, miss); + JUMP_TO_JUMP_TARGET(); + } + } #else Py_INCREF(res_o); #endif diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index d6076f130e0bf6..c872b4c68f7ef0 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -6166,6 +6166,11 @@ int increfed = _Py_TryIncrefCompare(&entries[index].me_value, res_o); stack_pointer = _PyFrame_GetStackPointer(frame); DEOPT_IF(!increfed, LOAD_GLOBAL); + PyDictObject *dict = (PyDictObject*) BUILTINS(); + if (builtins_keys != _Py_atomic_load_ptr_acquire(&dict->ma_keys)) { + Py_DECREF(res_o); + DEOPT_IF(true, LOAD_GLOBAL); + } #else Py_INCREF(res_o); #endif @@ -6211,6 +6216,11 @@ int increfed = _Py_TryIncrefCompare(&entries[index].me_value, res_o); stack_pointer = _PyFrame_GetStackPointer(frame); DEOPT_IF(!increfed, LOAD_GLOBAL); + PyDictObject *dict = (PyDictObject*) GLOBALS(); + if (globals_keys != _Py_atomic_load_ptr_acquire(&dict->ma_keys)) { + Py_DECREF(res_o); + DEOPT_IF(true, LOAD_GLOBAL); + } #else Py_INCREF(res_o); #endif diff --git a/Tools/cases_generator/analyzer.py b/Tools/cases_generator/analyzer.py index a725ec10d4e52a..bb92ece54fbe83 100644 --- a/Tools/cases_generator/analyzer.py +++ b/Tools/cases_generator/analyzer.py @@ -623,6 +623,7 @@ def has_error_without_pop(op: parser.InstDef) -> bool: "_Py_NewRef", "_Py_SINGLETON", "_Py_STR", + "_Py_atomic_load_ptr_acquire", "_Py_atomic_load_uintptr_relaxed", "_Py_set_eval_breaker_bit", "advance_backoff_counter",