Skip to content

rt: the host name is rt_gethostname; the Windows runtime no longer defines the C symbol gethostname - #1756

Merged
ehartford merged 1 commit into
withlang-dev:mainfrom
joshhickson:josh/win-gethostname
Sep 27, 2026
Merged

ehartford merged 1 commit into
withlang-dev:mainfrom
joshhickson:josh/win-gethostname

Conversation

@joshhickson

Copy link
Copy Markdown
Contributor

Fixes #1755.

The Windows runtime defined gethostname so rt_core's @[link_name("gethostname")] extern resolved where
there is no libc one. ws2_32.lib is on every Windows link, and its gethostname member defines both
__imp_gethostname and a gethostname thunk, so:

  • a C library that calls Winsock's gethostname (libcurl) failed to link: duplicate symbol: gethostname,
    defined at rt/windows_x86_64.w:878 and at ws2_32.lib(WS2_32.dll);
  • With code that c_imports Winsock's gethostname silently called the runtime's (0 and the NetBIOS name
    before WSAStartup, where Winsock answers -1 / WSANOTINITIALISED).

Change. Every backend provides rt_gethostname, as it provides rt_getenv and rt_sysinfo_os:

  • rt/windows_x86_64.w, rt/windows_aarch64.w, rt/wasm.w: gethostname → rt_gethostname, same body.
  • rt/linux_x86_64.w, rt/linux_aarch64.w, rt/darwin_aarch64.w: rt_gethostname forwards to libc
    through @[link_name("gethostname")] extern fn rt_libc_gethostname; its domain row moves here from
    rt_core's facade (same preserves lines).
  • rt/rt_core.w: extern fn rt_gethostname; with_sysinfo_hostname calls it.
  • src/FnAbi.w, src/CCodegen.w: "gethostname" leaves the runtime-ABI name lists; nothing in rt/
    defines it now. docs/with-abi.sha256 re-recorded. No WITH_ABI_VERSION bump: no produced symbol or
    pass mode changes. Happy to add a v6 entry if you read the list edit as a convention change.
  • test/behavior/behav_c_import_winsock_gethostname.w (only-on: windows): Winsock's gethostname before
    and after WSAStartup, and std.os.hostname(), in one program.

Evidence (native windows-x86_64). There is no Windows seed release that can evaluate main's build layer
(v0.15.2.1 fails with the four build/selfhost.w "missing return" errors #1686 describes), so both runs were
driven by the same locally built compiler: 8db10d1a built by a 334739d2 build, then 1e839443's
sources built with 8db10d1a's build layer. seed-driver, and therefore test-green, refuse that driver
by design in both runs.

base 3ccb998e + the new test this branch e7c78254
build green (720 s) green (715 s), abi-hash-check passes
:fixpoint stage2 == stage3, 16 units stage2 == stage3, 16 units
behavior-tests 10 of 1339 failed 9 of 1339 failed
new test fails at line 19 (the runtime answers) passes
runtime-domain-audit 387 rows, ok 389 rows, ok
everything else the same 9 behavior (zlib/bzip2/sqlite facades, c_facade parse/verify, owning_wrapper_strdup, build_rss_budget), 6 compile-error (sqlite/libcurl facade), 1 spec (spec_ss14_11_await_combinator_cancel_joins), deep-debug, contract-view, uat and examples failures identical

The other failures reproduce on the unmodified base with the same driver. unit-return-review is ok on
both clean trees. with analyze on the new test: audit:all ok, 0 violations.

The repros: the C-library program now links and host_probe returns 0. The c_import program prints
rc -1, WSAGetLastError 10093. The runtime objects: rt_windows_x86_64.o defines rt_gethostname and no
gethostname. --target builds for linux x86_64/aarch64 show U gethostname + T rt_gethostname, and for
windows aarch64 T rt_gethostname. Darwin and wasm could only be type-checked here (with check --no-prelude: ok). They still need a battery on those hosts.

Not in this PR: rt/windows_*.w also define pthread_self (the compiler calls it,
src/compiler/LlvmBridge.w:445), which would collide the same way with a linked pthreads library.

…fines the C symbol gethostname

rt/windows_x86_64.w (and windows_aarch64, wasm) defined `pub fn gethostname`
so rt_core's `@[link_name("gethostname")] extern fn rt_libc_gethostname`
resolved where there is no libc gethostname. FnAbi's
codegen_is_runtime_abi_symbol and CCodegen's cc_is_public_abi_name kept that
bare name. ws2_32.lib is on every Windows link (Link.w), and its gethostname
member defines both __imp_gethostname and the gethostname thunk. So:

- A C library that calls Winsock's gethostname (libcurl; any code built
  against winsock2.h) references __imp_gethostname, pulls that member in,
  and the link fails:
    lld-link: error: duplicate symbol: gethostname
    >>> defined at .../rt/windows_x86_64.w:878  out/tmp/with_runtime/rt_windows_x86_64.o
    >>> defined at ws2_32.lib(WS2_32.dll)
  llvm-nm: rt_windows_x86_64.o `T gethostname`; the C object
  `U __imp_gethostname`; WS2_32.Lib member WS2_32.dll `T __imp_gethostname`,
  `T gethostname`.
- With code that c_imports Winsock's gethostname links to the runtime's
  function instead: before WSAStartup it returns 0 and the NetBIOS name,
  where Winsock returns -1 with WSAGetLastError() == WSANOTINITIALISED.

Every backend now provides rt_gethostname, as it provides rt_getenv and
rt_sysinfo_os: Windows and wasm under the new name, same body; Linux and
Darwin forward to libc through @[link_name("gethostname")] extern fn
rt_libc_gethostname, whose domain row moves from rt_core's facade to theirs.
rt_core declares rt_gethostname. Nothing in rt/ defines a bare gethostname
now, so the name leaves codegen_is_runtime_abi_symbol and
cc_is_public_abi_name, and docs/with-abi.sha256 is re-recorded. No
WITH_ABI_VERSION bump: no produced symbol or pass mode changes.

test/behavior/behav_c_import_winsock_gethostname.w (only-on: windows) calls
Winsock's gethostname before and after WSAStartup and std.os.hostname() in
one program. On 3ccb998 it fails at line 19 (the runtime's function answers).
@ehartford

Copy link
Copy Markdown
Contributor

Rebased onto main 8a3cb8d and verified; marking ready.

Rebase. main's runtime facades gained seven preserves domain lines per libc function (signals, signal_mask, cwd, fds, rlimits, children, stdio). The conflicts resolved as follows:

  • rt_core.w keeps those lines on rt_libc_free and drops the rt_libc_gethostname row, as this PR intends.
  • In linux_x86_64, linux_aarch64 and darwin_aarch64, the moved rt_libc_gethostname row carries main's full nine-domain set.
  • docs/with-abi.sha256 is re-recorded from the merged src/FnAbi.w (a69910bc…); src/TypeLayout.w is unchanged.

windows-x86_64 (native Windows 10, VS 2026 Build Tools, pinned seed nightly-20260926-local-1-2e8defaa18ef and its SDK):

  • with build: ok.
  • behav_c_import_winsock_gethostname passes.
  • :behavior-tests fails 11 of 1409 and :native-compile-error-tests fails 7 of 1239: exactly the files plain main fails on the same host, none of them touched here. I'm fixing those separately.

linux-x86_64 (the pinned seed and SDK):

@ehartford
ehartford marked this pull request as ready for review September 27, 2026 21:06
@ehartford
ehartford merged commit eb2c86f into withlang-dev:main Sep 27, 2026
ehartford pushed a commit to joshhickson/with that referenced this pull request Sep 27, 2026
…#1756's name and takes this PR's UTF-8 body

Both conflicts were the one Windows host-name function: main renamed it
gethostname -> rt_gethostname so the runtime no longer defines Winsock's C
symbol (withlang-dev#1756, eb2c86f); this branch rewrote its body to return the wide
name as UTF-8 and to fail when it does not fit, and dropped the redundant
`0 as u16` fill cast as it did at every other site. The resolution is main's
name with this branch's body. rt_core.w merged cleanly (main's
rt_gethostname extern, this branch's rt_args call; rt_args is defined in
every backend). Checked: `with check --target=x86_64-pc-windows-msvc
rt/windows_x86_64.w` and `--target=aarch64-pc-windows-msvc
rt/windows_aarch64.w` are ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants