Skip to content

Commit

Permalink
Added long jump capability for WASIX
Browse files Browse the repository at this point in the history
  • Loading branch information
john-sharratt committed Sep 2, 2022
1 parent 7412d48 commit 6453f15
Show file tree
Hide file tree
Showing 18 changed files with 223 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
$(filter-out %/dcngettext.c %/textdomain.c %/bind_textdomain_codeset.c, \
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/locale/*.c)) \
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/*.c) \
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/setjmp/*.c) \
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/search/*.c) \
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/multibyte/*.c) \
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/regex/*.c) \
Expand Down Expand Up @@ -337,7 +338,6 @@ MUSL_OMIT_HEADERS += \
"resolv.h" \
"pty.h" \
"dlfcn.h" \
"setjmp.h" \
"ulimit.h" \
"sys/xattr.h" \
"wordexp.h" \
Expand Down
7 changes: 5 additions & 2 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,8 @@ __wasi_sock_set_opt_size
__wasi_sock_set_opt_time
__wasi_sock_shutdown
__wasi_sock_status
__wasi_stack_forget
__wasi_stack_checkpoint
__wasi_stack_restore
__wasi_stack_save
__wasi_thread_exit
__wasi_thread_id
__wasi_thread_join
Expand Down Expand Up @@ -406,6 +405,7 @@ __wasilibc_initialize_environ
__wasilibc_link
__wasilibc_link_newat
__wasilibc_link_oldat
__wasilibc_longjmp
__wasilibc_maybe_reinitialize_environ_eagerly
__wasilibc_maybe_reinitialize_environ_eagerly
__wasilibc_nocwd___wasilibc_rmdirat
Expand All @@ -429,6 +429,7 @@ __wasilibc_rename_oldat
__wasilibc_rmdirat
__wasilibc_set_stack_pointer
__wasilibc_set_tls_base
__wasilibc_setjmp
__wasilibc_stat
__wasilibc_tell
__wasilibc_tls_align
Expand Down Expand Up @@ -935,6 +936,7 @@ logbf
logbl
logf
logl
longjmp
lrand48
lrint
lrintf
Expand Down Expand Up @@ -1108,6 +1110,7 @@ setegid
setenv
seteuid
setgid
setjmp
setkey
setlinebuf
setlocale
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
#include <sched.h>
#include <search.h>
#include <semaphore.h>
#include <setjmp.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stdc-predef.h>
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm32-wasi/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2512,6 +2512,7 @@
#define _SC_XOPEN_XPG4 100
#define _SEARCH_H
#define _SEMAPHORE_H
#define _SETJMP_H
#define _SIGNAL_H
#define _SIZE_T
#define _STDALIGN_H
Expand Down Expand Up @@ -3957,6 +3958,7 @@
#define scalbn(x,y) __tg_real_2_1(scalbn, (x), (y))
#define scandir64 scandir
#define setbit(x,i) __bitop(x,i,|=)
#define setjmp setjmp
#define si_addr __si_fields.__sigfault.si_addr
#define si_addr_lsb __si_fields.__sigfault.si_addr_lsb
#define si_arch __si_fields.__sigsys.si_arch
Expand Down
3 changes: 1 addition & 2 deletions expected/wasm32-wasi/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ __imported_wasix_32v1_sock_set_opt_size
__imported_wasix_32v1_sock_set_opt_time
__imported_wasix_32v1_sock_shutdown
__imported_wasix_32v1_sock_status
__imported_wasix_32v1_stack_forget
__imported_wasix_32v1_stack_checkpoint
__imported_wasix_32v1_stack_restore
__imported_wasix_32v1_stack_save
__imported_wasix_32v1_thread_exit
__imported_wasix_32v1_thread_id
__imported_wasix_32v1_thread_join
Expand Down
7 changes: 5 additions & 2 deletions expected/wasm64-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,8 @@ __wasi_sock_set_opt_size
__wasi_sock_set_opt_time
__wasi_sock_shutdown
__wasi_sock_status
__wasi_stack_forget
__wasi_stack_checkpoint
__wasi_stack_restore
__wasi_stack_save
__wasi_thread_exit
__wasi_thread_id
__wasi_thread_join
Expand Down Expand Up @@ -406,6 +405,7 @@ __wasilibc_initialize_environ
__wasilibc_link
__wasilibc_link_newat
__wasilibc_link_oldat
__wasilibc_longjmp
__wasilibc_maybe_reinitialize_environ_eagerly
__wasilibc_maybe_reinitialize_environ_eagerly
__wasilibc_nocwd___wasilibc_rmdirat
Expand All @@ -429,6 +429,7 @@ __wasilibc_rename_oldat
__wasilibc_rmdirat
__wasilibc_set_stack_pointer
__wasilibc_set_tls_base
__wasilibc_setjmp
__wasilibc_stat
__wasilibc_tell
__wasilibc_tls_align
Expand Down Expand Up @@ -935,6 +936,7 @@ logbf
logbl
logf
logl
longjmp
lrand48
lrint
lrintf
Expand Down Expand Up @@ -1108,6 +1110,7 @@ setegid
setenv
seteuid
setgid
setjmp
setkey
setlinebuf
setlocale
Expand Down
1 change: 1 addition & 0 deletions expected/wasm64-wasi/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
#include <sched.h>
#include <search.h>
#include <semaphore.h>
#include <setjmp.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stdc-predef.h>
Expand Down
2 changes: 2 additions & 0 deletions expected/wasm64-wasi/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2512,6 +2512,7 @@
#define _SC_XOPEN_XPG4 100
#define _SEARCH_H
#define _SEMAPHORE_H
#define _SETJMP_H
#define _SIGNAL_H
#define _SIZE_T
#define _STDALIGN_H
Expand Down Expand Up @@ -3957,6 +3958,7 @@
#define scalbn(x,y) __tg_real_2_1(scalbn, (x), (y))
#define scandir64 scandir
#define setbit(x,i) __bitop(x,i,|=)
#define setjmp setjmp
#define si_addr __si_fields.__sigfault.si_addr
#define si_addr_lsb __si_fields.__sigfault.si_addr_lsb
#define si_arch __si_fields.__sigsys.si_arch
Expand Down
3 changes: 1 addition & 2 deletions expected/wasm64-wasi/undefined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ __imported_wasix_64v1_sock_set_opt_size
__imported_wasix_64v1_sock_set_opt_time
__imported_wasix_64v1_sock_shutdown
__imported_wasix_64v1_sock_status
__imported_wasix_64v1_stack_forget
__imported_wasix_64v1_stack_checkpoint
__imported_wasix_64v1_stack_restore
__imported_wasix_64v1_stack_save
__imported_wasix_64v1_thread_exit
__imported_wasix_64v1_thread_id
__imported_wasix_64v1_thread_join
Expand Down
171 changes: 145 additions & 26 deletions libc-bottom-half/headers/public/wasi/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ typedef uint32_t __wasi_size_t;
_Static_assert(sizeof(__wasi_size_t) == 4, "witx calculated size");
_Static_assert(_Alignof(__wasi_size_t) == 4, "witx calculated align");

/**
* Represents a large number of items
*/
typedef uint64_t __wasi_longsize_t;

_Static_assert(sizeof(__wasi_longsize_t) == 8, "witx calculated size");
_Static_assert(_Alignof(__wasi_longsize_t) == 8, "witx calculated align");

/**
* Non-negative file size or length of a region within a file.
*/
Expand Down Expand Up @@ -86,6 +94,68 @@ typedef uint64_t __wasi_tl_val_t;
_Static_assert(sizeof(__wasi_tl_val_t) == 8, "witx calculated size");
_Static_assert(_Alignof(__wasi_tl_val_t) == 8, "witx calculated align");

/**
* Value of a short hash
*/
typedef uint64_t __wasi_short_hash_t;

_Static_assert(sizeof(__wasi_short_hash_t) == 8, "witx calculated size");
_Static_assert(_Alignof(__wasi_short_hash_t) == 8, "witx calculated align");

/**
* Represents a 128bit number
*/
typedef struct __wasi_hugesize_t {
/**
* First set of 64 bits
*/
uint64_t b0;

/**
* second set of 64 bits
*/
uint64_t b1;

} __wasi_hugesize_t;

_Static_assert(sizeof(__wasi_hugesize_t) == 16, "witx calculated size");
_Static_assert(_Alignof(__wasi_hugesize_t) == 8, "witx calculated align");
_Static_assert(offsetof(__wasi_hugesize_t, b0) == 0, "witx calculated offset");
_Static_assert(offsetof(__wasi_hugesize_t, b1) == 8, "witx calculated offset");

/**
* Represents a part of the stack (128 bytes)
*/
typedef struct __wasi_stack_part_t {
__wasi_hugesize_t a1;

__wasi_hugesize_t a2;

__wasi_hugesize_t a3;

__wasi_hugesize_t a4;

__wasi_hugesize_t a5;

__wasi_hugesize_t a6;

__wasi_hugesize_t a7;

__wasi_hugesize_t a8;

} __wasi_stack_part_t;

_Static_assert(sizeof(__wasi_stack_part_t) == 128, "witx calculated size");
_Static_assert(_Alignof(__wasi_stack_part_t) == 8, "witx calculated align");
_Static_assert(offsetof(__wasi_stack_part_t, a1) == 0, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_part_t, a2) == 16, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_part_t, a3) == 32, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_part_t, a4) == 48, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_part_t, a5) == 64, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_part_t, a6) == 80, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_part_t, a7) == 96, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_part_t, a8) == 112, "witx calculated offset");

/**
* Represents the first 128 bits of a SHA256 hash
*/
Expand All @@ -107,6 +177,60 @@ _Static_assert(_Alignof(__wasi_hash_t) == 8, "witx calculated align");
_Static_assert(offsetof(__wasi_hash_t, b0) == 0, "witx calculated offset");
_Static_assert(offsetof(__wasi_hash_t, b1) == 8, "witx calculated offset");

/**
* Represents a snapshot of a point in time of the stack that can be restored later
*/
typedef struct __wasi_stack_snapshot_t {
/**
* Offset into the execution memory stack
*/
uint32_t memory_offset;

/**
* Offset into the execution host stack
*/
uint32_t host_offset;

/**
* Top part of the stack that is saved for later restoration
*/
__wasi_stack_part_t stack1;

__wasi_stack_part_t stack2;

__wasi_stack_part_t stack3;

__wasi_stack_part_t stack4;

__wasi_stack_part_t stack5;

__wasi_stack_part_t stack6;

__wasi_stack_part_t stack7;

__wasi_stack_part_t stack8;

/**
* Hash thats used as an integrity checked
*/
__wasi_hash_t hash;

} __wasi_stack_snapshot_t;

_Static_assert(sizeof(__wasi_stack_snapshot_t) == 1048, "witx calculated size");
_Static_assert(_Alignof(__wasi_stack_snapshot_t) == 8, "witx calculated align");
_Static_assert(offsetof(__wasi_stack_snapshot_t, memory_offset) == 0, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_snapshot_t, host_offset) == 4, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_snapshot_t, stack1) == 8, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_snapshot_t, stack2) == 136, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_snapshot_t, stack3) == 264, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_snapshot_t, stack4) == 392, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_snapshot_t, stack5) == 520, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_snapshot_t, stack6) == 648, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_snapshot_t, stack7) == 776, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_snapshot_t, stack8) == 904, "witx calculated offset");
_Static_assert(offsetof(__wasi_stack_snapshot_t, hash) == 1032, "witx calculated offset");

/**
* Identifiers for clocks.
*/
Expand Down Expand Up @@ -4352,41 +4476,36 @@ _Noreturn void __wasi_thread_exit(
__wasi_exitcode_t rval
);
/**
* Creates a snapshot of the current stack which allows it to be restored
* later using its stack hash.
* This function signature must exactly match the `stack_restore` function
* in order for the trampoline to work properly.
* This function will manipulate the __stackpointer
* Creates a checkpoint of the current stack which allows it to be restored
* later using its stack hash. The value supplied will be returned upon
* restoration (and hence must be none zero) - zero will be returned when
* the stack is first recorded.
* This function will read the __stack_pointer global
* @return
* Returns zero upon registration and the value when restored
*/
void __wasi_stack_save(
__wasi_errno_t __wasi_stack_checkpoint(
/**
* Hash of the stack that the current thread will be restored to
* Reference to the stack snapshot that will be filled
*/
__wasi_option_hash_t * hash
);
__wasi_stack_snapshot_t * snapshot,
__wasi_longsize_t *retptr0
) __attribute__((__warn_unused_result__));
/**
* Restores the current stack to a previous stack described by its
* stack hash.
* This function signature must exactly match the `stack_save` function
* in order for the trampoline to work properly.
* This function will manipulate the __stackpointer
* Restores the current stack to a previous stack described by a supplied
* stack snapshot.
* This function will manipulate the __stack_pointer global
*/
void __wasi_stack_restore(
_Noreturn void __wasi_stack_restore(
/**
* Hash of the stack we will be jumping too - or none if we do not jump.
* Reference to the stack snapshot that will be restored
*/
__wasi_option_hash_t * hash
);
/**
* Destroys a stack snapshot that was previously made using the `stack_save`
* system call - stack hashes are reference countered thus if the same snapshot
* is taken the memory remains consistent.
*/
void __wasi_stack_forget(
const __wasi_stack_snapshot_t * snapshot,
/**
* Hash of the stack that the current thread will be forgetten
* Value to be returned when the stack is restored
* (if zero this will change to one)
*/
const __wasi_hash_t * hash
__wasi_longsize_t val
);
/**
* Forks the current process into a new subprocess. If the function
Expand Down
3 changes: 3 additions & 0 deletions libc-bottom-half/headers/public/wasi/libc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __wasi_libc_h
#define __wasi_libc_h

#include <wasi/api.h>
#include <__typedef_off_t.h>
#include <__struct_timespec.h>

Expand Down Expand Up @@ -46,6 +47,8 @@ unsigned long long __wasilibc_tls_size(void);
unsigned long long __wasilibc_tls_align(void);
void* __wasilibc_get_tls_base(void);
void __wasilibc_set_tls_base(void *val);
int __wasilibc_setjmp(__wasi_stack_snapshot_t * buf);
_Noreturn void __wasilibc_longjmp(__wasi_stack_snapshot_t * buf, int val);

/// Return the current file offset. Like `lseek(fd, 0, SEEK_CUR)`, but without
/// depending on `lseek`.
Expand Down
Loading

0 comments on commit 6453f15

Please sign in to comment.