From 66830e92bec67548c1231db9c440ad1793f024f9 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 22 Sep 2026 05:33:19 +0200 Subject: [PATCH] Record the wait, not its shadow: a dev-only seam for cultmesh_quic_next_event MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Self's ruling of 2026-09-22 called it after seven Soul passes: a wall-clock probe against cultmesh_quic_next_event's timeout can never tell the host's argument from a clamp, floor, round, scale or later-poll fault that happens to be the identity at the probe's own value, because there is always another function that matches. Move the observation to where the rule is decided. Add cultmesh_quic_debug_last_wait_ms / _reset_last_wait_ms, built exactly like the existing hold seam: gated behind CULTMESH_QUIC_DEBUG_ASSERTS, absent from a shipped build. CULTMESH_QUIC_DEBUG_RECORD_WAIT wraps the literal timeout_ms handed to the condition wait as a pass-through, so what is recorded and what is actually waited on are the same evaluation and cannot diverge except by changing what the macro wraps. Two scenarios: - waitseam asserts the recorded wait equals the host's own argument, exactly, over 0, 1, 15, 16, 39, 40, 999, 1000, 1001, 7300 and INT32_MAX (the header states no narrower maximum), and over 40 polls on one runtime past the point every prior scenario stopped asking. timeout_ms <= 0 never reaches the wait at all, so its probe checks the honest converse — the seam stays untouched — which is what catches a seam that records the raw argument on every call instead of only the value it actually hands its wait. - pollhammer runs one thread hammering every gate-touching entry point that cannot itself queue an event, in a yield loop, against another thread's idle poll, once on a clean runtime and once after a refused listener_open has left runtime->error non-empty. The idle poll must still stay for its timeout. polltimeout, pollbusy and holdtimeout stay as generous-margin proof that the recorded wait is really waited, not as the precision instrument they used to have to be: the fastest-of-ten mechanism, the 15 ms probe's tight tolerance, and the stated-limits table they rested on are gone, because waitseam now kills every clamp, floor, round, scale, offset and later-poll mutant by equality regardless of timing. The Windows priority raise these three scenarios carried is gone with them: a flat 800 ms late tolerance already covers the worst overshoot the old table recorded (127 ms at normal priority under load), so raising the process above the load bought nothing the new margin does not already have. holdtimeout's long probe moves to 1600 ms, above the stated 1500 ms ceiling on the hold guard, with the hold kept armed to 3000 ms so a guard that parks a naturally-timed-out call anyway overshoots by well more than the margin. Verified locally on win32-x64: waitseam and pollhammer pass against the unmutated bridge; a seam-loosening mutant (recording the raw argument outside the wait's own branch) fails waitseam's 0 ms probe; an inline +25 ms offset on the wait's argument fails waitseam by exact mismatch; the S5 hold-guard mutant (timeout_ms > 1500) fails holdtimeout by 1.4 s overshoot; K1 (++wakes > 64), K2 (!error.empty()) and a no-predicate revert all fail pollhammer's idle poll within tens of milliseconds. All were hand-reverted and diffed clean against the working tree before this commit. Co-Authored-By: Claude Sonnet 5 --- .../cultmesh_quic_native.cpp | 35 +- .../include/cultmesh_quic_native.h | 18 + .../tests/cultmesh_quic_native_tests.cpp | 372 +++++++++++++----- 3 files changed, 314 insertions(+), 111 deletions(-) diff --git a/native/GameCult.Mesh.Quic.Native/cultmesh_quic_native.cpp b/native/GameCult.Mesh.Quic.Native/cultmesh_quic_native.cpp index bfd09afc..4c2ae972 100644 --- a/native/GameCult.Mesh.Quic.Native/cultmesh_quic_native.cpp +++ b/native/GameCult.Mesh.Quic.Native/cultmesh_quic_native.cpp @@ -88,14 +88,24 @@ // decrements the count and notifies. That is the ordinary exit every export // makes, and it is the bridge's wait — not the hold — that keeps the runtime // alive underneath it. +// - RECORD_WAIT wraps the exact `timeout_ms` handed to the condition wait in +// `cultmesh_quic_next_event`, in place, as a pass-through: it stores the +// value and returns it unchanged, so a mutation to what is actually waited +// on changes what this reports rather than the two ever being able to +// diverge. A scenario reads it and asserts equality against the timeout it +// asked for, deterministically and with no timing tolerance — the method +// Self's ruling of 2026-09-22 asks for in place of inferring the same fact +// from how long a call took to return. #if defined(CULTMESH_QUIC_DEBUG_ASSERTS) #define CULTMESH_QUIC_DEBUG_PEAK(count) DebugRaisePeak(count) #define CULTMESH_QUIC_DEBUG_AT_CLOSE(count) DebugRecordCallsAtClose(count) #define CULTMESH_QUIC_DEBUG_HELD(lock) DebugHoldCall(lock) +#define CULTMESH_QUIC_DEBUG_RECORD_WAIT(timeout_ms) DebugRecordWait(timeout_ms) #else #define CULTMESH_QUIC_DEBUG_PEAK(count) ((void)0) #define CULTMESH_QUIC_DEBUG_AT_CLOSE(count) ((void)0) #define CULTMESH_QUIC_DEBUG_HELD(lock) (false) +#define CULTMESH_QUIC_DEBUG_RECORD_WAIT(timeout_ms) (timeout_ms) #endif namespace { @@ -238,6 +248,7 @@ std::atomic debug_calls_at_close{-1}; std::mutex debug_hold_gate; std::condition_variable debug_hold_signal; bool debug_hold_armed = false; +std::atomic debug_last_wait_ms{-1}; void DebugRaisePeak(int count) { int peak = debug_peak_calls.load(std::memory_order_relaxed); @@ -258,6 +269,16 @@ bool DebugHoldCall(std::unique_lock& lock) { debug_hold_signal.wait(hold, [] { return !debug_hold_armed; }); return true; } + +// A pass-through: it stores `timeout_ms` and returns it unmodified, so wrapping +// it around the exact expression handed to the condition wait makes the two +// impossible to desync by construction. Whatever that expression evaluates to +// — the plain argument today, or a derivation a future change or a mutation +// introduces — is what gets recorded, because it is the same evaluation. +int32_t DebugRecordWait(int32_t timeout_ms) { + debug_last_wait_ms.store(timeout_ms); + return timeout_ms; +} #endif // Every host-facing export enters through this. It refuses once @@ -1175,8 +1196,10 @@ CULTMESH_API int32_t cultmesh_quic_next_event( std::unique_lock lock(runtime->gate); if (runtime->events.empty() && timeout_ms > 0) { // The host's timeout bounds the wait; an event or the close ends it - // sooner, and `woken` is which of the two happened. - const bool woken = runtime->signal.wait_for(lock, std::chrono::milliseconds(timeout_ms), + // sooner, and `woken` is which of the two happened. RECORD_WAIT wraps + // this exact argument; see the development seam note near its macros. + const bool woken = runtime->signal.wait_for( + lock, std::chrono::milliseconds(CULTMESH_QUIC_DEBUG_RECORD_WAIT(timeout_ms)), [runtime] { return !runtime->events.empty() || runtime->closing; }); // Development builds only; a no-op unless a scenario has armed the hold. // A held call returns the same 0 an empty queue would have, and makes no @@ -1462,4 +1485,12 @@ CULTMESH_API int32_t cultmesh_quic_debug_peak_calls(void) { CULTMESH_API int32_t cultmesh_quic_debug_calls_at_close(void) { return debug_calls_at_close.load(); } + +CULTMESH_API int32_t cultmesh_quic_debug_last_wait_ms(void) { + return debug_last_wait_ms.load(); +} + +CULTMESH_API void cultmesh_quic_debug_reset_last_wait_ms(void) { + debug_last_wait_ms.store(-1); +} #endif diff --git a/native/GameCult.Mesh.Quic.Native/include/cultmesh_quic_native.h b/native/GameCult.Mesh.Quic.Native/include/cultmesh_quic_native.h index bf30d7eb..100b1efc 100644 --- a/native/GameCult.Mesh.Quic.Native/include/cultmesh_quic_native.h +++ b/native/GameCult.Mesh.Quic.Native/include/cultmesh_quic_native.h @@ -341,6 +341,24 @@ CULTMESH_QUIC_API int32_t cultmesh_quic_debug_peak_calls(void); * since the hold was armed. This is the number the wait exists for. */ CULTMESH_QUIC_API int32_t cultmesh_quic_debug_calls_at_close(void); +/* The `timeout_ms` most recently handed to the condition wait inside + * `cultmesh_quic_next_event`, recorded at the point the wait is entered rather + * than observed from outside. A poll whose queue is not empty, or whose + * `timeout_ms` is not greater than zero, never enters that wait and leaves this + * unchanged. -1 until the first such wait, or after + * `cultmesh_quic_debug_reset_last_wait_ms`. + * + * This exists because a wall-clock probe cannot tell the host's timeout from + * any function of it that is the identity at the probe's own value — a floor, + * a round, a clamp, a later-poll fault, there is always another one that + * matches. This reads what the bridge actually handed its wait instead of + * inferring it from how long a call took. */ +CULTMESH_QUIC_API int32_t cultmesh_quic_debug_last_wait_ms(void); + +/* Resets the value `cultmesh_quic_debug_last_wait_ms` reports to -1, without + * touching the hold or the two call counters above. */ +CULTMESH_QUIC_API void cultmesh_quic_debug_reset_last_wait_ms(void); + #endif /* CULTMESH_QUIC_DEBUG_ASSERTS */ #if defined(__cplusplus) diff --git a/native/GameCult.Mesh.Quic.Native/tests/cultmesh_quic_native_tests.cpp b/native/GameCult.Mesh.Quic.Native/tests/cultmesh_quic_native_tests.cpp index 4502d7f5..6dae9a8a 100644 --- a/native/GameCult.Mesh.Quic.Native/tests/cultmesh_quic_native_tests.cpp +++ b/native/GameCult.Mesh.Quic.Native/tests/cultmesh_quic_native_tests.cpp @@ -23,6 +23,8 @@ // cultmesh_quic_native_tests pollbusy [iterations] // cultmesh_quic_native_tests latecall [iterations] // cultmesh_quic_native_tests holdtimeout [iterations] +// cultmesh_quic_native_tests waitseam [iterations] +// cultmesh_quic_native_tests pollhammer [iterations] // // `closerace` hunts the race: many pollers, many iterations, and whatever the // schedule gives. `holdclose` takes the guessing out of it — the bridge's @@ -43,6 +45,21 @@ // expired, so the quiesce the hold scenarios assert is the bridge's doing and // not the fixture's. // +// Self's ruling of 2026-09-22, after seven Soul passes against `polltimeout`, +// `pollbusy` and `holdtimeout` alone: a wall-clock probe cannot tell the host's +// timeout from any function of it that is the identity at the probe's own +// value, because there is always another function that matches. `waitseam` +// puts the observation where the rule is decided instead of where its effect +// eventually shows: it reads `cultmesh_quic_debug_last_wait_ms`, the bridge's +// own account of what it handed its condition wait, and asserts equality +// against the host's argument, deterministically, with no timing tolerance. +// `polltimeout`, `pollbusy` and `holdtimeout` stay only as generous-margin +// proof that the recorded wait is really waited and not merely recorded. +// `pollhammer` covers the wait's predicate the same way `pollbusy` used to try +// to: one thread hammers every gate-touching entry point that cannot itself +// queue an event, in a yield loop, while another holds a single idle poll, and +// the poll must still stay for its timeout. +// // Exit code 0 means every assertion held. Anything else, including a sanitizer // abort or the bridge's own assertion, is a failure. Under ThreadSanitizer run // it with `TSAN_OPTIONS=halt_on_error=1`, or a reported race leaves the exit @@ -50,12 +67,6 @@ #include -#if defined(_WIN32) -#define WIN32_LEAN_AND_MEAN -#define NOMINMAX -#include -#endif - #include #include #include @@ -164,26 +175,11 @@ std::string CloseRaceOnce(int pollers) { return {}; } -// The three timeouts `polltimeout` asks for. More than one, because a single -// value cannot tell a bridge that honours the host's timeout from one that waits -// on that same number of its own: a constant has to satisfy every band, and no -// constant is in two of them. -// -// A constant was never the hard case, though, and the numbers are chosen for the -// waits that are computed from the argument instead. Any mapping that is the -// identity at every probe passes for free, so each probe is placed to leave one -// family of ordinary mappings nowhere to be identity: -// -// - 15 ms is as small as the OS timer lets a measurement be honest about. A -// floor — `max(timeout, 100)`, so a polling host cannot spin — and an added -// constant — `timeout + 100` of grace — are both the whole of the wait here, -// and nothing larger can see either one under the scheduler's noise. -// - 200 is the ordinary host poll, and sits between the other two so that a -// constant cannot satisfy both of its neighbours. -// - 7300 is long and deliberately not round. A clamp — `min(timeout, 1000)` to -// notice a shutdown, `min(timeout, 5000)` because five seconds is surely -// enough — is identity at any probe at or under its ceiling, and a round -// probe is exactly where somebody's round ceiling sits. +// The three timeouts `polltimeout` asks for: a spread, not a hunt for any +// particular derivation. `waitseam` is what proves the bridge hands its +// condition wait the host's own argument, exactly, at every value that matters +// — these three exist only to prove that value is then really waited on, so +// the spread just needs to be a short one, an ordinary one and a long one. constexpr int32_t kShortestPollMs = 15; constexpr int32_t kShortPollMs = 200; constexpr int32_t kLongPollMs = 7300; @@ -193,61 +189,14 @@ constexpr int32_t kLongPollMs = 7300; // duration looks like. This is scheduler granularity, not slack. constexpr int kEarlyToleranceMs = 20; -// How much later than the timeout a single poll may return: an allowance for -// the scheduler, plus a sixteenth of what was asked for. -// -// This is what the probes that look for gross failures use: a constant of the -// bridge's own, a clamp, a hold that parks a call until it is released, a wait -// that restarts on every wake. Each of those is wrong by far more than this. -constexpr int kSchedulerSlackMs = 60; -constexpr int LateToleranceMs(int32_t timeout_ms) { return kSchedulerSlackMs + timeout_ms / 16; } - -// The 15 ms probe is held to something much tighter, because it is the only -// place a floor or an added constant shows and a scheduler allowance would hide -// both. Scheduler delay only ever adds, so the probe is asked -// `kShortestAttempts` times and the fastest is what is checked: noise has to -// delay every attempt to fail the bridge, and a floor or offset delays every -// attempt by construction. Every attempt is still checked for being early. -constexpr int kShortestAttempts = 10; -constexpr int kTimerSlackMs = 30; - -// Both allowances were checked against a loaded machine, two busy threads per -// logical CPU for five rounds of all three timed scenarios, and not a quiet one. -// Worst overshoot seen, win32-x64 and linux-x64 on 8 logical CPUs: -// - a single poll: 15 and 41 ms; -// - the fastest of the 15 ms attempts: 0 and 0 ms. -// On win32-x64 that needs `MeasureAboveTheLoad` below. At normal priority under -// the same load a single poll landed up to 127 ms late and the fastest of ten -// 15 ms attempts up to 87 ms late — Windows makes a woken thread wait out busy -// threads' time slices — and a tolerance of 45 ms plus a sixteenth failed the -// unmutated bridge in four rounds of five. -// -// The honest limit. Each of these was compiled into the bridge and run through -// all three timed scenarios on both targets; what survives is not claimed: -// - an added constant: `timeout + 25` survived and `timeout + 40` died, on the -// 15 ms probe alone; -// - a floor: `max(timeout, 40)` survived and `max(timeout, 50)` died, on the -// 15 ms probe alone and on linux-x64 by 5 ms, so a floor near 50 is a kill -// that rests on timing; -// - a scaling: `timeout * 106 / 100` survived and `timeout * 108 / 100` died, -// on the 7300 ms probe alone; -// - a clamp: `min(timeout, 7285)` survived and `min(timeout, 7275)` died; a -// clamp at or above 7300 is the identity at every probe; -// - a shortening of up to `kEarlyToleranceMs` at any probe. -// Each run prints its worst overshoot per probe, so the allowances can be -// rechecked on another machine rather than trusted. - -// The timed scenarios measure the bridge's wait, and on a loaded Windows machine -// what they would otherwise measure is the scheduler's: a thread woken at the -// end of its wait queues behind busy threads of equal priority for whole time -// slices. Raising this process above them takes that out of the measurement, -// and it needs no privilege on Windows. Linux schedules a woken thread promptly -// at normal priority under the same load, so nothing is asked of it there. -void MeasureAboveTheLoad() { -#if defined(_WIN32) - SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); -#endif -} +// How much later than the timeout a poll may return. Generous on purpose: these +// scenarios are no longer where a clamp, a floor, a round or a scale is caught +// — `waitseam` catches those by equality, with no clock involved — so this only +// has to catch a wait that is wrong by a lot: a constant of the bridge's own, a +// wait that never happens, a hold that parks a call its own timeout already +// ended. Each of those misses by seconds, not milliseconds, so one flat, wide +// margin serves every probe this file times, on a machine under real load. +constexpr int kGenerousLateToleranceMs = 800; struct TimedPoll { int32_t result; @@ -304,21 +253,10 @@ std::string PollTimeoutOnce(std::array& overshoot) { return "cultmesh_quic_runtime_open returned " + std::to_string(opened); std::string failure; - // The fastest of several, each checked for being early; see kShortestAttempts. - TimedPoll fastest{0, LLONG_MAX}; - for (int attempt = 0; attempt < kShortestAttempts && failure.empty(); ++attempt) { - const TimedPoll poll = PollFor(runtime, kShortestPollMs); - failure = CheckTimedPoll(poll, kShortestPollMs, INT_MAX); - if (poll.elapsed_ms < fastest.elapsed_ms) fastest = poll; - } - if (failure.empty()) { - overshoot[0].Record(fastest, kShortestPollMs); - failure = CheckTimedPoll(fastest, kShortestPollMs, kTimerSlackMs); - } - for (size_t probe = 1; probe < kTimedProbesMs.size() && failure.empty(); ++probe) { + for (size_t probe = 0; probe < kTimedProbesMs.size() && failure.empty(); ++probe) { const TimedPoll poll = PollFor(runtime, kTimedProbesMs[probe]); overshoot[probe].Record(poll, kTimedProbesMs[probe]); - failure = CheckTimedPoll(poll, kTimedProbesMs[probe], LateToleranceMs(kTimedProbesMs[probe])); + failure = CheckTimedPoll(poll, kTimedProbesMs[probe], kGenerousLateToleranceMs); } cultmesh_quic_runtime_close(runtime); @@ -326,15 +264,14 @@ std::string PollTimeoutOnce(std::array& overshoot) { } int PollTimeout(int iterations) { - MeasureAboveTheLoad(); std::array overshoot{}; for (int iteration = 0; iteration < iterations; ++iteration) { const std::string failure = PollTimeoutOnce(overshoot); if (!failure.empty()) return Fail("polltimeout iteration " + std::to_string(iteration) + ": " + failure); } - std::printf("polltimeout %dx: ok (worst overshoot %lld ms at %d best of %d, %lld ms at %d, %lld ms at %d)\n", - iterations, overshoot[0].worst_ms, kTimedProbesMs[0], kShortestAttempts, overshoot[1].worst_ms, kTimedProbesMs[1], + std::printf("polltimeout %dx: ok (worst overshoot %lld ms at %d, %lld ms at %d, %lld ms at %d)\n", + iterations, overshoot[0].worst_ms, kTimedProbesMs[0], overshoot[1].worst_ms, kTimedProbesMs[1], overshoot[2].worst_ms, kTimedProbesMs[2]); return 0; } @@ -390,7 +327,7 @@ std::string PollBusyOnce(Overshoot& overshoot) { // The timing first: a poll that left on the first wake returns before the // other thread has had time to make many calls, and that is the bridge // failing, not the fixture. - const std::string failure = CheckTimedPoll(poll, kBusyPollMs, LateToleranceMs(kBusyPollMs)); + const std::string failure = CheckTimedPoll(poll, kBusyPollMs, kGenerousLateToleranceMs); if (!failure.empty()) return failure + ", while the host's other thread was calling in"; // A poll that passed with fewer calls than this was never woken, and passed // because the fixture did nothing rather than because the bridge held. @@ -401,7 +338,6 @@ std::string PollBusyOnce(Overshoot& overshoot) { } int PollBusy(int iterations) { - MeasureAboveTheLoad(); Overshoot overshoot{}; for (int iteration = 0; iteration < iterations; ++iteration) { const std::string failure = PollBusyOnce(overshoot); @@ -737,12 +673,15 @@ std::string LateCallOnce() { // lands at the last number instead of its own. // // Two polls, a short one and a long one, because a guard that tests the timeout -// instead of the wake — park it unless it was short — is identity on any single -// value on one side of its threshold. The long one sits above the thresholds -// such a guard would plausibly carry. +// instead of the wake — park it unless it was long — is identity on any single +// value on one side of its threshold. The long one sits above 1500, the stated +// ceiling on how long the hold guard may treat a long wait as surely worth +// parking; the hold stays armed well past it so a guard that parks anyway is +// held for the rest of the arming instead of leaving on its own timeout, which +// is the overshoot this scenario's late tolerance is wide enough to see. constexpr int32_t kHeldTimeoutPollMs = 150; -constexpr int32_t kHeldLongTimeoutPollMs = 1300; -constexpr int kHoldArmedMs = 2200; +constexpr int32_t kHeldLongTimeoutPollMs = 1600; +constexpr int kHoldArmedMs = 3000; // The seam's own rule, and the only scenario that can see it: the hold parks a // call the wait woke, and not one whose own timeout expired. @@ -792,7 +731,7 @@ std::string HoldTimeoutOnce(std::array& overshoot) { if (poll.elapsed_ms < timeout_ms - kEarlyToleranceMs) return "a poll asking for " + std::to_string(timeout_ms) + " ms returned after " + std::to_string(poll.elapsed_ms) + " ms, so the fixture never let its timeout run out"; - if (poll.elapsed_ms > timeout_ms + LateToleranceMs(timeout_ms)) + if (poll.elapsed_ms > timeout_ms + kGenerousLateToleranceMs) return "a poll asking for " + std::to_string(timeout_ms) + " ms returned after " + std::to_string(poll.elapsed_ms) + " ms: the hold parked a call its own timeout had " "already ended, and it left when the hold did"; @@ -801,7 +740,6 @@ std::string HoldTimeoutOnce(std::array& overshoot) { } int HoldTimeout(int iterations) { - MeasureAboveTheLoad(); std::array overshoot{}; for (int iteration = 0; iteration < iterations; ++iteration) { const std::string failure = HoldTimeoutOnce(overshoot); @@ -833,8 +771,221 @@ int HoldClose(int iterations, int pollers) { return 0; } +// The values `waitseam` asks for: the smallest, the seam's own sentinel-versus- +// zero boundary, one on either side of a 16 ms quantum and of a 40 ms one, the +// ordinary `pollbusy` timeout and its neighbours, `polltimeout`'s long probe, +// and INT32_MAX. Nothing in section 6 of the header states a maximum on +// `timeout_ms` narrower than what `int32_t` itself holds, so INT32_MAX is the +// ceiling this checks and nothing wider is claimed. +constexpr std::array kWaitSeamProbesMs{ + 0, 1, 15, 16, 39, 40, 999, 1000, 1001, 7300, INT32_MAX}; + +// How long a probe's own poller thread is given to reach the wait before this +// gives up on it and fails with the probe named, rather than hanging. +constexpr int kWaitSeamDeadlineMs = 2000; + +// One probe: ask a fresh, otherwise-idle runtime for `timeout_ms` and read +// `cultmesh_quic_debug_last_wait_ms` — the bridge's own account of what it +// handed its condition wait — instead of measuring how long the call took. +// Every clamp, floor, round, scale, offset and later-poll mapping Soul's seven +// passes found is the identity at some wall-clock probe; none of them is the +// identity here unless it is also the identity at every value below, because +// this reads the argument itself rather than inferring it from elapsed time. +// +// `timeout_ms <= 0` never reaches the wait at all — `cultmesh_quic_next_event` +// returns at once when the queue is empty — so there is nothing for the seam to +// record, and the honest check is the converse: it must still say nothing, +// which is exactly what a seam that recorded the raw argument on every call, +// instead of only the value it actually handed its wait, would get wrong. +std::string WaitSeamProbeOnce(int32_t timeout_ms) { + void* runtime = nullptr; + const int32_t opened = cultmesh_quic_runtime_open("cultmesh-quic-native-tests", &runtime); + if (opened != 0 || runtime == nullptr) + return "cultmesh_quic_runtime_open returned " + std::to_string(opened); + cultmesh_quic_debug_reset_last_wait_ms(); + + std::string failure; + if (timeout_ms <= 0) { + std::vector payload(static_cast(kPayloadCapacity)); + cultmesh_quic_event event{}; + int32_t required = 0; + const int32_t result = cultmesh_quic_next_event( + runtime, timeout_ms, &event, payload.data(), kPayloadCapacity, &required); + const int32_t recorded = cultmesh_quic_debug_last_wait_ms(); + if (result != 0) + failure = "a poll asking for " + std::to_string(timeout_ms) + " ms returned " + + std::to_string(result) + ", not the 0 an idle runtime owes it"; + else if (recorded != -1) + failure = "a poll asking for " + std::to_string(timeout_ms) + " ms recorded " + + std::to_string(recorded) + " ms on the wait seam, though the bridge's own guard " + "never enters a wait for a timeout that is not greater than zero"; + cultmesh_quic_runtime_close(runtime); + return failure; + } + + // A real wait, on a thread of its own: a probe at INT32_MAX has to be + // released by the close, not waited out. + std::thread poller([runtime, timeout_ms] { + std::vector payload(static_cast(kPayloadCapacity)); + cultmesh_quic_event event{}; + int32_t required = 0; + cultmesh_quic_next_event(runtime, timeout_ms, &event, payload.data(), kPayloadCapacity, &required); + }); + + const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(kWaitSeamDeadlineMs); + int32_t recorded = cultmesh_quic_debug_last_wait_ms(); + while (recorded == -1 && std::chrono::steady_clock::now() < deadline) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + recorded = cultmesh_quic_debug_last_wait_ms(); + } + + cultmesh_quic_runtime_close(runtime); + poller.join(); + + if (recorded != timeout_ms) + failure = "a poll asking for " + std::to_string(timeout_ms) + " ms recorded " + + std::to_string(recorded) + " ms on the wait seam: the bridge handed its condition " + "wait something other than the host's own argument"; + return failure; +} + +// Past the twelfth poll on one runtime: no scenario before this one ever asked +// a runtime for a thirteenth, so a wait that only changes shape after twelve +// had nowhere to be seen. Forty short waits on a runtime that stays open for +// all of them, each checked by equality. +constexpr int32_t kWaitSeamRepeatedMs = 5; +constexpr int kWaitSeamRepeatedPolls = 40; + +std::string WaitSeamRepeatedOnce() { + void* runtime = nullptr; + const int32_t opened = cultmesh_quic_runtime_open("cultmesh-quic-native-tests", &runtime); + if (opened != 0 || runtime == nullptr) + return "cultmesh_quic_runtime_open returned " + std::to_string(opened); + + std::string failure; + for (int poll = 0; poll < kWaitSeamRepeatedPolls && failure.empty(); ++poll) { + cultmesh_quic_debug_reset_last_wait_ms(); + std::vector payload(static_cast(kPayloadCapacity)); + cultmesh_quic_event event{}; + int32_t required = 0; + cultmesh_quic_next_event( + runtime, kWaitSeamRepeatedMs, &event, payload.data(), kPayloadCapacity, &required); + const int32_t recorded = cultmesh_quic_debug_last_wait_ms(); + if (recorded != kWaitSeamRepeatedMs) + failure = "poll " + std::to_string(poll + 1) + " of " + + std::to_string(kWaitSeamRepeatedPolls) + " on one runtime recorded " + + std::to_string(recorded) + " ms, not " + std::to_string(kWaitSeamRepeatedMs); + } + cultmesh_quic_runtime_close(runtime); + return failure; +} + +int WaitSeam(int iterations) { + for (int iteration = 0; iteration < iterations; ++iteration) { + for (int32_t probe : kWaitSeamProbesMs) { + const std::string failure = WaitSeamProbeOnce(probe); + if (!failure.empty()) + return Fail("waitseam iteration " + std::to_string(iteration) + " probe " + + std::to_string(probe) + ": " + failure); + } + const std::string repeated = WaitSeamRepeatedOnce(); + if (!repeated.empty()) + return Fail("waitseam iteration " + std::to_string(iteration) + ": " + repeated); + } + std::printf("waitseam %dx: ok (%zu probes, %d repeated polls on one runtime)\n", + iterations, kWaitSeamProbesMs.size(), kWaitSeamRepeatedPolls); + return 0; +} + #endif // CULTMESH_QUIC_DEBUG_ASSERTS +// How long `pollhammer`'s idle poll asks for. One host thread hammers every +// gate-touching entry point that cannot itself queue an event, in a yield +// loop, while another holds this one poll idle; each hammered call's +// `CallScope` destructor notifies every waiter on the runtime's condition +// variable (cultmesh_quic_native.cpp:286), so the idle poll is woken many times +// over for every timeout it is given here. That is exactly the shape a wake +// count or an error-state predicate exploits, and exactly what a revert that +// returns on any wake at all fails immediately. +// +// `cultmesh_quic_connection_open`, `cultmesh_quic_listener_open` and +// `cultmesh_quic_stream_open` touch the same gate but can each queue a real +// event on this runtime, and a genuine event is allowed to end the idle poll +// early — hammering with those would make an early return ambiguous between +// the fault this scenario hunts and a real delivery, so they are left out. The +// refused `listener_open` below is a setup step run once, not part of the +// loop, and it leaves no listener behind. +constexpr int32_t kHammerPollMs = 1000; + +std::string PollHammerOnce(bool with_recorded_error) { + void* runtime = nullptr; + const int32_t opened = cultmesh_quic_runtime_open("cultmesh-quic-native-tests", &runtime); + if (opened != 0 || runtime == nullptr) + return "cultmesh_quic_runtime_open returned " + std::to_string(opened); + + if (with_recorded_error) { + // Not a certificate at all: PKCS12 parsing fails before any network + // I/O, so this is refused synchronously and leaves no listener behind, + // and `runtime->error` is non-empty for the rest of this runtime's life. + const uint8_t junk[] = {0x00, 0x01, 0x02, 0x03}; + uint64_t listener_id = 0; + uint16_t bound_port = 0; + const int32_t result = cultmesh_quic_listener_open( + runtime, nullptr, 0, junk, static_cast(sizeof(junk)), "", &listener_id, &bound_port); + if (result >= 0) { + cultmesh_quic_runtime_close(runtime); + return "cultmesh_quic_listener_open accepted a junk PKCS12 credential"; + } + } + + std::atomic hammering{true}; + std::thread hammer([runtime, &hammering] { + constexpr uint64_t kBogusId = 0xffffffffffffffffull; + const uint8_t frame[] = {0x00}; + std::array message{}; + while (hammering.load(std::memory_order_relaxed)) { + cultmesh_quic_last_error(runtime, message.data(), static_cast(message.size())); + cultmesh_quic_last_status(runtime); + cultmesh_quic_connection_shutdown(runtime, kBogusId, 0); + cultmesh_quic_stream_shutdown(runtime, kBogusId, 0); + cultmesh_quic_stream_send_frame(runtime, kBogusId, frame, 1, 0); + cultmesh_quic_connection_certificate_complete(runtime, kBogusId, 0); + cultmesh_quic_listener_close(runtime, kBogusId); + std::this_thread::yield(); + } + }); + + const TimedPoll poll = PollFor(runtime, kHammerPollMs); + hammering.store(false, std::memory_order_relaxed); + hammer.join(); + cultmesh_quic_runtime_close(runtime); + + if (poll.result != 0) + return "the idle poll returned " + std::to_string(poll.result) + + ", not the 0 an idle runtime owes it"; + if (poll.elapsed_ms < kHammerPollMs - kEarlyToleranceMs) + return "the idle poll returned after " + std::to_string(poll.elapsed_ms) + + " ms while another thread hammered the gate, ending its wait early"; + if (poll.elapsed_ms > static_cast(kHammerPollMs) + kGenerousLateToleranceMs) + return "the idle poll returned after " + std::to_string(poll.elapsed_ms) + + " ms: well past its own timeout"; + return {}; +} + +int PollHammer(int iterations) { + for (int iteration = 0; iteration < iterations; ++iteration) { + std::string failure = PollHammerOnce(false); + if (!failure.empty()) + return Fail("pollhammer iteration " + std::to_string(iteration) + " (clean runtime): " + failure); + failure = PollHammerOnce(true); + if (!failure.empty()) + return Fail("pollhammer iteration " + std::to_string(iteration) + + " (after a refused call recorded an error): " + failure); + } + std::printf("pollhammer %dx: ok\n", iterations); + return 0; +} + int CloseRace(int iterations, int pollers) { for (int iteration = 0; iteration < iterations; ++iteration) { const std::string failure = CloseRaceOnce(pollers); @@ -850,8 +1001,8 @@ int CloseRace(int iterations, int pollers) { int main(int argc, char** argv) { if (argc < 2) { std::fprintf(stderr, - "usage: %s closerace|holdclose|holdtimeout|polltimeout|pollbusy|payloadfit|latecall " - "[iterations] [pollers]\n", + "usage: %s closerace|holdclose|holdtimeout|polltimeout|pollbusy|pollhammer|payloadfit|" + "latecall|waitseam [iterations] [pollers]\n", argv[0]); return 2; } @@ -865,11 +1016,14 @@ int main(int argc, char** argv) { if (scenario == "closerace") return CloseRace(iterations, pollers); if (scenario == "polltimeout") return PollTimeout(iterations); if (scenario == "pollbusy") return PollBusy(iterations); + if (scenario == "pollhammer") return PollHammer(iterations); if (scenario == "payloadfit") return PayloadFit(iterations); - if (scenario == "holdclose" || scenario == "latecall" || scenario == "holdtimeout") { + if (scenario == "holdclose" || scenario == "latecall" || scenario == "holdtimeout" || + scenario == "waitseam") { #if defined(CULTMESH_QUIC_DEBUG_ASSERTS) if (scenario == "latecall") return LateCall(iterations); if (scenario == "holdtimeout") return HoldTimeout(iterations); + if (scenario == "waitseam") return WaitSeam(iterations); return HoldClose(iterations, pollers); #else std::fprintf(stderr,