Repository navigation
fix(build): send the MinGW linker --stack, not MSVC's /STACK: - #3613
Conversation
The Windows stack-size reservation had two problems that only surface off
the MSVC toolchain.
`#[cfg(windows)]` in a build script describes the machine running the
build, not the machine the binary is for, so cross-compiling to Windows
from Linux silently skipped the flag while a Linux-targeted build on a
Windows host wrongly emitted it. Key the check off CARGO_CFG_TARGET_OS
instead, which is the target.
The flag itself is also toolchain-specific. `link.exe` understands
`/STACK:`, but the MinGW `ld` behind `x86_64-pc-windows-gnu` does not --
it reads the argument as a filename and fails the link with:
ld.exe: cannot find /STACK:8388608: No such file or directory
Select `-Wl,--stack,<bytes>` when CARGO_CFG_TARGET_ENV is `gnu` and keep
`/STACK:` otherwise. CI never caught this because windows-latest defaults
to MSVC.
Co-Authored-By: Claude Opus 5 <[email protected]>
…tion Every Windows target reports target_env `msvc` or `gnu` (gnullvm reports `gnu`), so msvc is the only one with its own spelling. Matching on it makes any other environment take the `-Wl,--stack` form the gcc and clang drivers accept. Read the reservation back from the built rtk.exe's PE header in a Windows-only test, so a linker that rejects or drops the flag fails a test rather than shipping a 1 or 2 MiB stack. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The test matrix links windows with link.exe only, so a stack flag that GNU ld rejects (the failure build.rs used to have) cannot fail CI. Add a windows-latest leg that tests x86_64-pc-windows-gnu; the runner's host stays msvc, so build.rs also runs with host and target apart. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
KuSh
left a comment
There was a problem hiding this comment.
Round 1 — APPROVED
Claim: build.rs reserves the 8 MiB Windows main-thread stack with the spelling the target's linker takes, and decides on the build target rather than the host, so x86_64-pc-windows-gnu links and a cross-build to Windows keeps the reservation.
Scope: accept. #3239 and #2895 made the same change and were closed in favour of this PR; #3239 emits identical link args on the 20 *-windows-* targets, and #2895 gated on the host instead of the target.
Ran: x86_64-pc-windows-gnu cross-built from Linux (mingw) at develop, at the head you pushed and at the merge with develop: SizeOfStackReserve 2 MiB → 8 MiB, on rtk.exe and on the test executable. The verbatim set_windows_stack_size over the 20 Windows targets plus linux, macos, wasi and an unset env. GNU ld, lld-link, ld.lld and clang on a COFF object, for the spelling each takes. Then the real runners (below). Invariants: non-default targets, every decision. Not applicable: savings floor, under the hook, split streams.
Pins: 3 decisions, 5 mutated, 0 survive with the added test (the head you pushed had no test, so all 3 were unpinned).
Matrix: executed this round; pin check: executed this round
CI @885050598: all green, 12 jobs and the CLA check. The new stack test ran and passed (1 test) on test (windows-latest) (link.exe) and on test (windows-latest, x86_64-pc-windows-gnu) (MinGW), both 37 suites, 4167 passed, 0 failed.
Blocks merge (0)
Fixup pushed (maintainerCanModify)
Three commits on the branch, none rewrites yours:
7bf595fdfbuild.rsspecial-casesmsvc(/STACK:N) and gives every other Windows env-Wl,--stack,N. Output is the same on all 20 targets; only an unset or unknowntarget_env, which no stable target has, now gets the-Wlform.tests/windows_stack_reserve_test.rsreadsSizeOfStackReserveback from the running test executable (64-bit Windows, PE32+). It reads the test executable and not rtk.exe becausecore::test_isolationrefuses any file outsidetests/commonthat namesCARGO_BIN_EXE_rtk; both come from the samerustc-link-arg.a18bea5abci.ymlgets awindows-latestleg withx86_64-pc-windows-gnuin the existingtestmatrix, so the MinGW link is built and tested.CARGO_BUILD_TARGET: ${{ matrix.target || 'host-tuple' }}is set for every leg, so the run command stayscargo test --all; an explicit target puts each leg in cross mode, whereRUSTFLAGSno longer reach build scripts or proc-macros (CI sets none).885050598merges develop into the branch.semgreptakes its baseline from the PR's merge-base, which was29f9bb716, so it reported develop's ownsrc/hooks/init/opencode.rs:257as new; with the merge-base on develop's tip it passes.
Mutations on the new code, each read back through the test's own header parser:
- target gate removed: every Linux link fails on the
-Wl,--stackargument - default arm emits nothing: gnu test executable reads 2 MiB, test fails
- default arm back to
/STACK:: GNU ld rejects it, the link fails - msvc arm emits the GNU spelling, and msvc arm emits nothing:
lld-linkimages read 1 MiB, test fails (not run against link.exe itself; the msvc leg above reads the passing case)
Checked and correct — no need to re-verify
- Every Windows target reports
target_envgnuormsvc(gnullvm reportsgnu, withtarget_abillvm), so both spellings cover all 20 targets. - GNU ld rejects
/STACK:Noutright;lld-linkignores-Wl,--stack,Nwith a warning and keeps its default. Routing onmsvckeeps msvc-flavoured linkers on/STACK:. - Default reservation: 2 MiB with GNU ld, 1 MiB with link.exe and
ld.lld. Startup needs about 220 KiB (release) and about 2 MiB (debug) of main-thread stack on Linux, so the reservation matters for debug and test builds. #[cfg(windows)]in a build script describes the host: develop cross-built from Linux emits no stack argument at all.linker-flavor=lddoes not link with or without the flag, so it is not something this change affects.#[cfg(test)]insidebuild.rsis never run bycargo test, which is why the pin is an integration test.
Your questions
None from you. One for you: would you like Fixes #3215 in the description? #1814 reports the same link failure.
Next
fixup 7bf595fdf, a18bea5ab and the merge 885050598 pushed, code-reviewed: it inverts the match, adds the stack test and a gnu CI leg, and moves the semgrep baseline. Approving.
Rounds: 1. Threads: 0 open.
Problem
The Windows stack-size reservation in
build.rshas two toolchain bugs that only surface away from MSVC.1. Host vs target.
#[cfg(windows)]inside a build script describes the machine running the build, not the machine the binary is for. Cross-compiling to Windows from Linux silently skipped the flag; a Linux-targeted build on a Windows host wrongly emitted it.2. Wrong flag for the GNU toolchain.
link.exeunderstands/STACK:. The MinGWldbehindx86_64-pc-windows-gnudoes not — it reads the argument as a filename:Any
cargo build/cargo testonx86_64-pc-windows-gnufails to link. CI never caught this becausewindows-latestdefaults to MSVC.Fix
CARGO_CFG_TARGET_OS(the target) rather thancfg!(windows)(the host).-Wl,--stack,<bytes>whenCARGO_CFG_TARGET_ENVisgnu, keep/STACK:<bytes>otherwise.Behaviour on MSVC is unchanged — same flag, same 8 MiB.
Testing
No unit test: this is build-script output, observable only through the linker invocation itself. Verified by building the workspace on
stable-x86_64-pc-windows-gnu, which fails to link before the change and succeeds after:cargo fmt --checkandcargo clippy --all-targetsclean.🤖 Generated with Claude Code