hle: Get and SetEnvironmentVariable over the process environment #75

Manually merged
github merged 3 commits from github-bot/gh-75 into main 2026-11-26 17:09:33 +00:00 AGit
Owner

@⁠Olima34 on GitHub · 2026-11-26 13:41 UTC · original
Olima34:feat/hle-environment-variables → main · +149 −1 · 2 files · 3 commits · merged 2026-11-26 17:05 UTC

What this changes

Both Series S tester logs (#66, #68) show the AMD FidelityFX upscaler DLL calling GetEnvironmentVariableA and SetEnvironmentVariableA through api-ms-win-core-processenvironment-l1-1-0.dll, which fell to the unimplemented-import stub returning 0. The A and W forms are now in system.rs, as Microsoft documents them: the length without the NUL, the room the value and its NUL need when the buffer is too small, ERROR_ENVVAR_NOT_FOUND for a missing variable, and a null value removes it. Names match ignoring case.

They work on the process environment the C runtime's getenv and _putenv already use, so a variable set either way is seen both ways. Windows' CRT keeps its own copy, which SetEnvironmentVariable does not update; this is noted in docs/native-host.md. Names containing = are refused, since the host environment cannot hold them (Windows' hidden =C: variables).

New test environment_variables_are_set_read_and_removed_ignoring_case, which also checks that the API set resolves the functions.

How it was tested

  • cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory (all pass, native probe tests included)
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings, also with --all-features; clippy for x86_64-apple-darwin reports nothing in the files touched (host_posix is not built for Windows)
  • arm64 cargo test --workspace and cargo xtask policy
  • The new test fails on main and passes with the change
  • Ran the game: not done, no copy of it here

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none here)
  • New diagnostics are behind a VICTORY_* variable and documented (none added)
**@⁠Olima34** on GitHub · 2026-11-26 13:41 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/75) `Olima34:feat/hle-environment-variables` → `main` · +149 −1 · 2 files · 3 commits · merged 2026-11-26 17:05 UTC ## What this changes Both Series S tester logs (#66, #68) show the AMD FidelityFX upscaler DLL calling `GetEnvironmentVariableA` and `SetEnvironmentVariableA` through `api-ms-win-core-processenvironment-l1-1-0.dll`, which fell to the unimplemented-import stub returning 0. The A and W forms are now in `system.rs`, as [Microsoft documents them](https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentvariablea): the length without the NUL, the room the value and its NUL need when the buffer is too small, `ERROR_ENVVAR_NOT_FOUND` for a missing variable, and a null value removes it. Names match ignoring case. They work on the process environment the C runtime's `getenv` and `_putenv` already use, so a variable set either way is seen both ways. Windows' CRT keeps its own copy, which `SetEnvironmentVariable` does not update; this is noted in docs/native-host.md. Names containing `=` are refused, since the host environment cannot hold them (Windows' hidden `=C:` variables). New test `environment_variables_are_set_read_and_removed_ignoring_case`, which also checks that the API set resolves the functions. ## How it was tested - [x] `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory` (all pass, native probe tests included) - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings`, also with `--all-features`; clippy for `x86_64-apple-darwin` reports nothing in the files touched (`host_posix` is not built for Windows) - [x] arm64 `cargo test --workspace` and `cargo xtask policy` - [x] The new test fails on `main` and passes with the change - [ ] Ran the game: not done, no copy of it here ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none here) - [x] New diagnostics are behind a `VICTORY_*` variable and documented (none added)
The Series S tester logs (#66, #68) show the AMD FidelityFX upscaler DLL calling GetEnvironmentVariableA and SetEnvironmentVariableA through api-ms-win-core-processenvironment-l1-1-0.dll, which fell to the unimplemented-import stub returning 0. The A and W forms now work on the process environment the C runtime's getenv and _putenv already use: names match ignoring case, a buffer too small gets the room the value and its NUL need, a missing variable is ERROR_ENVVAR_NOT_FOUND, and a null value removes the variable, as Microsoft documents.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:45:19 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 14:55 UTC · original · requested changes

Get/SetEnvironmentVariable A/W look right. One issue: set_env in system.rs calls std::env::set_var/remove_var from title threads, which races with concurrent getenv. Please guard it with a lock (or add a shortcut: comment explaining the limit).

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 14:55 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/75#pullrequestreview-6076268671) · **requested changes** Get/SetEnvironmentVariable A/W look right. One issue: set_env in system.rs calls std::env::set_var/remove_var from title threads, which races with concurrent getenv. Please guard it with a lock (or add a `shortcut:` comment explaining the limit).
Review: SetEnvironmentVariable now holds a lock across finding the existing name (ignoring case) and setting it, so two title threads setting Foo and FOO cannot make two variables. std::env already locks around each call, so the C runtime's getenv and GetEnvironmentVariable (both std::env) cannot race a set; host C code calling libc's getenv still can, which a shortcut comment now records.
Author
Owner

@⁠Olima34 on GitHub · 2026-11-26 15:01 UTC · original

Done in the latest commit. A lock alone would not stop the race you describe: std::env::set_var/var already take std's own environment lock, and both the C runtime's getenv (env_value) and GetEnvironmentVariable go through std::env. What can still race is host C code calling libc's getenv directly, which no lock of ours covers, so that limit is now a shortcut: comment. I did add a lock where it does help: set_env holds it across finding the existing name (ignoring case) and setting it, so two title threads setting Foo and FOO can no longer make two variables.

**@⁠Olima34** on GitHub · 2026-11-26 15:01 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/75#issuecomment-7195667171) Done in the latest commit. A lock alone would not stop the race you describe: `std::env::set_var`/`var` already take std's own environment lock, and both the C runtime's `getenv` (`env_value`) and `GetEnvironmentVariable` go through `std::env`. What can still race is host C code calling libc's `getenv` directly, which no lock of ours covers, so that limit is now a `shortcut:` comment. I did add a lock where it does help: `set_env` holds it across finding the existing name (ignoring case) and setting it, so two title threads setting `Foo` and `FOO` can no longer make two variables.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 16:03 UTC · original · approved these changes

Agreed that a lock would add nothing over std's own environment lock; the commit now says what still races (host C code calling libc getenv directly). CI is green. Approving.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 16:03 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/75#pullrequestreview-6077224750) · **approved these changes** Agreed that a lock would add nothing over std's own environment lock; the commit now says what still races (host C code calling libc getenv directly). CI is green. Approving.
github manually merged commit 27e4e2b3fe into main 2026-11-26 17:09:33 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.