You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Windows: the runtime defines the C symbol gethostname, so a C library that calls Winsock's fails to link, and a c_import call reaches the runtime instead of Winsock #1755
Spec: §16.1 (c_import makes the header's declarations available: a call to gethostname from winsock2.h is a call to Winsock's) and §16.8 (link directives: link: "ws2_32" links Winsock).
rt/windows_x86_64.w:878 (and rt/windows_aarch64.w:779, rt/wasm.w:369) define pub fn gethostname(name: *mut u8, len: u64) -> i32 so that rt/rt_core.w:53's @[link_name("gethostname")] extern fn rt_libc_gethostname resolves where there is no libc gethostname. src/FnAbi.w:176 and src/CCodegen.w:893 keep that name bare.
src/compiler/Link.w:706 puts ws2_32.lib on every Windows link. Its gethostname member defines both __imp_gethostname and the gethostname thunk.
1. A C library that calls Winsock's gethostname does not link. Code built against winsock2.h calls
through __imp_gethostname, which pulls that member in:
// vendor/host.c, compiled by build.w's compile_c_object#include<winsock2.h>#include"host.h"inthost_probe(void) {
WSADATAwsa; charname[256];
if (WSAStartup(MAKEWORD(2, 2), &wsa) !=0) return-2;
intrc=gethostname(name, (int)sizeofname);
WSACleanup();
returnrc;
}
use c_import("host.h", link: "host")
fn main:
print(f"host_probe rc {host_probe()}")
lld-link: error: duplicate symbol: gethostname
>>> defined at /with-src/.../rt/windows_x86_64.w:878
>>> out/tmp/with_runtime/rt_windows_x86_64.o
>>> defined at ws2_32.lib(WS2_32.dll)
This is how a libcurl program fails (with get c.libcurl 8.22.0). The libcurl release UAT stops earlier on
Windows, at #1495, so CI has not reached this link.
2. With code that calls Winsock's gethostname gets the runtime's. With emits a bare gethostname
reference, which the runtime object already defines:
use c_import("int gethostname(char *name, int namelen);\nint WSAGetLastError(void);\n", link: "ws2_32")
fn main:
var buf: [256]u8 = [0 as u8; 256]
let rc = unsafe { gethostname(&raw mut buf as *mut [256]u8 as *mut c_char, 256) }
print(f"rc {rc}, WSAGetLastError {WSAGetLastError()}")
Expected (Winsock, before WSAStartup): rc -1, WSAGetLastError 10093. Actual: rc 0, WSAGetLastError 203,
with the NetBIOS computer name in the buffer.
Reproduced on main 3ccb998e (windows-x86_64, native). The code involved is unchanged on 5466e60c.
Workaround: none for a C library that calls gethostname. (The program that found this runs curl.exe
instead of libcurl.)
Same class, not covered here:rt/windows_*.w also define pthread_self, which the compiler calls
(src/compiler/LlvmBridge.w:445). A program that links a pthreads library on Windows would collide the same
way.
Activity