summaryrefslogtreecommitdiff
path: root/test/wasm2c/minimal.txt
diff options
context:
space:
mode:
authorKeith Winstein <keithw@cs.stanford.edu>2023-01-25 10:28:11 -0800
committerGitHub <noreply@github.com>2023-01-25 10:28:11 -0800
commit777e651edbe2a1931bd0d9746da5eaae8cc61ca9 (patch)
tree21ec1ac065b1ef7a6ef98ce36f3f26bec40b7813 /test/wasm2c/minimal.txt
parent165d5d62485bf09ddac404ab908a7c0ebf6b9851 (diff)
downloadwabt-777e651edbe2a1931bd0d9746da5eaae8cc61ca9.tar.gz
wabt-777e651edbe2a1931bd0d9746da5eaae8cc61ca9.tar.bz2
wabt-777e651edbe2a1931bd0d9746da5eaae8cc61ca9.zip
wasm2c: serialize types at wasm2c-time (#2120)
This makes wasm2c serialize each function type, rather than registering function types at module-initialization time. The serialized function type is the SHA-256 of the mangled param and result types (with a space between params and results). At runtime in call_indirect, a known (immediate) function type is compared against the function type stored in a funcref structure. For call_indirects to functions local to the module, or for any call_indirect when the toolchain merges string constants across compilation units (generally, GCC and clang), this can be done by comparing the pointers to each function type. Otherwise, the actual 32-byte values are compared. The function type IDs can be looked up at runtime with `Z_[modname]_get_func_type`, which matches the API from `wasm_rt_register_func_type`. A new `callback` example demos this. wasm2c does the SHA-256 either by linking against libcrypto or, if not available or if requested via `cmake -DUSE_INTERNAL_SHA256=ON`, by using a vendored (header-only) PicoSHA2. There is no runtime dependency on SHA-256 in the wasm2c runtime or generated modules. This eliminates the last of the per-module state, so this commit also removes the [modname]_init_module() function and the s_module_initialized bool.
Diffstat (limited to 'test/wasm2c/minimal.txt')
-rw-r--r--test/wasm2c/minimal.txt45
1 files changed, 27 insertions, 18 deletions
diff --git a/test/wasm2c/minimal.txt b/test/wasm2c/minimal.txt
index 857dc879..6a706d40 100644
--- a/test/wasm2c/minimal.txt
+++ b/test/wasm2c/minimal.txt
@@ -32,9 +32,9 @@ typedef struct Z_test_instance_t {
char dummy_member;
} Z_test_instance_t;
-void Z_test_init_module(void);
void Z_test_instantiate(Z_test_instance_t*);
void Z_test_free(Z_test_instance_t*);
+wasm_rt_func_type_t Z_test_get_func_type(uint32_t param_count, uint32_t result_count, ...);
#ifdef __cplusplus
}
@@ -44,6 +44,7 @@ void Z_test_free(Z_test_instance_t*);
/* Automatically generated by wasm2c */
#include <assert.h>
#include <math.h>
+#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#if defined(_MSC_VER)
@@ -72,10 +73,15 @@ void Z_test_free(Z_test_instance_t*);
#define UNREACHABLE TRAP(UNREACHABLE)
-#define CALL_INDIRECT(table, t, ft, x, ...) \
- (LIKELY((x) < table.size && table.data[x].func && \
- table.data[x].func_type == func_types[ft]) || \
- TRAP(CALL_INDIRECT), \
+static inline bool func_types_eq(const wasm_rt_func_type_t a,
+ const wasm_rt_func_type_t b) {
+ return (a == b) || LIKELY(a && b && !memcmp(a, b, 32));
+}
+
+#define CALL_INDIRECT(table, t, ft, x, ...) \
+ (LIKELY((x) < table.size && table.data[x].func && \
+ func_types_eq(ft, table.data[x].func_type)) || \
+ TRAP(CALL_INDIRECT), \
((t)table.data[x].func)(__VA_ARGS__))
#ifdef SUPPORT_MEMORY64
@@ -537,7 +543,7 @@ static inline void memory_init(wasm_rt_memory_t* dest,
}
typedef struct {
- uint32_t func_type_index;
+ wasm_rt_func_type_t type;
wasm_rt_function_ptr_t func;
size_t module_offset;
} wasm_elem_segment_expr_t;
@@ -548,17 +554,16 @@ static inline void funcref_table_init(wasm_rt_funcref_table_t* dest,
u32 dest_addr,
u32 src_addr,
u32 n,
- void* module_instance,
- const u32* func_types) {
+ void* module_instance) {
if (UNLIKELY(src_addr + (uint64_t)n > src_size))
TRAP(OOB);
if (UNLIKELY(dest_addr + (uint64_t)n > dest->size))
TRAP(OOB);
for (u32 i = 0; i < n; i++) {
const wasm_elem_segment_expr_t* src_expr = &src[src_addr + i];
- dest->data[dest_addr + i] = (wasm_rt_funcref_t){
- func_types[src_expr->func_type_index], src_expr->func,
- (char*)module_instance + src_expr->module_offset};
+ dest->data[dest_addr + i] =
+ (wasm_rt_funcref_t){src_expr->type, src_expr->func,
+ (char*)module_instance + src_expr->module_offset};
}
}
@@ -629,18 +634,22 @@ DEFINE_TABLE_SET(externref)
DEFINE_TABLE_FILL(funcref)
DEFINE_TABLE_FILL(externref)
-static bool s_module_initialized = false;
-
-void Z_test_init_module(void) {
- assert(wasm_rt_is_initialized());
- s_module_initialized = true;
-}
+#if defined(__GNUC__) || defined(__clang__)
+#define FUNC_TYPE_T(x) static const char* const x
+#else
+#define FUNC_TYPE_T(x) static const char x[]
+#endif
void Z_test_instantiate(Z_test_instance_t* instance) {
assert(wasm_rt_is_initialized());
- assert(s_module_initialized);
}
void Z_test_free(Z_test_instance_t* instance) {
}
+
+wasm_rt_func_type_t Z_test_get_func_type(uint32_t param_count, uint32_t result_count, ...) {
+ va_list args;
+
+ return NULL;
+}
;;; STDOUT ;;)