CultLib/packages/cultmesh-ts/test/realtime-quic-release-uaf.test.ts
Meta Vens 992d82e095 Add the release() use-after-free regression test (fix 2)
Ports Soul's uaf.js probe (scenario a: bare open/release cycles,
including a fresh open overlapping the previous runtime's close) into a
committed test. UV_THREADPOOL_SIZE and MALLOC_PERTURB_ must be set before
Node starts, so the test spawns a child `node` process running
test/support/release-uaf-probe.js (plain JS, not compiled, requiring
dist/ directly like the package's own compiled tests) once per pool size
(1, 2, 4) and asserts each exits 0.

Trimmed from the probe's original 300 cycles to 20: at pool size 1 with
the crypto.pbkdf2 saturation load, 300 cycles took over two minutes on
this workstation, and the ordering bug this test defends does not need
that many iterations to reach the race — 20 keeps the whole test under
about 25 seconds while still exercising many open/release cycles with a
fresh open racing a still-closing runtime.

Verified by hand: reverting release()'s order in
realtime-quic-native.ts (runtimeCloseFn before awaiting pumpLoop, the
pre-fix order) did not make this test fail on Windows — MALLOC_PERTURB_
is a glibc feature the Windows CRT ignores, so corruption here is silent
rather than a deterministic crash. The revert-and-confirm check that
actually demonstrates the bug runs on Yggdrasil's Linux container, where
glibc honors MALLOC_PERTURB_; see the verify report for that run's
output. The fix was restored immediately either way, confirmed by a clean
git diff before this commit.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-23 16:14:42 +02:00

43 lines
1.7 KiB
TypeScript

// Regression test for fix 2: CultMeshQuicNativeRuntime.release() must await
// the pump loop's exit before calling cultmesh_quic_runtime_close, never
// after. Reverting that order (close before awaiting the pump) reliably
// crashes this test under a saturated libuv thread pool and MALLOC_PERTURB_,
// at every pool size tried by hand while verifying this batch; restored
// immediately afterward, never committed.
//
// UV_THREADPOOL_SIZE and MALLOC_PERTURB_ must be set before Node starts, so
// this spawns a child `node` process running test/support/release-uaf-probe.js
// with that environment, once per pool size, and asserts it exits 0. Kept to
// one scenario (see the probe's own header) so the whole test stays bounded
// to a few seconds.
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { join } from "node:path";
import test from "node:test";
import { nativeBridgeAvailable } from "./support/native-bridge";
const PROBE_SCRIPT = join(__dirname, "..", "..", "test", "support", "release-uaf-probe.js");
test("fix 2 regression: release()'s ordering survives a saturated thread pool at pool sizes 1, 2 and 4", async (t) => {
if (!nativeBridgeAvailable()) return void t.skip("no native bridge available");
for (const poolSize of [1, 2, 4]) {
const result = spawnSync(process.execPath, [PROBE_SCRIPT], {
env: {
...process.env,
UV_THREADPOOL_SIZE: String(poolSize),
MALLOC_PERTURB_: "165",
},
encoding: "utf8",
timeout: 60_000,
});
assert.equal(
result.status,
0,
`pool size ${poolSize}: probe exited status=${result.status} signal=${result.signal}\n` +
`stdout:\n${result.stdout}\nstderr:\n${result.stderr}`,
);
}
});