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
Domain: github.com
{"@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-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:7efb768b-14b3-fdc1-8abd-8552fe8604db |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 88BC:27E5CD:3DA1D2D:4EC2B2D:6B0207C2 |
| html-safe-nonce | ec561970a6d1f633c1b64f4c01c7fc75a638cbeaf71673170468ba8558ae6d16 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OEJDOjI3RTVDRDozREExRDJEOjRFQzJCMkQ6NkIwMjA3QzIiLCJ2aXNpdG9yX2lkIjoiODY5ODIwMjE5MzgxNzM3MjYxMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 93cd98f109fa9da1f85f81c70b37488b1e8cb2ccfc991a3e57c34b0f7fa640b3 |
| hovercard-subject-tag | issue:4791701339 |
| 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/152851/issue_layout |
| twitter:image | https://opengraph.githubassets.com/03b76c0770533ae7acb0bacb01b604feaa08b4479ab4ad164bd33717d1ce3ee9/python/cpython/issues/152851 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/03b76c0770533ae7acb0bacb01b604feaa08b4479ab4ad164bd33717d1ce3ee9/python/cpython/issues/152851 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | devdanzin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width