From 1a34f91cddb203e0653e773a6ead82aee214cdb2 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 3 Dec 2021 15:14:02 -0500 Subject: [PATCH] refactor: table instructions --- runtime/compiletime/table_instructions.c | 53 ++++++++++++++++++++++++ runtime/src/memory/64bit_nix.c | 27 ------------ runtime/src/memory/common.c | 16 ------- 3 files changed, 53 insertions(+), 43 deletions(-) create mode 100644 runtime/compiletime/table_instructions.c diff --git a/runtime/compiletime/table_instructions.c b/runtime/compiletime/table_instructions.c new file mode 100644 index 0000000..575f1c5 --- /dev/null +++ b/runtime/compiletime/table_instructions.c @@ -0,0 +1,53 @@ + +#include "types.h" + +/* This file contains the stub functions that the aWsm compiler expects + * This corresponds to awsm/src/codegen/runtime_stubs.rs + * This should be linked with the *.bc file generated by aWsm in order to compile a module as a *.so + */ + +extern thread_local struct wasm_module_instance current_wasm_module_instance; + +INLINE void +add_function_to_table(uint32_t idx, uint32_t type_id, char *pointer) +{ + assert(idx < INDIRECT_TABLE_SIZE); + assert(local_sandbox_context_cache.module_indirect_table != NULL); + + /* TODO: atomic for multiple concurrent invocations? Issue #97 */ + if (local_sandbox_context_cache.module_indirect_table[idx].type_id == type_id + && local_sandbox_context_cache.module_indirect_table[idx].func_pointer == pointer) + return; + + local_sandbox_context_cache.module_indirect_table[idx] = (struct indirect_table_entry){ + .type_id = type_id, .func_pointer = pointer + }; +} + +/* + * Table handling functionality + * This was moved from compiletime in order to place the + * function in the callstack in GDB. It can be moved back + * to runtime/compiletime/memory/64bit_nix.c to remove the + * additional function call + */ +INLINE char * +get_function_from_table(uint32_t idx, uint32_t type_id) +{ +#ifdef LOG_FUNCTION_TABLE + fprintf(stderr, "get_function_from_table(idx: %u, type_id: %u)\n", idx, type_id); + fprintf(stderr, "indirect_table_size: %u\n", INDIRECT_TABLE_SIZE); +#endif + assert(idx < INDIRECT_TABLE_SIZE); + + struct indirect_table_entry f = local_sandbox_context_cache.module_indirect_table[idx]; +#ifdef LOG_FUNCTION_TABLE + fprintf(stderr, "assumed type: %u, type in table: %u\n", type_id, f.type_id); +#endif + // FIXME: Commented out function type check because of gocr + // assert(f.type_id == type_id); + + assert(f.func_pointer != NULL); + + return f.func_pointer; +} diff --git a/runtime/src/memory/64bit_nix.c b/runtime/src/memory/64bit_nix.c index 498b7ae..4728236 100644 --- a/runtime/src/memory/64bit_nix.c +++ b/runtime/src/memory/64bit_nix.c @@ -89,30 +89,3 @@ instruction_memory_grow(uint32_t count) return rc; } -/* - * Table handling functionality - * This was moved from compiletime in order to place the - * function in the callstack in GDB. It can be moved back - * to runtime/compiletime/memory/64bit_nix.c to remove the - * additional function call - */ -char * -get_function_from_table(uint32_t idx, uint32_t type_id) -{ -#ifdef LOG_FUNCTION_TABLE - fprintf(stderr, "get_function_from_table(idx: %u, type_id: %u)\n", idx, type_id); - fprintf(stderr, "indirect_table_size: %u\n", INDIRECT_TABLE_SIZE); -#endif - assert(idx < INDIRECT_TABLE_SIZE); - - struct indirect_table_entry f = local_sandbox_context_cache.module_indirect_table[idx]; -#ifdef LOG_FUNCTION_TABLE - fprintf(stderr, "assumed type: %u, type in table: %u\n", type_id, f.type_id); -#endif - // FIXME: Commented out function type check because of gocr - // assert(f.type_id == type_id); - - assert(f.func_pointer != NULL); - - return f.func_pointer; -} diff --git a/runtime/src/memory/common.c b/runtime/src/memory/common.c index a3b4939..1c166aa 100644 --- a/runtime/src/memory/common.c +++ b/runtime/src/memory/common.c @@ -14,22 +14,6 @@ initialize_region(uint32_t offset, uint32_t data_count, char *data) memcpy(get_memory_ptr_for_runtime(offset, data_count), data, data_count); } -void -add_function_to_table(uint32_t idx, uint32_t type_id, char *pointer) -{ - assert(idx < INDIRECT_TABLE_SIZE); - assert(local_sandbox_context_cache.module_indirect_table != NULL); - - /* TODO: atomic for multiple concurrent invocations? Issue #97 */ - if (local_sandbox_context_cache.module_indirect_table[idx].type_id == type_id - && local_sandbox_context_cache.module_indirect_table[idx].func_pointer == pointer) - return; - - local_sandbox_context_cache.module_indirect_table[idx] = (struct indirect_table_entry){ - .type_id = type_id, .func_pointer = pointer - }; -} - /* If we are using runtime globals, we need to populate them */ WEAK void populate_globals()