Title: gh-148286: Fix undefined behaviour in per-thread refcount merging by matthiasgoergens · Pull Request #154915 · python/cpython · GitHub
Open Graph Title: gh-148286: Fix undefined behaviour in per-thread refcount merging by matthiasgoergens · Pull Request #154915 · python/cpython
X Title: gh-148286: Fix undefined behaviour in per-thread refcount merging by matthiasgoergens · Pull Request #154915 · python/cpython
Description: _PyObject_MergePerThreadRefcounts() merges each thread's local reference count deltas into the shared count: _Py_atomic_add_ssize(&obj->ob_ref_shared, refcnt << _Py_REF_SHARED_SHIFT); refcnt is a delta, so it is routinely negative — in practice almost always -1 — and shifting a negative value left is undefined behaviour. This does the shift in the unsigned domain and converts back. This is not reachable from the UBSan CI job today. The sanitizer matrix in .github/workflows/build.yml pairs UBSan only with free-threading: false, so Python/uniqueid.c is never built under UBSan at all. Building --disable-gil together with --with-undefined-behavior-sanitizer shows how load-bearing it is: the first line of output from ./python -c pass is the UBSan report, raised during interpreter startup via _PyImport_InitExternal, and it recurs from gc_collect_main on every collection. Measured over the full test suite on that configuration, with every entry in Tools/ubsan/suppressions.txt disabled: UB reports test failures failing files before 1762 529 61 after 0 0 0 Most of those failures are tests asserting that a subprocess produced no stderr, which the UBSan diagnostic breaks. I have not touched the CI matrix here, since adding a job is a maintainer's call and this fix stands on its own. But the reason this went unnoticed is structural rather than accidental, and I would like to follow up separately with a proposal to add free-threading: true to the UBSan matrix — with this fix in, that configuration is green, so it would be a regression guard rather than a source of new work. The same appears to be true of --enable-experimental-jit, which CI builds but never sanitizes: I ran the full suite against a machine-code JIT build under UBSan and it is clean today, while gh-139269 (an unaligned uint64_t store in Python/jit.c, which segfaulted release builds) was originally found by exactly that combination. I will write that up with the measurements rather than expand the scope of this PR. Issue: gh-148286
Open Graph Description: _PyObject_MergePerThreadRefcounts() merges each thread's local reference count deltas into the shared count: _Py_atomic_add_ssize(&obj->ob_ref_shared, refcnt << _Py_REF_SHARED_SHIF...
X Description: _PyObject_MergePerThreadRefcounts() merges each thread's local reference count deltas into the shared count: _Py_atomic_add_ssize(&obj->ob_ref_shared, refcnt <<...
Opengraph URL: https://github.com/python/cpython/pull/154915
X: @github
Domain: github.com
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:4476c5d4-c606-83b1-e657-9deac0c0c2cd |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 954E:2F543F:238F142:2ECC640:6B024F43 |
| html-safe-nonce | 1b6fbb7fcfadd71f329a272332292e775c1be2e2266730f0f5d27e66354c82fa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NTRFOjJGNTQzRjoyMzhGMTQyOjJFQ0M2NDA6NkIwMjRGNDMiLCJ2aXNpdG9yX2lkIjoiMjA1MDA2ODY1OTAzNjExNDc1NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 806ce50c910633fb3f62b2535d68a33128dfaff2049a44b98aa599e6b05c4362 |
| hovercard-subject-tag | pull_request:4168107916 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/python/cpython/pull/154915/files |
| twitter:image | https://avatars.githubusercontent.com/u/57026?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/57026?s=400&v=4 |
| og:image:alt | _PyObject_MergePerThreadRefcounts() merges each thread's local reference count deltas into the shared count: _Py_atomic_add_ssize(&obj->ob_ref_shared, refcnt << _Py_REF_SHARED_SHIF... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width