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
5 changes: 4 additions & 1 deletion core/libs/prx/libSceSystemService/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ if(BUILD_TESTING)
add_test(NAME guest_return_from_main COMMAND guest_system_exit_tests --return-from-main)
add_test(NAME guest_system_relaunch COMMAND guest_system_exit_tests --relaunch-driver)
add_test(NAME guest_nested_exit COMMAND guest_system_exit_tests --nested-exit-driver)
add_test(NAME guest_exit_finalizers_before_shutdown COMMAND guest_system_exit_tests --exit-order-driver)
set_tests_properties(guest_system_exit guest_requested_exit PROPERTIES
PASS_REGULAR_EXPRESSION "Guest shutdown completed"
FAIL_REGULAR_EXPRESSION "Guest exit handler ran|Guest thread resumed during exit")
Expand All @@ -50,6 +51,8 @@ if(BUILD_TESTING)
PASS_REGULAR_EXPRESSION "Guest relaunch completed" TIMEOUT 30)
set_tests_properties(guest_nested_exit PROPERTIES
PASS_REGULAR_EXPRESSION "Guest nested exit completed" TIMEOUT 30)
set_tests_properties(guest_exit_finalizers_before_shutdown PROPERTIES
PASS_REGULAR_EXPRESSION "Guest finalizers ran before shutdown" TIMEOUT 30)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_test_executable(guest_system_load_exec_standard_error_tests ${LIBS_INCLUDE_DIR}/tests/GuestSystemServiceLoadExecStandardError.cpp)
target_include_directories(guest_system_load_exec_standard_error_tests PRIVATE ${LIBS_INCLUDE_DIR})
Expand All @@ -58,7 +61,7 @@ if(BUILD_TESTING)
set_tests_properties(guest_system_load_exec_standard_error PROPERTIES TIMEOUT 20)
endif()
if(WIN32)
set_tests_properties(guest_system_exit guest_requested_exit guest_system_exit_parameters guest_immediate_exit guest_return_from_main guest_quick_exit guest_system_relaunch guest_nested_exit PROPERTIES ENVIRONMENT_MODIFICATION
set_tests_properties(guest_system_exit guest_requested_exit guest_system_exit_parameters guest_immediate_exit guest_return_from_main guest_quick_exit guest_system_relaunch guest_nested_exit guest_exit_finalizers_before_shutdown PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(guest_system_service_tests ${LIBS_INCLUDE_DIR}/tests/GuestSystemService.cpp)
Expand Down
3 changes: 2 additions & 1 deletion core/libs/prx/libc/src/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ void StartExit() {
if (!runningExit) LibcAwaitExit_nid_postfix();
} else {
runningExit = true;
LibcRunShutdown_nid_postfix();
}
}

Expand Down Expand Up @@ -165,11 +164,13 @@ extern "C" {
[[noreturn]] void LibcExit_nid_no_patch(int code) {
StartExit();
CxaFinalize_nid_no_patch(nullptr);
LibcRunShutdown_nid_postfix();
std::exit(code);
}

[[noreturn]] void LibcExitWithoutHandlers_nid_no_patch(int code) {
StartExit();
LibcRunShutdown_nid_postfix();
std::fflush(nullptr);
LibcTerminate_nid_no_patch(code);
}
Expand Down
35 changes: 34 additions & 1 deletion core/libs/tests/GuestSystemExit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ void MainShutdown() { AppendMarker("shutdown"); }
void SecondaryShutdown() { AppendMarker("secondary shutdown"); }
void APS5_VABI RemainingHandler(void*) { AppendMarker("remaining handler"); }
void APS5_VABI ExitingHandler(void*) { exit_nid_postfix(7); }
void GuestExitShutdown() { AppendMarker("shutdown"); }
void APS5_VABI CheckShutdownStillRunning(void*) {
if (LibcShutdownToken_nid_postfix().stop_requested()) std::_Exit(8);
AppendMarker("guest finalizer");
}
int RunChild(const std::filesystem::path& self, const char* mode) {
#ifdef _WIN32
std::wstring commandLine = L"\"" + self.wstring() + L"\" " + std::wstring(mode, mode + std::strlen(mode));
Expand Down Expand Up @@ -130,10 +135,27 @@ int DriveNestedExit() {
std::error_code ignored;
std::filesystem::remove(marker, ignored);
Require(code == 7);
Require(text == "remaining handler\n");
Require(text == "guest finalizer\nremaining handler\nshutdown\n");
std::puts("Guest nested exit completed");
return 0;
}
int DriveExitOrder() {
const auto self = OwnExecutable();
const auto marker = std::filesystem::temp_directory_path() / ("anyps5-exit-order-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()) + ".txt");
SetVariable(RelaunchMarker, marker.string().c_str());
const int code = RunChild(self, "--exit-order");
std::string text;
{
std::ifstream file(marker);
text.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
}
std::error_code ignored;
std::filesystem::remove(marker, ignored);
Require(code == 0);
Require(text == "guest finalizer\nshutdown\n");
std::puts("Guest finalizers ran before shutdown");
return 0;
}
int DriveRelaunch() {
const auto self = OwnExecutable();
const auto secondary = self.parent_path() / ("picker.picker.bin" + self.extension().string());
Expand Down Expand Up @@ -187,11 +209,22 @@ int main(int argc, char** argv) {
return DriveNestedExit();
}
if (argc > firstArgument && std::strcmp(argv[firstArgument], "--nested-exit") == 0) {
LibcRegisterShutdown_nid_postfix(GuestExitShutdown);
Require(__cxa_atexit_nid_postfix(RemainingHandler, nullptr, nullptr) == 0);
Require(__cxa_atexit_nid_postfix(CheckShutdownStillRunning, nullptr, nullptr) == 0);
Require(__cxa_atexit_nid_postfix(ExitingHandler, nullptr, nullptr) == 0);
exit_nid_postfix(0);
return 2;
}
if (argc > firstArgument && std::strcmp(argv[firstArgument], "--exit-order-driver") == 0) {
return DriveExitOrder();
}
if (argc > firstArgument && std::strcmp(argv[firstArgument], "--exit-order") == 0) {
LibcRegisterShutdown_nid_postfix(GuestExitShutdown);
Require(__cxa_atexit_nid_postfix(CheckShutdownStillRunning, nullptr, nullptr) == 0);
exit_nid_postfix(0);
return 2;
}
if (argc > firstArgument && std::strcmp(argv[firstArgument], "--relaunch-driver") == 0) {
return DriveRelaunch();
}
Expand Down
Loading