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 core/libs/prx/libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ if(BUILD_TESTING)
set_tests_properties(guest_time_conversion PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(guest_timespec_get_tests ${LIBS_INCLUDE_DIR}/tests/GuestTimespecGet.cpp)
target_include_directories(guest_timespec_get_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_timespec_get_tests PRIVATE libc)
configure_windows_unwind(guest_timespec_get_tests)
add_test(NAME guest_timespec_get COMMAND guest_timespec_get_tests)
if(WIN32)
set_tests_properties(guest_timespec_get PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(guest_mktime_tests ${LIBS_INCLUDE_DIR}/tests/GuestMktime.cpp)
target_include_directories(guest_mktime_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_mktime_tests PRIVATE libc)
Expand Down
18 changes: 18 additions & 0 deletions core/libs/prx/libc/src/Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <windows.h>
#endif

#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/GuestTime.hpp"
#include "prx/libc/include/SonyCrt.hpp"
Expand Down Expand Up @@ -568,6 +569,23 @@ int64_t APS5_VABI _Xtime_get_ticks_nid_postfix() {
#endif
}

int APS5_VABI timespec_get_nid_postfix(KernelTimespec* ts, int base) {
if (ts == nullptr || base != TIME_UTC) return 0;
#ifdef _WIN32
FILETIME ft{};
GetSystemTimePreciseAsFileTime(&ft);
const uint64_t hundredNs = ((static_cast<uint64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime) - 116444736000000000ULL;
ts->tv_sec = static_cast<std::int64_t>(hundredNs / 10000000ULL);
ts->tv_nsec = static_cast<std::int64_t>((hundredNs % 10000000ULL) * 100ULL);
#else
timespec host{};
if (clock_gettime(CLOCK_REALTIME, &host) != 0) return 0;
ts->tv_sec = host.tv_sec;
ts->tv_nsec = host.tv_nsec;
#endif
return TIME_UTC;
}

double APS5_VABI libc_difftime_nid_postfix(int64_t time1, int64_t time0) {
if (time1 >= time0) return static_cast<double>(static_cast<uint64_t>(time1) - static_cast<uint64_t>(time0));
return -static_cast<double>(static_cast<uint64_t>(time0) - static_cast<uint64_t>(time1));
Expand Down
28 changes: 28 additions & 0 deletions core/libs/tests/GuestTimespecGet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <cstdint>
#include <ctime>
#include <cstdlib>

struct GuestTimespec {
std::int64_t tv_sec;
std::int64_t tv_nsec;
};

extern "C" int APS5_VABI timespec_get_nid_postfix(GuestTimespec* ts, int base);

static void Require(bool value) { if (!value) std::abort(); }

int main() {
Require(timespec_get_nid_postfix(nullptr, TIME_UTC) == 0);
GuestTimespec ts{};
Require(timespec_get_nid_postfix(&ts, 0) == 0);
GuestTimespec filled{-1, -1};
Require(timespec_get_nid_postfix(&filled, TIME_UTC) == TIME_UTC);
Require(filled.tv_sec > 0);
Require(filled.tv_nsec >= 0 && filled.tv_nsec < 1000000000LL);
GuestTimespec later{-1, -1};
Require(timespec_get_nid_postfix(&later, TIME_UTC) == TIME_UTC);
Require(later.tv_nsec >= 0 && later.tv_nsec < 1000000000LL);
Require(later.tv_sec > filled.tv_sec || (later.tv_sec == filled.tv_sec && later.tv_nsec >= filled.tv_nsec));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This second-read ordering check assumes UTC wall-clock time is monotonic. Both clock_gettime(CLOCK_REALTIME) and GetSystemTimePreciseAsFileTime can move backward after an administrator/NTP clock correction, so the test can fail even when timespec_get correctly returns UTC. Please avoid comparing two wall-clock reads, or bound the result against an independently sampled wall clock with an allowance that does not require monotonicity.

return 0;
}
Loading