emscripten-core / emscripten-core/emscripten

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

Open
#26,040 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

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

## Reproducer

```c
#include
#include

__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.

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

gcc can also read the TLS variable correctly.

```console
$ 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.

```console
$ 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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.