Skip to content
Merged
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
12 changes: 10 additions & 2 deletions core/libs/prx/libc/src/Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,11 +611,13 @@ double APS5_VABI difftime_nid_postfix(int64_t time1, int64_t time0) {

GuestTm* APS5_VABI libc_gmtime_nid_postfix(const int64_t* timer) {
static thread_local GuestTm result;
if (timer == nullptr) return nullptr;
return gmtime_s_nid_postfix(timer, &result);
}

GuestTm* APS5_VABI libc_localtime_nid_postfix(const int64_t* timer) {
static thread_local GuestTm result;
if (timer == nullptr) return nullptr;
return localtime_s_nid_postfix(timer, &result);
}

Expand All @@ -624,7 +626,10 @@ GuestTm* APS5_VABI localtime_nid_postfix(const int64_t* timer) {
}

GuestTm* APS5_VABI localtime_s_nid_postfix(const int64_t* timer, GuestTm* result) {
if (timer == nullptr || result == nullptr) return nullptr;
if (timer == nullptr || result == nullptr) {
SonyInvokeConstraintHandler("localtime_s: bad argument", nullptr, EINVAL);
return nullptr;
}
const ZoneScope zone;
std::tm host{};
const int callerError = errno;
Expand Down Expand Up @@ -664,7 +669,10 @@ GuestTm* APS5_VABI localtime_s_nid_postfix(const int64_t* timer, GuestTm* result
}

GuestTm* APS5_VABI gmtime_s_nid_postfix(const int64_t* timer, GuestTm* result) {
if (timer == nullptr || result == nullptr) return nullptr;
if (timer == nullptr || result == nullptr) {
SonyInvokeConstraintHandler("gmtime_s: bad argument", nullptr, EINVAL);
return nullptr;
}
if (!utcCalendarFields(*timer, *result)) return nullptr;
result->tm_gmtoff = 0;
result->tm_zone = "UTC";
Expand Down
2 changes: 2 additions & 0 deletions core/libs/prx/libc/src/TimeExtras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ std::int64_t wrappingAdd(std::int64_t left, std::int64_t right) {
extern "C" {

GuestTm* APS5_VABI gmtime_r_nid_postfix(const std::int64_t* timer, GuestTm* result) {
if (timer == nullptr || result == nullptr) return nullptr;
return gmtime_s_nid_postfix(timer, result);
}

GuestTm* APS5_VABI localtime_r_nid_postfix(const std::int64_t* timer, GuestTm* result) {
if (timer == nullptr || result == nullptr) return nullptr;
return localtime_s_nid_postfix(timer, result);
}

Expand Down
37 changes: 37 additions & 0 deletions core/libs/tests/GuestTimeConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ static void Require(bool value, const char* message) {
}
}

static int g_constraintCalls = 0;
static std::string g_constraintMessage;

static void APS5_VABI RecordConstraint(const char* message, void* pointer, int error) {
Require(pointer == nullptr && error == 22, "constraint handler arguments");
++g_constraintCalls;
g_constraintMessage = message;
}

static void CheckNullArguments() {
void* const previous = set_constraint_handler_s_nid_postfix(reinterpret_cast<void*>(&RecordConstraint));
const std::int64_t timer = 0;
GuestTm result{};
for (const auto convert : {gmtime_s_nid_postfix, localtime_s_nid_postfix}) {
const char* expected = convert == gmtime_s_nid_postfix ? "gmtime_s: bad argument" : "localtime_s: bad argument";
for (int which = 0; which < 3; ++which) {
g_constraintCalls = 0;
Require(convert(which == 0 ? &timer : nullptr, which == 1 ? &result : nullptr) == nullptr,
"null argument was accepted");
Require(g_constraintCalls == 1 && g_constraintMessage == expected, "null argument did not report a bad argument");
}
g_constraintCalls = 0;
Require(convert(&timer, &result) == &result && g_constraintCalls == 0, "valid conversion reported a violation");
}
g_constraintCalls = 0;
for (const auto convert : {libc_gmtime_nid_postfix, gmtime_nid_postfix, libc_localtime_nid_postfix, localtime_nid_postfix}) {
Require(convert(nullptr) == nullptr, "null timer was accepted");
}
for (const auto convert : {gmtime_r_nid_postfix, localtime_r_nid_postfix}) {
Require(convert(nullptr, &result) == nullptr && convert(&timer, nullptr) == nullptr && convert(nullptr, nullptr) == nullptr,
"null argument was accepted");
}
Require(g_constraintCalls == 0, "non-_s conversion reported a violation");
set_constraint_handler_s_nid_postfix(previous);
}

static bool Equal(const GuestTm& left, const GuestTm& right) {
return left.tm_sec == right.tm_sec && left.tm_min == right.tm_min && left.tm_hour == right.tm_hour
&& left.tm_mday == right.tm_mday && left.tm_mon == right.tm_mon && left.tm_year == right.tm_year
Expand Down Expand Up @@ -489,6 +525,7 @@ static void CheckRejectedZone() {
int main() {
Require(setenv_nid_postfix("TZ", "UTC-2", 1) == 0, "Could not set the guest time zone");
tzset_nid_postfix();
CheckNullArguments();

constexpr std::array<UtcCase, 7> utcCases{{
{0, 70, 0, 1, 0, 0, 0, 4, 0},
Expand Down
1 change: 1 addition & 0 deletions docs/dev/debt/libc.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@
- [Colons in guest names](../../../core/libs/prx/libc/src/General.cpp) (libc) - on Windows (NTFS) a guest name containing `:` is not a plain file name: `name:stream` opens or creates the alternate data stream `stream` of the file `name`. Creating `/savedata0/C:file` makes an empty file `C` whose stream `file` holds the data, and `stat` of `/savedata0/C` then reports a regular file. `mkdir` of a name containing `:`, such as `C:` or `dir:x`, fails with EINVAL. `name:stream` already behaved this way before drive-letter components were kept under their mount
- [tzname](../../../core/libs/prx/libc/src/Time.cpp) (libc) points at the host's zone names, read at load and again by `tzset`: Windows CRT names are converted from the active ANSI code page to UTF-8 for guest `tzname` and `tm_zone`, while the abbreviations a `TZ` string gives (`EST5EDT` is `EST` and `EDT` on both hosts) stay unchanged. Without `TZ`, the CRT's full names such as `GMT Standard Time` are now valid UTF-8, though ICU's [uprv_tzname](https://github.com/unicode-org/icu/blob/457157a92aa053e632cc7fcfd0e12f8a943b2d11/icu4c/source/common/putil.cpp#L1244-L1264) (ES-DE in PPSA99169) still cannot map them to a zone ID. A payload process on a PS5 (firmware 13.60) read both names empty with `TZ` unset, and its `%Z` printed nothing whatever `tm_zone` held; the names a title process sees are not known
- [localtime, mktime](../../../core/libs/prx/libc/src/Time.cpp) (libc) on Windows convert through the CRT's `localtime_s` and `mktime`, which accept about 1970 to January 3001 UTC and apply the CRT's daylight-saving rule. A time outside that range is converted a whole number of 400-year Gregorian cycles away, where the calendar repeats, so it gets the rule the CRT applies to that year and not the zone's historical one. On macOS `mktime` converts through the host's `mktime`, which refuses every year before 1900. A local time before 1900 is instead solved for directly: the offset `localtime_r` gives for a candidate time, which follows the zone's history (local mean time before its first transition), is subtracted from the local fields, and the step is repeated until the offset holds. `tm_isdst` is not consulted there, so a time with `tm_isdst` set where the zone had no daylight offset is taken as standard time. FreeBSD's [`time1`](https://github.com/freebsd/freebsd-src/blob/stable/11/contrib/tzcode/stdtime/localtime.c#L2036) would instead shift it by the difference between one of the zone's daylight types and a standard one. `gmtime` converts every time whose year fits an `int` on both hosts.
- [gmtime_s and localtime_s](../../../core/libs/prx/libc/src/Time.cpp) (libc) call the constraint handler with "gmtime_s: bad argument" or "localtime_s: bad argument", `EINVAL` and a null pointer when either argument is null, as the `libc.prx` PPSA07923 ships does; `gmtime`, `localtime`, `gmtime_r` and `localtime_r` return null for a null argument without that call. The titles' `gmtime_s` also gives wrong dates outside about 1901 to 2099 and never rejects a time (it returns a wrapped year even for `INT64_MAX`); these functions keep correct dates and return null for an unrepresentable time instead
- [timelocal](../../../core/libs/prx/libc/src/Time.cpp) (libc) sets `tm_isdst = -1` before converting through `mktime`, following FreeBSD 11 [timelocal](https://github.com/freebsd/freebsd-src/blob/stable/11/contrib/tzcode/stdtime/localtime.c#L2126-L2132).
- [strftime and asctime](../../../core/libs/prx/libc/src/Time.cpp) (libc) follow a payload process on a PS5 (firmware 13.60) in the C locale. Out-of-range fields in `%G`, `%g`, `%V`, `%U` and `%W` follow the SDK 7.00 `libc.prx` that PPSA06416 ships, read from its disassembly and run, and are measured on a console only for the cases in the test; it counts a year divisible by 100 as leap only when `tm_year / 400` is 100 or -300. `%k`, `%l`, `%P`, `%s`, `%+`, `%v`, flags and widths are copied as written, `E` or `O` before an unknown conversion is dropped, and a format ending in either throws, where the console crashed. Negative fields print 0 and fields wrap at 100 (`%j` at 1000, `%u` and `%w` at 10); `%Y` and `%G` are the year mod 10000, `%C` is `tm_year / 100 + 19`. A negative weekday or month gives `Sun` or `Jan`. `%z` and `%Z` ignore `tm_gmtoff` and `tm_zone` and use the guest zone and `tzname`. `asctime_r` shares the `asctime` formatting, unmeasured; a null buffer throws
- [wcsftime](../../../core/libs/prx/libc/src/Time.cpp) (libc) has the `strftime` gaps above. A null buffer returns 0, where the console writes through it, and a size at or above `SIZE_MAX / _Getmbcurmax()` returns 0 with `EINVAL`, where the console formats as usual. A null format or `tm` throws. Conversion-overflow continuation follows [reported native-PC measurements of firmware 13.42](https://github.com/boykopovar/AnyPS5/pull/10895#issuecomment-6494817352), not a physical-console run. `%Z`/`%z`, failures within a conversion and differences in title-bundled `libc.prx` remain unverified by that measurement
Expand Down
Loading