hle: Get and SetEnvironmentVariable over the process environment #75

Merged
Olima34 merged 3 commits from upstream-pr/75 into main 2026-11-26 17:05:02 +00:00
Olima34 commented 2026-11-26 13:41:22 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-26T13:41:22Z.


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)
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6627920057 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/75). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-26T13:41:22Z. --- ## 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)
LakshanSivakumar commented 2026-11-26 14:55:56 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T14:55:56Z.

Review: CHANGES_REQUESTED


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).

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6076268671 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/75#pullrequestreview-6076268671). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T14:55:56Z. **Review:** CHANGES_REQUESTED --- 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).
Olima34 commented 2026-11-26 15:01:20 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-26T15:01:20Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7195667171 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/75#issuecomment-7195667171). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-26T15:01:20Z. --- 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.
LakshanSivakumar commented 2026-11-26 16:03:55 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T16:03:55Z.

Review: APPROVED


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6077224750 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/75#pullrequestreview-6077224750). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T16:03:55Z. **Review:** APPROVED --- 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!75
No description provided.