refactor: Indirect table

mmap-opt
Sean McBride 4 years ago
parent ef09fcc3f1
commit 6b043666f6

@ -105,8 +105,6 @@ INCLUDES += -Iinclude/ -Ithirdparty/dist/include/
CFILES += src/*.c
CFILES += src/arch/${ARCH}/*.c
CFILES += src/libc/*.c
CFILES += src/memory/common.c
CFILES += src/memory/64bit_nix.c
CFILES += thirdparty/dist/lib/http_parser.o
# Configuring Jasmine

@ -128,48 +128,16 @@ set_i64(uint32_t offset, int64_t v)
EXPORT 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
};
wasm_indirect_table_set(local_sandbox_context_cache.module_indirect_table, idx, type_id, 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
*/
char *
/* char * is used as a generic pointer to a function pointer */
EXPORT 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;
return wasm_indirect_table_get(local_sandbox_context_cache.module_indirect_table, idx, type_id);
}
EXPORT int32_t
get_global_i32(uint32_t offset)
{

@ -56,7 +56,7 @@ struct module {
struct awsm_abi abi;
_Atomic uint32_t reference_count; /* ref count how many instances exist here. */
struct indirect_table_entry indirect_table[INDIRECT_TABLE_SIZE];
struct wasm_indirect_table *indirect_table;
struct pool ** linear_memory_pool;
};

@ -4,8 +4,8 @@
/* Cache of Frequently Accessed Members used to avoid pointer chasing */
struct sandbox_context_cache {
struct wasm_linear_memory * memory;
struct indirect_table_entry *module_indirect_table;
struct wasm_linear_memory * memory;
struct wasm_indirect_table *module_indirect_table;
};
extern thread_local struct sandbox_context_cache local_sandbox_context_cache;

@ -5,7 +5,59 @@
/* memory also provides the table access functions */
#define INDIRECT_TABLE_SIZE (1 << 10)
struct indirect_table_entry {
struct wasm_indirect_table_entry {
uint32_t type_id;
void * func_pointer;
};
struct wasm_indirect_table {
uint32_t length;
uint32_t capacity;
struct wasm_indirect_table_entry data[];
};
static inline struct wasm_indirect_table *
wasm_indirect_table_allocate(size_t capacity)
{
struct wasm_indirect_table *self = (struct wasm_indirect_table *)malloc(
sizeof(struct wasm_indirect_table) + capacity * sizeof(struct wasm_indirect_table_entry));
self->capacity = capacity;
self->length = 0;
return self;
}
static inline void
wasm_indirect_table_free(struct wasm_indirect_table *self)
{
assert(self != NULL);
free(self);
}
static inline char *
wasm_indirect_table_get(struct wasm_indirect_table *self, uint32_t idx, uint32_t type_id)
{
assert(self != NULL);
assert(idx < self->capacity);
struct wasm_indirect_table_entry f = self->data[idx];
// FIXME: Commented out function type check because of gocr
// assert(f.type_id == type_id);
assert(f.func_pointer != NULL);
return f.func_pointer;
}
static inline void
wasm_indirect_table_set(struct wasm_indirect_table *self, uint32_t idx, uint32_t type_id, char *pointer)
{
assert(self != NULL);
assert(idx < self->capacity);
/* TODO: atomic for multiple concurrent invocations? Issue #97 */
if (self->data[idx].type_id == type_id && self->data[idx].func_pointer == pointer) return;
self->data[idx] = (struct wasm_indirect_table_entry){ .type_id = type_id, .func_pointer = pointer };
}

@ -16,6 +16,7 @@
#include "panic.h"
#include "runtime.h"
#include "scheduler.h"
#include "wasm_indirect_table.h"
const int JSON_MAX_ELEMENT_COUNT = 16;
const int JSON_MAX_ELEMENT_SIZE = 1024;
@ -188,6 +189,11 @@ module_new(char *name, char *path, uint32_t stack_size, uint32_t max_memory, uin
admissions_info_initialize(&module->admissions_info, admissions_percentile, expected_execution,
module->relative_deadline);
/* WebAssembly Indirect Table */
/* TODO: Should this be part of the module or per-sandbox? */
/* TODO: How should this table be sized? */
module->indirect_table = wasm_indirect_table_allocate(INDIRECT_TABLE_SIZE);
/* Request Response Buffer */
if (request_size == 0) request_size = MODULE_DEFAULT_REQUEST_RESPONSE_SIZE;
if (response_size == 0) response_size = MODULE_DEFAULT_REQUEST_RESPONSE_SIZE;

Loading…
Cancel
Save