Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dm/host/hle_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ 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: CalculateSpanBetween and the user-difference check read it
return RES_OK;
}
// Both user clocks come from host time; only the type byte distinguishes them (0xcd = 2 for the
// adjustable one). Missing entirely before this: a game that snapshots the adjustable clock read a
// null. From LNPANDY/NX2Apple#26 (m1guelpf), with its guarded-buffer test.
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;
Expand Down
26 changes: 25 additions & 1 deletion dm/host/tests/t_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,35 @@ 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; }
uint8_t a[0xd0], b[0xd0], c[0xd0]; int64_t ts = -1;
// Before the first snapshot: h_time_GetCurrentTime reads DM_FIXED_TIME once and caches it
setenv("DM_FIXED_TIME", "1700000000", 1);
setenv("TZ", "UTC", 1); tzset();
CHECK(snap(a) == RES_OK, "snapshot a");
CHECK(a[0xcd] == 1, "standard snapshot type is 1");
// The adjustable clock is the same data from host time with type 2; the guard bytes prove the
// entry writes exactly the 0xd0 bytes a ClockSnapshot has (LNPANDY/NX2Apple#26's test).
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");
unsetenv("DM_FIXED_TIME");
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");
Expand Down
Loading