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
7 changes: 7 additions & 0 deletions core/libs/prx/libSceSystemService/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ 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)
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})
target_link_libraries(guest_system_load_exec_standard_error_tests PRIVATE libSceSystemService libkernel libc)
add_test(NAME guest_system_load_exec_standard_error COMMAND guest_system_load_exec_standard_error_tests)
set_tests_properties(guest_system_load_exec_standard_error PROPERTIES TIMEOUT 20)
endif()
if(WIN32)
set_tests_properties(guest_system_exit guest_system_exit_parameters guest_immediate_exit guest_return_from_main guest_quick_exit guest_system_relaunch guest_nested_exit PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
Expand Down
3 changes: 3 additions & 0 deletions core/libs/prx/libSceSystemService/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ void StartHostExecutable(HostLaunch& launch) {
std::vector<char*> argv;
for (auto& argument : launch.arguments) argv.push_back(argument.data());
argv.push_back(nullptr);
if (const int host = HostStandardError_nid_no_patch(); host >= 0 && ::dup2(host, 2) != 2) {
throw std::runtime_error("sceSystemServiceLoadExec: cannot give the host standard error back to descriptor 2: " + std::string(std::strerror(errno)));
}
::execv(launch.executable.c_str(), argv.data());
throw std::runtime_error("sceSystemServiceLoadExec: execv failed: " + std::string(std::strerror(errno)));
}
Expand Down
3 changes: 3 additions & 0 deletions core/libs/prx/libSceSystemService/LoadExec.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "LoadExec.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/Shutdown.hpp"
#include <array>
#include <algorithm>
Expand Down Expand Up @@ -153,6 +154,8 @@ Handle Duplicate(HANDLE source, DWORD access) {
pointers.push_back(nullptr);
LibcRunShutdown_nid_postfix();
std::fflush(nullptr);
if (const int host = HostStandardError_nid_no_patch(); host >= 0 && dup2(host, 2) != 2)
throw std::system_error(errno, std::generic_category(), "LoadExec: giving the host standard error back to descriptor 2");
std::filesystem::current_path(target.parent_path());
if ((descriptor > 3 && close_range(3, static_cast<unsigned>(descriptor - 1), 0) != 0) ||
close_range(static_cast<unsigned>(std::max(3, descriptor + 1)), ~0u, 0) != 0)
Expand Down
74 changes: 74 additions & 0 deletions core/libs/tests/GuestSystemServiceLoadExecStandardError.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <sys/wait.h>
#include <unistd.h>

extern "C" {
int APS5_VABI sceSystemServiceLoadExec(const char*, const char* const*);
int APS5_VABI dup2_nid_postfix(int, int);
}

namespace {

constexpr const char* Marker = "APS5_LOAD_EXEC_STANDARD_ERROR";
constexpr const char* AfterLoadExec = "--after-load-exec";

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

std::string Drain(int descriptor) {
std::string text;
char buffer[4096];
for (ssize_t count; (count = ::read(descriptor, buffer, sizeof(buffer))) > 0;) text.append(buffer, static_cast<std::size_t>(count));
::close(descriptor);
return text;
}

void Run(const char* mode, const char* const* arguments) {
int host[2] = {-1, -1};
int guest[2] = {-1, -1};
Require(::pipe(host) == 0);
Require(::pipe(guest) == 0);
const pid_t child = ::fork();
Require(child >= 0);
if (child == 0) {
::close(host[0]);
::close(guest[0]);
if (::dup2(host[1], 2) != 2) ::_exit(70);
::close(host[1]);
if (dup2_nid_postfix(guest[1], 2) != 2) ::_exit(71);
::close(guest[1]);
if (::setenv(Marker, mode, 1) != 0) ::_exit(72);
sceSystemServiceLoadExec("/app0/eboot.bin", arguments);
::_exit(73);
}
::close(host[1]);
::close(guest[1]);
const std::string hostText = Drain(host[0]);
const std::string guestText = Drain(guest[0]);
int status = 0;
Require(::waitpid(child, &status, 0) == child);
Require(WIFEXITED(status) && WEXITSTATUS(status) == 0);
Require(hostText == std::string("started ") + mode + "\n");
Require(guestText.empty());
}

}

int main(int argc, char** argv) {
if (const char* mode = std::getenv(Marker); mode != nullptr) {
if (std::strcmp(mode, "list") == 0) Require(argc == 1 && std::strcmp(argv[0], AfterLoadExec) == 0);
else Require(std::strcmp(mode, "none") == 0 && argc == 1);
std::fprintf(stderr, "started %s\n", mode);
return 0;
}
const char* const arguments[] = {AfterLoadExec, nullptr};
Run("list", arguments);
Run("none", nullptr);
std::puts("LoadExec standard error tests passed");
return 0;
}
1 change: 1 addition & 0 deletions docs/dev/debt/libSceSystemService.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@

- [sceSystemServiceLoadExec](../../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - besides `"exit"`, it starts the title's own `/app0/eboot.bin` or another executable under `/app0` that the relinker converted with `--executable`, which is looked up beside the running one (`/app0/picker/picker.bin` as `picker.picker.bin` with the running executable's extension). The main executable is the one `APS5_EBOOT` names, which the first call sets to its own path. After the guest shutdown the old process ends without running guest exit handlers or host destructors. `"exit"` ends the process after the host shutdown without running the title's `atexit` and `__cxa_atexit` handlers, since the console's shell closes the title instead; a line the title writes just before through its own thread, such as a forwarded standard error line, may not be printed. [VLC for PS5](https://github.com/ZiZc3/VLC/blob/86bb7d474a303f62bb3c2fc12f728db4846085b8/src/platform_ps5.c#L565-L571) (PPSA85300) relaunches itself with no arguments; RetroArch (PPSA99169) starts its frontend picker with `{"", NULL}`, and the picker starts `eboot.bin` with `{"--ps5-mode=retroarch", NULL}` or `/app0/es-de/es-de.bin` ([picker.cpp](https://github.com/mihawk-99/PS5_RetroArch/blob/bead80725cc11e3c5c65bd9424b0a4494ae36d45/frontends/picker/picker.cpp#L444-L446)), so the list holds only arguments. A list with a first entry becomes the whole guest `argv`, with no program name in front ([LoadExec.cpp](../../../core/libs/prx/libSceSystemService/LoadExec.cpp)): a [PS5 RetroArch console transcript](https://github.com/mihawk-99/PS5_RetroArch/blob/bead80725cc11e3c5c65bd9424b0a4494ae36d45/evidence/loadexec-second-image/relaunch-test.jsonl) records a restart with one argument entering with `argc=1`, `argv[0]="--ps5-relaunch=1"`, and a launch from the home screen with `argc=1`, `argv[0]=""` ([relaunch_ps5.cpp](https://github.com/mihawk-99/PS5_RetroArch/blob/bead80725cc11e3c5c65bd9424b0a4494ae36d45/src/relaunch_ps5.cpp#L161-L164)). For such a list Linux replaces the process with `fexecve`: the PID and the standard streams stay, the other descriptors are closed and the working directory becomes the executable's. Windows starts a suspended process that inherits only the standard streams and a handle of the old process, and its entry stub waits for the old process to end before it loads the title's libraries, so a shell or script that started the title sees it end. A null list or one without a first entry keeps the earlier launch: the new executable gets only its own path as its command line (`CreateProcessW` on Windows, `execv` on Linux), so its `argv[0]` is the host path, and the old process ends without waiting for it. What the console passes for such a list is not known, nor are its argument limits and error codes. Paths outside `/app0`, unconverted executables and macOS hosts throw
- [Running application ID](../../../core/libs/prx/libSceSystemService/Export.cpp) (libSceSystemService) - `sceSystemServiceGetAppIdOfRunningBigApp` reports 1 as the running title's app ID. The value is AnyPS5's own: the console's shell assigns app IDs and their values are not known here, so other calls that take an app ID must use the same one. `sceSystemServiceKillApp` with that ID and the arguments -1, 0 and 0, which [SwanStationPS5](https://github.com/darkxex/SwanStationPS5/blob/4cdc981d1a64ffb7f2af73f7a27a9e91a3cb5eb4/src/main.c#L1050-L1058) passes to close itself and [ps5-payload-dev's hbldr](https://github.com/ps5-payload-dev/websrv/blob/46374edee6627885abac717de24da7a1d73af6f1/src/ps5/hbldr.c#L425-L435) passes to close the running game, ends the title as its own `exit(0)` would: the guest's shutdown and `atexit` handlers run and the process exits with status 0. On the console the call returns and the shell then ends the title, and whether those handlers run there is unknown; here there is no shell, so the call does not return. Another app ID or other arguments throw.
- [LoadExec descriptors](../../../core/libs/prx/libSceSystemService/LoadExec.cpp) (libSceSystemService) - on Linux, a call with an argument list replaces the process with `fexecve`, which first closes every descriptor above 2; a call without one uses `execv`, and the guest's descriptors stay open across it. On both paths descriptor 2 first gets back the host standard error a guest redirect kept, so the runtime's output does not follow the title's redirect (RetroArch's trace file, VLC's log) into the next executable. Which descriptors the console passes to the new process is not known
Loading