Merge pull request #161 from asimplestray/feat/libc-lifecycle

feat(libc): implement cxa lifecycle aliases and init_env
This commit is contained in:
boykopovar
2026-09-30 20:35:36 +03:00
committed by GitHub
6 changed files with 115 additions and 28 deletions
+9
View File
@@ -315,6 +315,15 @@ if(BUILD_TESTING)
set_tests_properties(guest_environment PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_executable(guest_lifecycle_tests tests/GuestLifecycle.cpp)
target_include_directories(guest_lifecycle_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_lifecycle_tests PRIVATE libc)
configure_windows_unwind(guest_lifecycle_tests)
add_test(NAME guest_lifecycle COMMAND guest_lifecycle_tests)
if(WIN32)
set_tests_properties(guest_lifecycle PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_executable(guest_standard_stream_tests tests/GuestStandardStreams.cpp)
target_include_directories(guest_standard_stream_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(guest_standard_stream_tests PRIVATE libc)
-26
View File
@@ -8,29 +8,11 @@ uint32_t Need_sceLibc = 1;
extern "C" {
void APS5_VABI init_env_nid_postfix(const InitEnvParams* params) {
(void)params;
NotImplemented_nid_no_patch(__func__);
}
void APS5_VABI catchReturnFromMain_nid_postfix(int status) {
(void)status;
NotImplemented_nid_no_patch(__func__);
}
int APS5_VABI cxa_atexit_nid_postfix(void (*func)(void*), void* arg, void* d) {
(void)func;
(void)arg;
(void)d;
NotImplemented_nid_no_patch(__func__);
return 0;
}
void APS5_VABI cxa_finalize_nid_postfix(void* d) {
NotImplemented_nid_no_patch(__func__);
(void)d;
}
int APS5_VABI std_execute_once_nid_postfix(int* flag, int (*func)(void*, void*, void**), void* arg) {
(void)flag;
(void)func;
@@ -43,14 +25,6 @@ extern "C" {
LibcHeapTraceInfo_nid_no_patch(info);
}
int APS5_VABI LibcInternalExtCxaThreadAtexit_nid_postfix(void (*destructor)(void*), void* object, void* module_id) {
(void)destructor;
(void)object;
(void)module_id;
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI LibcHeapErrorReportForGame_nid_postfix(
uint64_t msp, uint64_t ptr, uint64_t error,
uint64_t arg3, uint64_t arg4, uint64_t arg5
+9
View File
@@ -29,6 +29,15 @@ int APS5_VABI __cxa_thread_atexit_impl_nid_postfix(void (*func)(void*), void* ar
return __cxxabiv1::__cxa_thread_atexit(func, arg, dso);
}
int APS5_VABI LibcInternalExtCxaThreadAtexit_nid_postfix(void (*destructor)(void*), void* object, void* module_id) {
#ifdef _WIN32
(void)module_id;
return __cxa_thread_atexit_impl_nid_postfix(destructor, object, nullptr);
#else
return __cxa_thread_atexit_impl_nid_postfix(destructor, object, module_id);
#endif
}
const std::error_category* _ZSt17iostream_categoryv_nid_postfix() { return &std::iostream_category(); }
}
+6
View File
@@ -17,6 +17,7 @@
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"
#include "prx/libc/include/GuestLocale.hpp"
#include "SceTypes.hpp"
namespace {
@@ -296,4 +297,9 @@ void APS5_VABI _init_env_nid_postfix() {
ApplicationHeapInitialize_nid_no_patch(ApplicationProcessParameters_nid_no_patch());
}
void APS5_VABI init_env_nid_postfix(const InitEnvParams* params) {
(void)params;
_init_env_nid_postfix();
}
}
+10 -2
View File
@@ -21,7 +21,7 @@ namespace {
std::recursive_mutex g_sysLock;
struct ExitDestructor {
void (*func)(void*);
void (APS5_VABI *func)(void*);
void* arg;
void* dsoHandle;
};
@@ -38,7 +38,7 @@ FileStream _Stderr_nid_postfix{stderr};
FileStream _Stdout_nid_postfix{stdout};
FileStream _Stdin_nid_postfix{stdin};
int APS5_VABI __cxa_atexit_nid_postfix(void (*func)(void*), void* arg, void* dsoHandle) {
int APS5_VABI __cxa_atexit_nid_postfix(void (APS5_VABI *func)(void*), void* arg, void* dsoHandle) {
std::lock_guard lock(g_exitMutex);
g_exitDestructors.push_back({func, arg, dsoHandle});
if (!g_exitRunnerRegistered) {
@@ -64,6 +64,14 @@ void CxaFinalize_nid_no_patch(void* dsoHandle) {
}
}
int APS5_VABI cxa_atexit_nid_postfix(void (APS5_VABI *func)(void*), void* arg, void* dsoHandle) {
return __cxa_atexit_nid_postfix(func, arg, dsoHandle);
}
void APS5_VABI cxa_finalize_nid_postfix(void* dsoHandle) {
CxaFinalize_nid_no_patch(dsoHandle);
}
unsigned int APS5_VABI _Atomic_fetch_add_4_nid_postfix(volatile unsigned int* target, unsigned int value, int memoryOrder) {
(void)memoryOrder;
return GccAtomicFetchAdd(target, value);
+81
View File
@@ -0,0 +1,81 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <atomic>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <thread>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
struct InitEnvParams {
int argc;
std::uint32_t pad;
const char* argv[3];
};
extern "C" {
int APS5_VABI cxa_atexit_nid_postfix(void (APS5_VABI *)(void*), void*, void*);
void APS5_VABI cxa_finalize_nid_postfix(void*);
int APS5_VABI LibcInternalExtCxaThreadAtexit_nid_postfix(void (*)(void*), void*, void*);
void APS5_VABI init_env_nid_postfix(const InitEnvParams*);
}
namespace {
void Require(bool value) {
if (!value) throw std::runtime_error("lifecycle check failed");
}
int order = 0;
int firstAt = -1;
int secondAt = -1;
int otherAt = -1;
void APS5_VABI First(void*) { firstAt = order++; }
void APS5_VABI Second(void*) { secondAt = order++; }
void APS5_VABI Other(void*) { otherAt = order++; }
std::atomic<int> threadCleanups{0};
void ThreadDone(void*) { ++threadCleanups; }
}
int main() {
void* first = reinterpret_cast<void*>(1);
void* second = reinterpret_cast<void*>(2);
Require(cxa_atexit_nid_postfix(First, nullptr, first) == 0);
Require(cxa_atexit_nid_postfix(Second, nullptr, first) == 0);
Require(cxa_atexit_nid_postfix(Other, nullptr, second) == 0);
cxa_finalize_nid_postfix(first);
Require(secondAt == 0 && firstAt == 1 && otherAt == -1);
cxa_finalize_nid_postfix(reinterpret_cast<void*>(3));
Require(otherAt == -1);
cxa_finalize_nid_postfix(second);
Require(otherAt == 2);
std::thread worker([] {
void* image =
#ifdef _WIN32
reinterpret_cast<void*>(GetModuleHandleW(nullptr));
#else
dlopen(nullptr, RTLD_NOW);
#endif
Require(image != nullptr);
Require(LibcInternalExtCxaThreadAtexit_nid_postfix(ThreadDone, nullptr, image) == 0);
#ifndef _WIN32
dlclose(image);
#endif
});
worker.join();
Require(threadCleanups == 1);
try {
init_env_nid_postfix(nullptr);
} catch (const std::runtime_error& error) {
Require(std::strstr(error.what(), "not implemented") == nullptr);
return 0;
}
return 0;
}