From 1623ef70d074ef9bd2fc5a30a4f1fe9af0e81f98 Mon Sep 17 00:00:00 2001 From: Miguel Piedrafita Date: Mon, 26 Oct 2026 14:58:17 -0700 Subject: [PATCH] support implement adjustable system clocks --- dm/host/hle_time.c | 7 +++++++ dm/host/tests/t_time.c | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/dm/host/hle_time.c b/dm/host/hle_time.c index d74039ddb..e8d9be0a9 100644 --- a/dm/host/hle_time.c +++ b/dm/host/hle_time.c @@ -92,6 +92,13 @@ HLE_FORCE("_ZN2nn4time13ClockSnapshot29CreateWithStandardSystemClockEPS1_", "sna h_time_ToCalendarTime((CalendarTime *)(s + 0x50), (CalendarAdditionalInfo *)(s + 0x60), &now); memcpy(s + 0x90, tp, 0x18); tzset(); strlcpy((char *)s + 0xa8, tzname[0] && *tzname[0] ? tzname[0] : "UTC", 0x24); + s[0xcd] = 1; // Standard clock snapshot type. + return RES_OK; +} +// Both user clocks use host time. The type field identifies the requested clock. +HLE_FORCE("_ZN2nn4time13ClockSnapshot31CreateWithAdjustableSystemClockEPS1_", "adjustable clock snapshot from host time; the host has no time service session", Result, h_time_AdjSnapshot, uint8_t *s) { + h_time_Snapshot(s); + s[0xcd] = 2; return RES_OK; } // Arithmetic on two snapshots: the SDK hands both to the time service (a call through its session, AbortImpl when there is none: STEINS;GATE RE:BOOT's and Tears of the Kingdom's SDK; diff --git a/dm/host/tests/t_time.c b/dm/host/tests/t_time.c index 7f26e900e..8e61ca2b0 100644 --- a/dm/host/tests/t_time.c +++ b/dm/host/tests/t_time.c @@ -19,11 +19,29 @@ static void put64(uint8_t *s, int off, int64_t v) { memcpy(s + off, &v, 8); } static int64_t get64(const uint8_t *s, int off) { int64_t v; memcpy(&v, s + off, 8); return v; } int main(void) { Result (*snap)(uint8_t *) = hook("_ZN2nn4time13ClockSnapshot29CreateWithStandardSystemClockEPS1_"); + Result (*adj_snap)(uint8_t *) = hook("_ZN2nn4time13ClockSnapshot31CreateWithAdjustableSystemClockEPS1_"); Result (*span)(int64_t *, const uint8_t *, const uint8_t *) = hook("_ZN2nn4time20CalculateSpanBetweenEPNS_8TimeSpanERKNS0_13ClockSnapshotES5_"); int64_t (*udiff)(const uint8_t *, const uint8_t *) = hook("_ZN2nn4time48CalculateStandardUserSystemClockDifferenceByUserERKNS0_13ClockSnapshotES3_"); - if (!snap || !span || !udiff) { printf("time: %d checks, %d failed\n", checks, fails); return 1; } + if (!snap || !adj_snap || !span || !udiff) { printf("time: %d checks, %d failed\n", checks, fails); return 1; } + setenv("DM_FIXED_TIME", "1700000000", 1); + setenv("TZ", "UTC", 1); tzset(); uint8_t a[0xd0], b[0xd0], c[0xd0]; int64_t ts = -1; CHECK(snap(a) == RES_OK, "snapshot a"); + CHECK(a[0xcd] == 1, "standard snapshot type is 1"); + uint8_t guarded[0xf0]; memset(guarded, 0xa5, sizeof guarded); + uint8_t *adjustable = guarded + 0x10; + CHECK(adj_snap(adjustable) == RES_OK, "adjustable snapshot"); + int guards_ok = 1; + for (int i = 0; i < 0x10; i++) guards_ok &= guarded[i] == 0xa5 && guarded[0xe0 + i] == 0xa5; + CHECK(guards_ok, "adjustable snapshot writes only its 0xd0 bytes"); + CHECK(adjustable[0xcd] == 2, "adjustable snapshot type is 2"); + CHECK(get64(adjustable, 0x40) == 1700000000, "adjustable user time follows the host clock"); + const CalendarTime *cal = (const CalendarTime *)(adjustable + 0x50); + CHECK(cal->year == 2023 && cal->month == 11 && cal->day == 14 && cal->hour == 22 && cal->minute == 13 && cal->second == 20, "adjustable snapshot has the UTC calendar for the fixed time"); + uint8_t zero[0x20] = {0}; + CHECK(!memcmp(adjustable + 0x20, zero, sizeof zero) && get64(adjustable, 0x48) == 0, "offline snapshot has no network clock context or time"); + CHECK(!memcmp(a + 0x98, adjustable + 0x98, 16), "both snapshots use the same steady clock source"); + CHECK(udiff(a, adjustable) == 0, "changing snapshot type does not report a user clock change"); CHECK(span(&ts, a, a) == RES_OK && ts == 0, "span of a snapshot with itself is 0"); memcpy(b, a, sizeof b); put64(b, 0x90, get64(a, 0x90) + 90); // b: 90 s of steady clock later CHECK(span(&ts, a, b) == RES_OK && ts == 90 * 1000000000LL, "span from the steady time points, in nanoseconds");