Skip to content

fix(libkernel): connect libSceNet sockets - #18474

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
THEMANnt:fix/libkernel-connect-sce-net
Nov 12, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
THEMANnt:fix/libkernel-connect-sce-net

Conversation

@THEMANnt

Copy link
Copy Markdown
Contributor

What

Route POSIX connect for a libSceNet descriptor through the socket owner, preserving its connection bookkeeping and the kernel entry point's address validation.

Tested

Windows x64, GCC 15.2. Built and passed guest_sce_net_kernel_forwards on this branch. Connect a bound UDP socket to a loopback peer through libkernel and check that a null address returns EFAULT. #17253 changes native address validation, and #18167 touches nearby regression code.

All six socket bridge changes also passed the combined Windows socket and public header checks. Repository conventions passed for this branch. No retail title was run.

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

@JJenkx

JJenkx commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 45d177c61f, merged onto main f57c971559 (45d177c61f, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_sce_net_kernel_forwards passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@THEMANnt

THEMANnt commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Windows CI passed. Both macOS pipe-write jobs fail at link time because the shared test fixture is missing NotImplementedAt_nid_no_patch. Both proposed fixes, #17962 and #18375, passed on Intel and Apple Silicon macOS. Merging either PR is sufficient to fix the linker failure. I’ll rerun CI once the first one lands in main.

@github-actions

github-actions Bot commented Nov 11, 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 17253 core/libs/prx/libkernel/Socket/src/Udp.cpp L1676
PR 18167 core/libs/tests/GuestNetKernelForwards.cpp L76

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 5836 core/libs/prx/libSceNet/Export.cpp
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12682 core/libs/prx/libSceNet/Export.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12688 core/libs/prx/libSceNet/Export.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12700 core/libs/prx/libSceNet/Export.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12965 core/libs/prx/libSceNet/Export.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 18197 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 18515 core/libs/prx/libSceNet/Export.cpp

@gabichuuuuu

Copy link
Copy Markdown
Contributor

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

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

  • Tests built from this PR's test files: guest_sce_net_kernel_forwards.
  • All of them pass in 3 of 3 runs.
  • Negative control: with this PR's code reverted to main (files it adds removed) and rebuilt, guest_sce_net_kernel_forwards fails in 2 of 2 runs, so the tests detect the change.

AI-assisted: yes.

@MarcZX10 MarcZX10 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.

Reviewed — no blocking findings

Read the diff of all four changed files and the head code it touches; I found no concrete defect, and nothing was built or run.

What I ran

Head SHA 45d177c61f240840d3f6d091fc752059e3aeb164, base main. Read-only review through the repository reader, no build and no test run.

Reader calls
python3 repo_read.py search "connect_nid_postfix|bool Failed|Failed\(" core/libs/prx/libSceNet core/libs/tests/GuestNetKernelForwards.cpp core/libs/tests/GuestTest.hpp
python3 repo_read.py search "ForwardsFor" core/libs
python3 repo_read.py search "sceNetConnect" core/libs
python3 repo_read.py search "Forwards" core/libs
python3 repo_read.py cat core/libs/prx/libSceNet/Export.cpp 1495 1545
python3 repo_read.py cat core/libs/prx/libSceNet/Export.cpp 1088 1100
python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 285 312
python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 1668 1700
python3 repo_read.py cat core/libs/prx/libkernel/Socket/src/Udp.cpp 1822 1830
python3 repo_read.py cat core/libs/tests/GuestNetKernelForwards.cpp 1 40
python3 repo_read.py cat core/libs/tests/GuestNetKernelForwards.cpp 52 80

Results

Check Result Note
Diff of the four changed files passed Read in full as hunks
Forwards field order and the new lambda passed connect sits between bind and getsockname in both
Recursion through sceNetConnect passed libSceNet does not call connect_nid_postfix
Null address returns EFAULT (14) passed Read only: AddressLengthError returns 14 before forwarding
Build and test guest_sce_net_kernel_forwards not run No build in this run
Windows socket and header checks not run Author-reported only
Negative control

Not run. I made no change to the code under test.

Findings

Blocking

None.

Non-blocking

  • core/libs/tests/GuestNetKernelForwards.cpp:78 adds a connect and checks only its return value. A later send without an address, or a recv from another peer being dropped, would show the connection took effect. The test does not check either. Non-blocking, because the path itself reads correctly.

Sources

  • Forwarded connect path:
    int APS5_VABI connect_nid_postfix(int descriptor, const void* address, std::uint32_t length) {
    if (const int error = AddressLengthError(address, length)) return Fail(error);
    if (const auto* forwards = ForwardsFor(descriptor).forwards; forwards != nullptr) {
    if (const int error = forwards->connect(descriptor, address, length)) return Fail(error);
    return 0;
    }
  • Address length check before forwarding:
    int AddressLengthError(const void* input, std::uint32_t length) {
    if (length > 255) return 63;
    if (length < 2) return 22;
    return input ? 0 : 14;
    }
  • Export lambda:
    [](int descriptor, const void* address, std::uint32_t length) {
    return ErrorOf(sceNetBind_nid_postfix(descriptor, address, length));
    },
    [](int descriptor, const void* address, std::uint32_t length) {
    return ErrorOf(sceNetConnect(descriptor, address, length));
    },
    [](int descriptor, void* address, std::uint32_t* length) {
    return ErrorOf(sceNetGetsockname(descriptor, address, length));
    },
  • sceNetConnect body:
    int APS5_VABI sceNetConnect(int s, const void* addr, uint32_t addrlen) {
    Sock socket;
    {
    std::lock_guard lk(g_mutex);
    auto it = g_socks.find(s);
    if (it == g_socks.end()) return fail(NET_EBADF);
    #ifdef _WIN32
    if (it->second.type == NET_SOCK_STREAM && pending_connect_refused(it->second)) return fail(NET_ECONNREFUSED);
    #endif
    it->second.reported_error = 0;
    socket = it->second;
    }
    std::array<std::uint8_t, sizeof(sockaddr_storage)> address{};
    if (const int error = ReadGuestAddress(addr, addrlen, address)) return fail(error);
    if (socket.type == NET_SOCK_DGRAM && address[1] == 0) {
    if (const int error = disconnect_datagram(socket)) return error;
    return fail(NET_EAFNOSUPPORT);
    }
    sockaddr_storage native{};
    NativeLength native_length = 0;
    if (!guest_to_native_address(address.data(), addrlen, native, native_length)) return fail(NET_EINVAL);
    #ifdef _WIN32
    if (socket.type == NET_SOCK_STREAM) RefuseClosedLoopbackAtOnce_nid_no_patch(socket.native->value, reinterpret_cast<const sockaddr*>(&native));
    #endif
    if (native.ss_family != (socket.family == NET_AF_INET ? AF_INET : AF_INET6)) return fail(NET_EAFNOSUPPORT);
    if (socket.type == NET_SOCK_STREAM) {
    std::lock_guard lock(g_mutex);
    const auto current = g_socks.find(s);
    if (current != g_socks.end() && current->second.native == socket.native) current->second.connectAttempted = true;
    }
    const int connectTimeout = socket.native->connectTimeout;
    #ifndef _WIN32
    if (socket.type == NET_SOCK_STREAM && connectTimeout > 0 && !socket.native->nonblock) {
    const int error = connect_within(socket.native->value, reinterpret_cast<const sockaddr*>(&native), native_length, connectTimeout);
    if (error == CONNECT_EXPIRED) throw std::runtime_error("sceNetConnect: the connect timeout expired");
    if (error != 0) return fail(error);
    #ifdef __APPLE__
    note_local_address(s, socket);
    #endif
    return 0;
    }
    #endif
    if (::connect(socket.native->value, reinterpret_cast<const sockaddr*>(&native), native_length) == 0) {
    #ifdef _WIN32
    if (socket.type == NET_SOCK_STREAM) note_connect_pending(s, socket, false);
    #endif
    #if defined(_WIN32) || defined(__APPLE__)
    note_local_address(s, socket);
    #endif
    return 0;
    }

Not verified

  • Build and run of guest_sce_net_kernel_forwards, on Linux or Windows. The Windows run in the description is the author's report, not mine.
  • The description's claim that the connection bookkeeping is preserved. The bookkeeping sits in sceNetConnect after line 1545, and I did not read that part.
  • Wrong-length handling for a forwarded connect. bind checks the length per family before forwarding (Udp.cpp:1825-1828); the connect path does not, so the check falls to sceNetConnect and guest_to_native_address. I did not read that function.
  • The related PRs #17253 and #18167 named in the description, and the claim that no other open PR implements the same functions.
  • Full text of the four changed files was not read; I read the hunks and their callers only.

To get this to a good state

No change is required for this review. Before merge, the author's own run of guest_sce_net_kernel_forwards on the head 45d177c61f240840d3f6d091fc752059e3aeb164 should be rerun on Linux as well as Windows, and the reader's sceNetConnect tail (after line 1545) should be checked for the bookkeeping the description claims.

@THEMANnt
THEMANnt force-pushed the fix/libkernel-connect-sce-net branch from 45d177c to 0c1f72b Compare November 12, 2026 16:12
@THEMANnt

Copy link
Copy Markdown
Contributor Author

The connect regression now sends a datagram without passing a destination after the forwarded connect and checks that the peer receives the payload. I also checked the connection bookkeeping at the end of sceNetConnect.

guest_sce_net_kernel_forwards passed three consecutive runs on 0c1f72b939 on both Windows x64 (GCC 15.2) and Ubuntu 26.04.1 under WSL2 (GCC 15.2). Both were Release builds.

@oneandonlydean
oneandonlydean merged commit 3b87c74 into boykopovar:main Nov 12, 2026
14 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.

5 participants