Skip to content

feat(libcurl): bridge curl-owned sockets to guest descriptors - #17603

Merged
oneandonlydean merged 9 commits into
boykopovar:mainfrom
MarrasFederico:feat/libcurl-guest-sockets
Nov 15, 2026
Merged

oneandonlydean merged 9 commits into
boykopovar:mainfrom
MarrasFederico:feat/libcurl-guest-sockets

Conversation

@MarrasFederico

@MarrasFederico MarrasFederico commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What

Expose curl connections as real guest descriptors instead of passing host socket numbers across the guest ABI. The bridge installs duplicate native-handle leases in the existing kernel socket table, checks owner identity before retiring a lease, and follows curl's close and resolver-removal notifications. It retains the close callback and DATA snapshot independently of the easy-handle adapter, including pooled connections that outlive the originating easy handle.

Implements multi socket/action/all/assign/fdset, extra socket descriptors in multi wait/poll, open/sockopt/close callback and DATA options, multi socket callback/DATA, and ACTIVESOCKET/LASTSOCKET. POSIX pipe/file extras are leased too. Socket addresses are translated to BSD guest layouts; guest mode tracking follows the pinned backend's nonblocking transition without changing the submodule or its original implementation.

Sources: curl 801bd5138ce31aa0d906fa4e2eabfc599d74e793 socket creation and close, sockopt/connect ordering, fdset/wait, resolver removal, and socket getinfo. Raw source downloads matched the pinned checkout byte for byte.

Remaining gaps are explicit in debt: console ABI/error verification, virtual extra descriptors, non-socket extras on Windows, and lease-failure mapping. Forms, MIME callback duplication and HTTP/2 push ownership still throw, with specific closing conditions. The 12 stub-only export rows match the source.

Tested

Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic, GCC 15.2.0, Release, BUILD_TESTING=ON, CMake 4.4.4 with Ninja 1.13 from a virtualenv. Merged main (1965e614cb) via normal merge commit 24b1f05812, then added b6b02f8a4c (test(libcurl): hold loopback server response until wait loop starts, full b6b02f8a4c4fed86e9ce851e015bca8d511d546d) to fix the Windows loopback race in core/libs/prx/libcurl/tests/CurlSockets.cpp; the published history was not rebased, squashed or force-pushed.

  • Built target(s): the 11 guest_curl* test executables and the libcurl library target.
  • ctest -R '^guest_curl' --output-on-failure: 100% tests passed out of 11 (exit 0).
  • Negative control for the new bounds assertions: removing the descriptor < 0 || descriptor >= 1024 guard from Add makes guest_curl_sockets fail at core/libs/prx/libcurl/tests/CurlSockets.cpp:445; restoring the guard makes the test pass again.
  • Merge resolution (core/libs/prx/libcurl/CMakeLists.txt and docs/dev/debt/libcurl.md):
    • CMakeLists.txt: Preserved main's src/FormFree.cpp and MimeCallback test topic while cleanly adding the PR's src/CurlSockets.cpp, WIN32 FD_SETSIZE=1024, and the Sockets test topic with WIN32 ws2_32 linkage. Verified that all .cpp files under core/libs/prx/libcurl/src/ match CMakeLists.txt exactly without duplicate or missing entries.
    • docs/dev/debt/libcurl.md: Preserved main's text (including curl_formfree and MIME callbacks entries under Functional), keeping the PR's updated Socket ownership and Push ownership entries (all <= 1000 characters). Unimplemented exports list was updated to remove the 4 exports implemented by this PR (curl_multi_assign, curl_multi_fdset, curl_multi_socket, curl_multi_socket_action), leaving 12 stub entries that match Export.cpp 1:1.
  • Nit 1 (descriptor bound check in Add):
    • Implemented in core/libs/prx/libcurl/src/CurlSockets.cpp line 323: Add checks descriptor >= 0 && descriptor < 1024, returning false / no insertion when out of bounds, and exposed Add_nid_no_patch in CurlSockets.hpp. In project, highest is only updated when Add returns true.
    • Extended guest_curl_sockets (core/libs/prx/libcurl/tests/CurlSockets.cpp) in ErrorsAndExtraDescriptors() to verify that negative descriptors (-1), boundary descriptors (1024), and large descriptors (99999) return false without modifying guest_fd_set bits, and valid descriptors (42) set the bit properly. Also updated test helper IsSet with bound checks.
  • Nit 2 (ACTIVESOCKET):
    • Investigated curl 7.81 source (3rdparty/curl tag curl-7_81_0, commit 801bd5138ce31aa0d906fa4e2eabfc599d74e793). In lib/getinfo.c:540 and lib/connect.c:1468, CURLINFO_ACTIVESOCKET returns Curl_getconnectinfo(...), which returns CURL_SOCKET_BAD only if curl's connection cache contains no active connection for the handle (!find.found). Upstream curl has no concept of AnyPS5 guest registrations or guest-closed descriptors. curl_multi_socket_action (lib/multi.c:3044) returns CURLM_OK for unmatched sockets, not CURL_SOCKET_BAD. Without proof in the pinned upstream source that curl returns CURL_SOCKET_BAD for unregistered guest sockets, the existing throw (NotImplemented_nid_no_patch("libcurl: unregistered active socket")) was preserved per project rules.
  • Windows and Linux verification (b6b02f8a4c):
    • Windows: guest_curl_sockets passed 30/30 and ^guest_curl passed 11/11.
    • Linux: ^guest_curl passed 11/11.
  • Conventions and checks:
    • python3 tools/check_conventions.py --base upstream/main: All conventions checks passed (0 errors, 0 warnings).
    • python3 core/libs/tests/test_public_headers.py --audit-only: 413 libraries, 34494 exports: unique declarations and umbrella coverage verified.
    • git diff --check upstream/main...HEAD: completely clean (exit code 0).

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

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Progress report (667dbde → b6b02f8)

📈 System libraries: 40.05% (+0.02%, +7 implemented functions)

✅ 7 implemented
Library Name
libcurl curl_easy_getinfo_nid_postfix
libcurl curl_easy_setopt_nid_postfix
libcurl curl_multi_assign_nid_postfix
libcurl curl_multi_poll_nid_postfix
libcurl curl_multi_socket_action_nid_postfix
libcurl curl_multi_socket_nid_postfix
libcurl curl_multi_wait_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libcurl 59/85 (69.41%) 66/85 (77.65%) +8.24%

@JJenkx

JJenkx commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Windows check of head 60986f7f3a, merged onto main 7854b95c17 (fast-forward).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release, Vulkan hidden for this CPU-only run. The linux check is skipped on this PR.

  • The 236 tests registered in libkernel's and libcurl's CMake files that build on Windows, guest_curl and guest_curl_strings among them, pass in 3 of 3 runs.
  • The new guest_curl_Sockets_tests does not link on Windows:
CurlSockets.cpp.obj: undefined reference to `CurlSocketNonblock_nid_no_patch'   (x3)
CurlSockets.cpp.obj: undefined reference to `__imp_closesocket' / `__imp_bind' / `__imp_accept' / `__imp_getsockname' / `__imp_getsockopt' / `__imp_htonl' / `__imp_htons'

Two things seem to be missing on Windows:

  • ws2_32 for the test target (target_link_libraries(guest_curl_Sockets_tests PRIVATE ws2_32) under if(WIN32)), since the test calls Winsock directly.
  • An export for CurlSocketNonblock_nid_no_patch. It's defined in src/CurlSockets.cpp inside libcurl.prx, but on Windows a test that links the DLL's import library only sees exported symbols. Either export it or compile the source into the test.

I haven't tried either change, so I couldn't run guest_curl_sockets or the other curl topic tests in this PR.

AI-assisted: yes.

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 17390 core/libs/prx/libcurl/CMakeLists.txt L87, L121
core/libs/prx/libcurl/Export.cpp L1

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 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12682 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12688 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12700 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12965 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 14970 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 17253 core/libs/prx/libkernel/Socket/src/Udp.cpp

@DonRyuzaki

Copy link
Copy Markdown
Contributor

I followed up on @JJenkx's Windows linker finding with an independent local build and runtime probe of exact head 60986f7f3ae7c8ce2bad74d6af3bd30deec6b0ac, base 7854b95c1761a8677014e73097dc69480001c542, clean source. MinGW GCC 15.2 Release build of guest_curl_Sockets_tests fails on unresolved Winsock imports. A diagnostic-only local ws2_32 link for this test target makes it link; I did not observe the separately reported CurlSocketNonblock_nid_no_patch undefined symbol in this build.

With that local link change, the registered guest_curl_sockets CTest reaches a further runtime failure at tests/CurlSockets.cpp:257: the callback-enabled SocketTransfer(true) completes, then the callback-free SocketTransfer(false) returns curl result 7 (CURLE_COULDNT_CONNECT) at Done(). I confirmed the path order and result using temporary test-only logging. A separate diagnostic run that changed the callback-free URL from localhost to numeric 127.0.0.1 still failed at the same completion assertion, so simply choosing IPv4 did not clear it. These diagnostic edits are saved in my local logs and were restored; no fix is pushed.

The author's Linux socket test passes, but this Windows runtime path remains unverified on the unchanged PR because the test does not link there. I did not run a title, external network transfer, or the full suite. The verbose instrumentation also changed timing, so the uninstrumented diagnostic run is the primary runtime result. AI-assisted: yes (Codex).

…e-write fixture

Forward the static host backend nonblocking hook locally in the Windows Transfers test, where the DLL does not export it.
Link the Windows Sockets test with ws2_32 for its direct Winsock calls.
Read socket family and type through SO_PROTOCOL_INFOW because Windows getsockname rejects unbound sockets.
Update the pipe-write fixture to provide NotImplementedAt, matching the macro introduced on main on November 1.
@MarrasFederico

Copy link
Copy Markdown
Contributor Author

Thanks @JJenkx and @DonRyuzaki for the independent Windows builds and runtime probes. I pushed f6d0ddb8ac6b95bc58590d1bd24bb8917c87553c with these four fixes:

  1. Transfers test linkage: its standalone host curl copy links the static backend, whose nonblocking hook was unresolved through the Windows DLL import library. A Windows-only forwarding definition in the test calls the archive's original CurlHostNonblock_nid_no_patch; Linux keeps the bridge implementation to avoid ELF interposition.
  2. Sockets test linkage: the test calls Winsock directly, so its Windows target now explicitly links ws2_32.
  3. Unbound Windows sockets: Borrow previously queried getsockname, which fails with WSAEINVAL before bind/connect. It now reads family and type from SO_PROTOCOL_INFOW on Windows; the POSIX path is unchanged.
  4. macOS pipe-write fixture: main has used NotImplementedAt_nid_no_patch through the macro introduced on November 1, but this fixture still defined the old symbol. The fixture now defines the matching symbol; this pre-existing main failure is included here because the macOS pipe-write job runs for PRs touching Udp.cpp.

Linux verification: configuration and the prescribed build succeeded; all 9 guest_curl* CTests passed, as did conventions and whitespace checks. Only the four specified files changed.

CI for this head:

  • Windows: passed. The log reports 0 failures out of 3778 tests; all nine curl tests, including guest_curl_transfers and guest_curl_sockets, passed.
  • pipe-write (macos-15) and pipe-write (macos-15-intel): both passed, including CTest repeated three times.
  • Conventions and library-portability checks also passed.

I could not execute anything on Windows on this laptop, so the Windows verification above is from CI only. Could you please rerun guest_curl_sockets on your Windows setups?

AI-assisted: yes

@ThorstenFR-Cyber

Copy link
Copy Markdown

Windows check of head f6d0ddb, tested as the PR head (not merged onto main).

Setup: Windows 11 (build 26340), WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Release, BUILD_TESTING=ON.

guest_curl_Sockets_tests and guest_curl_Transfers_tests now build and link.
ctest -R guest_curl --repeat until-fail:3: all 9 guest_curl* tests pass in 3 of 3 runs, including guest_curl_sockets and guest_curl_transfers.
AI-assisted: yes.

@gabichuuuuu

Copy link
Copy Markdown
Contributor

Windows check of head f6d0ddb8ac, merged onto main e37e9fa3f9 (clean merge, built together with other open PRs that touch different files).

Setup: Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 (r7), Ninja, Release, BUILD_TESTING=ON, Python 3.15.

  • Tests chosen with tools/select_tests.py explain from this PR's test files: guest_curl_sockets, guest_curl_transfers.
  • The new guest_curl_sockets test fails 3 of 3 with socket test failed at core/libs/prx/libcurl/tests/CurlSockets.cpp:450 (exit 0xc0000409): curl_multi_wait on a socketpair descriptor with one byte pending does not report it ready (CURLM_OK && ready == 1 && (revents & CURL_WAIT_POLLIN)).
  • The other selected test (guest_curl_transfers) passes in 3 of 3 runs.
  • No negative control was run in this pass, so this does not show that the tests fail without the change.

AI-assisted: yes.

@MarrasFederico

Copy link
Copy Markdown
Contributor Author

Thanks @ThorstenFR-Cyber and @gabichuuuuu for the additional Windows runs. The socketpair readiness failure is a separate remaining finding from the earlier linker and unbound-socket fixes: one run passes the exact head, while the other fails after merging it with main e37e9fa. I’ll investigate the extra guest descriptor → native socket → revents path and retain the readiness assertion. Could you share the full failing log and the heads of the other PRs included in that merged tree, if available? I’m not treating the green head-only/CI runs as resolving this report.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@MarrasFederico I reviewed f6d0ddb. The socket lifetime, owner checks and lock order look right to me. Before this can merge, the Windows failure gabichuuuuu reported needs fixing: with the head merged onto current main, guest_curl_sockets fails at tests/CurlSockets.cpp:450 (curl_multi_wait on a socketpair with one byte pending, exit 0xc0000409). Could you merge current main, fix that path, and post a Windows run of guest_curl_sockets on the new head? Two optional nits while you're there: a bound check on the descriptor in Add (CurlSockets.cpp:323), and returning -1 from ACTIVESOCKET instead of throwing when the guest has already closed its descriptor (CurlSockets.cpp:490).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 12, 2026
@MarrasFederico

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed 7df5a03: main is merged, Add rejects descriptors outside [0, 1024), and fdset updates its highest descriptor only after successful insertion. The new boundary tests and the recorded Linux curl suite pass (10/10 tests, three runs each); removing the guard fails the negative control.

The earlier green Windows run tested f6d0ddb, not this new head, so it does not resolve the reported socketpair failure. Could you share the failing tree SHA and log, or rerun this head? I have corrected the description to make that distinction explicit. The optional ACTIVESOCKET change remains unimplemented; its existing unsupported-path throw is preserved.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@MarrasFederico I reviewed 7df5a03. The design holds up: the bridged descriptors go into libkernel's own socket table, so they dispatch through the normal path and do not overlap the libSceNet descriptors, the ownership checks show no double close or reuse race, and no lock is held across a blocking call or a guest callback. Two things stop it from passing.

  1. guest_curl_sockets still fails on Windows on this exact head. It exits with 0xc0000409 at tests/CurlSockets.cpp:301, in the callback-free transfer, after the transfer has finished, and it is the only failing test of about 4180. This is the earlier ask and it is not addressed yet. One thing to check: that transfer uses localhost, which on Windows can resolve to ::1 while the test server listens on IPv4 only; connecting to 127.0.0.1 would rule that out.
  2. Both macOS pipe-write jobs fail with "redefinition of NotImplementedAt_nid_no_patch" at core/libs/tests/PipeWrite.cpp:19. Main now defines that symbol itself, so the PipeWrite.cpp hunk in this PR duplicates it. Please drop that hunk.

…sockets

# Conflicts:
#	core/libs/prx/libkernel/Socket/src/Udp.cpp
Drop the PipeWrite.cpp hunk now that main defines NotImplementedAt_nid_no_patch itself, and split the post-transfer assertion so a failure names the unmet condition.
@MarrasFederico

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Pushed 5274251, after merging current main (85c79e7221; the only conflict was getpeername in Udp.cpp, where main's sceNet forwarding is kept and bridged descriptors skip the expired-peer check):

  1. Windows abort at CurlSockets.cpp:301: I followed your suggestion and the callback-free transfer now connects to 127.0.0.1 instead of localhost. I can't reproduce the failure on Linux, so I haven't confirmed the cause is ::1. I also split the post-transfer assertion into separate checks for body, socket notifications and timer callbacks. If it still fails on Windows, the line number will show which condition is unmet. A Windows rerun of guest_curl_sockets on this head would settle it.
  2. macOS pipe-write: dropped the PipeWrite.cpp hunk; the file is now identical to main. The bridge functions this PR adds to Udp.cpp are defined in that file, so the fixture needs no new symbols. I can't build the macOS job locally.

Linux: ctest -R ^guest_curl --repeat until-fail:3 passes 10/10.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 13, 2026
@MarrasFederico

Copy link
Copy Markdown
Contributor Author

Updated b6b02f8a4c to fix the Windows CI race in core/libs/prx/libcurl/tests/CurlSockets.cpp.

  • Cause: on Windows loopback, the transfer could finish inside the first curl_multi_socket_action call (running == 0). Because no active socket remained when multi_socket ran singlesocket(), the socket callback was never invoked (state.notifications == 0 at line 302) and the wait loop was skipped.
  • Fix: Server in core/libs/prx/libcurl/tests/CurlSockets.cpp now supports holding the HTTP response until ReleaseResponse() is called. SocketTransfer releases it after the initial curl_multi_socket_action call, and FdsetTransfer releases it after verifying the descriptor sets and before select.
  • Verification: Windows guest_curl_sockets passed 30/30 and ^guest_curl passed 11/11; Linux ^guest_curl passed 11/11.

@ThorstenFR-Cyber

Copy link
Copy Markdown

Windows check of head b6b02f8a4c, tested as the PR head (not merged onto current main).

Setup: Windows 11 (build 26340), WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Ninja, Release, BUILD_TESTING=ON.

  • ctest -R '^guest_curl_sockets$' --repeat until-fail:30: 30 / 30 pass.
  • ctest -R '^guest_curl' --repeat until-fail:3: all 11 guest_curl* tests pass in 3 of 3 runs, including guest_curl_sockets, guest_curl_transfers, guest_curl_allocators and guest_curl_mimecallback.

AI-assisted: yes.

@MarrasFederico

Copy link
Copy Markdown
Contributor Author

Thanks @ThorstenFR-Cyber for checking b6b02f8a4c on Windows.

@oneandonlydean
oneandonlydean merged commit 06d1766 into boykopovar:main Nov 15, 2026
14 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants