Skip to content

Instantly share code, notes, and snippets.

@devdanzin
Last active June 20, 2026 01:08
Show Gist options
  • Select an option

  • Save devdanzin/b628c59722820b55c61a400a973771d5 to your computer and use it in GitHub Desktop.

Select an option

Save devdanzin/b628c59722820b55c61a400a973771d5 to your computer and use it in GitHub Desktop.
OOM-0003: Abort: uninitialized `_co_unique_id` assert in `code_dealloc` (`codeobject.c:2440`)
# Authoritative gdb backtrace (3.16_ft_debug_asan, commit 15d7406)
# Abort: assert(co->_co_unique_id == _Py_INVALID_UNIQUE_ID) in code_dealloc.
# co->_co_unique_id holds uninitialized garbage: init_code() failed before
# _PyCode_New() reached the line that assigns the unique id.
Program received signal SIGABRT, Aborted.
#0 __pthread_kill_implementation (threadid=..., signo=..., no_tid=...) at ./nptl/pthread_kill.c:44
#1 __pthread_kill_internal (threadid=..., signo=...) at ./nptl/pthread_kill.c:89
#2 __GI___pthread_kill (threadid=..., signo=...) at ./nptl/pthread_kill.c:100
#3 __GI_raise (sig=...) at ../sysdeps/posix/raise.c:26
#4 __GI_abort () at ./stdlib/abort.c:77
#5 __libc_message_impl (...) at ../sysdeps/posix/libc_fatal.c:138
#6 __libc_message_wrapper (...) at ../include/stdio.h:203
#7 __assert_fail (assertion=..., file=..., line=..., function=...) at ./assert/assert.c:37
#8 code_dealloc (self=...) at Objects/codeobject.c:2440 <- assert co->_co_unique_id == _Py_INVALID_UNIQUE_ID
#9 _Py_Dealloc (op=...) at Objects/object.c:3319
#10 Py_DECREF (lineno=..., op=..., filename=...) at ./Include/refcount.h:359
#11 _PyCode_New (con=...) at Objects/codeobject.c:747 <- Py_DECREF(co) after init_code(co, con) < 0
#12 r_object (p=...) at Python/marshal.c:1676 <- minimal repro path (marshal.loads)
#13 r_object (p=...) at Python/marshal.c:1426
# Original fuzzer vehicle reaches _PyCode_New the same way, via
# importlib._bootstrap_external._compile_bytecode -> marshal during an import under OOM.

Abort: uninitialized _co_unique_id assert in code_dealloc (codeobject.c:2440)

_PyCode_New sets _co_unique_id only after init_code() succeeds; when init_code's tlbc allocation fails under OOM, the resulting Py_DECREF(co) reaches code_dealloc, which asserts on the never-initialized (garbage) field on free-threaded debug builds.

AI Disclaimer: this gist was drafted by Claude Code, which also generated the reduced reproducer.

Crash report

On free-threaded builds, _PyCode_New() assigns co->_co_unique_id only after init_code() succeeds. The field is not zero-initialized by the PyObject_GC_NewVar allocation. Under OOM, an allocation inside init_code() (the thread-local bytecode array, _PyCodeArray_New) fails, _PyCode_New() runs Py_DECREF(co), and code_dealloc() asserts on the never-initialized _co_unique_id (garbage, not 0), aborting the interpreter.

Reproducer

import marshal, _testcapi, faulthandler
faulthandler.enable()
blob = marshal.dumps(compile("def f(x):\n    return x + 1\n", "<gen>", "exec"))
_testcapi.set_nomemory(9, 0)   # fail every allocation from #9 onward
try:
    marshal.loads(blob)        # _PyCode_New -> init_code fails -> Py_DECREF(co) -> assert
finally:
    _testcapi.remove_mem_hooks()

Deterministic at start=9 on the free-threaded debug+ASan build. marshal.loads is just a convenient way to drive _PyCode_New; the OOM budget must be large enough for the code-object allocation to succeed but small enough that a later allocation inside init_code fails.

Backtrace

#8  code_dealloc      Objects/codeobject.c:2440   <- assert co->_co_unique_id == _Py_INVALID_UNIQUE_ID
#9  _Py_Dealloc       Objects/object.c:3319
#10 Py_DECREF         Include/refcount.h:359
#11 _PyCode_New       Objects/codeobject.c:747    <- Py_DECREF(co) after init_code(co, con) < 0
#12 r_object          Python/marshal.c:1676       (marshal.loads)

(gdb) frame 8; print co->_co_unique_id -> a nonzero garbage value (never set to _Py_INVALID_UNIQUE_ID, which is 0).

Root cause

Objects/codeobject.c, _PyCode_New() (L716):

    co = PyObject_GC_NewVar(PyCodeObject, &PyCode_Type, size);   /* L736: does NOT zero _co_unique_id */
    ...
    if (init_code(co, con) < 0) {
        Py_DECREF(co);                 /* L747: -> code_dealloc on a half-built code object */
        return NULL;
    }
#ifdef Py_GIL_DISABLED
    co->_co_unique_id = _PyObject_AssignUniqueId((PyObject *)co);   /* L752: only set AFTER init_code succeeds */
    _PyObject_GC_TRACK(co);
#endif

init_code() (L511) sets many fields but never initializes co->_co_unique_id. Its free-threaded branch allocates the tlbc array (L570):

    co->co_tlbc = _PyCodeArray_New(INITIAL_SPECIALIZED_CODE_SIZE);   /* PyMem_Calloc, fails under OOM */
    if (co->co_tlbc == NULL) {
        return -1;                     /* -> _PyCode_New runs Py_DECREF(co) */
    }

When that PyMem_Calloc fails, init_code returns -1 before line 752 ever runs, so _co_unique_id still holds uninitialized heap garbage from PyObject_GC_NewVar. code_dealloc() (L2440) then asserts co->_co_unique_id == _Py_INVALID_UNIQUE_ID and aborts. The defect is a missing initialization, not a use-after-free.

Suggested fix

Initialize the field before any path that can reach code_dealloc. Either in init_code() near the other field inits:

#ifdef Py_GIL_DISABLED
    co->_co_unique_id = _Py_INVALID_UNIQUE_ID;
#endif

or in _PyCode_New() immediately after the PyObject_GC_NewVar NULL-check, before init_code(co, con). (The later real assignment at L752 then overwrites it on the success path.)

Notes

Found by OOM-injection fuzzing (set_nomemory). Free-threading-specific: both _co_unique_id and the code_dealloc assert are gated on #ifdef Py_GIL_DISABLED. Reproduces as an abort only on the free-threaded debug build -- release builds define -DNDEBUG, so the assert is compiled out. On the FT release build the same uninitialized _co_unique_id instead feeds the deferred-refcount machinery and can segfault at a different site (latent memory-safety hazard); GIL builds (jit, upstream) lack the field entirely and run the reproducer cleanly. Per the OOM-catalog convention for assert-based aborts, the non-debug builds are recorded as n/a.

Four fuzzer vehicles (random, _osx_support, tabnanny, xmlrpc_client) all abort at the identical codeobject.c:2440 assertion; each merely imports a stdlib module under OOM, which unmarshals its .pyc via importlib._bootstrap_external._compile_bytecode and reaches _PyCode_New.

Versions

  • main (3.16.0a0, commit 15d7406); aborts on the free-threaded debug+ASan build. Release/JIT/upstream builds: assertion compiled out (n/a).

Part of python/cpython#151763 — an umbrella tracking 35 OOM-related crash findings.

"""
Minimal reproducer: abort on assert(co->_co_unique_id == _Py_INVALID_UNIQUE_ID)
in code_dealloc() when init_code() fails under OOM.
Affected: CPython 3.16.0a0 (main), free-threaded (Py_GIL_DISABLED) builds only.
The assertion is compiled in on free-threaded DEBUG builds; release
builds define NDEBUG so assert() is a no-op (see Notes).
Crash: SIGABRT, Objects/codeobject.c:2440
Assertion `co->_co_unique_id == _Py_INVALID_UNIQUE_ID' failed.
Requires: a free-threaded debug build exposing _testcapi.set_nomemory.
Backtrace (gdb):
#8 code_dealloc Objects/codeobject.c:2440 (assert _co_unique_id == INVALID)
#9 _Py_Dealloc Objects/object.c:3319
#10 Py_DECREF Include/refcount.h:359
#11 _PyCode_New Objects/codeobject.c:747 (Py_DECREF(co) after init_code(...) < 0)
#12 r_object Python/marshal.c:1676 (marshal.loads -> rebuild code object)
Root cause (Objects/codeobject.c):
_PyCode_New() (L716) allocates the code object with PyObject_GC_NewVar()
(L736), which does NOT zero-initialize the trailing _co_unique_id field.
It then calls init_code(co, con) (L746). The _co_unique_id field is only
assigned *after* init_code() succeeds, at L752:
if (init_code(co, con) < 0) {
Py_DECREF(co); // L747: init_code failed -> dealloc
return NULL;
}
co->_co_unique_id = _PyObject_AssignUniqueId((PyObject *)co); // L752
Inside init_code() the free-threaded branch allocates the thread-local
bytecode array (L570):
co->co_tlbc = _PyCodeArray_New(INITIAL_SPECIALIZED_CODE_SIZE);
if (co->co_tlbc == NULL) { // fails under OOM
return -1; // -> _PyCode_New does Py_DECREF(co)
}
Under OOM that PyMem_Calloc fails, init_code returns -1, and Py_DECREF(co)
runs code_dealloc() on a code object whose _co_unique_id was never set to
_Py_INVALID_UNIQUE_ID (== 0). The field holds uninitialized garbage, so the
debug-only assert at L2440 fails and aborts.
The OOM sweep is needed so the code-object allocation itself succeeds (start
small) while a later allocation inside init_code (the tlbc array) fails.
Observed crash at start=9 on this build; a single set_nomemory(9, 0) suffices.
Likely fix: initialize co->_co_unique_id = _Py_INVALID_UNIQUE_ID in init_code()
(or right after PyObject_GC_NewVar in _PyCode_New) before any path that can lead
to code_dealloc().
Self-sweeping: `python repro.py` runs the trigger under set_nomemory(N, 0) for N in a
sweep, each in a FRESH subprocess (a fresh process avoids cache warm-up shifting the OOM
window), and stops at the first N that crashes. Needs a free-threaded debug build (the
assert is compiled out under NDEBUG and the field is gated on Py_GIL_DISABLED). Bare
trigger (fixed N=9):
import marshal, _testcapi
blob = marshal.dumps(compile("def f(x):\n return x + 1\n", "<gen>", "exec"))
_testcapi.set_nomemory(9, 0)
marshal.loads(blob)
"""
import os
import sys
import subprocess
TRIGGER = r"""
import marshal
import _testcapi
import faulthandler
faulthandler.enable()
# Build a marshalled code object up front (before any OOM injection), so the
# crashing work is purely the code-object reconstruction in marshal.loads().
blob = marshal.dumps(compile("def f(x):\n return x + 1\n", "<gen>", "exec"))
_testcapi.set_nomemory({n}, 0) # fail every allocation from #{n} onward
try:
marshal.loads(blob) # _PyCode_New: init_code's _PyCodeArray_New fails
# -> Py_DECREF(co) -> code_dealloc assert -> SIGABRT
finally:
_testcapi.remove_mem_hooks()
"""
SIGNATURE = "co->_co_unique_id == _Py_INVALID_UNIQUE_ID"
def main():
env = {**os.environ, "ASAN_OPTIONS": "detect_leaks=0:abort_on_error=0"}
env["PYTHON_GIL"] = "0" # free-threading-only bug: needs the GIL disabled
for n in range(80):
out = subprocess.run([sys.executable, "-c", TRIGGER.format(n=n)],
capture_output=True, text=True, env=env)
if SIGNATURE in out.stdout + out.stderr:
print("reproduced at set_nomemory(%d, 0):" % n)
sys.stdout.write(out.stderr or out.stdout)
return 1
print("no crash in range(80); widen it for your build")
return 0
if __name__ == "__main__":
raise SystemExit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment