Skip to content

Commit e6db23f

Browse files
authored
gh-107265: Fix code_hash for ENTER_EXECUTOR case (#108188)
1 parent 4b32d4f commit e6db23f

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

Lib/test/test_capi/test_misc.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,7 +2341,7 @@ def long_loop():
23412341
long_loop()
23422342
self.assertEqual(opt.get_count(), 10)
23432343

2344-
def test_code_richcompare(self):
2344+
def test_code_restore_for_ENTER_EXECUTOR(self):
23452345
def testfunc(x):
23462346
i = 0
23472347
while i < x:
@@ -2350,7 +2350,9 @@ def testfunc(x):
23502350
opt = _testinternalcapi.get_counter_optimizer()
23512351
with temporary_optimizer(opt):
23522352
testfunc(1000)
2353-
self.assertEqual(testfunc.__code__, testfunc.__code__.replace())
2353+
code, replace_code = testfunc.__code__, testfunc.__code__.replace()
2354+
self.assertEqual(code, replace_code)
2355+
self.assertEqual(hash(code), hash(replace_code))
23542356

23552357

23562358
def get_first_executor(func):

Objects/codeobject.c

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,30 +1781,33 @@ code_richcompare(PyObject *self, PyObject *other, int op)
17811781
for (int i = 0; i < Py_SIZE(co); i++) {
17821782
_Py_CODEUNIT co_instr = _PyCode_CODE(co)[i];
17831783
_Py_CODEUNIT cp_instr = _PyCode_CODE(cp)[i];
1784+
uint8_t co_code = co_instr.op.code;
1785+
uint8_t co_arg = co_instr.op.arg;
1786+
uint8_t cp_code = cp_instr.op.code;
1787+
uint8_t cp_arg = cp_instr.op.arg;
17841788

1785-
if (co_instr.op.code == ENTER_EXECUTOR) {
1786-
const int exec_index = co_instr.op.arg;
1789+
if (co_code == ENTER_EXECUTOR) {
1790+
const int exec_index = co_arg;
17871791
_PyExecutorObject *exec = co->co_executors->executors[exec_index];
1788-
co_instr.op.code = exec->vm_data.opcode;
1789-
co_instr.op.arg = exec->vm_data.oparg;
1792+
co_code = exec->vm_data.opcode;
1793+
co_arg = exec->vm_data.oparg;
17901794
}
1791-
assert(co_instr.op.code != ENTER_EXECUTOR);
1792-
co_instr.op.code = _PyOpcode_Deopt[co_instr.op.code];
1795+
assert(co_code != ENTER_EXECUTOR);
1796+
co_code = _PyOpcode_Deopt[co_code];
17931797

1794-
if (cp_instr.op.code == ENTER_EXECUTOR) {
1795-
const int exec_index = cp_instr.op.arg;
1798+
if (cp_code == ENTER_EXECUTOR) {
1799+
const int exec_index = cp_arg;
17961800
_PyExecutorObject *exec = cp->co_executors->executors[exec_index];
1797-
cp_instr.op.code = exec->vm_data.opcode;
1798-
cp_instr.op.arg = exec->vm_data.oparg;
1801+
cp_code = exec->vm_data.opcode;
1802+
cp_arg = exec->vm_data.oparg;
17991803
}
1800-
assert(cp_instr.op.code != ENTER_EXECUTOR);
1801-
cp_instr.op.code = _PyOpcode_Deopt[cp_instr.op.code];
1804+
assert(cp_code != ENTER_EXECUTOR);
1805+
cp_code = _PyOpcode_Deopt[cp_code];
18021806

1803-
eq = co_instr.cache == cp_instr.cache;
1804-
if (!eq) {
1807+
if (co_code != cp_code || co_arg != cp_arg) {
18051808
goto unequal;
18061809
}
1807-
i += _PyOpcode_Caches[co_instr.op.code];
1810+
i += _PyOpcode_Caches[co_code];
18081811
}
18091812

18101813
/* compare constants */
@@ -1883,10 +1886,22 @@ code_hash(PyCodeObject *co)
18831886
SCRAMBLE_IN(co->co_firstlineno);
18841887
SCRAMBLE_IN(Py_SIZE(co));
18851888
for (int i = 0; i < Py_SIZE(co); i++) {
1886-
int deop = _Py_GetBaseOpcode(co, i);
1887-
SCRAMBLE_IN(deop);
1888-
SCRAMBLE_IN(_PyCode_CODE(co)[i].op.arg);
1889-
i += _PyOpcode_Caches[deop];
1889+
_Py_CODEUNIT co_instr = _PyCode_CODE(co)[i];
1890+
uint8_t co_code = co_instr.op.code;
1891+
uint8_t co_arg = co_instr.op.arg;
1892+
if (co_code == ENTER_EXECUTOR) {
1893+
_PyExecutorObject *exec = co->co_executors->executors[co_arg];
1894+
assert(exec != NULL);
1895+
assert(exec->vm_data.opcode != ENTER_EXECUTOR);
1896+
co_code = _PyOpcode_Deopt[exec->vm_data.opcode];
1897+
co_arg = exec->vm_data.oparg;
1898+
}
1899+
else {
1900+
co_code = _Py_GetBaseOpcode(co, i);
1901+
}
1902+
SCRAMBLE_IN(co_code);
1903+
SCRAMBLE_IN(co_arg);
1904+
i += _PyOpcode_Caches[co_code];
18901905
}
18911906
if ((Py_hash_t)uhash == -1) {
18921907
return -2;

0 commit comments

Comments
 (0)