Skip to content

Free the result of an instrumented comparison - #119

Open
ilgrad wants to merge 1 commit into
google:masterfrom
ilgrad:fix/trace-cmp-refcount
Open

ilgrad wants to merge 1 commit into
google:masterfrom
ilgrad:fix/trace-cmp-refcount

Conversation

@ilgrad

@ilgrad ilgrad commented Nov 30, 2026

Copy link
Copy Markdown

Problem

Every comparison in instrumented code goes through _trace_cmp, and its result is never freed. _trace_cmp (and prefuzz_trace_cmp before Fuzz()) gets a new reference from PyObject_RichCompare and returns it as a py::handle. pybind11's caster for py::handle return values increments the reference count again (return_value_policy::move does not change that for handles), so each result keeps one reference that nobody owns.

True and False are immortal, so plain int/str/bytes comparisons hide this. A rich comparison that returns a new object does not: a numpy or JAX array, a JAX tracer, or any __eq__/__lt__ that returns an object. A JAX harness that checks jnp.all(x <= 1.0) loses the comparison's array each time. In my case a JAX fuzz target grew by about 0.5 MB per input and hit libFuzzer's 2 GB RSS limit after 30 minutes. Most of the memory was live BatchTracer and ArrayImpl objects that nothing in Python referred to.

Reproducer

# cmpmod.py
def eq(a, b):
    return a == b
import gc, sys, weakref
import atheris
with atheris.instrument_imports(include=["cmpmod"]):
    import cmpmod

class Result: pass
class Box:
    def __eq__(self, other): return Result()
    __hash__ = object.__hash__

refs = [weakref.ref(cmpmod.eq(Box(), 1.0)) for _ in range(1000)]
gc.collect()
print(sum(r() is not None for r in refs), "of 1000 results alive")

With atheris 3.1.0 from PyPI and with master (e36da74) built on CPython 3.14.10 and clang 23, all 1000 results stay alive. 200 comparisons ndarray(100_000) <= 1.0 hold 19.1 MB.

Fix

_trace_cmp in core.cc and unittest.cc, and prefuzz_trace_cmp in atheris.cc, now return py::object and take ownership with py::reinterpret_steal<py::object>(ret). The declaration in atheris.h changes with them. The error path is unchanged.

Tests

  • fuzz_test.py, testComparisonResultFreed: an instrumented function compares an object whose __eq__ returns a new object, and checks through a weakref that the result is freed. This covers _trace_cmp after Fuzz() starts.
  • instrument_bytecode_test.py, test_instrumented_comparison_result_is_freed: the same check for a function patched with trace_dataflow=True before Fuzz(), which covers prefuzz_trace_cmp.

Both tests fail without the fix: the first prints Comparison result leaked. Both pass with it. With the fix the reproducer shows 0 of 1000 results alive, and the 200 numpy comparisons hold 0.0 MB.

I ran every *_test.py under src/ in a CPython 3.14 venv with the extension built against the system clang 23 libFuzzer; all 12 files pass. ruff check src reports the same findings as on master; I did not run pytype, and I did not run run_tests.sh itself, which builds its own virtualenv.

_trace_cmp returned the new reference from PyObject_RichCompare as a
py::handle, and pybind11 adds a reference when it converts a handle to a
return value, so every comparison result that is not cached by the
interpreter (an ndarray, a JAX array or tracer, any custom object) was never
freed. Return a py::object that steals the reference instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant