Skip to content

Commit

Permalink
Experimental Reactor support.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sunfishcode committed Jun 5, 2019
1 parent 92eaf25 commit d146934
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ endif
# directories in the source tree.
BASICS_DIR = $(CURDIR)/basics
BASICS_INC = $(BASICS_DIR)/include
BASICS_CRT_SOURCES = $(wildcard $(BASICS_DIR)/crt/*.c)
BASICS_CRT1_SOURCE = $(BASICS_DIR)/crt/crt1.c
BASICS_SOURCES = $(wildcard $(BASICS_DIR)/sources/*.c)
DLMALLOC_DIR = $(CURDIR)/dlmalloc
DLMALLOC_SRC_DIR = $(DLMALLOC_DIR)/src
Expand All @@ -54,7 +54,7 @@ LIBC_BOTTOM_HALF_ALL_SOURCES = \
$(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c)
LIBWASI_EMULATED_MMAN_SOURCES = \
$(shell find $(LIBC_BOTTOM_HALF_DIR)/mman -name \*.c)
LIBC_BOTTOM_HALF_CRT_SOURCES = $(wildcard $(LIBC_BOTTOM_HALF_DIR)/crt/*.c)
LIBC_BOTTOM_HALF_CRT1_SOURCE = $(LIBC_BOTTOM_HALF_DIR)/crt/crt1.c
LIBC_TOP_HALF_DIR = $(CURDIR)/libc-top-half
LIBC_TOP_HALF_MUSL_DIR = $(LIBC_TOP_HALF_DIR)/musl
LIBC_TOP_HALF_MUSL_SRC_DIR = $(LIBC_TOP_HALF_MUSL_DIR)/src
Expand Down Expand Up @@ -407,9 +407,9 @@ ifeq ($(THREAD_MODEL), single)
endif

ifeq ($(BUILD_LIBC_BOTTOM_HALF),no)
override CRT_SOURCES = $(BASICS_CRT_SOURCES)
override CRT1_SOURCE = $(BASICS_CRT1_SOURCE)
else
override CRT_SOURCES = $(LIBC_BOTTOM_HALF_CRT_SOURCES)
override CRT1_SOURCE = $(LIBC_BOTTOM_HALF_CRT1_SOURCE)
endif

startup_files: include_dirs
Expand All @@ -418,7 +418,8 @@ startup_files: include_dirs
#
@mkdir -p "$(OBJDIR)"
cd "$(OBJDIR)" && \
"$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT_SOURCES) -MD -MP && \
"$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT1_SOURCE) -MD -MP && \
"$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT1_SOURCE) -MD -MP -DREACTOR_RUNTIME -o reactor-crt1.o && \
mkdir -p "$(SYSROOT_LIB)" && \
mv *.o "$(SYSROOT_LIB)"

Expand Down
16 changes: 15 additions & 1 deletion basics/crt/crt1.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
extern void __wasm_call_ctors(void);
void _Exit(int) __attribute__((noreturn));

#ifdef REACTOR_RUNTIME
extern void reactor_setup(int, char *[]);
#else
extern int main(int, char *[]);
extern void __prepare_for_exit(void);
void _Exit(int) __attribute__((noreturn));
#endif

#ifdef REACTOR_RUNTIME
void __wasi_unstable_reactor_start(void) {
#else
void _start(void) {
#endif
/* The linker synthesizes this to call constructors. */
__wasm_call_ctors();

#ifdef REACTOR_RUNTIME
/* Call reactor_setup with the arguments. */
reactor_setup(argc, argv);
#else
/* Call main with no arguments. */
int r = main(0, 0);

Expand All @@ -17,4 +30,5 @@ void _start(void) {
if (r != 0) {
_Exit(r);
}
#endif
}
1 change: 1 addition & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ __uflow
__unlist_locked_file
__uselocale
__utc
__wasi_unstable_reactor_start
__wasilibc_fd_renumber
__wasilibc_find_relpath
__wasilibc_init_preopen
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ __wasi_sock_send
__wasi_sock_shutdown
__wasm_call_ctors
main
reactor_setup
16 changes: 15 additions & 1 deletion libc-bottom-half/crt/crt1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@

extern char **__environ;
extern void __wasm_call_ctors(void);
void _Exit(int) __attribute__((noreturn));

#ifdef REACTOR_RUNTIME
extern void reactor_setup(int, char *[]);
#else
extern int main(int, char *[]);
extern void __prepare_for_exit(void);
void _Exit(int) __attribute__((noreturn));
#endif

static __wasi_errno_t populate_args(size_t *argc, char ***argv) {
__wasi_errno_t err;
Expand Down Expand Up @@ -104,7 +109,11 @@ static __wasi_errno_t populate_libpreopen(void) {
return __WASI_ESUCCESS;
}

#ifdef REACTOR_RUNTIME
void __wasi_unstable_reactor_start(void) {
#else
void _start(void) {
#endif
/* Record the preopened resources. */
if (populate_libpreopen() != __WASI_ESUCCESS) {
_Exit(EX_OSERR);
Expand All @@ -125,6 +134,10 @@ void _start(void) {
/* The linker synthesizes this to call constructors. */
__wasm_call_ctors();

#ifdef REACTOR_RUNTIME
/* Call reactor_setup with the arguments. */
reactor_setup(argc, argv);
#else
/* Call main with the arguments. */
int r = main(argc, argv);

Expand All @@ -135,4 +148,5 @@ void _start(void) {
if (r != 0) {
_Exit(r);
}
#endif
}

0 comments on commit d146934

Please sign in to comment.