diff options
Diffstat (limited to 'wasm2c')
-rw-r--r-- | wasm2c/README.md | 7 | ||||
-rw-r--r-- | wasm2c/examples/callback/Makefile | 2 | ||||
-rw-r--r-- | wasm2c/examples/fac/Makefile | 2 | ||||
-rw-r--r-- | wasm2c/examples/fac/fac.c | 6 | ||||
-rw-r--r-- | wasm2c/examples/fac/fac.h | 4 | ||||
-rw-r--r-- | wasm2c/examples/rot13/Makefile | 2 | ||||
-rw-r--r-- | wasm2c/wasm-rt-exceptions-impl.c | 68 | ||||
-rw-r--r-- | wasm2c/wasm-rt-exceptions.h | 69 | ||||
-rw-r--r-- | wasm2c/wasm-rt-impl.c | 53 | ||||
-rw-r--r-- | wasm2c/wasm-rt-impl.h | 13 | ||||
-rw-r--r-- | wasm2c/wasm-rt.h | 86 |
11 files changed, 180 insertions, 132 deletions
diff --git a/wasm2c/README.md b/wasm2c/README.md index 68fdb11a..5271e892 100644 --- a/wasm2c/README.md +++ b/wasm2c/README.md @@ -341,8 +341,9 @@ exhaustion. ### Runtime support for exception handling -Several additional symbols must be defined if wasm2c is being run with -support for exceptions (`--enable-exceptions`): +Several additional symbols must be defined if wasm2c is being run with support +for exceptions (`--enable-exceptions`). These are defined in +`wasm-rt-exceptions.h`. These symbols are: ```c void wasm_rt_load_exception(const char* tag, uint32_t size, const void* values); @@ -357,7 +358,7 @@ wasm_rt_try(target) ``` A C implementation of these functions is also available in -[`wasm-rt-impl.h`](wasm-rt-impl.h) and [`wasm-rt-impl.c`](wasm-rt-impl.c). +[`wasm-rt-exceptions-impl.c`](wasm-rt-exceptions-impl.c). `wasm_rt_load_exception` sets the active exception to a given tag, size, and contents. diff --git a/wasm2c/examples/callback/Makefile b/wasm2c/examples/callback/Makefile index c226ff31..ece54c34 100644 --- a/wasm2c/examples/callback/Makefile +++ b/wasm2c/examples/callback/Makefile @@ -14,6 +14,6 @@ callback.wasm: callback.wat ../../../bin/wat2wasm ../../../bin/wat2wasm --debug-names $< -o $@ callback.c: callback.wasm ../../../bin/wasm2c - ../../../bin/wasm2c $< -o $@ + ../../../bin/wasm2c $< -o $@ --disable-simd .PHONY: all clean diff --git a/wasm2c/examples/fac/Makefile b/wasm2c/examples/fac/Makefile index a5179a71..623fcfc3 100644 --- a/wasm2c/examples/fac/Makefile +++ b/wasm2c/examples/fac/Makefile @@ -12,6 +12,6 @@ fac.wasm: fac.wat ../../../bin/wat2wasm ../../../bin/wat2wasm $< -o $@ fac.c: fac.wasm ../../../bin/wasm2c - ../../../bin/wasm2c $< -o $@ + ../../../bin/wasm2c $< -o $@ --disable-simd .PHONY: all clean diff --git a/wasm2c/examples/fac/fac.c b/wasm2c/examples/fac/fac.c index 970abbe1..683c5d80 100644 --- a/wasm2c/examples/fac/fac.c +++ b/wasm2c/examples/fac/fac.c @@ -4,10 +4,14 @@ #include <stdarg.h> #include <stddef.h> #include <string.h> -#if defined(_MSC_VER) +#if defined(__MINGW32__) +#include <malloc.h> +#elif defined(_MSC_VER) #include <intrin.h> #include <malloc.h> #define alloca _alloca +#elif defined(__FreeBSD__) || defined(__OpenBSD__) +#include <stdlib.h> #else #include <alloca.h> #endif diff --git a/wasm2c/examples/fac/fac.h b/wasm2c/examples/fac/fac.h index 5450fe42..f85dc5a9 100644 --- a/wasm2c/examples/fac/fac.h +++ b/wasm2c/examples/fac/fac.h @@ -6,6 +6,10 @@ #include "wasm-rt.h" +#if defined(WASM_RT_ENABLE_EXCEPTION_HANDLING) +#include "wasm-rt-exceptions.h" +#endif + #if defined(WASM_RT_ENABLE_SIMD) #include "simde/wasm/simd128.h" #endif diff --git a/wasm2c/examples/rot13/Makefile b/wasm2c/examples/rot13/Makefile index b957c19f..4461af7c 100644 --- a/wasm2c/examples/rot13/Makefile +++ b/wasm2c/examples/rot13/Makefile @@ -14,6 +14,6 @@ rot13.wasm: rot13.wat ../../../bin/wat2wasm ../../../bin/wat2wasm $< -o $@ rot13.c: rot13.wasm ../../../bin/wasm2c - ../../../bin/wasm2c $< -o $@ + ../../../bin/wasm2c $< -o $@ --disable-simd .PHONY: all clean diff --git a/wasm2c/wasm-rt-exceptions-impl.c b/wasm2c/wasm-rt-exceptions-impl.c new file mode 100644 index 00000000..8eee492e --- /dev/null +++ b/wasm2c/wasm-rt-exceptions-impl.c @@ -0,0 +1,68 @@ +/* + * Copyright 2018 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "wasm-rt.h" + +#include "wasm-rt-exceptions.h" + +#include <string.h> + +#define MAX_EXCEPTION_SIZE 256 + +static WASM_RT_THREAD_LOCAL wasm_rt_tag_t g_active_exception_tag; +static WASM_RT_THREAD_LOCAL uint8_t g_active_exception[MAX_EXCEPTION_SIZE]; +static WASM_RT_THREAD_LOCAL uint32_t g_active_exception_size; + +static WASM_RT_THREAD_LOCAL wasm_rt_jmp_buf* g_unwind_target; + +void wasm_rt_load_exception(const wasm_rt_tag_t tag, + uint32_t size, + const void* values) { + if (size > MAX_EXCEPTION_SIZE) { + wasm_rt_trap(WASM_RT_TRAP_EXHAUSTION); + } + + g_active_exception_tag = tag; + g_active_exception_size = size; + + if (size) { + memcpy(g_active_exception, values, size); + } +} + +WASM_RT_NO_RETURN void wasm_rt_throw(void) { + WASM_RT_LONGJMP(*g_unwind_target, WASM_RT_TRAP_UNCAUGHT_EXCEPTION); +} + +WASM_RT_UNWIND_TARGET* wasm_rt_get_unwind_target(void) { + return g_unwind_target; +} + +void wasm_rt_set_unwind_target(WASM_RT_UNWIND_TARGET* target) { + g_unwind_target = target; +} + +wasm_rt_tag_t wasm_rt_exception_tag(void) { + return g_active_exception_tag; +} + +uint32_t wasm_rt_exception_size(void) { + return g_active_exception_size; +} + +void* wasm_rt_exception(void) { + return g_active_exception; +} diff --git a/wasm2c/wasm-rt-exceptions.h b/wasm2c/wasm-rt-exceptions.h new file mode 100644 index 00000000..7ccba2f7 --- /dev/null +++ b/wasm2c/wasm-rt-exceptions.h @@ -0,0 +1,69 @@ +/* + * Copyright 2018 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WASM_RT_EXCEPTIONS_H_ +#define WASM_RT_EXCEPTIONS_H_ + +#include "wasm-rt.h" + +/** + * A tag is represented as an arbitrary pointer. + */ +typedef const void* wasm_rt_tag_t; + +/** + * Set the active exception to given tag, size, and contents. + */ +void wasm_rt_load_exception(const wasm_rt_tag_t tag, + uint32_t size, + const void* values); + +/** + * Throw the active exception. + */ +WASM_RT_NO_RETURN void wasm_rt_throw(void); + +/** + * The type of an unwind target if an exception is thrown and caught. + */ +#define WASM_RT_UNWIND_TARGET wasm_rt_jmp_buf + +/** + * Get the current unwind target if an exception is thrown. + */ +WASM_RT_UNWIND_TARGET* wasm_rt_get_unwind_target(void); + +/** + * Set the unwind target if an exception is thrown. + */ +void wasm_rt_set_unwind_target(WASM_RT_UNWIND_TARGET* target); + +/** + * Tag of the active exception. + */ +wasm_rt_tag_t wasm_rt_exception_tag(void); + +/** + * Size of the active exception. + */ +uint32_t wasm_rt_exception_size(void); + +/** + * Contents of the active exception. + */ +void* wasm_rt_exception(void); + +#endif diff --git a/wasm2c/wasm-rt-impl.c b/wasm2c/wasm-rt-impl.c index 788fe1d7..db4458ae 100644 --- a/wasm2c/wasm-rt-impl.c +++ b/wasm2c/wasm-rt-impl.c @@ -36,13 +36,7 @@ #include <sys/mman.h> #endif -#if _MSC_VER -#include <malloc.h> -#define alloca _alloca -#endif - #define PAGE_SIZE 65536 -#define MAX_EXCEPTION_SIZE 256 #if WASM_RT_INSTALL_SIGNAL_HANDLER static bool g_signal_handler_installed = false; @@ -60,12 +54,6 @@ WASM_RT_THREAD_LOCAL uint32_t wasm_rt_saved_call_stack_depth; WASM_RT_THREAD_LOCAL wasm_rt_jmp_buf g_wasm_rt_jmp_buf; -static WASM_RT_THREAD_LOCAL wasm_rt_tag_t g_active_exception_tag; -static WASM_RT_THREAD_LOCAL uint8_t g_active_exception[MAX_EXCEPTION_SIZE]; -static WASM_RT_THREAD_LOCAL uint32_t g_active_exception_size; - -static WASM_RT_THREAD_LOCAL wasm_rt_jmp_buf* g_unwind_target; - #ifdef WASM_RT_TRAP_HANDLER extern void WASM_RT_TRAP_HANDLER(wasm_rt_trap_t code); #endif @@ -88,45 +76,6 @@ void wasm_rt_trap(wasm_rt_trap_t code) { #endif } -void wasm_rt_load_exception(const wasm_rt_tag_t tag, - uint32_t size, - const void* values) { - if (size > MAX_EXCEPTION_SIZE) { - wasm_rt_trap(WASM_RT_TRAP_EXHAUSTION); - } - - g_active_exception_tag = tag; - g_active_exception_size = size; - - if (size) { - memcpy(g_active_exception, values, size); - } -} - -WASM_RT_NO_RETURN void wasm_rt_throw(void) { - WASM_RT_LONGJMP(*g_unwind_target, WASM_RT_TRAP_UNCAUGHT_EXCEPTION); -} - -WASM_RT_UNWIND_TARGET* wasm_rt_get_unwind_target(void) { - return g_unwind_target; -} - -void wasm_rt_set_unwind_target(WASM_RT_UNWIND_TARGET* target) { - g_unwind_target = target; -} - -wasm_rt_tag_t wasm_rt_exception_tag(void) { - return g_active_exception_tag; -} - -uint32_t wasm_rt_exception_size(void) { - return g_active_exception_size; -} - -void* wasm_rt_exception(void) { - return g_active_exception; -} - #ifdef _WIN32 static void* os_mmap(size_t size) { void* ret = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); @@ -399,7 +348,7 @@ uint64_t wasm_rt_grow_memory(wasm_rt_memory_t* memory, uint64_t delta) { void wasm_rt_free_memory(wasm_rt_memory_t* memory) { #if WASM_RT_USE_MMAP const uint64_t mmap_size = get_allocation_size_for_mmap(memory); - os_munmap(memory->data, mmap_size); // ignore error + os_munmap(memory->data, mmap_size); // ignore error #else free(memory->data); #endif diff --git a/wasm2c/wasm-rt-impl.h b/wasm2c/wasm-rt-impl.h index 088619f4..c7d7fdbf 100644 --- a/wasm2c/wasm-rt-impl.h +++ b/wasm2c/wasm-rt-impl.h @@ -30,19 +30,6 @@ extern "C" { /** A setjmp buffer used for handling traps. */ extern WASM_RT_THREAD_LOCAL wasm_rt_jmp_buf g_wasm_rt_jmp_buf; -#if WASM_RT_INSTALL_SIGNAL_HANDLER && !defined(_WIN32) -#define WASM_RT_LONGJMP_UNCHECKED(buf, val) siglongjmp(buf, val) -#else -#define WASM_RT_LONGJMP_UNCHECKED(buf, val) longjmp(buf, val) -#endif - -#define WASM_RT_LONGJMP(buf, val) \ - /* Abort on failure as this may be called in the trap handler */ \ - if (!((buf).initialized)) \ - abort(); \ - (buf).initialized = false; \ - WASM_RT_LONGJMP_UNCHECKED((buf).buffer, val) - #if WASM_RT_USE_STACK_DEPTH_COUNT /** Saved call stack depth that will be restored in case a trap occurs. */ extern WASM_RT_THREAD_LOCAL uint32_t wasm_rt_saved_call_stack_depth; diff --git a/wasm2c/wasm-rt.h b/wasm2c/wasm-rt.h index 03f78834..9fd12dee 100644 --- a/wasm2c/wasm-rt.h +++ b/wasm2c/wasm-rt.h @@ -318,37 +318,7 @@ bool wasm_rt_is_initialized(void); void wasm_rt_free(void); /** - * Stop execution immediately and jump back to the call to `wasm_rt_impl_try`. - * The result of `wasm_rt_impl_try` will be the provided trap reason. - * - * This is typically called by the generated code, and not the embedder. - */ -WASM_RT_NO_RETURN void wasm_rt_trap(wasm_rt_trap_t); - -/** - * Return a human readable error string based on a trap type. - */ -const char* wasm_rt_strerror(wasm_rt_trap_t trap); - -/** - * A tag is represented as an arbitrary pointer. - */ -typedef const void* wasm_rt_tag_t; - -/** - * Set the active exception to given tag, size, and contents. - */ -void wasm_rt_load_exception(const wasm_rt_tag_t tag, - uint32_t size, - const void* values); - -/** - * Throw the active exception. - */ -WASM_RT_NO_RETURN void wasm_rt_throw(void); - -/** - * A hardened jmp_buf that allows us to checks if it is initialized before use + * A hardened jmp_buf that allows checking for initialization before use */ typedef struct { /* Is the jmp buf intialized? */ @@ -357,44 +327,40 @@ typedef struct { jmp_buf buffer; } wasm_rt_jmp_buf; -/** - * The type of an unwind target if an exception is thrown and caught. - */ -#define WASM_RT_UNWIND_TARGET wasm_rt_jmp_buf +#if WASM_RT_INSTALL_SIGNAL_HANDLER && !defined(_WIN32) +#define WASM_RT_SETJMP_SETBUF(buf) sigsetjmp(buf, 1) +#else +#define WASM_RT_SETJMP_SETBUF(buf) setjmp(buf) +#endif -/** - * Get the current unwind target if an exception is thrown. - */ -WASM_RT_UNWIND_TARGET* wasm_rt_get_unwind_target(void); +#define WASM_RT_SETJMP(buf) \ + ((buf).initialized = true, WASM_RT_SETJMP_SETBUF((buf).buffer)) -/** - * Set the unwind target if an exception is thrown. - */ -void wasm_rt_set_unwind_target(WASM_RT_UNWIND_TARGET* target); +#if WASM_RT_INSTALL_SIGNAL_HANDLER && !defined(_WIN32) +#define WASM_RT_LONGJMP_UNCHECKED(buf, val) siglongjmp(buf, val) +#else +#define WASM_RT_LONGJMP_UNCHECKED(buf, val) longjmp(buf, val) +#endif -/** - * Tag of the active exception. - */ -wasm_rt_tag_t wasm_rt_exception_tag(void); +#define WASM_RT_LONGJMP(buf, val) \ + /* Abort on failure as this may be called in the trap handler */ \ + if (!((buf).initialized)) \ + abort(); \ + (buf).initialized = false; \ + WASM_RT_LONGJMP_UNCHECKED((buf).buffer, val) /** - * Size of the active exception. + * Stop execution immediately and jump back to the call to `wasm_rt_impl_try`. + * The result of `wasm_rt_impl_try` will be the provided trap reason. + * + * This is typically called by the generated code, and not the embedder. */ -uint32_t wasm_rt_exception_size(void); +WASM_RT_NO_RETURN void wasm_rt_trap(wasm_rt_trap_t); /** - * Contents of the active exception. + * Return a human readable error string based on a trap type. */ -void* wasm_rt_exception(void); - -#if WASM_RT_INSTALL_SIGNAL_HANDLER && !defined(_WIN32) -#define WASM_RT_SETJMP_SETBUF(buf) sigsetjmp(buf, 1) -#else -#define WASM_RT_SETJMP_SETBUF(buf) setjmp(buf) -#endif - -#define WASM_RT_SETJMP(buf) \ - ((buf).initialized = true, WASM_RT_SETJMP_SETBUF((buf).buffer)) +const char* wasm_rt_strerror(wasm_rt_trap_t trap); #define wasm_rt_try(target) WASM_RT_SETJMP(target) |