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/22b71f61343c81df5bea9b7fca798e87 to your computer and use it in GitHub Desktop.

Select an option

Save devdanzin/22b71f61343c81df5bea9b7fca798e87 to your computer and use it in GitHub Desktop.
[SUPERSEDED — duplicate of OOM-0036 / python/cpython#151818] OOM-0005: Abort: negative-refcount over-decref in `_PyFrame_ClearLocals` (frame.c:101)
OOM-0005 -- eval-loop stackref over-decref under OOM, caught as a clean heap-use-after-free
because the over-decref'd local is ALSO referenced elsewhere (on a plain debug build the same
over-decref surfaces earlier as a _Py_NegativeRefcount abort at the PyStackRef_XCLOSE site).
Reduced repro: repro.py (pkgutil.get_importer(str) -> os.fsdecode -> os.fspath, under set_nomemory).
============================================================================================
(A) debug-gil-nojit-asan -- AddressSanitizer: heap-use-after-free (commit 1b9fe5c)
SUMMARY: heap-use-after-free Include/refcount.h:286 in Py_INCREF
READ of size 4 (the later use of the freed object):
#0 Py_INCREF Include/refcount.h:286
#1 Py_XINCREF Include/refcount.h:510
#2 _Py_XNewRef Include/refcount.h:542
#3 _Py_dict_lookup_threadsafe Objects/dictobject.c:1729
#4 _PyDict_SubscriptKnownHash Objects/dictobject.c:3711
#5 _PyEval_EvalFrameDefault Python/generated_cases.c.h:686
FREED by (the OOM-0005 over-decref -- THE BUG):
#1 unicode_dealloc Objects/unicodeobject.c:1710
#2 _Py_Dealloc Objects/object.c:3319
#3 Py_DECREF_MORTAL Include/internal/pycore_object.h:419
#4 PyStackRef_XCLOSE Include/internal/pycore_stackref.h:726 <- the bug
#5 _PyFrame_ClearLocals Python/frame.c:101 <- the bug
#6 _PyFrame_ClearExceptCode Python/frame.c:126
#7 clear_thread_frame Python/ceval.c:1954
#8 _PyEval_EvalFrameDefault Python/generated_cases.c.h:13908 (exit_unwind)
PREVIOUSLY ALLOCATED (the victim object = str(0), "0"):
#1 _PyMem_DebugRawMalloc Objects/obmalloc.c:3136
#4 PyUnicode_New Objects/unicodeobject.c:1326
#5 long_to_decimal_string_internal Objects/longobject.c:2233
#6 long_to_decimal_string Objects/longobject.c:2323
#7 PyObject_Str Objects/object.c:826
============================================================================================
(B) debug-ft-nojit-asan -- the same freed local is instead used by os.fspath -> SIGSEGV
(faulthandler: "Fatal Python error: Segmentation fault"; gdb: path->ob_type == 0xdddddddddddddddd)
#5 PyType_HasFeature Include/object.h:755 reads Py_TYPE(path)->tp_flags on the freed arg
#6 PyOS_FSPath Modules/posixmodule.c:17168
#7 os_fspath_impl Modules/posixmodule.c:17214
#8 os_fspath Modules/clinic/posixmodule.c.h:12574
#9 _Py_BuiltinCallFastWithKeywords_StackRef Python/ceval.c:841 (pkgutil.get_importer -> os.fsdecode)
So PyOS_FSPath / os.fspath is NOT a distinct bug: it is one downstream USE of the local that
_PyFrame_ClearLocals over-decrefs. The over-decref (frames #4-#8 of stack A) is OOM-0005.

[SUPERSEDED — duplicate of python/cpython#151818] eval-loop stackref over-decref under OOM (_PyFrame_ClearLocals, frame.c:101)

Update (2026-06-25). This was originally filed as a distinct eval-loop stackref over-decref ("OOM-0005"). Reverse-execution under rr later showed that both of its reproducers are in fact the list.append()-under-MemoryError double-free tracked in python/cpython#151818 (_CALL_LIST_APPEND steals the appended item and _PyList_AppendTakeRef decrefs it on the failed resize, but ERROR_NO_POP leaves the dead stackref on the value stack to be closed a second time during the unwind). _PyFrame_ClearLocals / PyStackRef_XCLOSE (frame.c:101 / pycore_stackref.h:726) is the detector, not a separate defect. There is no distinct bug here — see #151818.

The reproducer below is retained as an additional pure-stdlib reproducer of #151818.

AI disclaimer: this reproducer and write-up were produced with Claude Code; reviewed and reproduced.

What it shows (a reproducer for #151818)

A str argument is threaded through pkgutil.get_importer(s) → os.fsdecode(s) → os.fspath(s) while a second reference to it is kept in a dict. Under an allocation-failure sweep, the argument is appended to a list whose grow fails; the failed _CALL_LIST_APPEND double-frees it (the item has a live second reference), and the second close lands during the frame unwind at _PyFrame_ClearLocals → PyStackRef_XCLOSE. Because the dict still references the freed string, a later access is a use-after-free.

Reproducer

Deterministic (≥5/5) on debug+ASan builds (free-threaded and GIL); needs a debug build exposing _testcapi.set_nomemory:

import pkgutil
import faulthandler
faulthandler.enable()
from _testcapi import set_nomemory

d = {"s": str}
d["arg"] = d["s"](0)            # a heap str ("0"), also kept alive by this dict

def sweep(thunk):
    for start in range(60):
        set_nomemory(start)     # fail every allocation from #start onward
        try:
            thunk()
        except BaseException:
            pass

def call_get_importer():
    pkgutil.get_importer(d["arg"])

sweep(call_get_importer)
  • debug GIL + ASan: ASan heap-use-after-free; freed-by = PyStackRef_XCLOSE@pycore_stackref.h:726 ← _PyFrame_ClearLocals@frame.c:101 (exit_unwind); read = a later Py_INCREF via a dict lookup; allocated-by = the victim str.
  • debug free-threaded + ASan: the freed local is read by PyOS_FSPath → PyType_HasFeature on freed memory → SIGSEGV.
  • plain debug build: caught earlier as _Py_NegativeRefcount at the PyStackRef_XCLOSE site.

rr reverse-execution from the crash back through the victim's refcount history shows the freeing chain runs through _PyList_AppendTakeRefListResize (Objects/listobject.c:531) ← _CALL_LIST_APPEND — i.e. #151818. See OOM-0005-backtrace.txt for the captured stacks.

Versions

CPython main (3.16.0a0), commit 1b9fe5c. Same fix as #151818.

"""OOM-0005 reproducer (stdlib only) -- eval-loop stackref over-decref under OOM.
`_PyFrame_ClearLocals` (Python/frame.c:101) closes a `_PyStackRef` still on a frame's value
stack via `PyStackRef_XCLOSE` during the exception-unwind (`exit_unwind`) path. Under OOM an
error path left that stackref dangling, so the close drives the referenced object's refcount
below zero. On a debug build this aborts (`_Py_NegativeRefcount`); when the object is ALSO
referenced elsewhere, the close frees a still-live object -> a real use-after-free.
This reproducer demonstrates the use-after-free directly. The freed object is the `str`
argument threaded through `pkgutil.get_importer(s) -> os.fsdecode(s) -> os.fspath(s)`; the same
string is also kept alive in a dict, so the over-decref frees it while still referenced:
- debug-gil-nojit-asan: ASan reports a clean `heap-use-after-free` whose freed-by stack is
exactly this site (`PyStackRef_XCLOSE`@pycore_stackref.h:726 <- `_PyFrame_ClearLocals`@frame.c:101,
exit_unwind); the later read is a `Py_INCREF` via a dict lookup; the alloc is the victim str.
- debug-ft-nojit-asan: the same freed local is instead used by `PyOS_FSPath`
(posixmodule.c:17168) -> `PyType_HasFeature` reads `Py_TYPE(path)->tp_flags` on freed memory
(ob_type == 0xdd) -> SIGSEGV.
So the `PyOS_FSPath`/`os.fspath` segv is not a separate bug -- it is one downstream USE of the
local that `_PyFrame_ClearLocals` over-decrefs.
Deterministic (>=5/5) on the free-threaded and GIL debug+ASan builds; requires a build exposing
`_testcapi.set_nomemory`. The structure matters -- the crashing call must run through a nested
Python frame (the `sweep(thunk)` wrapper), matching how the fuzzer's `oom_run(thunk)` invokes it;
a flat module-level loop does not reproduce. See backtrace.txt.
rr note (2026-06-24): `rr` reverse-execution confirmed the over-decref here is NOT the
`_CALL_LIST_APPEND` list.append double-free (that is OOM-0036, python/cpython#151818). The str
argument is referenced by the holding dict + several call frames + the raised OSError's fields +
an args tuple, and the OOM-unwind dealloc cascade decrefs it one time too many. (An earlier
`xml.dom.minidom.parse(0)` reproducer for OOM-0005 was found by rr to actually be OOM-0036 and
was removed; it now lives in the OOM-0036 report as repro_xml_minidom.py.)
Run: ./python repro.py # ft-debug-asan: SIGSEGV in PyOS_FSPath ; gil-debug-asan: ASan heap-use-after-free
"""
import pkgutil
import faulthandler
faulthandler.enable()
from _testcapi import set_nomemory
d = {"s": str}
d["arg"] = d["s"](0) # a heap str ("0"), also kept alive by this dict
def sweep(thunk):
for start in range(60):
set_nomemory(start) # fail every allocation from #start onward
try:
thunk()
except BaseException:
pass
def call_get_importer():
pkgutil.get_importer(d["arg"]) # -> os.fsdecode -> os.fspath; the arg is freed mid-unwind
sweep(call_get_importer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment