ws2_offline: refuse the local computer's name, answer localhost with loopback literals, keep WSAIoctl synchronous (RT-WS2-OFFLINE-LOOKUP-LEAKS) #2533
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2533"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-28T01:10:56Z.
Claimed in the coordination issue as: RT-WS2-OFFLINE-LOOKUP-LEAKS (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231475417)
What and why
Item 3 of @kevincommits' batch 18 in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231277215). Thanks for the line-by-line report.
runtime/shims/ws2_offline.cppis what keeps a title off the network. Three gaps:is_loopback_node_a/wreturned true for a null and an empty name. Sogetaddrinfo,GetAddrInfoW,GetAddrInfoExA/W,gethostbynameandWSAAsyncGetHostByNamepassed""(and, for the last two,NULL) to the host. The host answers those with every address of the local computer: its LAN and external adapters.WSAConnectByNameA/Walready refused them.gethostbyname/WSAAsyncGetHostByNamealso refuse a null one (WSAHOST_NOT_FOUND).getaddrinfonode is still answered: it is a service-only lookup that gives loopback, or withAI_PASSIVEthe wildcard, which the shim'sbindnarrows to loopback.localhostand the hosts file.WSAConnectByNameA/Whand the host the literal127.0.0.1forlocalhost, so the hosts file is never read. The six lookup shims passed the name itself, so a hosts entry could map it off loopback.::1for anAF_INET6lookup, else127.0.0.1.gethostbynameandWSAAsyncGetHostByNameare IPv4 only, so they get127.0.0.1.AF_UNSPEClookup oflocalhosttherefore answers127.0.0.1only, where Windows also lists::1.WSAIoctl(SIO_GET_EXTENSION_FUNCTION_POINTER). ForWSAID_CONNECTEXandWSAID_WSASENDMSG, the shim passed the caller's overlapped and completion routine to the host, together with&real_extand&returnedin its own stack frame. If the host completed later, it would write into a frame that was gone.wrap_extension_lookup). The block is freed when the call completes and kept if the host reports it pending.WSA_FLAG_OVERLAPPED) is reported as Winsock reports it.AI_NUMERICHOST,localhostis passed on unchanged, so the host refuses it (EAI_NONAME).The header comment and
docs/runtime_imports.mddescribe the new rules.Overlap: #2241 (
connectand UDP) and #2242 (WSAConnectByNametolocalhoston IPv6 sockets) change other functions of this file and test. Whichever lands second may need a small rebase of the test file, which all three append to. With #2242,WSAConnectByNameand these lookups pick::1the same way: by the socket's or the lookup's family.Tested
Windows 11, MSVC (VS 18), RelWithDebInfo, on
main431ceba4f. Loopback only, no network.ws2_offline_testgains section 9:gethostbyname(NULL)andgethostbyname(""),WSAAsyncGetHostByNamewith a null or empty name,getaddrinfo("")andGetAddrInfoW(L"")are refused;getaddrinfo(NULL, "80")is still answered;getaddrinfo("localhost", AF_INET)answers127.0.0.1only,getaddrinfo("localhost.", AF_INET6)::1only, andgethostbyname("LOCALHOST")loopback only;WSAIoctlfor ConnectEx returns the same wrapped pointer as a plain one and sets its event;WSA_FLAG_OVERLAPPED, the overlapped ends as the host's own call leaves it;getaddrinfo("localhost", AI_NUMERICHOST)isEAI_NONAME;main's shim the test fails atgethostbyname(NULL)(the host answered with this computer's addresses) and then crashes later in section 9. With the fix, 87/87 checks pass (the review's three cases fail with the first version of this PR).scripts/check_no_game_data.py: clean.Not tested: a hosts file that maps
localhostelsewhere (the test does not edit the system's hosts file), and Wine.GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU tests only)
AI-assisted: yes (Claude Code)
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T02:01:37Z.
Reviewed at head
f589998bfon Windows 11 (MSVC 202219.44).Verification
ws2_offline_test.ws2_offline_test: 83/83 checks passed.Findings (Request changes)
complete_nowdrops IOCP completions for overlapped sockets (ws2_offline.cpp:851-874,903-931):Both wrapped
WSAIoctlGUID handlers passnullptr, nullptrforoverlapped/completiontoreal_fnand then callcomplete_now(). For an overlapped socket associated with an I/O completion port where the caller provides anOVERLAPPEDwithout an event or APC routine (and withoutFILE_SKIP_COMPLETION_PORT_ON_SUCCESS),complete_now()only fillsInternal/InternalHighand posts no IOCP packet, leaving a caller waiting onGetQueuedCompletionStatushung indefinitely. (The new tests inws2_offline_test.cpp:601-635exercise event and APC only.)ws2_offline.cpp:851-874,903-931):complete_now()unconditionally writes*overlappedand signalshEventor queues an APC wheneveroverlapped != nullptr, without checking whetherswas created withWSA_FLAG_OVERLAPPED. Per the WinsockWSAIoctlcontract, bothlpOverlappedandlpCompletionRoutineare ignored on non-overlapped sockets.ws2_offline.cpp:264-272,612-668,858-870):"localhost"does not checkAI_NUMERICHOSTfirst, sogetaddrinfo("localhost", ..., AI_NUMERICHOST)can succeed instead of returningEAI_NONAME.complete_now(), heap-allocatingCallforQueueUserAPCleaks if the calling thread exits before entering an alertable wait.AI-assisted review + local MSVC verification.
Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-28T02:07:40Z.
@timbot103 thanks, all four points were right. Fixed in
37163d100.1 and 2 (completion port, non-overlapped sockets):
complete_nowis gone. The shim no longer completes the caller's overlapped itself. The host'sWSAIoctlgets the caller'slpOverlappedandlpCompletionRoutineagain, so the completion is reported exactly as Winsock reports it for that socket: a port packet, the event, the routine, or nothing for a socket withoutWSA_FLAG_OVERLAPPED.The original problem was the stack frame, and that is fixed in
wrap_extension_lookup:&real_exton the stack and not into the caller's buffer.WSA_IO_PENDING, since the host may still write to it then.ConnectEx/WSASendMsg, never the native one.3a (
AI_NUMERICHOST):localhostis passed on unchanged underAI_NUMERICHOST, so the host refuses it withEAI_NONAMEand does no lookup.3b (the APC record leak): gone with
complete_now.Evidence. What Windows 11 itself does for this lookup with an overlapped (a standalone probe on an overlapped socket):
rc == 0,lpcbBytesReturned == 8, andInternal/InternalHigh0.ws2_offline_testnow checks:GetQueuedCompletionStatusreturns the packet (our key, our OVERLAPPED), and the buffer holds the wrappedConnectEx;WSA_FLAG_OVERLAPPED: the event and OVERLAPPED end the same as after the host's ownWSAIoctlon the same socket;getaddrinfo("localhost", AI_NUMERICHOST)isEAI_NONAME, and"127.0.0.1"is still answered.The event and routine checks now expect what Windows does (no byte count in the OVERLAPPED).
With the previous commit's shim these 3 new checks fail. With this one, 87/87 pass. Windows 11, MSVC (VS 18), RelWithDebInfo.
AI-assisted (Claude Code)
Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T06:38:38Z.
#2533 | head
37163d100f(merged cleanly with main6dfca64280) |ctest --test-dir runtime/build -C RelWithDebInfo -R "^ws2_offline$" --output-on-failure| red on main:ws2_offline_testfails and crashes with0xC0000005(STATUS_ACCESS_VIOLATION) aftergethostbyname(NULL)/""was allowed throughis_loopback_node_ainstead of being refused withWSAHOST_NOT_FOUND| green on PR+main:1/1CTest passes (ws2_offline87/87checks in1.08s, verifying empty/null local-host name refusal,localhost->127.0.0.1/::1literal rewrite,AI_NUMERICHOSTpreservation, and overlapped/IOCP/completion-routine/non-overlappedWSAIoctlextension-pointer wrapping);check_no_game_data.py0violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yesImported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T14:36:13Z.
Review: COMMENTED
Static code audit against
37163d100andec740bcc3. I would request changes for four issues.runtime/shims/ws2_offline.cpp:879adds a directWSAGetLastErrorimport. The standalone test linksws2_32, but the runtime without the optional Rust decoders has no such link dependency. Resolve it through the existing host lookup before calling the provider.runtime/shims/ws2_offline.cpp:270and:274drop::1forAF_UNSPECand null hints. A client cannot discover an IPv6-only localhost listener through these calls. Preserve both loopback families under those hints with native result ownership.runtime/shims/ws2_offline.cpp:876writes the wrapper before the provider readsin_buf. With shared input and output storage that overwrites the GUID. Pass the saved GUID through storage that survives the provider's use.runtime/shims/ws2_offline.cpp:885leaks the native scratch allocation on everyWSA_IO_PENDINGreturn, even after completion or cancellation. Give the storage a completion lifetime or guarantee synchronous lookup while preserving native notification behavior.The reported Windows tests cover event, IOCP, APC and non-overlapped cases. Please add coverage for overlapping buffers, a forced pending result, both loopback families with default hints, and the Ex variants.
AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T20:10:29Z.
Review: COMMENTED
Reviewed and locally tested
37163d100(runtime/shims/ws2_offline.cpp,runtime/test/ws2_offline_test.cpp).Local verification:
ws2_offline_test(MSVC Release / Ninja) and ranctest -R ^ws2_offline$: 1/1 passed.python -I scripts/check_no_game_data.py: passed.Holding off on approval because code inspection confirms the remaining issues in
runtime/shims/ws2_offline.cpp:wrap_extension_lookup(runtime/shims/ws2_offline.cpp:879) callsWSAGetLastError()directly instead of resolving it dynamically throughhost_proc(unlikeset_wsa_errorand the other hostws2_32.dllcalls), introducing a direct import-library dependency onws2_32.wrap_extension_lookup(runtime/shims/ws2_offline.cpp:876) overwritesout_bufwith&wrappedbefore invokingreal_fn(..., in_buf, ...). If a caller aliasesin_buf == out_buf, the requested extension GUID is clobbered before Winsock reads it.WSA_IO_PENDING(runtime/shims/ws2_offline.cpp:885), the heap-allocatedHostAnsweris never freed.AF_UNSPEC, rewriting"localhost"to"127.0.0.1"drops::1loopback results.AI-assisted (Antigravity, Gemini).
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T22:04:17Z.
Verified by OreoFR
c1ecf04ed+ PR head37163d100ctest -R "^ws2_offline$" + gta6rt buildAI-assisted (Claude Code, Claude Opus 5.5).
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-28T23:23:13Z.
Correction: I missed a link failure in this review. With this change,
gta6rt.execallsWSAGetLastErrordirectly inrt::ws2::wrap_extension_lookup(runtime/shims/ws2_offline.cpp:879), while the rest of the shim reaches it throughhost_proc.gta6rtdoes not linkws2_32, so the link fails withLNK2019 __imp_WSAGetLastError, thenLNK1120.My build did not catch it: in my configuration the Rust XMA2/Opus decoders' interface libraries bring
ws2_32into thegta6rtlink. I reproduced it here by linkinggta6rt.exewithoutws2_32.lib. That adds exactly one unresolved symbol overmain:__imp_WSAGetLastError, referenced inwrap_extension_lookup. A fix throughhost_proc(nows2_32) is being prepared by another tester.Original comment below.
Tested
37163d100merged (no commit) withmainaa918e5d9. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40, Windows 11 10.0.26300; MSVC 19.52 (Visual Studio 18), RelWithDebInfo, Ninja.ws2_offlinepasses.runtime/shims/ws2_offline.cppback tomain,ws2_offlineprintsFAIL geth(the output stops there) and then crashes: ctest reportsSEGFAULT.No blocker from my side. AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T23:25:03Z.
Merged after TheGeogeo's red/green check (check: green with this PR, red with
main's code, no regression over the relinked tests) and a re-check here: no conflict withmain1c97c43fd, same head as checked,no-game-datagreen, and the only ctest failures in the Windows/macOS CI jobs are tests this diff does not touch (known onmainor Vulkan: vk_xdxr_postbuild_readback vk_bindless_dimensions d3d12_compile_pool xbox_present ). The Python jobs fail onmaintoo.AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T23:38:25Z.
Heads-up after merge: this breaks the
gta6rt.exelink on Windows/MSVC builds without the Rust XMA decoder (LNK2019 __imp_WSAGetLastError, thenLNK1120, reported by OreoFR onmaina9e247cd6).ws2_offline.cpp:36resolves every host ws2_32 call dynamically so gta6rt needs nows2_32.lib; the new directWSAGetLastError()call does not, and CI only linked because the XMA decoder target bringsws2_32(runtime/CMakeLists.txt:155). A fix through the existinghost_proc<LastErrFn>("WSAGetLastError")(ws2_offline.cpp:388) is being prepared by OreoFR's agent. Until it lands, rebuildruntime/from1bc055e6c. My merge, my miss.AI-assisted (Claude Code, Claude Opus 5.5), posted for MoonEyes.