Skip to content

runtime/shims: remove function-local static guards in winhttp_offline - #7198

Open
emirhan-karaca wants to merge 1 commit into
ItsNotRexed:mainfrom
emirhan-karaca:fix/winhttp-offline-tls-safe-init
Open

emirhan-karaca wants to merge 1 commit into
ItsNotRexed:mainfrom
emirhan-karaca:fix/winhttp-offline-tls-safe-init

Conversation

@emirhan-karaca

Copy link
Copy Markdown
Contributor

@ItsNotRexed pinging as requested for an EXE/runtime PR.

Claim: #7049 (comment) (item 2)
Context: RT-488 / #7074 (function-local dynamic static init guard null-dereference on guest threads under Wine / static-CRT cross-builds and when the mapped executable owns TLS slot 0).

Problem

runtime/shims/winhttp_offline.cpp used five function-local dynamic static initializers (live_mutex()::m, live()::s, log_refused()::logged, log_refused()::mutex, and crack_url()::host) plus a function-local std::once_flag in open_session().

When libHttpClient.GDK.dll or XCurl.dll calls WinHTTP from game_thread under Wine, static-CRT clang-cl cross-builds, or with #4313 (mapped retail EXE owning TLS slot 0), MSVC/clang-cl's _Init_thread_header guard reads _Init_thread_epoch from TLS slot 0 — the exact same bug class that caused the RT-488 / #7074 null dereference in library_loader.cpp.

Changes

  • runtime/shims/winhttp_offline.cpp:
    • Replaced live_mutex() / live() with constant-initialized SRWLOCK g_live_lock = SRWLOCK_INIT and a lazy immortal std::set<Handle*>* g_live allocated under g_live_lock (acquire/close_handle treat !g_live as invalid without allocating).
    • Replaced log_refused()'s logged/mutex with constant-initialized SRWLOCK g_log_lock = SRWLOCK_INIT and lazy immortal std::set<std::wstring>* g_logged.
    • Replaced open_session()'s static std::once_flag with constant-initialized std::atomic<bool> g_banner_logged{false}.
    • Replaced crack_url()'s function-local lambda static with constant-initialized INIT_ONCE g_crack_url_once = INIT_ONCE_STATIC_INIT (InitOnceExecuteOnce).
    • Zero line overlap with #4209 (which edits lines 253–385 of winhttp_offline.cpp and lines 141–250 of winhttp_offline_test.cpp).
  • runtime/test/winhttp_offline_test.cpp:
    • Added a 4-thread concurrent stress test exercising WinHttpOpen, WinHttpConnect, WinHttpOpenRequest, WinHttpSendRequest refused-server deduplication, WinHttpCrackUrl, and WinHttpCloseHandle.

Verification

  • Compiler: MinGW-w64 g++ 16.2.0 (-std=c++20 -O2 -Wall -Wextra -Werror on winhttp_offline.cpp in both ANSI and -DUNICODE -D_UNICODE modes and on winhttp_offline_test.cpp: 0 warnings).
  • Binary guard-symbol control (nm -C and nm -u on winhttp_offline.o):
    • Before (origin/main 09b9ba886): 5 function-local guard variables (live_mutex()::m, live()::s, log_refused()::mutex, log_refused()::logged, crack_url()::host) plus undefined __cxa_guard_acquire / __cxa_guard_release / __cxa_guard_abort / __once_proxy / pthread_once.
    • After (patched): 0 guard variables, 0 __cxa_guard_*, 0 _tls_index, 0 __once_proxy.
  • GREEN runs:
    • ANSI: 3/3 PASS (exit=0 | 84 check(s), 0 failure(s)).
    • UNICODE: 1/1 PASS (exit=0 | 84 check(s), 0 failure(s)).
  • Hygiene: CRLF=0 on both files, git diff --check exit 0.

Disclosures

  • AI-assisted: Implementation and test additions were AI-assisted; all edits, symbol diffs, and runs were locally verified.
  • No game files: Zero game data, binaries, keys, or shaders touched or included.
  • Local environment note: Verified locally with MinGW-w64 g++ 16.2.0 (linking untouched registry.cpp with a scratch _AddressOfReturnAddress intrinsic stub); no local MSVC cl.exe or CMake. Full MSVC /W4 /permissive- /utf-8 + ctest runs in CI.

@ciagnij

ciagnij commented Nov 23, 2026

Copy link
Copy Markdown

Independent native Windows 11 x64 / MSVC 19.44.35223 check of dbcaaa9 on main 8575d36. gta6rt and winhttp_offline_test built in RelWithDebInfo. winhttp_offline, startup_trace and run_tiny_game each passed 10 consecutive CTest runs with the patch. All three also passed once on baseline. dumpbin /symbols reported _Init_thread_header, _Init_thread_footer, _Init_thread_abort and _Init_thread_epoch in the baseline WinHTTP object and none in the patched object. This checks the MSVC static-init guard removal and native synthetic behavior. The Wine and guest-owned TLS-slot-0 conditions were outside this run. No game files or network requests. AI-assisted (Codex).

@JRamos1993

JRamos1993 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested dbcaaa9 merged onto main e0816d6 on an RTX 5090 (driver 621.24), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:

  • Full ctest: 265/268. The failures main e0816d6 also has without this PR: d3d12_indirect_dispatch_count, d3d12_vulkan_device, xbox_present.
  • winhttp_offline: passes 20 of 20 runs, and passes under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Warnings: none new in the files this PR changes.

AI-assisted (Claude Code).

@Pierrot1398

Copy link
Copy Markdown

Tested dbcaaa9 on NVIDIA GeForce RTX 5060 Ti, driver 621.24 (32.0.16.2124), Windows 11 Pro 26300, MSVC (VS 18 Build Tools) RelWithDebInfo, Vulkan headers via -DLEONIDA_VULKAN_HEADERS_FETCH=ON: pass.

  • ctest -R ^winhttp_offline$ --repeat until-fail:3: 3/3 pass, including the new 4-thread × 25-iteration case (one refused-server log line for mt-concurrent.invalid:443).

Read the full diff before building. No game files. AI-assisted (Claude Code).

@myokex

myokex commented Nov 23, 2026

Copy link
Copy Markdown

Tested dbcaaa9fe on Windows 11, AMD Radeon RX 7800 XT (RDNA3, driver 32.0.32029.2010), MSVC 2022 RelWithDebInfo, CMake 4.4.4, Vulkan SDK 1.4.363.0; merged alone onto main 042c5f378, full ctest -j8; synthetic, no game files: pass. winhttp_offline passes, including the concurrent-sessions case. Code read: winhttp.dll is loaded from System32 only for WinHttpCrackUrl (URL parsing). The failures main itself has on this card (d3d12_vulkan_device, d3d12_texture_tiles, d3d12_indirect_dispatch_count, d3d12_pipeline_library_host, and vk_queue_query_sync in most runs; see #166) are unchanged. d3d12_bindless_churn failed once in this run; it is a flake under parallel ctest -j8 on this card (it fails with unrelated PRs too, and passes 10/10 alone on main), not this PR. AI-assisted (Claude Code).

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.

5 participants