Skip to content

atexit callback reads an incorrect value from a TLS variable when -sPROXY_TO_PTHREAD=1 #26040

@ktock

Description

@ktock

In Emscripten, an atexit callback reads an unexpected value from a TLS variable when -sPROXY_TO_PTHREAD=1 is used.

Reproducer

#include <stdio.h>
#include <stdlib.h>

__thread int a = 0;

static void print_a(void)
{
  printf("a=%d\n", a);
}

int main(int argc, char **argv)
{
  a = 1;
  atexit(print_a);
  exit(0);
}

Expected behaviour

When -sPROXY_TO_PTHREAD=1 is not used, atexit callback can read the correct value from the TLS variable.

$ emcc -o ./test.js -sEXIT_RUNTIME=1 ./test.c
$ node ./test.js
a=1

gcc can also read the TLS variable correctly.

$ gcc -o ./a.out ./test.c
$ ./a.out
a=1

Actual behaviour

When -sPROXY_TO_PTHREAD=1 is used, Emscripten's atexit callback reads an incorrect value from the TLS variable.

$ emcc -o ./test.js -pthread -sPROXY_TO_PTHREAD=1 -sEXIT_RUNTIME=1 ./test.c
$ node ./test.js
a=0

emcc -v

emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.23-git (28bcb86466a273859b8adb43cb167b97e05e145d)
clang version 22.0.0git (https:/github.com/llvm/llvm-project 8518d2c4057d9aa4249b8466a4d77771e4f1bf4f)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /emsdk/upstream/bin
Build config: +assertions

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