Skip to content

tests: contract.register publishes a probe and its kernel together, in every suite - #5058

Merged
ashhart merged 2 commits into
mainfrom
lead/contract-register
Nov 25, 2026
Merged

ashhart merged 2 commits into
mainfrom
lead/contract-register

Conversation

@ashhart

@ashhart ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Kernel-contract suites registered each probe with two fallible appends:

try s.probes.append(s.a, p);
try s.kernels.append(s.a, p.kernel());

If the second append fails, the probe's own errdefer frees p, but p is already in s.probes, so the suite's teardown frees it again. Suites with no errdefer leak on any failure instead. The pattern was in 85 places across 59 suites, and about ten open pull requests copied it and were asked in review to fix it.

The fix: contract.register (zig/src/core/contract/register.zig):

  • reserves room in both lists first;
  • then publishes with appendAssumeCapacity on both, so ownership moves in one step that can't fail halfway.

On error.OutOfMemory neither list holds the probe, and the caller's errdefer still frees it. Usage:

errdefer p.deinit(); // with errdefer s.a.destroy(p) after create, as the suite's teardown frees it
try ct.register(s.a, &s.probes, &s.kernels, p, p.kernel());

What changed:

  • All 85 sites call the helper.
  • 107 errdefers were added, each mirroring its suite's teardown and running only until registration.
  • Test code and the contract harness only: no kernel, engine or token change.

Test: register_test.zig (in test-kernel-contract) counts frees under std.testing.FailingAllocator:

Pattern Kernel list's growth fails
two appends double free
two appends without errdefer leak when the probe list fails
register freed exactly once in every case

The test fails when register's body is swapped back to the old two appends.

Checks:

  • test-kernel-contract passes on Linux.

  • tf-kernel-contract type-checks for macOS.

  • fmt, AST and the line caps are clean; lean is 164, as on main.

  • Synthena

…ister

The kernel-contract suites published each probe with two fallible appends, its list then the kernel list.
When the second failed, the probe was already in its list: a suite whose errdefer frees the probe freed it
twice (the errdefer, then Suite.deinit), and one without an errdefer leaked it on any earlier failure.

contract.register (zig/src/core/contract/register.zig) reserves room in both lists, then publishes with
appendAssumeCapacity on both, so a failure leaves neither holding the probe and the caller's errdefer
still owning it; once it returns, the lists own the probe. It is generic over the probe and takes the kernel
value, so a suite whose kernel is p.kernel(name) or a literal uses it too; either list may be passed as a
pointer or as a pointer field.

All 85 two-append sites on main (59 files under zig/tests/contract) now call it, and every probe has an
errdefer that mirrors its suite's teardown (deinit once initialized, destroy where the teardown destroys)
and runs only until it is published: 107 errdefers were added where a suite had none or only half.
register_test.zig, run by test-kernel-contract, counts a probe's frees under std.testing.FailingAllocator:
the old pattern frees it twice when the kernel list's growth fails and never when the probe list's does;
register frees it exactly once either way, and once at teardown on success. Test code and the contract
harness only: no kernel, engine or token change.
@ashhart
ashhart force-pushed the lead/contract-register branch from 36f102e to a5bbb03 Compare November 25, 2026 15:27
@ashhart
ashhart merged commit c328b35 into main Nov 25, 2026
2 of 12 checks passed
@ashhart
ashhart deleted the lead/contract-register branch November 25, 2026 15:27
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