fix(libc): share guest environment with exported environ #15095

Merged
lgqdaxia merged 3 commits from upstream-pr/15095 into main 2026-11-15 19:14:35 +00:00
lgqdaxia commented 2026-11-02 15:49:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by lgqdaxia on 2026-11-02T15:49:25Z.


What

Follow-up to Discussion #11161: bind libc's environment APIs to libkernel's exported environ, so API calls and direct table replacements see the same values.

Loading libkernel adopts the existing environment; actual unload detaches to libc-owned storage and reload binds again. Whole/null/offset table replacements, live putenv aliases, owned-string retention and allocation-failure preservation are covered. The guest environment remains separate from host HOME/PATH.

Adoption and unload are separate tests. Adoption never skips; unload may report loader retention. The Linux skip audit accepts only that exact unload test and diagnostic, not other skips or Windows retention.

Tested

Nov 12, head bd2dd42740 on main e4aa14827b: rebased and moved the three new test executables into the required per-executable CMake fragments. Registration commands and all other topic changes are unchanged. All 41 skip-audit checks, fragment parsing/BUILD_TESTING=OFF inclusion, Python syntax, whitespace and exact PR conventions pass.

Scope: same-process coherence. Console initial values, exact storage identity and duplicate/malformed tables remain recorded debt.

Earlier validation and overlap notes

On earlier head a6e86fe483, hosted checks passed Windows and the relinker matrix; full Linux and pipe-write jobs were skipped. A Linux check of that head passed five checks plus the unload-retention skip, repeated three times; unload/reload was not exercised.

Original head a535212ecd: Windows production libraries/relinker and affected targets built; 9 focused CTests and 65 public-header tests passed. Original-main Shared/Binding controls failed. Four actual-source Linux component checks passed.

Follow-up 15f38f293b: both split Windows tests passed; the real-project registration audit passed (1825 executables). Linux components passed Shared/adoption/unload (3/3); a test-only NODELETE component passed Shared/adoption and skipped unload. These component results do not claim a full Linux product build. Historical skip-policy checks passed on Windows/Linux.

BunnyFl's review of f0252c7af0 reports five Windows environment checks passing and three expected negative-control failures. Later main integrations retain the original changes and maintainer commits.

#7715's initial-environment values are a real overlap, not a prerequisite; no combined runtime result or merge order is established. Earlier adjacent declarations, registrations and debt edits are retained. Historical results keep their tested heads/platforms.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5123155694 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15095). Originally posted by [lgqdaxia](https://github.com/lgqdaxia) on 2026-11-02T15:49:25Z. --- ### What Follow-up to [Discussion #11161](https://github.com/boykopovar/AnyPS5/discussions/11161): bind libc's environment APIs to libkernel's exported `environ`, so API calls and direct table replacements see the same values. Loading libkernel adopts the existing environment; actual unload detaches to libc-owned storage and reload binds again. Whole/null/offset table replacements, live `putenv` aliases, owned-string retention and allocation-failure preservation are covered. The guest environment remains separate from host `HOME`/`PATH`. Adoption and unload are separate tests. Adoption never skips; unload may report loader retention. The Linux skip audit accepts only that exact unload test and diagnostic, not other skips or Windows retention. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> **Nov 12, head `bd2dd42740` on main `e4aa14827b`:** rebased and moved the three new test executables into the required per-executable CMake fragments. Registration commands and all other topic changes are unchanged. All 41 skip-audit checks, fragment parsing/`BUILD_TESTING=OFF` inclusion, Python syntax, whitespace and exact PR conventions pass. Scope: same-process coherence. Console initial values, exact storage identity and duplicate/malformed tables remain recorded debt. <details> <summary>Earlier validation and overlap notes</summary> On earlier head `a6e86fe483`, [hosted checks](https://github.com/boykopovar/AnyPS5/actions/runs/42482026705) passed Windows and the relinker matrix; full Linux and pipe-write jobs were skipped. A [Linux check](https://github.com/boykopovar/AnyPS5/pull/15095#issuecomment-6726614385) of that head passed five checks plus the unload-retention skip, repeated three times; unload/reload was not exercised. Original head `a535212ecd`: Windows production libraries/relinker and affected targets built; 9 focused CTests and 65 public-header tests passed. Original-main Shared/Binding controls failed. Four actual-source Linux component checks passed. Follow-up `15f38f293b`: both split Windows tests passed; the real-project registration audit passed (1825 executables). Linux components passed Shared/adoption/unload (3/3); a test-only NODELETE component passed Shared/adoption and skipped unload. These component results do not claim a full Linux product build. Historical skip-policy checks passed on Windows/Linux. [BunnyFl's review of `f0252c7af0`](https://github.com/boykopovar/AnyPS5/pull/15095#pullrequestreview-5804549019) reports five Windows environment checks passing and three expected negative-control failures. Later main integrations retain the original changes and maintainer commits. #7715's initial-environment values are a real overlap, not a prerequisite; no combined runtime result or merge order is established. Earlier adjacent declarations, registrations and debt edits are retained. Historical results keep their tested heads/platforms. </details> ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
BunnyFl commented 2026-11-02 16:03:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T16:03:41Z.

Review: APPROVED


The direction matches the console's design. In firmware 13.42, environ (+2thxYZ4syk) is a data export of libkernel.sprx (.data, 0x84278), and libSceLibcInternal.sprx (sha256 a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285) imports it as an undefined data symbol. Its getenv (smbQukfxYJM, 0x74c00) loads the table through that import on every call, with no private copy (read from disassembly only, not measured on a console):

74c11: test rdi, rdi ; je ret-null
74c30..74c42: scan the name; '=' in the name -> return NULL
74c4a: mov rax, [rip+0x13de27]   ; GOT slot: &environ
74c51: mov r14, [rax]            ; environ
74c59: mov r12, [r14]            ; walk environ[i]

So making libc read and write libkernel's exported table, as this PR does, is how the console's libc behaves.

Build and test on Windows (WinLibs GCC, BUILD_TESTING=ON), PR merged into current main:

  • guest_environment_allocation, guest_environment_binding, guest_environment_shared, guest_environment, guest_execvp and guest_system_configuration pass.
  • With main's Environment.cpp, Process.cpp and General.hpp restored (the PR's Api.hpp declaration kept so the fixtures compile), guest_environment_binding and guest_environment_shared abort. guest_environment_allocation compiles Environment.cpp itself, so it has no main control.

I didn't check the composition with #7715's ASOBI_ROOT seed that the PR body mentions.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5739454970 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15095#pullrequestreview-5739454970). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T16:03:41Z. **Review:** APPROVED --- The direction matches the console's design. In firmware 13.42, `environ` (`+2thxYZ4syk`) is a data export of `libkernel.sprx` (`.data`, `0x84278`), and `libSceLibcInternal.sprx` (sha256 `a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285`) imports it as an undefined data symbol. Its `getenv` (`smbQukfxYJM`, `0x74c00`) loads the table through that import on every call, with no private copy (read from disassembly only, not measured on a console): ``` 74c11: test rdi, rdi ; je ret-null 74c30..74c42: scan the name; '=' in the name -> return NULL 74c4a: mov rax, [rip+0x13de27] ; GOT slot: &environ 74c51: mov r14, [rax] ; environ 74c59: mov r12, [r14] ; walk environ[i] ``` So making libc read and write libkernel's exported table, as this PR does, is how the console's libc behaves. Build and test on Windows (WinLibs GCC, `BUILD_TESTING=ON`), PR merged into current `main`: - `guest_environment_allocation`, `guest_environment_binding`, `guest_environment_shared`, `guest_environment`, `guest_execvp` and `guest_system_configuration` pass. - With `main`'s `Environment.cpp`, `Process.cpp` and `General.hpp` restored (the PR's `Api.hpp` declaration kept so the fixtures compile), `guest_environment_binding` and `guest_environment_shared` abort. `guest_environment_allocation` compiles `Environment.cpp` itself, so it has no `main` control. I didn't check the composition with #7715's `ASOBI_ROOT` seed that the PR body mentions.
BunnyFl commented 2026-11-03 01:56:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-03T01:56:44Z.

Review: APPROVED


Re-review of 15f38f293b (delta over my approval at a535212ecd): the adoption checks now sit in guest_environment_binding, which has no skip code. Only guest_environment_binding_unload keeps SKIP_RETURN_CODE 77. The tools/check_skips.py exception is narrow: it applies outside the Windows branch, only to that exact test name, and only when the one skipped line is the exact string printed at core/libs/tests/GuestEnvironmentBinding.cpp:74.

Windows 11, WinLibs GCC, merged onto current main:

  • PR: guest_environment_binding, guest_environment_binding_unload, check_skips and check_skips_ctest pass. The unload test ran rather than skipping.
  • Main's General.hpp, Environment.cpp, Process.cpp and libkernel Api.hpp restored: both binding tests abort (0xc0000409).

Not run here: Linux, the only platform where the loader-retention skip and its audit exception can trigger.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5746189950 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15095#pullrequestreview-5746189950). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-03T01:56:44Z. **Review:** APPROVED --- Re-review of `15f38f293b` (delta over my approval at `a535212ecd`): the adoption checks now sit in `guest_environment_binding`, which has no skip code. Only `guest_environment_binding_unload` keeps `SKIP_RETURN_CODE 77`. The `tools/check_skips.py` exception is narrow: it applies outside the Windows branch, only to that exact test name, and only when the one skipped line is the exact string printed at `core/libs/tests/GuestEnvironmentBinding.cpp:74`. Windows 11, WinLibs GCC, merged onto current `main`: - PR: `guest_environment_binding`, `guest_environment_binding_unload`, `check_skips` and `check_skips_ctest` pass. The unload test ran rather than skipping. - Main's `General.hpp`, `Environment.cpp`, `Process.cpp` and libkernel `Api.hpp` restored: both binding tests abort (`0xc0000409`). Not run here: Linux, the only platform where the loader-retention skip and its audit exception can trigger.
BunnyFl commented 2026-11-04 20:53:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-04T20:53:36Z.

Review: APPROVED


@lgqdaxia re-checked at 1e006482e7, still approved.

git range-diff against my last approval (15f38f293b): the test commit is identical, and in the fix commit only context lines moved, from the main changes it was rebased over (a neighbouring declaration in General.hpp, and the mergesort and select debt entries main rewrote). None of the PR's own +/- lines changed.

What I ran

Windows 11 Pro 10.0.26300, WinLibs GCC 15.2, Release, this head merged cleanly onto main b611e634f7 (2efb13e049):

  • guest_environment_allocation, guest_environment_binding, guest_environment_binding_unload, guest_environment_shared and guest_system_configuration: 5 of 5 pass.
  • Negative control: the same tree with core/libs/prx/libkernel/System/src/Process.cpp from main (the fixed empty environ) fails guest_environment_binding, guest_environment_binding_unload and guest_environment_shared (0xc0000409).
  • tools/tests/test_check_skips.py: 37 tests pass (run on main 9f35c937da + #15855 + this head).

Overlap

tools/pr_overlap.py --branch on this head:

  • Line overlaps it confirmed are all adjacent edits rather than the same change: #12970 registers its test next to guest_environment_shared in core/libs/prx/libkernel/CMakeLists.txt; #12750 and #14902 edit the same unexpected = ... line of tools/check_skips.py that this PR extends; #9161 touches the same line of tools/tests/test_check_skips.py.
  • The tool could not compare 78 PRs that conflict with main. I compared the ones that change this PR's code files (Environment.cpp, Process.cpp, General.hpp, Api.hpp, GuestSystemConfiguration.cpp): #5021, #7804, #11939, #13486, #14085, #9447 and #15781 change nothing environment-related there.
  • #7715 (renzipego-cpu) is the one real overlap: it seeds the initial entries of the guest environment in core/libs/prx/libc/src/Environment.cpp, which this PR rewrites. Whichever lands second has to redo its change on the other.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5775619545 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15095#pullrequestreview-5775619545). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-04T20:53:36Z. **Review:** APPROVED --- @lgqdaxia re-checked at `1e006482e7`, still approved. `git range-diff` against my last approval (`15f38f293b`): the test commit is identical, and in the fix commit only context lines moved, from the `main` changes it was rebased over (a neighbouring declaration in `General.hpp`, and the `mergesort` and `select` debt entries `main` rewrote). None of the PR's own +/- lines changed. ## What I ran Windows 11 Pro 10.0.26300, WinLibs GCC 15.2, Release, this head merged cleanly onto `main` `b611e634f7` (`2efb13e049`): - `guest_environment_allocation`, `guest_environment_binding`, `guest_environment_binding_unload`, `guest_environment_shared` and `guest_system_configuration`: 5 of 5 pass. - Negative control: the same tree with `core/libs/prx/libkernel/System/src/Process.cpp` from `main` (the fixed empty `environ`) fails `guest_environment_binding`, `guest_environment_binding_unload` and `guest_environment_shared` (`0xc0000409`). - `tools/tests/test_check_skips.py`: 37 tests pass (run on `main` `9f35c937da` + #15855 + this head). ## Overlap `tools/pr_overlap.py --branch` on this head: - Line overlaps it confirmed are all adjacent edits rather than the same change: #12970 registers its test next to `guest_environment_shared` in `core/libs/prx/libkernel/CMakeLists.txt`; #12750 and #14902 edit the same `unexpected = ...` line of `tools/check_skips.py` that this PR extends; #9161 touches the same line of `tools/tests/test_check_skips.py`. - The tool could not compare 78 PRs that conflict with `main`. I compared the ones that change this PR's code files (`Environment.cpp`, `Process.cpp`, `General.hpp`, `Api.hpp`, `GuestSystemConfiguration.cpp`): #5021, #7804, #11939, #13486, #14085, #9447 and #15781 change nothing environment-related there. - **#7715** (renzipego-cpu) is the one real overlap: it seeds the initial `entries` of the guest environment in `core/libs/prx/libc/src/Environment.cpp`, which this PR rewrites. Whichever lands second has to redo its change on the other.
BunnyFl commented 2026-11-06 17:49:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-06T17:49:27Z.

Review: APPROVED


@lgqdaxia re-checked at f0252c7af0. The approval stands.

git range-diff against my last approval (1e006482e7): the test commit is identical. In the fix commit only a context line moved: main rewrote the neighbouring __progname entry in docs/dev/debt/libkernel.md, which is the line next to the environ entry this PR removes. None of the PR's own +/- lines changed.

What I ran

Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0, Release, this head merged onto current main (merge 8b59608b17, a clean auto-merge of docs/dev/debt/libkernel.md):

  • guest_environment_allocation, guest_environment_binding, guest_environment_binding_unload, guest_environment_shared and guest_system_configuration: 5 of 5 pass.
  • Negative control: the same tree with main's core/libs/prx/libkernel/System/src/Process.cpp fails guest_environment_binding, guest_environment_binding_unload and guest_environment_shared (0xc0000409); 2 of 5 pass.
  • tools/check_conventions.py --base upstream/main --head on this head: all checks pass.

Overlap

tools/pr_overlap.py --branch on this head gives the same picture as at 1e006482e7. The confirmed line overlaps are the adjacent tools/check_skips.py and tools/tests/test_check_skips.py edits of #12750, #14902 and #9161. #7715 still rewrites the guest environment's initial entries in core/libs/prx/libc/src/Environment.cpp and remains the one real overlap: whichever lands second has to redo its change on the other.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5804549019 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15095#pullrequestreview-5804549019). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-06T17:49:27Z. **Review:** APPROVED --- @lgqdaxia re-checked at `f0252c7af0`. The approval stands. `git range-diff` against my last approval (`1e006482e7`): the test commit is identical. In the fix commit only a context line moved: `main` rewrote the neighbouring `__progname` entry in `docs/dev/debt/libkernel.md`, which is the line next to the `environ` entry this PR removes. None of the PR's own +/- lines changed. ## What I ran Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0, Release, this head merged onto current `main` (merge `8b59608b17`, a clean auto-merge of `docs/dev/debt/libkernel.md`): - `guest_environment_allocation`, `guest_environment_binding`, `guest_environment_binding_unload`, `guest_environment_shared` and `guest_system_configuration`: 5 of 5 pass. - Negative control: the same tree with `main`'s `core/libs/prx/libkernel/System/src/Process.cpp` fails `guest_environment_binding`, `guest_environment_binding_unload` and `guest_environment_shared` (`0xc0000409`); 2 of 5 pass. - `tools/check_conventions.py --base upstream/main --head` on this head: all checks pass. ## Overlap `tools/pr_overlap.py --branch` on this head gives the same picture as at `1e006482e7`. The confirmed line overlaps are the adjacent `tools/check_skips.py` and `tools/tests/test_check_skips.py` edits of #12750, #14902 and #9161. #7715 still rewrites the guest environment's initial entries in `core/libs/prx/libc/src/Environment.cpp` and remains the one real overlap: whichever lands second has to redo its change on the other. AI-assisted: yes (Claude Code)
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
boykopovar/AnyPS5!15095
No description provided.