In `open_connection()` (`WinHttpConnect`), `make_handle(Kind::Connect, s,
s->async)` previously inserted the new `Handle` into `live()` and
synchronously invoked `WINHTTP_CALLBACK_STATUS_HANDLE_CREATED` before
`c->server = server` and `c->port = port` were assigned, and without
holding a temporary reference on the handle while `HANDLE_CREATED` ran.
This caused three issues when a `HANDLE_CREATED` callback acted on the
handle inline or handed it to another thread:
1. Calling `WinHttpCloseHandle(self)` inside `HANDLE_CREATED` erased and
freed `Handle` (`refs` 1 -> 0 -> `delete h`) before `make_handle()`
returned, causing `open_connection()` to write `server` and `port`
into freed heap memory (use-after-free).
2. Because `make_handle()` held no reference across `HANDLE_CREATED`,
`WinHttpCloseHandle(self)` inside `HANDLE_CREATED` fired
`WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING` re-entrantly before
`HANDLE_CREATED` returned and still returned the freed non-null
`HINTERNET` handle from `WinHttpConnect` / `WinHttpOpenRequest`,
whereas native `winhttp.dll` (measured on Windows 11 26200) fires
`HANDLE_CLOSING` after `HANDLE_CREATED` exits and returns `NULL` with
`ERROR_WINHTTP_OPERATION_CANCELLED` (12017).
3. Opening and sending a request from inside `HANDLE_CREATED` (or from a
worker thread notified by it) caused `log_refused()` / `server_of()`
to read an empty `server` and port `0` (`:0`) or race with
`std::wstring` assignment.
Initialize `h->server` and `h->port` inside `make_handle()` before
inserting into `live()`, hold an extra reference across
`WINHTTP_CALLBACK_STATUS_HANDLE_CREATED` so `HANDLE_CLOSING` fires after
`HANDLE_CREATED` returns, and return `nullptr` with
`ERROR_WINHTTP_OPERATION_CANCELLED` if the callback closed the handle.
Verification (synthetic tests only, no game files):
- `winhttp_offline_test`: 57/57 checks pass via CTest and direct run
- MSVC `/fsanitize=address`: 57/57 checks pass with 0 ASan errors
(reproduced `heap-use-after-free` on unpatched `origin/main`)
- Adjacent non-GPU runtime shim tests (`loader`, `dll_thread_calls`,
`unimplemented_policy`, `xmem`, `process_cmdline`, `file_system`,
`winhttp_offline`, `xgameruntime`): 8/8 passed via CTest
- `python -I scripts/check_no_game_data.py`: passed
Coordination claim:
https://github.com/ItsNotRexed/Leonida/issues/4257#issuecomment-7088991989
AI-assisted implementation and testing.
WinHttpSendRequest assigned its dwContext to the request unconditionally, so a client that set the context with
WINHTTP_OPTION_CONTEXT_VALUE and sent with 0 got 0 back from WinHttpQueryOption and in its REQUEST_ERROR and
HANDLE_CLOSING callbacks. The host's winhttp.dll (Windows 11, asynchronous session, loopback) keeps the option's
value in all three. A nonzero dwContext still replaces it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- rdna2spirv: declare each packed vertex input register once; a register whose elements disagree on the component
type is refused (SHADER-644).
- runtime: a title's VirtualAlloc drops the console's MEM_GRAPHICS / MEM_TITLE type bits the host rejects (RT-217).
- runtime: XPersistentLocalStorageImpl is served from <overlay>\persistent with the package's 2048 MB quota
(RT-219; layout from the public docs, not measured in a caller).
- runtime: G:\ names the package and T:\ a scratch folder in the overlay; paths under the configured folders and
every path with no folders configured stay host paths (RT-218).
- winhttp_offline: header blocks and timeouts the host WinHTTP rejects are rejected the same way.
- cfg_structure.run: a limit of zero or less runs no blocks.
- shader_meta: an s_movk_i32 with a register operand and a float immediate beyond float32 are left unknown instead
of raising.
Each comes with a test that fails on the old code. Conflicts with main resolved here (internal_dir() hides T:'s
folder; both file_system_test blocks kept).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
make_handle copied the parent's status callback and notification mask to
a new handle and left its context at 0. The host's winhttp.dll also
passes WINHTTP_OPTION_CONTEXT_VALUE on: a connection and a request start
with the context their parent has when they are made, WinHttpQueryOption
returns it and their HANDLE_CREATED and HANDLE_CLOSING callbacks carry
it. A context set on the handle itself, or given to WinHttpSendRequest,
still replaces it.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The package's XCurl.dll and libHttpClient.GDK.dll import 16 WinHTTP functions
(no Winsock) and bound to the host's winhttp.dll, so a mapped title could reach
the network. winhttp.dll is now a module the shims own and all 16 are answered
in-process:
- sessions, connections and requests are local handles; no name lookup, no
socket;
- WinHttpSendRequest fails with ERROR_WINHTTP_CANNOT_CONNECT; on a
WINHTTP_FLAG_ASYNC session it returns TRUE and reports
WINHTTP_CALLBACK_STATUS_REQUEST_ERROR {API_SEND_REQUEST, CANNOT_CONNECT} from
a thread-pool thread with the request's context (not after the request was
closed);
- reading a response and WinHttpWriteData fail with
ERROR_WINHTTP_INCORRECT_HANDLE_STATE; options, timeouts and headers are
accepted, CONTEXT_VALUE and HANDLE_TYPE can be queried;
- callbacks are inherited by child handles (HANDLE_CREATED), and every handle
reports HANDLE_CLOSING once, last (reference-counted, so a callback that
closes its own request is safe);
- WinHttpCrackUrl stays the host's (parsing only, loaded on first use).
Each refused server is logged once. winhttp_offline: 40/40 checks, .invalid
names only, no connection opened.
AI-assisted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>