mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #1107 from Sanjaysrinivas/fix/libc-thread-safe-time
fix(libc): avoid time conversion races
This commit is contained in:
@@ -45,6 +45,16 @@ if(WIN32)
|
||||
endif()
|
||||
|
||||
if(BUILD_TESTING)
|
||||
add_test_executable(guest_time_conversion_tests ${LIBS_INCLUDE_DIR}/tests/GuestTimeConversion.cpp)
|
||||
target_include_directories(guest_time_conversion_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(guest_time_conversion_tests PRIVATE libc)
|
||||
configure_windows_unwind(guest_time_conversion_tests)
|
||||
add_test(NAME guest_time_conversion COMMAND guest_time_conversion_tests)
|
||||
set_tests_properties(guest_time_conversion PROPERTIES TIMEOUT 20)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_time_conversion PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_test_executable(guest_scan_count_tests ${LIBS_INCLUDE_DIR}/tests/GuestScanCount.cpp)
|
||||
target_include_directories(guest_scan_count_tests PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_link_libraries(guest_scan_count_tests PRIVATE libc)
|
||||
|
||||
@@ -11,6 +11,9 @@
|
||||
|
||||
extern "C" {
|
||||
|
||||
std::tm* APS5_VABI localtime_s_nid_postfix(const int64_t* timer, std::tm* result);
|
||||
std::tm* APS5_VABI gmtime_s_nid_postfix(const int64_t* timer, std::tm* result);
|
||||
|
||||
int64_t APS5_VABI libc_time_nid_postfix(int64_t* timer) {
|
||||
std::time_t t = std::time(nullptr);
|
||||
if (timer != nullptr) *timer = static_cast<int64_t>(t);
|
||||
@@ -44,20 +47,12 @@ double APS5_VABI difftime_nid_postfix(int64_t time1, int64_t time0) {
|
||||
|
||||
std::tm* APS5_VABI libc_gmtime_nid_postfix(const int64_t* timer) {
|
||||
static thread_local std::tm result;
|
||||
const std::time_t t = static_cast<std::time_t>(*timer);
|
||||
const std::tm* converted = std::gmtime(&t);
|
||||
if (converted == nullptr) return nullptr;
|
||||
result = *converted;
|
||||
return &result;
|
||||
return gmtime_s_nid_postfix(timer, &result);
|
||||
}
|
||||
|
||||
std::tm* APS5_VABI libc_localtime_nid_postfix(const int64_t* timer) {
|
||||
static thread_local std::tm result;
|
||||
const std::time_t t = static_cast<std::time_t>(*timer);
|
||||
const std::tm* converted = std::localtime(&t);
|
||||
if (converted == nullptr) return nullptr;
|
||||
result = *converted;
|
||||
return &result;
|
||||
return localtime_s_nid_postfix(timer, &result);
|
||||
}
|
||||
|
||||
std::tm* APS5_VABI localtime_nid_postfix(const int64_t* timer) {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <barrier>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <limits>
|
||||
#include <thread>
|
||||
|
||||
extern "C" {
|
||||
std::tm* APS5_VABI libc_gmtime_nid_postfix(const std::int64_t*);
|
||||
std::tm* APS5_VABI gmtime_nid_postfix(const std::int64_t*);
|
||||
std::tm* APS5_VABI libc_localtime_nid_postfix(const std::int64_t*);
|
||||
std::tm* APS5_VABI localtime_nid_postfix(const std::int64_t*);
|
||||
std::tm* APS5_VABI gmtime_s_nid_postfix(const std::int64_t*, std::tm*);
|
||||
std::tm* APS5_VABI localtime_s_nid_postfix(const std::int64_t*, std::tm*);
|
||||
}
|
||||
|
||||
using Converter = std::tm* (APS5_VABI *)(const std::int64_t*);
|
||||
using BufferedConverter = std::tm* (APS5_VABI *)(const std::int64_t*, std::tm*);
|
||||
|
||||
static void Require(bool value, const char* message) {
|
||||
if (!value) {
|
||||
std::fprintf(stderr, "%s\n", message);
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
|
||||
static bool Equal(const std::tm& left, const std::tm& 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
|
||||
&& left.tm_wday == right.tm_wday && left.tm_yday == right.tm_yday && left.tm_isdst == right.tm_isdst;
|
||||
}
|
||||
|
||||
static void CheckConcurrent(Converter convert, BufferedConverter buffered, const char* name) {
|
||||
constexpr std::array<std::int64_t, 8> timers{0, 86400, 946684800, 1078012800, 1609459200, 1709164800, 1893456000, 2145916799};
|
||||
std::array<std::tm, timers.size()> expected{};
|
||||
for (std::size_t index = 0; index < timers.size(); ++index)
|
||||
Require(buffered(&timers[index], &expected[index]) == &expected[index], "Buffered conversion failed");
|
||||
std::barrier start(static_cast<std::ptrdiff_t>(timers.size()));
|
||||
std::atomic<bool> matches{true};
|
||||
std::array<std::thread, timers.size()> workers;
|
||||
for (std::size_t index = 0; index < workers.size(); ++index) {
|
||||
workers[index] = std::thread([&, index] {
|
||||
start.arrive_and_wait();
|
||||
for (int iteration = 0; iteration < 50000; ++iteration) {
|
||||
const auto* result = convert(&timers[index]);
|
||||
if (result == nullptr || !Equal(*result, expected[index])) {
|
||||
matches = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& worker : workers) worker.join();
|
||||
Require(matches, name);
|
||||
const std::int64_t invalid = std::numeric_limits<std::int64_t>::max();
|
||||
Require(convert(&invalid) == nullptr, "Unrepresentable time was accepted");
|
||||
}
|
||||
|
||||
int main() {
|
||||
#ifdef _WIN32
|
||||
_putenv_s("TZ", "UTC-2");
|
||||
_tzset();
|
||||
#else
|
||||
setenv("TZ", "UTC-2", 1);
|
||||
tzset();
|
||||
#endif
|
||||
const std::int64_t epoch = 0;
|
||||
std::tm utc{}, local{};
|
||||
Require(gmtime_s_nid_postfix(&epoch, &utc) == &utc && utc.tm_year == 70 && utc.tm_mon == 0
|
||||
&& utc.tm_mday == 1 && utc.tm_hour == 0, "UTC epoch conversion failed");
|
||||
Require(localtime_s_nid_postfix(&epoch, &local) == &local && local.tm_year == 70 && local.tm_mon == 0
|
||||
&& local.tm_mday == 1 && local.tm_hour == 2, "Local epoch conversion failed");
|
||||
CheckConcurrent(libc_gmtime_nid_postfix, gmtime_s_nid_postfix, "Concurrent libc_gmtime returned another thread's date");
|
||||
CheckConcurrent(gmtime_nid_postfix, gmtime_s_nid_postfix, "Concurrent gmtime returned another thread's date");
|
||||
CheckConcurrent(libc_localtime_nid_postfix, localtime_s_nid_postfix, "Concurrent libc_localtime returned another thread's date");
|
||||
CheckConcurrent(localtime_nid_postfix, localtime_s_nid_postfix, "Concurrent localtime returned another thread's date");
|
||||
}
|
||||
Reference in New Issue
Block a user