]> git.proxmox.com Git - wasi-libc.git/commitdiff
Support threads in the new crt1-command.c ctor check. (#339)
authorDan Gohman <dev@sunfishcode.online>
Tue, 8 Nov 2022 21:37:44 +0000 (13:37 -0800)
committerGitHub <noreply@github.com>
Tue, 8 Nov 2022 21:37:44 +0000 (13:37 -0800)
Use an atomic compare-and-swap for checking whether constructors have
been run, when threads are enabled.

libc-bottom-half/crt/crt1-command.c

index 6e2bcd94277d142acb0e3fc9e4a32813672cbc7b..48be79f7ef586e6e256db32f5aaff0ee58ed9c81 100644 (file)
@@ -1,19 +1,33 @@
+#ifdef _REENTRANT
+#include <stdatomic.h>
+#endif
 #include <wasi/api.h>
 extern void __wasm_call_ctors(void);
 extern int __main_void(void);
 extern void __wasm_call_dtors(void);
 
-// Commands should only be called once per instance. This simple check ensures
-// that the `_start` function isn't started more than once.
-static volatile int started = 0;
-
 __attribute__((export_name("_start")))
 void _start(void) {
-    // Don't allow the program to be called multiple times.
+    // Commands should only be called once per instance. This simple check
+    // ensures that the `_start` function isn't started more than once.
+    //
+    // We use `volatile` here to prevent the store to `started` from being
+    // sunk past any subsequent code, and to prevent any compiler from
+    // optimizing based on the knowledge that `_start` is the program
+    // entrypoint.
+#ifdef _REENTRANT
+    static volatile _Atomic int started = 0;
+    int expected = 0;
+    if (!atomic_compare_exchange_strong(&started, &expected, 1)) {
+       __builtin_trap();
+    }
+#else
+    static volatile int started = 0;
     if (started != 0) {
        __builtin_trap();
     }
     started = 1;
+#endif
 
     // The linker synthesizes this to call constructors.
     __wasm_call_ctors();