Skip to content

pycvc: evaluator.stats(), evaluator_state.stats and process.state keep their owner alive - #864

Merged
transfix merged 1 commit into
masterfrom
fix/pycvc-state-exec-owner-keepalive
Oct 28, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/pycvc-state-exec-owner-keepalive

Conversation

@transfix

@transfix transfix commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Three pycvc accessors return a reference into another object: evaluator.stats() (the evaluator's stats), evaluator_state.stats (the state's stats) and process.state (the process's evaluator state). pycvc.i means each one to keep its owner alive by stashing the owner's proxy on the returned proxy (val._pycvc_owner = self). None of the three stashes was ever generated. The %pythonappend directives sit inside pycvc.i's namespace cvc { namespace state_exec { block, but they named their declarations fully qualified (cvc::state_exec::evaluator::stats), and SWIG silently ignores a feature named that way there. So Python could free the evaluator, the state or the process while a reference into it was still in use.

The new TSan job in #858 found it. It reported 13 heap-use-after-free reports in test_pycvc_state_exec_eval.py, all in the two tests written to check this keep-alive:

  • test_stats_is_a_reference_that_keeps_its_owner: evaluation_stats reads (get_step_count, is_complete, elapsed_seconds) after _wrap_delete_evaluator_state and _wrap_delete_evaluator freed the owner.
  • test_process_struct: _wrap_evaluator_state_done_get and evaluator_state.stack after _wrap_delete_process.

Both tests passed anyway, because the freed memory still held the values they read. The logs are in run 40628489064's sanitizers-tsan-logs artifact.

The fix names the three directives relative to the namespace (evaluator::stats, evaluator_state::_stats, process::_state), as the async_host ones further down already do, and adds a comment saying why. The generated pycvc.py now stashes the owner. I checked every other %pythonappend in pycvc.i against a generated pycvc.py, and these three were the only ones missing.

The two tests now take a weak reference to the owner. They check that the owner is still alive while the reference into it is, and gone once that reference is, which fails without the fix. On Windows, both tests failed against the generated pycvc.py with the three stashes removed, and with the fix all nine pycvc_exec* and pycvc_state_exec* tests pass. Until this lands, #858 suppresses these reports in .github/sanitizers/tsan.supp with an entry that names this PR.

…p their owner alive

These three return a reference into another object (the evaluator's stats, the
state's stats, the process's evaluator state), and pycvc.i means each to keep
its owner's proxy alive by stashing it on the returned proxy
(val._pycvc_owner = self). None of the three stashes was generated: the
%pythonappend directives sit inside pycvc.i's `namespace cvc { namespace
state_exec {` block but named their declarations fully qualified
(cvc::state_exec::evaluator::stats), and SWIG silently ignores a feature named
that way there. So Python could free the evaluator, state or process while a
reference into it was still in use.

The new TSan job (#858) reported it 13 times as
heap-use-after-free in test_pycvc_state_exec_eval.py, in the two tests written
to check that keep-alive: test_stats_is_a_reference_that_keeps_its_owner (reads
of evaluation_stats after _wrap_delete_evaluator_state and
_wrap_delete_evaluator) and test_process_struct (_wrap_evaluator_state_done_get
and evaluator_state.stack after _wrap_delete_process). Both tests passed
anyway, since the freed memory still held the values they read.

The directives now name the declarations relative to the namespace, as the
async_host ones further down already do, and the generated pycvc.py stashes the
owner. The two tests now hold a weak reference to the owner and check that it
is still alive while the reference is, and gone once the reference is, which
fails without the fix.
transfix added a commit that referenced this pull request Oct 28, 2026
…ressions for the baseline's two bugs

The asan-ubsan job failed to link libcvc.so: UBSan's vptr check references the
typeinfo of every polymorphic class it checks, and Jolt is built without RTTI,
as are the two libcvc files that subclass its classes, so that typeinfo exists
nowhere ("undefined reference to `typeinfo for JPH::GroupFilter'"). The job now
builds with -fno-sanitize=vptr; the rest of UBSan stays on.

The tsan job's first run found two real bugs, each fixed in its own PR, and
suppressed here until those land:
- a lock-order inversion between exec_coordinator's leader_mu_ and the message
  bus's lock, from start() announcing its election under leader_mu_ (6 reports
  in state_exec_coordinator_test and state_exec_integration_test; #863);
- pycvc's evaluator.stats(), evaluator_state.stats and process.state not
  keeping their owner alive, because their %pythonappend was never generated
  (13 heap-use-after-free reports in test_pycvc_state_exec_eval.py; #864).
transfix added a commit that referenced this pull request Oct 28, 2026
…settles its first baseline

The asan-ubsan job's first full run (40639662336) found:
- Every pycvc script stopped at its first C++ throw on ASan's CHECK that it had
  found the real __cxa_throw. ASan looks it up when it starts, and the
  interpreter is C, so libstdc++ was not loaded yet. run.sh now preloads
  libstdc++ after the ASan runtime.
- Four StepDeadlineTest cases in state_exec_scheduler_test failed with "native
  calls nested too deeply (0 levels)": libcvc measured its stack from a local,
  which ASan's use-after-return check moves to a fake stack. Fixed in #866;
  until it lands the job runs with detect_stack_use_after_return=0.
- LeakSanitizer reported state_exec's reference cycles in
  state_exec_async_test and state_exec_integration_test, all indirect leaks:
  garbage only collect_cycles() frees, and these tests exit before any
  collection. lsan.supp hides them by fixture, with the reasons.

test_pycvc_state_exec_eval.py is not run under ASan until #864 lands: it would
stop at the heap-use-after-free that #864 fixes, and ASan can't suppress one.
run.sh's py_skip lists it, and the job summary shows it as skipped.
@transfix
transfix marked this pull request as ready for review October 28, 2026 15:55
@transfix
transfix merged commit e7b28ac into master Oct 28, 2026
24 of 25 checks passed
transfix added a commit that referenced this pull request Oct 28, 2026
transfix added a commit that referenced this pull request Oct 28, 2026
#864 (pycvc keep-alives for references into an evaluator, a state or a
process) is merged, so tsan.supp no longer hides its heap-use-after-free
reports and test_pycvc_state_exec_eval.py runs under ASan again. py_skip stays,
empty, for the next report ASan can't suppress.
transfix added a commit that referenced this pull request Oct 28, 2026
The project version and the cvcpkg recipe family move to 3.7.2. Every recipe is at cvc_revision 1
already, so none restarts.

3.7.2 carries the dynamic plants on the terrain and in a Jolt world, and the four-wheel plant
(#869): --plant dynamic and dynamic4w drive the ground, collide with the
buildings and each other, and book the collisions. It also carries dyn_4w's port-Hamiltonian form
(#850), dyn_bike's wheel-frame front slip (#852), the books goldens for GRL-SNAM's twins (#848,
#849), pycvc's drive telemetry (#857) and keep-alives (#864), the macOS SDK's openssl (#846), and
the state_exec scheduler and evaluator work (#842, #843, #844, #861, #863).
transfix added a commit that referenced this pull request Oct 28, 2026
… deep workflow (#858)

* ci: TSan and ASan+UBSan jobs over the threaded state_exec suites, and the locks deep workflow

The state_exec lock work (docs/STATE_EXEC_LOCKS.md) adds code that runs on
several threads, and no CI lane ran a race detector or an address checker. This
lands them before any lock code, so that the races and leaks already in the
tree get a baseline first.

- ci.yml `sanitizers` (tsan, asan-ubsan): gcc's -fsanitize=thread, or
  -fsanitize=address,undefined, over libcvc itself (the cvcpkg deps stay
  uninstrumented), RelWithDebInfo, building only the targets it runs. It runs
  the existing suites that use threads (state_exec_scheduler, _async,
  _integration and _coordinator tests, ariadne_runtime_test, thread_pool_test,
  async_lane_test) and pycvc's exec scripts, with the sanitizer runtime
  preloaded into the interpreter. Suppressions go in .github/sanitizers/, each
  with a comment naming what it hides. Each lock phase appends its suites.
- ci.yml's pull_request filter takes perf/** as well, so the lock phases'
  stacked drafts get the full set of jobs.
- locks-deep.yml (daily, and by hand on a branch): a Release Linux build of the
  lock test targets, running their deep sizes. A dispatchable workflow must be
  on the default branch, so it lands first; on a ref without the lock tests it
  stops after the checkout.

* ci: sanitizers / asan-ubsan without UBSan's vptr check, and TSan suppressions for the baseline's two bugs

The asan-ubsan job failed to link libcvc.so: UBSan's vptr check references the
typeinfo of every polymorphic class it checks, and Jolt is built without RTTI,
as are the two libcvc files that subclass its classes, so that typeinfo exists
nowhere ("undefined reference to `typeinfo for JPH::GroupFilter'"). The job now
builds with -fno-sanitize=vptr; the rest of UBSan stays on.

The tsan job's first run found two real bugs, each fixed in its own PR, and
suppressed here until those land:
- a lock-order inversion between exec_coordinator's leader_mu_ and the message
  bus's lock, from start() announcing its election under leader_mu_ (6 reports
  in state_exec_coordinator_test and state_exec_integration_test; #863);
- pycvc's evaluator.stats(), evaluator_state.stats and process.state not
  keeping their owner alive, because their %pythonappend was never generated
  (13 heap-use-after-free reports in test_pycvc_state_exec_eval.py; #864).

* ci: sanitizers / asan-ubsan runs pycvc with libstdc++ preloaded, and settles its first baseline

The asan-ubsan job's first full run (40639662336) found:
- Every pycvc script stopped at its first C++ throw on ASan's CHECK that it had
  found the real __cxa_throw. ASan looks it up when it starts, and the
  interpreter is C, so libstdc++ was not loaded yet. run.sh now preloads
  libstdc++ after the ASan runtime.
- Four StepDeadlineTest cases in state_exec_scheduler_test failed with "native
  calls nested too deeply (0 levels)": libcvc measured its stack from a local,
  which ASan's use-after-return check moves to a fake stack. Fixed in #866;
  until it lands the job runs with detect_stack_use_after_return=0.
- LeakSanitizer reported state_exec's reference cycles in
  state_exec_async_test and state_exec_integration_test, all indirect leaks:
  garbage only collect_cycles() frees, and these tests exit before any
  collection. lsan.supp hides them by fixture, with the reasons.

test_pycvc_state_exec_eval.py is not run under ASan until #864 lands: it would
stop at the heap-use-after-free that #864 fixes, and ASan can't suppress one.
run.sh's py_skip lists it, and the job summary shows it as skipped.

* ci: sanitizers drop the #864 workarounds now that it is on master

#864 (pycvc keep-alives for references into an evaluator, a state or a
process) is merged, so tsan.supp no longer hides its heap-use-after-free
reports and test_pycvc_state_exec_eval.py runs under ASan again. py_skip stays,
empty, for the next report ASan can't suppress.

* ci: sanitizers drop the #863 suppression now that it is on master

#863 (exec_coordinator::start() announcing its election without its leader
lock) is merged, so tsan.supp no longer hides the lock-order inversion it fixed.
tsan.supp is empty again.

* ci: sanitizers drop the #866 workaround now that it is on master

#866 (the native-stack guard measuring from the frame, not a local) is merged,
so asan-ubsan runs with ASan's default use-after-return check again, which also
checks #866 under gcc's ASan. No workaround for a fix in flight is left.
@transfix
transfix deleted the fix/pycvc-state-exec-owner-keepalive branch October 29, 2026 20:54
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