René's URL Explorer Experiment


Title: `hashlib` BLAKE2 `.copy()` crashes with `Py_UNREACHABLE` in `py_blake2_clear` when the copy's allocation fails · Issue #152851 · python/cpython · GitHub

Open Graph Title: `hashlib` BLAKE2 `.copy()` crashes with `Py_UNREACHABLE` in `py_blake2_clear` when the copy's allocation fails · Issue #152851 · python/cpython

X Title: `hashlib` BLAKE2 `.copy()` crashes with `Py_UNREACHABLE` in `py_blake2_clear` when the copy's allocation fails · Issue #152851 · python/cpython

Description: Crash report What happened? _blake2.blake2b/_blake2.blake2s carry an impl enum that py_blake2_clear (the tp_clear/dealloc path) switches on, with a Py_UNREACHABLE() default. The .copy() method allocates a new object whose impl is left un...

Open Graph Description: Crash report What happened? _blake2.blake2b/_blake2.blake2s carry an impl enum that py_blake2_clear (the tp_clear/dealloc path) switches on, with a Py_UNREACHABLE() default. The .copy() method allo...

X Description: Crash report What happened? _blake2.blake2b/_blake2.blake2s carry an impl enum that py_blake2_clear (the tp_clear/dealloc path) switches on, with a Py_UNREACHABLE() default. The .copy() method allo...

Opengraph URL: https://github.com/python/cpython/issues/152851

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`hashlib` BLAKE2 `.copy()` crashes with `Py_UNREACHABLE` in `py_blake2_clear` when the copy's allocation fails","articleBody":"# Crash report\n\n### What happened?\n\n`_blake2.blake2b`/`_blake2.blake2s` carry an `impl` enum that `py_blake2_clear` (the `tp_clear`/dealloc path) switches on, with a `Py_UNREACHABLE()` default. The `.copy()` method allocates a new object whose `impl` is left uninitialized, then performs the (fallible) HACL\\* state allocation; if that allocation fails, the error path deallocates the half-built object without ever setting `impl`, so `py_blake2_clear` reads garbage and hits `Py_UNREACHABLE()`:\n\n```\nFatal Python error: py_blake2_clear: We've reached an unreachable state. ...\n```\n\nOn a debug build this is a deterministic fatal error; on a release build `Py_UNREACHABLE()` is `__builtin_unreachable()`, so a garbage `impl` is undefined behaviour (I observed a SIGSEGV on a free-threaded release build; and if the garbage value happens to match a valid enum case, `py_blake2_clear` calls `Hacl_Hash_*_free()` on an uninitialized state pointer — a wild free).\n\n## Reproducer\n\nThe failing allocation is HACL\\*'s raw `malloc()` for the copy's hash state, which `_testcapi.set_nomemory` (a PyMem hook) cannot reach — so the failure is injected at the C `malloc` layer with a tiny `LD_PRELOAD` shim. **Needs a non-ASan build** (ASan owns `malloc`, bypassing the shim).\n\n```c\n/* oomshim.c:  cc -shared -fPIC -O2 -o oomshim.so oomshim.c -ldl\n * arm_next_malloc() makes the very next malloc() return NULL. */\n#define _GNU_SOURCE\n#include \u003cdlfcn.h>\n#include \u003cerrno.h>\n#include \u003cstddef.h>\n#include \u003cstring.h>\nstatic void *(*real_malloc)(size_t); static void *(*real_calloc)(size_t,size_t);\nstatic void (*real_free)(void*); static int armed = 0;\nstatic char boot[16384]; static size_t boot_off = 0; static int initing = 0;\nstatic int is_boot(const void*p){return (const char*)p>=boot&&(const char*)p\u003cboot+sizeof boot;}\nstatic void init_reals(void){ initing=1; real_malloc=dlsym(RTLD_NEXT,\"malloc\");\n    real_calloc=dlsym(RTLD_NEXT,\"calloc\"); real_free=dlsym(RTLD_NEXT,\"free\"); initing=0; }\nvoid arm_next_malloc(void){ armed = 1; }\nvoid *malloc(size_t n){ if(!real_malloc){ if(initing){void*p=boot+boot_off;boot_off+=(n+15)&~(size_t)15;return p;} init_reals(); }\n    if(armed){ armed=0; errno=ENOMEM; return NULL; } return real_malloc(n); }\nvoid *calloc(size_t a,size_t b){ if(!real_calloc){ if(initing){void*p=boot+boot_off;boot_off+=((a*b)+15)&~(size_t)15;memset(p,0,a*b);return p;} init_reals(); }\n    return real_calloc(a,b); }\nvoid free(void*p){ if(is_boot(p))return; if(!real_free)init_reals(); if(real_free)real_free(p); }\n```\n\n```python\n# repro.py  —  LD_PRELOAD=./oomshim.so ./python repro.py\nimport ctypes, faulthandler\nfaulthandler.enable()\nimport _blake2\n\nlib = ctypes.CDLL(None)\nh = _blake2.blake2s(b\"data\")   # (blake2b behaves identically)\nlib.arm_next_malloc()          # next malloc() returns NULL\nh.copy()                       # -> Fatal Python error: py_blake2_clear ...\n```\n\n## Traceback (debug build, gdb)\n\n```\n#8  py_blake2_clear            Modules/blake2module.c:997   # default: Py_UNREACHABLE() — reads uninitialized self->impl\n#9  py_blake2_dealloc          Modules/blake2module.c:1008\n#10 _Py_Dealloc               Objects/object.c:3319\n#11 Py_DECREF                 Include/refcount.h:359\n#12 _blake2_blake2b_copy_impl Modules/blake2module.c:812    # Py_DECREF(cpy) after the copy failed\n```\n\n## Root cause\n\n`new_Blake2Object` (`blake2module.c:387`) uses `PyObject_GC_New` (memory not zeroed) and immediately `PyObject_GC_Track`s the object, leaving `impl` and the state pointers uninitialized; each caller is responsible for initializing them. `py_blake2_new` does so immediately, before any fallible allocation. But `blake2_blake2b_copy_unlocked` (`blake2module.c:749`) does the fallible HACL allocation first and only sets `cpy->impl = self->impl` on success (line 781); its `error:` path returns `-1` with `impl` still uninitialized, and `_blake2_blake2b_copy_impl` then `Py_DECREF`s that object (line 812) → `py_blake2_clear` reads the garbage discriminant.\n\n## Suggested fix\n\nInitialize the discriminant (and NULL the state pointer) before the fallible allocation, mirroring `py_blake2_new` — e.g. at the top of `blake2_blake2b_copy_unlocked`:\n\n```c\ncpy->impl = self->impl;\n/* leave the state pointer NULL until the copy succeeds, so py_blake2_clear is a no-op on error */\n```\n\n(Alternatively, have `new_Blake2Object` zero-initialize the struct so no caller can leave `impl` uninitialized.)\n\n## Environment\n\n- CPython `main` (3.16.0a0), commit `1b9fe5c`; reproduced on debug free-threaded and GIL builds (deterministic fatal) and a release free-threaded build (SIGSEGV). Linux x86-64.\n- The copy path's structure dates to the 2024 HACL BLAKE2 rewrite (`325e9b8`); most recently reworked in gh-135532 (#135838).\n\nFound via allocation-failure fuzzing with [fusil](https://github.com/devdanzin/fusil), created by @vstinner.\n\n*The investigation was conducted and this report was drafted with the help of Claude Code (Opus 4.8).*\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.16.0a0 (heads/main:1b9fe5c7226, Jun 20 2026, 23:56:12) [Clang 21.1.8 (6ubuntu1)]\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-153008\n* gh-153011\n* gh-153012\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/devdanzin","@type":"Person","name":"devdanzin"},"datePublished":"2026-07-02T05:59:35Z","url":"https://github.com/python/cpython/issues/152851"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:7efb768b-14b3-fdc1-8abd-8552fe8604db
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id88BC:27E5CD:3DA1D2D:4EC2B2D:6B0207C2
html-safe-nonceec561970a6d1f633c1b64f4c01c7fc75a638cbeaf71673170468ba8558ae6d16
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OEJDOjI3RTVDRDozREExRDJEOjRFQzJCMkQ6NkIwMjA3QzIiLCJ2aXNpdG9yX2lkIjoiODY5ODIwMjE5MzgxNzM3MjYxMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac93cd98f109fa9da1f85f81c70b37488b1e8cb2ccfc991a3e57c34b0f7fa640b3
hovercard-subject-tagissue:4791701339
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/152851/issue_layout
twitter:imagehttps://opengraph.githubassets.com/03b76c0770533ae7acb0bacb01b604feaa08b4479ab4ad164bd33717d1ce3ee9/python/cpython/issues/152851
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/03b76c0770533ae7acb0bacb01b604feaa08b4479ab4ad164bd33717d1ce3ee9/python/cpython/issues/152851
og:image:altCrash report What happened? _blake2.blake2b/_blake2.blake2s carry an impl enum that py_blake2_clear (the tp_clear/dealloc path) switches on, with a Py_UNREACHABLE() default. The .copy() method allo...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamedevdanzin
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/152851#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F152851
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F152851
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/152851
Reloadhttps://github.com/python/cpython/issues/152851
Reloadhttps://github.com/python/cpython/issues/152851
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/152851
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/152851
hashlib BLAKE2 .copy() crashes with Py_UNREACHABLE in py_blake2_clear when the copy's allocation failshttps://github.com/python/cpython/issues/152851#top
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
https://github.com/devdanzin
devdanzinhttps://github.com/devdanzin
on Jul 2, 2026https://github.com/python/cpython/issues/152851#issue-4791701339
Rewrite & cleanup HACL*-based extension modules #135532https://github.com/python/cpython/issues/135532
gh-135532: use defining_class for copying BLAKE-2 and SHA-3 objects #135838https://github.com/python/cpython/pull/135838
fusilhttps://github.com/devdanzin/fusil
@vstinnerhttps://github.com/vstinner
gh-152851: fix a crash when copying a BLAKE-2s/2b object #153008https://github.com/python/cpython/pull/153008
[3.15] gh-152851: fix a crash when copying a BLAKE-2s/2b object (GH-153008) #153011https://github.com/python/cpython/pull/153011
[3.14] gh-152851: fix a crash when copying a BLAKE-2s/2b object (GH-153008) #153012https://github.com/python/cpython/pull/153012
devdanzinhttps://github.com/devdanzin
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
on Jul 2, 2026https://github.com/python/cpython/issues/152851#event-27463258278
devdanzinhttps://github.com/devdanzin
on Jul 2, 2026https://github.com/python/cpython/issues/152851#event-27463356210
OOM-0043: filed upstream as python/cpython#152851https://github.com/devdanzin/cpython-oom-findings/commit/ec4828b3817409967bf5b39753d1ba27d99c69c8
python/cpython#152851https://github.com/python/cpython/issues/152851
ec4828bhttps://github.com/devdanzin/cpython-oom-findings/commit/ec4828b3817409967bf5b39753d1ba27d99c69c8
picnixzhttps://github.com/picnixz
picnixzhttps://github.com/picnixz
picnixzhttps://github.com/picnixz
on Jul 2, 2026https://github.com/python/cpython/issues/152851#event-27465652607
picnixzhttps://github.com/picnixz
on Jul 4, 2026https://github.com/python/cpython/issues/152851#event-27554241668
gh-152851: fix a crash when copying a BLAKE-2s/2b object (#153008)https://github.com/python/cpython/commit/5a400ceafbbd4456ac65fbdbcf5d62ce9e8e4f9f
gh-152851https://github.com/python/cpython/issues/152851
#153008https://github.com/python/cpython/pull/153008
5a400cehttps://github.com/python/cpython/commit/5a400ceafbbd4456ac65fbdbcf5d62ce9e8e4f9f
[3.15] gh-152851: fix a crash when copying a BLAKE-2s/2b object (GH-1…https://github.com/python/cpython/commit/e1320ae4c3c53e09108f2089fecab545def44d23
gh-152851https://github.com/python/cpython/issues/152851
GH-1…https://github.com/python/cpython/pull/153008
e1320aehttps://github.com/python/cpython/commit/e1320ae4c3c53e09108f2089fecab545def44d23
[3.14] gh-152851: fix a crash when copying a BLAKE-2s/2b object (GH-1…https://github.com/python/cpython/commit/d786fbdfcc9d63e0f92df60f464c46ac6b4ce292
gh-152851https://github.com/python/cpython/issues/152851
GH-1…https://github.com/python/cpython/pull/153008
d786fbdhttps://github.com/python/cpython/commit/d786fbdfcc9d63e0f92df60f464c46ac6b4ce292
picnixzhttps://github.com/picnixz
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Jul 4, 2026https://github.com/python/cpython/issues/152851#event-27554773466
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/152851
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/152851
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
type-crashA hard crash of the interpreter, possibly with a core dumphttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-crash%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.