Skip to content

Commit 21d93b9

Browse files
authored
Add a check to command modules to ensure that they're only started once. (#329)
* Add a check to command modules to ensure that they're only started once. Wasm command modules should only be called once per instance, because the programming model doesn't leave linear memory in a reusable state when the program exits. As use cases arise for loading wasm modules in environments that want to treat them like reactors, add a safety check to ensure that command modules are used according to their expectations.
1 parent 9d2f5a8 commit 21d93b9

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ extern void __wasm_call_ctors(void);
33
extern int __main_void(void);
44
extern void __wasm_call_dtors(void);
55

6+
// Commands should only be called once per instance. This simple check ensures
7+
// that the `_start` function isn't started more than once.
8+
static volatile int started = 0;
9+
610
__attribute__((export_name("_start")))
711
void _start(void) {
12+
// Don't allow the program to be called multiple times.
13+
if (started != 0) {
14+
__builtin_trap();
15+
}
16+
started = 1;
17+
818
// The linker synthesizes this to call constructors.
919
__wasm_call_ctors();
1020

0 commit comments

Comments
 (0)