Repository navigation
Conversation
_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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Every comparison in instrumented code goes through
_trace_cmp, and its result is never freed._trace_cmp(andprefuzz_trace_cmpbeforeFuzz()) gets a new reference fromPyObject_RichCompareand returns it as apy::handle. pybind11's caster forpy::handlereturn values increments the reference count again (return_value_policy::movedoes not change that for handles), so each result keeps one reference that nobody owns.TrueandFalseare immortal, so plainint/str/bytescomparisons 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 checksjnp.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 liveBatchTracerandArrayImplobjects that nothing in Python referred to.Reproducer
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 comparisonsndarray(100_000) <= 1.0hold 19.1 MB.Fix
_trace_cmpincore.ccandunittest.cc, andprefuzz_trace_cmpinatheris.cc, now returnpy::objectand take ownership withpy::reinterpret_steal<py::object>(ret). The declaration inatheris.hchanges 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 aweakrefthat the result is freed. This covers_trace_cmpafterFuzz()starts.instrument_bytecode_test.py,test_instrumented_comparison_result_is_freed: the same check for a function patched withtrace_dataflow=TruebeforeFuzz(), which coversprefuzz_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.pyundersrc/in a CPython 3.14 venv with the extension built against the system clang 23 libFuzzer; all 12 files pass.ruff check srcreports the same findings as onmaster; I did not runpytype, and I did not runrun_tests.shitself, which builds its own virtualenv.