Skip to content

Commit

Permalink
Add __wasix_init and __wasix_resume to support partial evaluation tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Arshia001 committed Aug 22, 2024
1 parent c20a27d commit 279e4f6
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 6 deletions.
2 changes: 2 additions & 0 deletions expected/wasm32-wasi-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ __wasilibc_tls_align
__wasilibc_tls_size
__wasilibc_unlinkat
__wasilibc_utimens
__wasix_init
__wasix_resume
__wasm_call_dtors
__wasm_signal
__wcscoll_l
Expand Down
10 changes: 10 additions & 0 deletions expected/wasm32-wasi-threads/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5427,6 +5427,16 @@
#define __FLT_MIN_EXP__ (-125)
#define __FLT_MIN__ 1.17549435e-38F
#define __FLT_RADIX__ 2
#define __FPCLASS_NEGINF 0x0004
#define __FPCLASS_NEGNORMAL 0x0008
#define __FPCLASS_NEGSUBNORMAL 0x0010
#define __FPCLASS_NEGZERO 0x0020
#define __FPCLASS_POSINF 0x0200
#define __FPCLASS_POSNORMAL 0x0100
#define __FPCLASS_POSSUBNORMAL 0x0080
#define __FPCLASS_POSZERO 0x0040
#define __FPCLASS_QNAN 0x0002
#define __FPCLASS_SNAN 0x0001
#define __compiler_ATOMIC_BOOL_LOCK_FREE 2
#define __compiler_ATOMIC_CHAR16_T_LOCK_FREE 2
#define __compiler_ATOMIC_CHAR32_T_LOCK_FREE 2
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ __wasilibc_tls_align
__wasilibc_tls_size
__wasilibc_unlinkat
__wasilibc_utimens
__wasix_init
__wasix_resume
__wasm_call_dtors
__wasm_signal
__wcscoll_l
Expand Down
10 changes: 10 additions & 0 deletions expected/wasm32-wasi/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5484,6 +5484,16 @@
#define __FLT_MIN_EXP__ (-125)
#define __FLT_MIN__ 1.17549435e-38F
#define __FLT_RADIX__ 2
#define __FPCLASS_NEGINF 0x0004
#define __FPCLASS_NEGNORMAL 0x0008
#define __FPCLASS_NEGSUBNORMAL 0x0010
#define __FPCLASS_NEGZERO 0x0020
#define __FPCLASS_POSINF 0x0200
#define __FPCLASS_POSNORMAL 0x0100
#define __FPCLASS_POSSUBNORMAL 0x0080
#define __FPCLASS_POSZERO 0x0040
#define __FPCLASS_QNAN 0x0002
#define __FPCLASS_SNAN 0x0001
#define __compiler_ATOMIC_BOOL_LOCK_FREE 2
#define __compiler_ATOMIC_CHAR16_T_LOCK_FREE 2
#define __compiler_ATOMIC_CHAR32_T_LOCK_FREE 2
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm64-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ __wasilibc_tls_align
__wasilibc_tls_size
__wasilibc_unlinkat
__wasilibc_utimens
__wasix_init
__wasix_resume
__wasm_call_dtors
__wasm_signal
__wcscoll_l
Expand Down
10 changes: 10 additions & 0 deletions expected/wasm64-wasi/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5482,6 +5482,16 @@
#define __FLT_MIN_EXP__ (-125)
#define __FLT_MIN__ 1.17549435e-38F
#define __FLT_RADIX__ 2
#define __FPCLASS_NEGINF 0x0004
#define __FPCLASS_NEGNORMAL 0x0008
#define __FPCLASS_NEGSUBNORMAL 0x0010
#define __FPCLASS_NEGZERO 0x0020
#define __FPCLASS_POSINF 0x0200
#define __FPCLASS_POSNORMAL 0x0100
#define __FPCLASS_POSSUBNORMAL 0x0080
#define __FPCLASS_POSZERO 0x0040
#define __FPCLASS_QNAN 0x0002
#define __FPCLASS_SNAN 0x0001
#define __compiler_ATOMIC_BOOL_LOCK_FREE 2
#define __compiler_ATOMIC_CHAR16_T_LOCK_FREE 2
#define __compiler_ATOMIC_CHAR32_T_LOCK_FREE 2
Expand Down
66 changes: 60 additions & 6 deletions libc-bottom-half/crt/crt1-command.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ extern void __wasm_call_ctors(void);
extern int __main_void(void);
extern void __wasm_call_dtors(void);

#ifdef _REENTRANT
static volatile _Atomic int __wasix_started = 0;
#else
static volatile int __wasix_started = 0;
#endif

__attribute__((export_name("_start")))
void _start(void) {
// Commands should only be called once per instance. This simple check
Expand All @@ -17,17 +23,15 @@ void _start(void) {
// 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)) {
if (!atomic_compare_exchange_strong(&__wasix_started, &expected, 1)) {
__builtin_trap();
}
#else
static volatile int started = 0;
if (started != 0) {
__builtin_trap();
if (__wasix_started != 0) {
__builtin_trap();
}
started = 1;
__wasix_started = 1;
#endif

#ifdef _REENTRANT
Expand All @@ -51,3 +55,53 @@ void _start(void) {
__wasi_proc_exit(r);
}
}

// This function acts as the top "half" of _start, up until the call to
// __main_void. This is useful when a module needs to do initialization
// separately, e.g. when using partial evaluation tools. When used with
// __wasix_resume, they perform the same logic as calling _start once.
__attribute__((export_name("__wasix_init")))
void __wasix_init(void) {
#ifdef _REENTRANT
int expected = 0;
if (!atomic_compare_exchange_strong(&__wasix_started, &expected, 1)) {
__builtin_trap();
}
#else
if (__wasix_started != 0) {
__builtin_trap();
}
__wasix_started = 1;
#endif

#ifdef _REENTRANT
__wasi_init_tp();
#endif

__wasm_call_ctors();
}

// This is the bottom "half" of _start, which can be called after
// __wasix_init to continue executing the module normally. This
// function also checks the value of __wasix_started to make sure
// __wasix_init was called beforehand.
__attribute__((export_name("__wasix_resume")))
void __wasix_resume(void) {
#ifdef _REENTRANT
if (atomic_load(&__wasix_started) != 1) {
__builtin_trap();
}
#else
if (__wasix_started != 1) {
__builtin_trap();
}
#endif

int r = __main_void();

__wasm_call_dtors();

if (r != 0) {
__wasi_proc_exit(r);
}
}

0 comments on commit 279e4f6

Please sign in to comment.