fix(scheduler): activate virtual-clock logging for sim/REPL sessions (Req 5 AC 3)

sched_sim_driver_start() now registers the core's tlog format function and
marks its core active for logging right after the virtual clock is seeded
(xce_core_register_tlog_format()/xce_core_set_active_for_logging()) --
every log line for the rest of the run shows frozen/scaled replay time
instead of real wall-clock time, matching what an operator debugging
against a replay's own timeline needs. sched_sim_driver_run() deactivates
on all 7 of its return paths, since there's no sim-driver teardown
function and g_active_core_for_logging would otherwise dangle past the
caller's stack frame.

Also fixes a genuine pre-existing ASAN stack-use-after-return (bug-1598)
the new deactivation test exposed: sim_picker_thread_fn is spawned but, by
design, never joined/cancelled, so a stack-local `xce_sched_sim_driver_t
sim` in test_sim_event_loop.c was always a latent UAF once its owning test
function returned -- just not previously triggered by any prior test
ordering. Switched the three affected fixtures in that file to `static`
storage (safe: sched_sim_driver_start() memsets the whole struct on entry,
and none of the three call sites are re-entrant).
This commit is contained in:
Rongsong Shen 2026-08-19 11:00:33 +00:00
parent 25e962309f
commit b450a6806b
No known key found for this signature in database
GPG key ID: 5BCDAAE0123E9FE2
4 changed files with 164 additions and 2 deletions

View file

@ -378,6 +378,48 @@ test_valid_fixture_reaches_ready_state_with_frozen_virtual_clock(void)
" PASS\n");
}
/* ---------------------------------------------------------------------- */
/* Test 1b (design review follow-up, Req 5 AC 3): sched_sim_driver_start() */
/* must activate the core's virtual clock for tlog logging -- otherwise */
/* every log line emitted during a --sim/--interactive run shows real */
/* wall-clock time instead of the frozen/virtual replay timestamp, */
/* defeating the whole point of debugging against a replay's own timeline. */
/* g_active_core_for_logging is core_clock.c's module-static (white-box */
/* #include above puts it in this TU) -- direct inspection is simpler and */
/* more precise than round-tripping through tlog's own formatter, which */
/* test_clock.c already covers at the unit level. */
/* ---------------------------------------------------------------------- */
static void test_valid_fixture_activates_virtual_clock_for_tlog_logging(void)
{
char path[PATH_MAX];
uint64_t snapshot_ts = 0;
sched_sim_args_t args;
xce_sched_sim_driver_t sim;
int rc;
/* Defensive reset -- other tests in this same process may have left
* g_active_core_for_logging pointing at a since-destroyed core. */
xce_core_set_active_for_logging(NULL);
write_valid_fixture(path, sizeof(path), &snapshot_ts, "_tlogactivate");
memset(&args, 0, sizeof(args));
args.replay_path = path;
args.sim_start_ts = SCHED_SIM_START_TS_LATEST;
args.sim_speed = 0;
rc = sched_sim_driver_start(&sim, &args);
TEST_ASSERT_EQUAL_INT(0, rc);
TEST_ASSERT_EQUAL_PTR(&(sim.core), g_active_core_for_logging);
xce_core_set_active_for_logging(NULL); /* leave clean for later tests */
printf("test_valid_fixture_activates_virtual_clock_for_tlog_logging:"
" PASS\n");
}
/* ---------------------------------------------------------------------- */
/* Test 2: starting against a file missing its initial snapshot entry */
/* fails with a clear error before any event is processed -- this task's */
@ -496,6 +538,7 @@ int main(void)
UNITY_BEGIN();
RUN_TEST(test_valid_fixture_reaches_ready_state_with_frozen_virtual_clock);
RUN_TEST(test_valid_fixture_activates_virtual_clock_for_tlog_logging);
RUN_TEST(test_missing_initial_snapshot_entry_fails_cleanly);
RUN_TEST(test_first_message_not_replayheader_fails_cleanly);
RUN_TEST(test_missing_replay_file_fails_cleanly);