Title: Code objects have incorrect hash/equality when code is instrumented for sys.monitoring · Issue #111984 · python/cpython · GitHub
Open Graph Title: Code objects have incorrect hash/equality when code is instrumented for sys.monitoring · Issue #111984 · python/cpython
X Title: Code objects have incorrect hash/equality when code is instrumented for sys.monitoring · Issue #111984 · python/cpython
Description: Bug report Bug description: A thread stress test in the coverage.py test suite was failing with seemingly impossible behavior: a code object is used as a key in a dict, and then cannot be found. I eventually tracked it down to a mismatch...
Open Graph Description: Bug report Bug description: A thread stress test in the coverage.py test suite was failing with seemingly impossible behavior: a code object is used as a key in a dict, and then cannot be found. I ...
X Description: Bug report Bug description: A thread stress test in the coverage.py test suite was failing with seemingly impossible behavior: a code object is used as a key in a dict, and then cannot be found. I ...
Opengraph URL: https://github.com/python/cpython/issues/111984
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Code objects have incorrect hash/equality when code is instrumented for sys.monitoring","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nA thread stress test in the coverage.py test suite was failing with seemingly impossible behavior: a code object is used as a key in a dict, and then cannot be found. I eventually tracked it down to a mismatch between how bytecodes are examined in code_hash and in code_richcompare.\r\n\r\nI have instructions for demonstrating the problem if you like, though it's a thread race condition, so it's a bit involved: https://gist.github.com/nedbat/2fb43527d102b8bb6e7fb7f3400fa3a9\r\n\r\n# Hash computation\r\n\r\nWhen computing the hash, opcodes are checked if they are instrumented, and if so, use the original opcodes:\r\n\r\n```c\r\nstatic Py_hash_t\r\ncode_hash(PyCodeObject *co)\r\n{\r\n //...\r\n for (int i = 0; i \u003c Py_SIZE(co); i++) {\r\n int deop = _Py_GetBaseOpcode(co, i);\r\n SCRAMBLE_IN(deop);\r\n SCRAMBLE_IN(_PyCode_CODE(co)[i].op.arg);\r\n i += _PyOpcode_Caches[deop];\r\n }\r\n```\r\n\r\n```c\r\n/* Get the underlying opcode, stripping instrumentation */\r\nint _Py_GetBaseOpcode(PyCodeObject *code, int i)\r\n{\r\n int opcode = _PyCode_CODE(code)[i].op.code;\r\n if (opcode == INSTRUMENTED_LINE) {\r\n opcode = code->_co_monitoring->lines[i].original_opcode;\r\n }\r\n if (opcode == INSTRUMENTED_INSTRUCTION) {\r\n opcode = code->_co_monitoring->per_instruction_opcodes[i];\r\n }\r\n CHECK(opcode != INSTRUMENTED_INSTRUCTION);\r\n CHECK(opcode != INSTRUMENTED_LINE);\r\n int deinstrumented = DE_INSTRUMENT[opcode];\r\n if (deinstrumented) {\r\n return deinstrumented;\r\n }\r\n return _PyOpcode_Deopt[opcode];\r\n}\r\n```\r\n\r\n```\r\n#define _PyCode_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive)\r\n```\r\n\r\n# Equality\r\n\r\nWhen checking equality, opcodes are not checked if they are instrumented:\r\n\r\n```\r\nstatic PyObject *\r\ncode_richcompare(PyObject *self, PyObject *other, int op)\r\n{\r\n // ...\r\n for (int i = 0; i \u003c Py_SIZE(co); i++) {\r\n _Py_CODEUNIT co_instr = _PyCode_CODE(co)[i];\r\n _Py_CODEUNIT cp_instr = _PyCode_CODE(cp)[i];\r\n co_instr.op.code = _PyOpcode_Deopt[co_instr.op.code];\r\n cp_instr.op.code = _PyOpcode_Deopt[cp_instr.op.code];\r\n eq = co_instr.cache == cp_instr.cache;\r\n if (!eq) {\r\n goto unequal;\r\n }\r\n i += _PyOpcode_Caches[co_instr.op.code];\r\n }\r\n```\r\n\r\nIt seems like the hash and the equality will be well matched if the code hasn't been instrumented. But if it has been, then they don't match.\r\n\r\n\r\n### CPython versions tested on:\r\n\r\n3.12\r\n\r\n### Operating systems tested on:\r\n\r\n_No response_","author":{"url":"https://github.com/nedbat","@type":"Person","name":"nedbat"},"datePublished":"2023-11-11T18:09:08Z","url":"https://github.com/python/cpython/issues/111984"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:92cb34d5-c8f7-c3ce-eca6-a16e579b5e7c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 91AC:283776:100D2C9:15860C6:6B005C97 |
| html-safe-nonce | 6fb9e3917ee380e57e56e8f5dc545542993cf6cabcfe551997590a0ec7eb0df6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MUFDOjI4Mzc3NjoxMDBEMkM5OjE1ODYwQzY6NkIwMDVDOTciLCJ2aXNpdG9yX2lkIjoiMTcwMTAwNjQ0MDU3MjkzNTMxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a8a68a8fd378f56c544c9d3a49606d3f5ab9fd899410d68db334985b523fbe16 |
| hovercard-subject-tag | issue:1989064764 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/111984/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bf41b536b00036e1dd33605eb2512e066fb05e3263a41351c8d3629858880e34/python/cpython/issues/111984 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bf41b536b00036e1dd33605eb2512e066fb05e3263a41351c8d3629858880e34/python/cpython/issues/111984 |
| og:image:alt | Bug report Bug description: A thread stress test in the coverage.py test suite was failing with seemingly impossible behavior: a code object is used as a key in a dict, and then cannot be found. I ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nedbat |
| hostname | github.com |
| expected-hostname | github.com |
| None | 29be975efff6507a5d135de9c7d634cf7ff8f0c55222a64386ab3202209aaf73 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | eacd927ba6112e3b01ba99c762bd699a4a4f5df7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width