Skip to content

Commit 907d10f

Browse files
committed
Don't run static constructors on arbitrary user exports.
Previously, "new-style commmands" considered every user-defined export to be a potential command entrypoint, so wasi-libc and wasm-ld cooperated to run the user's static constructors on each entrypoint. This form of new-style command turned out not to be useful, and it interferes with some use cases, so disable it. This is done by making an explicit call to `__wasm_call_ctors`, which tells wasm-ld that it shouldn't synthesize any calls to `__wasm_call_ctors` on its own.
1 parent 05b3b87 commit 907d10f

File tree

2 files changed

+12
-27
lines changed

2 files changed

+12
-27
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
#include <wasi/api.h>
2-
#include <stdlib.h>
32
extern void __wasm_call_ctors(void);
43
extern int __main_void(void);
54
extern void __wasm_call_dtors(void);
65

76
__attribute__((export_name("_start")))
87
void _start(void) {
8+
// The linker synthesizes this to call constructors.
9+
__wasm_call_ctors();
10+
911
// Call `__main_void` which will either be the application's zero-argument
1012
// `__main_void` function or a libc routine which obtains the command-line
1113
// arguments and calls `__main_argv_argc`.
1214
int r = __main_void();
1315

14-
// If main exited successfully, just return, otherwise call `exit`.
16+
// Call atexit functions, destructors, stdio cleanup, etc.
17+
__wasm_call_dtors();
18+
19+
// If main exited successfully, just return, otherwise call
20+
// `__wasi_proc_exit`.
1521
if (r != 0) {
16-
exit(r);
22+
__wasi_proc_exit(r);
1723
}
1824
}

libc-bottom-half/crt/crt1.c

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
#include <wasi/api.h>
2-
extern void __wasm_call_ctors(void);
3-
extern int __main_void(void);
4-
extern void __wasm_call_dtors(void);
5-
6-
__attribute__((export_name("_start")))
7-
void _start(void) {
8-
// The linker synthesizes this to call constructors.
9-
__wasm_call_ctors();
10-
11-
// Call `__main_void` which will either be the application's zero-argument
12-
// `__main_void` function or a libc routine which obtains the command-line
13-
// arguments and calls `__main_argv_argc`.
14-
int r = __main_void();
15-
16-
// Call atexit functions, destructors, stdio cleanup, etc.
17-
__wasm_call_dtors();
18-
19-
// If main exited successfully, just return, otherwise call
20-
// `__wasi_proc_exit`.
21-
if (r != 0) {
22-
__wasi_proc_exit(r);
23-
}
24-
}
1+
// We compile a plain crt1.o for toolchain compatibility, but it's
2+
// identical to crt1-command.o.
3+
#include <crt1-command.c>

0 commit comments

Comments
 (0)