Skip to content

Instantly share code, notes, and snippets.

@devdanzin
Last active June 25, 2026 18:16
Show Gist options
  • Select an option

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

Select an option

Save devdanzin/10e0fdaf3d89dbe394d94fbf765c70a1 to your computer and use it in GitHub Desktop.
[SUPERSEDED — duplicate of OOM-0036 / python/cpython#151818] OOM-0029: Abort: negative refcount on a MemoryError (tuple_dealloc, tupleobject.c:277)
./Include/refcount.h:520: _Py_NegativeRefcount: Assertion failed: object has negative ref count
Fatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed
object type name: MemoryError # the object whose refcount went negative
Program received signal SIGABRT, Aborted.
#8 _PyObject_AssertFailed at Objects/object.c:3278
#9 _Py_NegativeRefcount at Objects/object.c:275 # detector: object has negative ref count
#12 tuple_dealloc at Objects/tupleobject.c:277 # Py_DECREF of a tuple item (the over-decref'd MemoryError)
#13 subtype_dealloc at Objects/typeobject.c:2876
#14 _Py_Dealloc at Objects/object.c:3319
#17 list_dealloc at Objects/listobject.c:567 # a list is being torn down
# (the list->subtype->tuple dealloc cascade is incidental: it is merely where the
# already-corrupted MemoryError's refcount is next touched, not where it was over-decref'd)

⚠️ SUPERSEDED — not a distinct bug. rr reverse-execution proved this is a detector face of OOM-0036 — the _CALL_LIST_APPEND list.append()-under-MemoryError double-free, filed as python/cpython#151818 (fixes in flight). The crash site below is where the double-freed object is next touched, not the defect. Kept only as an extra reproducer — please don't pursue it separately.

Abort: negative refcount on a MemoryError (tuple_dealloc, tupleobject.c:277)

A _pyrepl.utils.disp_str OOM path over-decrefs a MemoryError; _Py_NegativeRefcount fires later when that item is freed in a list->subtype->tuple dealloc cascade.

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

Crash report

Under memory pressure a MemoryError instance is decref'd one time too many (its refcount goes negative). The corruption is detected later — when an unrelated object graph is torn down (list_dealloc → subtype_dealloc → tuple_dealloc) and that already-broken MemoryError is Py_DECREF'd again — by _Py_NegativeRefcount (object.c:275), which aborts (_PyObject_AssertFailed, debug builds). The deallocation cascade is incidental: it is only where the bad refcount is next touched, not where the unbalanced decref happened.

Reproducer

Minimal, stdlib-only (shrinkray-reduced from the _pyrepl.utils vehicle). Deterministic — aborts on every run (30/30); requires a debug build (PYTHON_GIL=1):

import _pyrepl.utils
from _testcapi import set_nomemory

for start in range(150):
    set_nomemory(start, 0)
    try:
        _pyrepl.utils.disp_str("\x004\x8A\xD5\x03")
    except MemoryError:
        pass

disp_str computes display widths; the argument is load-bearing — it mixes NUL/control bytes and high bytes (\x00 4 \x8a \xd5 \x03), exercising the control-char / wide-char path. Every simpler argument tested fails ("\x00", "\x8a", "\x00\x8a", "\x004\x03" without a high byte, and even "4\x8a\xd5\x03" without the leading NUL — all 0/20), so the specific mix is required. This narrows the over-decref to disp_str's OOM handling of this string, though the exact unbalanced-decref line is still not pinned (root cause PARTIAL).

The crash-time backtrace is obtained without gdb via ASAN_OPTIONS=...:handle_abort=1, which makes ASan print a symbolized C backtrace on the abort; the tuple_dealloc -> subtype_dealloc -> list_dealloc cascade is byte-identical on every run.

Backtrace

refcount.h:520: _Py_NegativeRefcount: Assertion failed: object has negative ref count
Fatal Python error: _PyObject_AssertFailed     (object type name: MemoryError)
#9  _Py_NegativeRefcount   Objects/object.c:275
#12 tuple_dealloc          Objects/tupleobject.c:277   # Py_DECREF(item) -> the over-decref'd MemoryError
#13 subtype_dealloc        Objects/typeobject.c:2876
#14 _Py_Dealloc            Objects/object.c:3319
#17 list_dealloc           Objects/listobject.c:567

The faulting object is a MemoryError (from the fatal's object type name).

Root cause

_Py_NegativeRefcount/_PyObject_AssertFailed are the detector, not the defect. The defect is an unbalanced Py_DECREF of a MemoryError on an allocation-failure path: under OOM, MemoryError objects are created/propagated heavily (CPython also keeps a pre-allocated MemoryError to avoid allocating while out of memory), and an error path drops one reference too many. The negative refcount then surfaces at the next unrelated decref of that object (here a tuple item freed during a subtype/list teardown). The exact over-decref site is not isolated from this single vehicle — this is a consumer-side symptom; an allocation-failure-injection run that records the decref balance for the shared/MemoryError objects would localize it.

Suggested fix

Audit Py_DECREF/Py_XDECREF of MemoryError (and the cached/pre-allocated MemoryError) on OOM error paths reachable from the _pyrepl.utils call chain — a double-decref or a decref-without-incref on the failure branch. The standard remedies are Py_XDECREF + NULL-after-free, or moving the decref out of a branch that didn't own the reference.

Notes

Found via OOM-injection fuzzing (_testcapi.set_nomemory); flagged oomNEW by fusil's in-loop dedup (it did not match the catalog). Negative-refcount-under-OOM family, but distinct from OOM-0005 (PyStackRef_XCLOSE/_PyFrame_ClearLocals, frame locals) and OOM-0019 (Py_XDECREF in _PyPegen_raise_error_known_location, the parser) — different subsystem and a different (incidental) detection chain. The shared theme is an unbalanced decref under OOM; these may or may not share a root. Debug-only: the _Py_NegativeRefcount check is Py_DEBUG-gated, so it aborts on ft_debug_asan/jit and is compiled out on the release builds (where the negative refcount is a silent use-after-free risk). Minimization DONE (2026-06-19): deterministic 8-line stdlib repro (disp_str("\x004\x8A\xD5\x03") under the sweep, 30/30) — shrinkray-reduced, oracle pinned to the [email protected]:277 cascade via the ASan handle_abort=1 symbolized backtrace (no gdb). Root cause still PARTIAL (the trigger is minimal, but the exact unbalanced-decref line inside disp_str's OOM path is not pinned — that needs a refcount watchpoint on the MemoryError).

Versions

  • main (3.16.0a0), commit 15d7406. Aborts on free-threaded debug+ASan and JIT debug+ASan; detector compiled out on the release builds.

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

"""OOM-0029 minimal reproducer (stdlib only) — reduced from the _pyrepl.utils fuzz vehicle with shrinkray.
Under a dense allocation-failure sweep, `_pyrepl.utils.disp_str(<mixed control/high string>)`
over-decrefs a `MemoryError` on an OOM error path. The corruption is silent until that
already-negative-refcount `MemoryError` is `Py_DECREF`'d again during an unrelated dealloc
cascade (`list_dealloc -> subtype_dealloc -> tuple_dealloc`), where `_Py_NegativeRefcount`
(Objects/object.c:275, reported at the caller `tuple_dealloc` Objects/tupleobject.c:277) fires
and aborts. Debug-only detector (Py_DEBUG); on release the negative refcount is a silent UAF.
Deterministic: aborts on every run (30/30, GIL=1). Requires a debug build. The crash-time
backtrace is symbolized with `ASAN_OPTIONS=...:handle_abort=1` (no gdb needed); the
`tuple_dealloc -> subtype_dealloc -> list_dealloc` cascade is identical on every run.
The argument is load-bearing: it mixes NUL/control bytes and high bytes
(`\x00`, `4`, `\x8a`, `\xd5`, `\x03`), exercising disp_str's control-char / wide-char display
path; a single character (`"\x00"`, `"\x8a"`) does not reproduce. The over-decref site itself
is not pinned to a single line (root cause PARTIAL); the full vehicle is preserved as
`vehicle_source.py`.
"""
import _pyrepl.utils
from _testcapi import set_nomemory
for start in range(150):
set_nomemory(start, 0)
try:
_pyrepl.utils.disp_str("\x004\x8A\xD5\x03")
except MemoryError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment