Skip to content

fix(libSceHttp): zero SceHttpUriElement padding written to the guest - #22121

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
domx4q:fix/http-uri-element-padding
Nov 17, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
domx4q:fix/http-uri-element-padding

Conversation

@domx4q

@domx4q domx4q commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

SceHttpUriElement (80 bytes) has four bytes of implicit padding after opaque (offset 4) and four after reserved (offset 76). sceHttpUriParse writes the guest element with *out = {} followed by member stores. The assignment is a trivial copy, which GCC compiles member by member, so the padding bytes the guest already had stay in the struct it gets back. GCC 15 and 16 leave them there with -fzero-init-padding-bits=all too: in a standalone repro with the same struct shape, a destination pre-filled with a5 still has a5 at byte 76 after *out = {}.

The fix names the padding as pad0 and pad1 with default initialisers, so every element written to the guest is fully defined regardless of compiler. The layout is unchanged (size 80, scheme at 8, port at 64), and a static_assert checks it. The test also asserts std::has_unique_object_representations_v<SceHttpUriElement>, which is false whenever the struct has implicit padding, so guest_http_tests does not compile with the fix reverted on any compiler. A runtime check pre-fills the sceHttpUriParse output with 0xa5 and requires bytes 4..7 and 76..79 to be zero.

Tested

Arch Linux (kernel 7.3, glibc 2.44), GCC 16, Release.

  • ctest --test-dir build -R '^guest_http(_uri_locale)?$': 2/2 passed.
  • ctest --test-dir build -R 'guest_http': 103/103 passed.
  • Negative control with only core/libs/SceTypes.hpp reverted to main: guest_http_tests fails to compile (GuestHttp.cpp:30: static assertion failed, the has_unique_object_representations_v assert). With that assert also removed, guest_http aborts (Subprocess aborted) on the new sceHttpUriParse padding check. With the fix restored both pass.
  • The earlier sceHttpUriCopy padding checks pass with the fix reverted, because sceHttpUriCopy clears the output with memset. The original GCC 16 abort on those checks no longer reproduces on current main. Those checks are kept as they describe the required output.
  • GCC 13 was not available here. The static_assert depends only on the struct layout, which is the same on every x86-64 compiler, so it fails there too. Whether the runtime check fails on GCC 13 depends on its codegen for *out = {}.
  • python3 tools/check_conventions.py --base origin/main: passed. tools/progress.py output is identical before and after the test commit. No debt entry is affected.

Overlap

python3 tools/pr_overlap.py --branch fix/http-uri-element-padding: 6 PRs share files, 4 could not be compared automatically because they conflict with the base. Checked by hand:

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 (Claude Code)

SceHttpUriElement has four bytes of padding after opaque and four after
reserved. sceHttpUriCopy, sceHttpUriParse and sceHttp2 build the element
with aggregate initialisation and copy the whole struct to the guest, so
the padding carried host stack bytes. GCC 15 and newer no longer clear
padding for `{}` initialisation, which made guest_http abort on the
check that sceHttpUriCopy zeroes bytes 4..7 of the output element.

Name the padding as pad0 and pad1 with default initialisers so every
element written to the guest is fully defined, and assert the layout.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 18082b32f1:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_http_tests built cleanly.
  • CTest (guest_http, guest_http_uri_locale):
    1/2 Test #3706: guest_http_uri_locale ............   Passed    0.01 sec
    2/2 Test #3704: guest_http .......................   Passed    0.33 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: the tests still pass with the change reverted, so they do not cover it

Requesting changes for the points above.

@github-actions

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 8649 core/libs/SceTypes.hpp
PR 9834 core/libs/tests/GuestHttp.cpp
PR 11134 core/libs/tests/GuestHttp.cpp

…piler

The previous checks only looked at sceHttpUriCopy, which already clears
the output with memset, so they passed with the fix reverted. Assert
std::has_unique_object_representations_v<SceHttpUriElement>, which is
false whenever the struct has implicit padding and therefore fails to
compile on every compiler without pad0 and pad1. Also pre-fill the
sceHttpUriParse output with 0xa5 and require bytes 4..7 and 76..79 to
be zero, since `*out = {}` copies members only and leaves the guest's
old padding bytes in place.
@domx4q

domx4q commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

You're right: the old checks only looked at sceHttpUriCopy, which already clears its output with memset, so they never covered the fix.

de8c8c3 adds two checks:

  • static_assert(std::has_unique_object_representations_v<SceHttpUriElement>). The trait is false whenever the struct has implicit padding, so with pad0/pad1 removed the test does not compile on any compiler. It depends only on the layout, not on how a compiler initialises {}.
  • A runtime check that pre-fills the sceHttpUriParse output with 0xa5 and requires bytes 4..7 and 76..79 to be zero afterwards. In the old code, *out = {} is a trivial copy that GCC does member by member, so the padding the guest already had comes back unchanged.

Arch Linux, GCC 16, Release:

$ ctest --test-dir build -R '^guest_http(_uri_locale)?$'
100% tests passed out of 2
$ ctest --test-dir build -R 'guest_http'
100% tests passed out of 103

Negative control, with only core/libs/SceTypes.hpp reverted to main:

$ cmake --build build --target guest_http_tests
core/libs/tests/GuestHttp.cpp:30:20: error: static assertion failed

With that static_assert also removed temporarily:

$ ctest --test-dir build -R '^guest_http$'
3704 - guest_http (Subprocess aborted)

Both pass again once the fix is back.

I have no GCC 13 here. The static_assert should fail there as well, because the padding is part of the x86-64 layout and not a codegen choice. The runtime check depends on codegen. In a standalone repro, GCC 15 and 16 leave the guest's byte at offset 76 even with -fzero-init-padding-bits=all, so it probably fails on GCC 13 too, but I could not confirm that.

I also updated the PR body: the earlier "Tested" text said guest_http aborted on the sceHttpUriCopy check, and that no longer reproduces on current main.

AI-assisted: yes (Claude Code)

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against de8c8c3858:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_http_tests built cleanly.
  • CTest (guest_http, guest_http_uri_locale):
    1/2 Test #3724: guest_http_uri_locale ............   Passed    0.01 sec
    2/2 Test #3722: guest_http .......................   Passed    0.31 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/SceTypes.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Checked struct SceHttpUriElement layout in core/libs/SceTypes.hpp: size remains exactly 80 bytes, with pad0 (uint32_t) at offset 4 filling the 4-byte gap before 64-bit pointers (scheme at offset 8) and pad1 (uint32_t) at offset 76 filling the 4-byte gap after reserved[10] (offset 66..76) up to 80-byte alignment.
    • Verified std::has_unique_object_representations_v in core/libs/tests/GuestHttp.cpp confirms there are zero implicit compiler padding bytes in SceHttpUriElement across LP64 and LLP64 platforms.
    • Audited sceHttpUriCopy and sceHttpUriParse guest-memory copying behavior: explicit initialization of pad0 = 0 and pad1 = 0 guarantees zero initialization of all struct bytes across modern GCC/Clang compilers regardless of aggregate init padding semantics.
    • Checked test coverage in core/libs/tests/GuestHttp.cpp: asserts static layout requirements, tests that sceHttpUriCopy and sceHttpUriParse zero out both pad0 (+4) and pad1 (+76) when the destination struct is pre-filled with garbage bytes (0x5a and 0xa5), satisfying the negative control requirement.
    • Confirmed AGENTS.md compliance: no unauthorized comments, no forbidden files, conventions passed, and no fake or unimplemented stubs introduced.

Looks Good To Me

@domx4q
domx4q force-pushed the fix/http-uri-element-padding branch from de8c8c3 to 9e0d5e7 Compare November 17, 2026 09:15
@oneandonlydean
oneandonlydean merged commit 9072b83 into boykopovar:main Nov 17, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants