feat(libc): implement guest environment variable storage

This commit is contained in:
SamG-Coder
2026-09-25 06:39:14 +10:00
parent f49cace1ca
commit 18790691bd
7 changed files with 175 additions and 26 deletions
+9
View File
@@ -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:$<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)
+1
View File
@@ -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
+102
View File
@@ -0,0 +1,102 @@
#include <cstdlib>
#include <cerrno>
#include <cstring>
#include <algorithm>
#include <mutex>
#include <string>
#include <string_view>
#include <vector>
#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<EnvironmentEntry>& Environment() {
// Seed once from the host. Guest mutations remain local to the guest runtime.
static std::vector<EnvironmentEntry> entries = [] {
std::vector<EnvironmentEntry> 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); }
}
}
-1
View File
@@ -91,4 +91,3 @@ int APS5_VABI atexit_nid_postfix(atexit_func_t func) {
}
#include "prx/libc/src/specifics/linux/PosixProcess.cpp"
@@ -1,25 +0,0 @@
#include <cstdlib>
#include <cerrno>
#include <cstring>
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<char*>(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);
}
}
+46
View File
@@ -0,0 +1,46 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <cstdlib>
#include <cstring>
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);
}
+17
View File
@@ -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.