Skip to content

Commit d146934

Browse files
committed
Experimental Reactor support.
This adds support for a new experimental "Reactor" executable model. The "Commands" and "Reactors" concepts are introduced here: WebAssembly/WASI#13 A companion Clang patch, which just consists of using the new reactor-crt1.o and Reactor-specific entry point name, is here: https://reviews.llvm.org/D62922 Instead of an entrypoint named "_start", which calls "main", which then scopes the lifetime of the program, Reactors have a "__wasi_unstable_reactor_start" function, which calls "reactor_setup". When "reactor_setup" exits, the intention is that the program should persist and be available for calling. At present, the main anticipated use for this is in environments like Node, where WASI-using modules can be imported and don't necessarily want the semantics of a "main" function. The "unstable" in "__wasi_unstable_reactor_start" reflects that this Reactor concept is not yet stable, and likely to evolve.
1 parent 92eaf25 commit d146934

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ endif
3333
# directories in the source tree.
3434
BASICS_DIR = $(CURDIR)/basics
3535
BASICS_INC = $(BASICS_DIR)/include
36-
BASICS_CRT_SOURCES = $(wildcard $(BASICS_DIR)/crt/*.c)
36+
BASICS_CRT1_SOURCE = $(BASICS_DIR)/crt/crt1.c
3737
BASICS_SOURCES = $(wildcard $(BASICS_DIR)/sources/*.c)
3838
DLMALLOC_DIR = $(CURDIR)/dlmalloc
3939
DLMALLOC_SRC_DIR = $(DLMALLOC_DIR)/src
@@ -54,7 +54,7 @@ LIBC_BOTTOM_HALF_ALL_SOURCES = \
5454
$(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c)
5555
LIBWASI_EMULATED_MMAN_SOURCES = \
5656
$(shell find $(LIBC_BOTTOM_HALF_DIR)/mman -name \*.c)
57-
LIBC_BOTTOM_HALF_CRT_SOURCES = $(wildcard $(LIBC_BOTTOM_HALF_DIR)/crt/*.c)
57+
LIBC_BOTTOM_HALF_CRT1_SOURCE = $(LIBC_BOTTOM_HALF_DIR)/crt/crt1.c
5858
LIBC_TOP_HALF_DIR = $(CURDIR)/libc-top-half
5959
LIBC_TOP_HALF_MUSL_DIR = $(LIBC_TOP_HALF_DIR)/musl
6060
LIBC_TOP_HALF_MUSL_SRC_DIR = $(LIBC_TOP_HALF_MUSL_DIR)/src
@@ -407,9 +407,9 @@ ifeq ($(THREAD_MODEL), single)
407407
endif
408408

409409
ifeq ($(BUILD_LIBC_BOTTOM_HALF),no)
410-
override CRT_SOURCES = $(BASICS_CRT_SOURCES)
410+
override CRT1_SOURCE = $(BASICS_CRT1_SOURCE)
411411
else
412-
override CRT_SOURCES = $(LIBC_BOTTOM_HALF_CRT_SOURCES)
412+
override CRT1_SOURCE = $(LIBC_BOTTOM_HALF_CRT1_SOURCE)
413413
endif
414414

415415
startup_files: include_dirs
@@ -418,7 +418,8 @@ startup_files: include_dirs
418418
#
419419
@mkdir -p "$(OBJDIR)"
420420
cd "$(OBJDIR)" && \
421-
"$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT_SOURCES) -MD -MP && \
421+
"$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT1_SOURCE) -MD -MP && \
422+
"$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT1_SOURCE) -MD -MP -DREACTOR_RUNTIME -o reactor-crt1.o && \
422423
mkdir -p "$(SYSROOT_LIB)" && \
423424
mv *.o "$(SYSROOT_LIB)"
424425

basics/crt/crt1.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
extern void __wasm_call_ctors(void);
2+
void _Exit(int) __attribute__((noreturn));
3+
4+
#ifdef REACTOR_RUNTIME
5+
extern void reactor_setup(int, char *[]);
6+
#else
27
extern int main(int, char *[]);
38
extern void __prepare_for_exit(void);
4-
void _Exit(int) __attribute__((noreturn));
9+
#endif
510

11+
#ifdef REACTOR_RUNTIME
12+
void __wasi_unstable_reactor_start(void) {
13+
#else
614
void _start(void) {
15+
#endif
716
/* The linker synthesizes this to call constructors. */
817
__wasm_call_ctors();
918

19+
#ifdef REACTOR_RUNTIME
20+
/* Call reactor_setup with the arguments. */
21+
reactor_setup(argc, argv);
22+
#else
1023
/* Call main with no arguments. */
1124
int r = main(0, 0);
1225

@@ -17,4 +30,5 @@ void _start(void) {
1730
if (r != 0) {
1831
_Exit(r);
1932
}
33+
#endif
2034
}

expected/wasm32-wasi/defined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ __uflow
231231
__unlist_locked_file
232232
__uselocale
233233
__utc
234+
__wasi_unstable_reactor_start
234235
__wasilibc_fd_renumber
235236
__wasilibc_find_relpath
236237
__wasilibc_init_preopen

expected/wasm32-wasi/undefined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,4 @@ __wasi_sock_send
6868
__wasi_sock_shutdown
6969
__wasm_call_ctors
7070
main
71+
reactor_setup

libc-bottom-half/crt/crt1.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55

66
extern char **__environ;
77
extern void __wasm_call_ctors(void);
8+
void _Exit(int) __attribute__((noreturn));
9+
10+
#ifdef REACTOR_RUNTIME
11+
extern void reactor_setup(int, char *[]);
12+
#else
813
extern int main(int, char *[]);
914
extern void __prepare_for_exit(void);
10-
void _Exit(int) __attribute__((noreturn));
15+
#endif
1116

1217
static __wasi_errno_t populate_args(size_t *argc, char ***argv) {
1318
__wasi_errno_t err;
@@ -104,7 +109,11 @@ static __wasi_errno_t populate_libpreopen(void) {
104109
return __WASI_ESUCCESS;
105110
}
106111

112+
#ifdef REACTOR_RUNTIME
113+
void __wasi_unstable_reactor_start(void) {
114+
#else
107115
void _start(void) {
116+
#endif
108117
/* Record the preopened resources. */
109118
if (populate_libpreopen() != __WASI_ESUCCESS) {
110119
_Exit(EX_OSERR);
@@ -125,6 +134,10 @@ void _start(void) {
125134
/* The linker synthesizes this to call constructors. */
126135
__wasm_call_ctors();
127136

137+
#ifdef REACTOR_RUNTIME
138+
/* Call reactor_setup with the arguments. */
139+
reactor_setup(argc, argv);
140+
#else
128141
/* Call main with the arguments. */
129142
int r = main(argc, argv);
130143

@@ -135,4 +148,5 @@ void _start(void) {
135148
if (r != 0) {
136149
_Exit(r);
137150
}
151+
#endif
138152
}

0 commit comments

Comments
 (0)