From 18790691bd72dbaa58c3e1c161beda8b99835986 Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Fri, 25 Sep 2026 06:39:14 +1000 Subject: [PATCH] feat(libc): implement guest environment variable storage --- core/libs/CMakeLists.txt | 9 ++ core/libs/prx/libc/CMakeLists.txt | 1 + core/libs/prx/libc/src/Environment.cpp | 102 ++++++++++++++++++ core/libs/prx/libc/src/Process.cpp | 1 - .../libc/src/specifics/linux/PosixProcess.cpp | 25 ----- core/libs/tests/GuestEnvironment.cpp | 46 ++++++++ docs/YAMAGI_QUAKE2.md | 17 +++ 7 files changed, 175 insertions(+), 26 deletions(-) create mode 100644 core/libs/prx/libc/src/Environment.cpp delete mode 100644 core/libs/prx/libc/src/specifics/linux/PosixProcess.cpp create mode 100644 core/libs/tests/GuestEnvironment.cpp diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index 6d96dd95a..70a665415 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -104,6 +104,15 @@ endforeach() add_custom_target(libs DEPENDS ${PATCHED_OUTPUTS}) if(BUILD_TESTING) + add_executable(guest_environment_tests tests/GuestEnvironment.cpp) + target_include_directories(guest_environment_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + target_link_libraries(guest_environment_tests PRIVATE libc) + configure_windows_unwind(guest_environment_tests) + add_test(NAME guest_environment COMMAND guest_environment_tests) + if(WIN32) + set_tests_properties(guest_environment PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + 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) diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index d3516042f..48a352602 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -19,6 +19,7 @@ target_sources(libc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/RuntimeSupport.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/LocaleSupport.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Process.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/Environment.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Errors.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/CxxAbiSupport.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/exception/Exports.cpp diff --git a/core/libs/prx/libc/src/Environment.cpp b/core/libs/prx/libc/src/Environment.cpp new file mode 100644 index 000000000..080c8b110 --- /dev/null +++ b/core/libs/prx/libc/src/Environment.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "prx/libc/include/general/VabiMacros.hpp" + +#ifndef _WIN32 +extern char** environ; +#endif + +namespace { +struct EnvironmentEntry { + std::string owned; + char* borrowed = nullptr; + char* Data() { return borrowed ? borrowed : owned.data(); } + bool Matches(std::string_view name) { + const char* data = Data(); + const char* separator = std::strchr(data, '='); + return separator && std::string_view(data, separator - data) == name; + } +}; +std::mutex environmentMutex; +std::vector& Environment() { + // Seed once from the host. Guest mutations remain local to the guest runtime. + static std::vector entries = [] { + std::vector result; +#ifdef _WIN32 + char** source = _environ; +#else + char** source = environ; +#endif + if (source) for (; *source; ++source) { + if (**source && **source != '=' && std::strchr(*source, '=')) + result.push_back({*source, nullptr}); + } + return result; + }(); + return entries; +} +bool ValidEnvironmentName(const char* name) { + return name && *name && !std::strchr(name, '='); +} +int EnvironmentError(int error) { errno = error; return -1; } +} + +extern "C" { +char* APS5_VABI getenv_nid_postfix(const char* name) { + if (!ValidEnvironmentName(name)) return nullptr; + try { + std::lock_guard lock(environmentMutex); + for (auto& entry : Environment()) + if (entry.Matches(name)) return entry.Data() + std::strlen(name) + 1; + return nullptr; + } catch (const std::bad_alloc&) { errno = 12; return nullptr; } +} + +int APS5_VABI setenv_nid_postfix(const char* name, const char* value, int overwrite) { + if (!ValidEnvironmentName(name) || !value) return EnvironmentError(22); + try { + std::lock_guard lock(environmentMutex); + auto& entries = Environment(); + auto found = std::find_if(entries.begin(), entries.end(), + [name](auto& entry) { return entry.Matches(name); }); + if (found != entries.end() && !overwrite) return 0; + EnvironmentEntry replacement{std::string(name) + '=' + value, nullptr}; + if (found != entries.end()) *found = std::move(replacement); + else entries.push_back(std::move(replacement)); + return 0; + } catch (const std::bad_alloc&) { return EnvironmentError(12); } +} + +int APS5_VABI unsetenv_nid_postfix(const char* name) { + if (!ValidEnvironmentName(name)) return EnvironmentError(22); + try { + std::lock_guard lock(environmentMutex); + auto& entries = Environment(); + const std::string key(name); + std::erase_if(entries, [&key](auto& entry) { return entry.Matches(key); }); + return 0; + } catch (const std::bad_alloc&) { return EnvironmentError(12); } +} + +int APS5_VABI putenv_nid_postfix(char* text) { + const char* separator = text ? std::strchr(text, '=') : nullptr; + if (!separator || separator == text) return EnvironmentError(22); + try { + std::lock_guard lock(environmentMutex); + auto& entries = Environment(); + const std::string_view name(text, separator - text); + auto found = std::find_if(entries.begin(), entries.end(), + [name](auto& entry) { return entry.Matches(name); }); + EnvironmentEntry replacement{{}, text}; + if (found != entries.end()) *found = std::move(replacement); + else entries.push_back(std::move(replacement)); + return 0; + } catch (const std::bad_alloc&) { return EnvironmentError(12); } +} +} diff --git a/core/libs/prx/libc/src/Process.cpp b/core/libs/prx/libc/src/Process.cpp index 5e8bf25b3..ae3a6e938 100644 --- a/core/libs/prx/libc/src/Process.cpp +++ b/core/libs/prx/libc/src/Process.cpp @@ -91,4 +91,3 @@ int APS5_VABI atexit_nid_postfix(atexit_func_t func) { } -#include "prx/libc/src/specifics/linux/PosixProcess.cpp" diff --git a/core/libs/prx/libc/src/specifics/linux/PosixProcess.cpp b/core/libs/prx/libc/src/specifics/linux/PosixProcess.cpp deleted file mode 100644 index a362f9f48..000000000 --- a/core/libs/prx/libc/src/specifics/linux/PosixProcess.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include - -extern "C" { - -int APS5_VABI setenv_nid_postfix(const char* name, const char* value, int overwrite) { - if (overwrite == 0 && std::getenv(name) != nullptr) { - return 0; - } - const size_t nameLength = std::strlen(name); - const size_t valueLength = std::strlen(value); - char* entry = static_cast(std::malloc(nameLength + valueLength + 2)); - if (entry == nullptr) { - errno = ENOMEM; - return -1; - } - std::memcpy(entry, name, nameLength); - entry[nameLength] = '='; - std::memcpy(entry + nameLength + 1, value, valueLength); - entry[nameLength + 1 + valueLength] = '\0'; - return ::putenv(entry); -} - -} diff --git a/core/libs/tests/GuestEnvironment.cpp b/core/libs/tests/GuestEnvironment.cpp new file mode 100644 index 000000000..df1910cf4 --- /dev/null +++ b/core/libs/tests/GuestEnvironment.cpp @@ -0,0 +1,46 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +extern "C" { +char* APS5_VABI getenv_nid_postfix(const char*); +int APS5_VABI setenv_nid_postfix(const char*, const char*, int); +int APS5_VABI unsetenv_nid_postfix(const char*); +int APS5_VABI putenv_nid_postfix(char*); +int* APS5_VABI __error_nid_postfix(); +} +static void Require(bool value) { if (!value) std::abort(); } +int main() { + const char* key = "ANYPS5_GUEST_ENV_TEST_4C27"; +#ifdef _WIN32 + Require(_putenv_s(key, "inherited") == 0); +#else + Require(::setenv(key, "inherited", 1) == 0); +#endif + Require(std::strcmp(getenv_nid_postfix(key), "inherited") == 0); + Require(setenv_nid_postfix(key, "kept out", 0) == 0); + Require(std::strcmp(getenv_nid_postfix(key), "inherited") == 0); + char value[] = "copied"; + Require(setenv_nid_postfix(key, value, 1) == 0); + value[0] = 'X'; + Require(std::strcmp(getenv_nid_postfix(key), "copied") == 0); + Require(std::strcmp(std::getenv(key), "inherited") == 0); + Require(setenv_nid_postfix(key, "", 1) == 0); + Require(getenv_nid_postfix(key) && *getenv_nid_postfix(key) == 0); + char borrowed[] = "ANYPS5_GUEST_ENV_TEST_4C27=one"; + Require(putenv_nid_postfix(borrowed) == 0); + char* position = std::strchr(borrowed, '=') + 1; + Require(getenv_nid_postfix(key) == position); + std::memcpy(position, "two", 3); + Require(std::strcmp(getenv_nid_postfix(key), "two") == 0); + Require(setenv_nid_postfix("anyps5_guest_env_test_4c27", "lower", 1) == 0); + Require(std::strcmp(getenv_nid_postfix(key), "two") == 0); + Require(setenv_nid_postfix("bad=name", "x", 1) == -1); + Require(*__error_nid_postfix() == 22); + Require(unsetenv_nid_postfix("") == -1); + char invalid[] = "no-separator"; + Require(putenv_nid_postfix(invalid) == -1); + Require(unsetenv_nid_postfix(key) == 0); + Require(getenv_nid_postfix(key) == nullptr); + Require(unsetenv_nid_postfix(key) == 0); + Require(unsetenv_nid_postfix("anyps5_guest_env_test_4c27") == 0); +} diff --git a/docs/YAMAGI_QUAKE2.md b/docs/YAMAGI_QUAKE2.md index d2b86cc65..e4b56936e 100644 --- a/docs/YAMAGI_QUAKE2.md +++ b/docs/YAMAGI_QUAKE2.md @@ -236,3 +236,20 @@ All fourteen CTest suites pass and patched compatibility libraries rebuild. The unchanged converted game now resolves fcntl and fprintf, and startup stops at `smbQukfxYJM` (`getenv`). This remains an import-resolution result: guest entry-point execution and a rendered frame are still unverified. + +## Follow-up: guest environment variables + +Implemented getenv, setenv, unsetenv, and putenv as one case-sensitive guest +environment, initialized from the host CRT environment on first access. Guest +updates do not mutate the host environment. setenv owns a copy, while putenv +retains the caller's string so later changes are visible. Empty values remain +distinct from missing variables; invalid update names return guest EINVAL. +The old allocating host-putenv setenv implementation has been replaced. + +Tests cover inherited values, overwrite control, copying versus borrowed +storage, empty values, case sensitivity, removal, invalid names, and unchanged +host values. All fifteen suites pass. Direct environ access and propagation to +host child processes or host locale functions are not implemented by this store. + +Latest unchanged-game startup resolves getenv and stops at `ay3uROQAc5A` +(`opendir`). Guest entry-point execution and rendered output remain unverified.