Skip to content

Windows: the runtime's pthread_self is a bare symbol in every program that links the runtime (leftover from #1755, #1756) #3311

Closed

Description

@joshhickson

Spec: §16.1 (c_import makes the header's declarations available: a call to a declared C function is a call to
the library's) and §16.5 (@[c_export("name")] exports a With function under that C name).
docs/spec/abi/with-abi.md:158 says the runtime's bare names are with_*.

This is the leftover I noted at the end of #1755 and #1756: the same kind of fault as gethostname was, for
pthread_self.

rt/windows_x86_64.w:1519 and rt/windows_aarch64.w:1368 define

pub fn pthread_self() -> i64:
    GetCurrentThreadId() as i64

so that src/compiler/LlvmBridge.w:13's extern fn pthread_self() -> i64 resolves on Windows, where there is no libc
one. Its only caller is cstr_thread_id (LlvmBridge.w:458). codegen_is_runtime_abi_symbol (src/FnAbi.w:216) and
src/CCodegen.w:987 keep the name bare. It is the only pub fn in rt/windows_x86_64.w that isn't named rt_*,
with_*, win_* or __with*. Those line numbers are on main 9391c8a8. The Linux and Darwin backends don't define it.
(The same list in FnAbi.w also keeps __open, i32_to_str, i64_to_string and str_from_byte bare. The last
three are With's own names, in rt/rt_core.w on every platform. __open is the Linux runtime's
(rt/linux_x86_64.w:191) and collides the same way there: @[c_export("__open")] is duplicate symbol: __open on a
Linux build of main 9391c8a8. I've kept this issue to pthread_self.)

Because the definition is in the runtime object, it is in every Windows program that links the runtime, not only
in the compiler. Three results, each on the windows-x86_64 build of d02ac049 on Windows 11, with paths in linker
output shortened:

1. A With program can call pthread_self() without declaring it.

fn main:
    print(f"{pthread_self()}")
> with run nodecl.w
18472

Exit code 0; the number is the thread id and changes per run. On Linux x86_64 the same file is refused, on the builds
of d02ac049 and of main 9391c8a8:

error: undefined variable
 --> nodecl.w:2:14
2 |     print(f"{pthread_self()}")
  |              ^^^^^^^^^^^^
error: run failed

2. A program that exports pthread_self does not link.

@[c_export("pthread_self")]
fn my_thread_self() -> i64:
    42

fn main:
    print(f"{my_thread_self()}")
> with run pself_c.w
lld-link: error: duplicate symbol: pthread_self
>>> defined at pself_c.w:2
>>>            pself_c.exe.o
>>> defined at out/tmp/with_runtime/wo_rt_d7f65711070d6197.o

error: run failed

Exit code 1. With @[c_export("other_self")] the same file prints 42.

3. A c_import declaration of pthread_self resolves to the runtime's, with no library linked.

use c_import("unsigned long long pthread_self(void);\nunsigned long GetCurrentThreadId(void);\n")

fn main:
    let a = pthread_self()
    let b = GetCurrentThreadId()
    print(f"pthread_self {a}, GetCurrentThreadId {b}")
> with run pself_a.w
pthread_self 29896, GetCurrentThreadId 29896

Exit code 0. Expected: undefined symbol: pthread_self. The only definition the link sees is the runtime's With
function, and a C declaration shouldn't bind to that. An undefined symbol is what any other name gets:

use c_import("unsigned long long other_self(void);\n")

fn main:
    print(f"other_self {other_self()}")
> with run other_a.w
lld-link: error: undefined symbol: other_self
>>> referenced by other_a.w:4
>>>               other_a.exe.o:(__with_main)

error: run failed

Exit code 1.

What I have not run. Anything on Windows aarch64: that half is from the source alone. Any Windows build newer
than d02ac049: the files cited above have no change to pthread_self between d02ac049 and 9391c8a8. And a link
against a real pthreads library (winpthreads, pthreads4w), which I don't have here.

Impact today. Nothing on the default link collides. A program that exports the name fails to link (2). A
program's own fn pthread_self(x: i32) -> i32 doesn't compile on Windows, whether or not it is called:
error: declaration pthread_self has no finalized FnAbi signature, then error: code generation failed, exit code

  1. The same function named other_self prints its value, and so does fn pthread_self(x: i32) -> i32 on Linux; a
    zero-parameter fn pthread_self() -> i64 is fine on Windows. fn __open(x: i32) -> i32 fails the same way on a
    Linux build of main 9391c8a8. I'd expect a program that links a real pthreads library to collide too, or to bind
    to the runtime's as in 3, but I haven't run that.

Fix direction (your call). The name is there for one caller in src/, reached through an extern fn of a runtime
function, which AGENTS.md says src/ shouldn't have ("never a runtime or libc function"). An rt_-named thread-id
function in each backend, called from LlvmBridge.w through rt.platform, would let "pthread_self" leave
codegen_is_runtime_abi_symbol and src/CCodegen.w:987. Since src/ is linked with the pinned seed's runtime, that is
two steps: the runtime function first, the caller after a seed that carries it.
scripts/bootstrap/windows_platform.c:345 defines a pthread_self as well.

Activity

  1. ehartford commented on Nov 14, 2026

    @ehartford
    Contributor

    Step 1 landed in #3321: rt_thread_id exists in every platform backend. Step 2 waits for a seed that carries it: LlvmBridge.cstr_thread_id calls rt_thread_id through rt.platform, then pthread_self leaves the Windows runtime, codegen_is_runtime_abi_symbol and CCodegen.

  2. ehartford commented on Nov 17, 2026

    @ehartford
    Contributor

    Step 2 (LlvmBridge uses rt_thread_id; the Windows runtime's pthread_self goes) needs a seed that carries rt_thread_id, and a seed is cut on all gating platforms or none. linux-aarch64 is red only for #3375, which waits on the blessed c_char words. Labelled until that ruling lets the seed be cut.

  3. ehartford commented on Nov 18, 2026

    @ehartford
    Contributor

    No longer waits on Eric: the c_char ruling landed (#3430) and he asked for a fresh seed cut on 2026-11-18 (CI run 44393110516 in progress). Step 2 follows the seed bump.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions