Skip to content

Commit

Permalink
Lock dict when specializing module lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
DinoV committed Jan 30, 2024
1 parent dc4cd2c commit ded73c4
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "opcode.h"

#include "pycore_code.h"
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION, Py_END_CRITICAL_SECTION
#include "pycore_descrobject.h" // _PyMethodWrapper_Type
#include "pycore_dict.h" // DICT_KEYS_UNICODE
#include "pycore_function.h" // _PyFunction_GetVersionForCurrentState()
Expand Down Expand Up @@ -600,17 +601,11 @@ static uint32_t function_get_version(PyObject *o, int opcode);
static uint32_t type_get_version(PyTypeObject *t, int opcode);

static int
specialize_module_load_attr(
PyObject *owner, _Py_CODEUNIT *instr, PyObject *name
) {
specialize_module_load_attr_lock_held(
PyDictObject *dict, _Py_CODEUNIT *instr, PyObject *name
)
{
_PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
PyModuleObject *m = (PyModuleObject *)owner;
assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
PyDictObject *dict = (PyDictObject *)m->md_dict;
if (dict == NULL) {
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_NO_DICT);
return -1;
}
if (dict->ma_keys->dk_kind != DICT_KEYS_UNICODE) {
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_ATTR_NON_STRING_OR_SPLIT);
return -1;
Expand Down Expand Up @@ -642,6 +637,23 @@ specialize_module_load_attr(
return 0;
}

static int
specialize_module_load_attr(
PyObject *owner, _Py_CODEUNIT *instr, PyObject *name
) {
PyModuleObject *m = (PyModuleObject *)owner;
assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
PyDictObject *dict = (PyDictObject *)m->md_dict;
if (dict == NULL) {
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_NO_DICT);
return -1;
}
int res;
Py_BEGIN_CRITICAL_SECTION(dict);
res = specialize_module_load_attr_lock_held(dict, instr, name);
Py_END_CRITICAL_SECTION();
return res;
}


/* Attribute specialization */
Expand Down

0 comments on commit ded73c4

Please sign in to comment.