From 58a38016b112dc5c7a4a0aaa0fcad2f74a757b3c Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 6 Jul 2020 09:47:35 -0400 Subject: [PATCH] chore: sandbox member cache->sandbox context cache --- runtime/compiletime/memory/64bit_nix.c | 34 +++++++++++++------------- runtime/include/types.h | 4 +-- runtime/src/current_sandbox.c | 6 ++--- runtime/src/libc/uvio.c | 2 +- runtime/src/memory/64bit_nix.c | 20 +++++++-------- runtime/src/memory/common.c | 12 ++++----- runtime/src/module.c | 8 +++--- runtime/src/sandbox.c | 4 +-- 8 files changed, 45 insertions(+), 45 deletions(-) diff --git a/runtime/compiletime/memory/64bit_nix.c b/runtime/compiletime/memory/64bit_nix.c index 892d374..4734eea 100644 --- a/runtime/compiletime/memory/64bit_nix.c +++ b/runtime/compiletime/memory/64bit_nix.c @@ -7,7 +7,7 @@ INLINE float get_f32(i32 offset) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; return *(float *)address; @@ -16,7 +16,7 @@ get_f32(i32 offset) INLINE double get_f64(i32 offset) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; return *(double *)address; @@ -25,7 +25,7 @@ get_f64(i32 offset) INLINE i8 get_i8(i32 offset) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; return *(i8 *)address; @@ -34,7 +34,7 @@ get_i8(i32 offset) INLINE i16 get_i16(i32 offset) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; return *(i16 *)address; @@ -43,7 +43,7 @@ get_i16(i32 offset) INLINE i32 get_i32(i32 offset) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; return *(i32 *)address; @@ -52,7 +52,7 @@ get_i32(i32 offset) INLINE i64 get_i64(i32 offset) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; return *(i64 *)address; @@ -61,7 +61,7 @@ get_i64(i32 offset) INLINE i32 get_global_i32(i32 offset) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; return *(i32 *)address; @@ -70,7 +70,7 @@ get_global_i32(i32 offset) INLINE i64 get_global_i64(i32 offset) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; return *(i64 *)address; @@ -80,7 +80,7 @@ get_global_i64(i32 offset) INLINE void set_f32(i32 offset, float v) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; *(float *)address = v; @@ -89,7 +89,7 @@ set_f32(i32 offset, float v) INLINE void set_f64(i32 offset, double v) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; *(double *)address = v; @@ -98,7 +98,7 @@ set_f64(i32 offset, double v) INLINE void set_i8(i32 offset, i8 v) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; *(i8 *)address = v; @@ -107,7 +107,7 @@ set_i8(i32 offset, i8 v) INLINE void set_i16(i32 offset, i16 v) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; *(i16 *)address = v; @@ -116,7 +116,7 @@ set_i16(i32 offset, i16 v) INLINE void set_i32(i32 offset, i32 v) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; *(i32 *)address = v; @@ -125,7 +125,7 @@ set_i32(i32 offset, i32 v) INLINE void set_i64(i32 offset, i64 v) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; *(i64 *)address = v; @@ -134,7 +134,7 @@ set_i64(i32 offset, i64 v) INLINE void set_global_i32(i32 offset, i32 v) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; *(i32 *)address = v; @@ -143,7 +143,7 @@ set_global_i32(i32 offset, i32 v) INLINE void set_global_i64(i32 offset, i64 v) { - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; void *address = &mem_as_chars[offset]; *(i64 *)address = v; @@ -155,7 +155,7 @@ get_function_from_table(u32 idx, u32 type_id) { assert(idx < INDIRECT_TABLE_SIZE); - struct indirect_table_entry f = local_sandbox_member_cache.module_indirect_table[idx]; + struct indirect_table_entry f = local_sandbox_context_cache.module_indirect_table[idx]; // assert(f.type_id == type_id); assert(f.func_pointer); diff --git a/runtime/include/types.h b/runtime/include/types.h index a8f50f5..a16d21b 100644 --- a/runtime/include/types.h +++ b/runtime/include/types.h @@ -74,13 +74,13 @@ struct indirect_table_entry { }; /* Cache of Frequently Accessed Members used to avoid pointer chasing */ -struct sandbox_member_cache { +struct sandbox_context_cache { struct indirect_table_entry *module_indirect_table; void * linear_memory_start; u32 linear_memory_size; }; -extern __thread struct sandbox_member_cache local_sandbox_member_cache; +extern __thread struct sandbox_context_cache local_sandbox_context_cache; /* TODO: LOG_TO_FILE logic is untested */ extern i32 runtime_log_file_descriptor; diff --git a/runtime/src/current_sandbox.c b/runtime/src/current_sandbox.c index 2262010..f73cc11 100644 --- a/runtime/src/current_sandbox.c +++ b/runtime/src/current_sandbox.c @@ -4,7 +4,7 @@ /* current sandbox that is active.. */ static __thread sandbox_t *worker_thread_current_sandbox = NULL; -__thread struct sandbox_member_cache local_sandbox_member_cache = { +__thread struct sandbox_context_cache local_sandbox_context_cache = { .linear_memory_start = NULL, .linear_memory_size = 0, .module_indirect_table = NULL, @@ -29,14 +29,14 @@ current_sandbox_set(struct sandbox *sandbox) { /* Unpack hierarchy to avoid pointer chasing */ if (sandbox == NULL) { - local_sandbox_member_cache = (struct sandbox_member_cache){ + local_sandbox_context_cache = (struct sandbox_context_cache){ .linear_memory_start = NULL, .linear_memory_size = 0, .module_indirect_table = NULL, }; worker_thread_current_sandbox = NULL; } else { - local_sandbox_member_cache = (struct sandbox_member_cache){ + local_sandbox_context_cache = (struct sandbox_context_cache){ .linear_memory_start = sandbox->linear_memory_start, .linear_memory_size = sandbox->linear_memory_size, .module_indirect_table = sandbox->module->indirect_table, diff --git a/runtime/src/libc/uvio.c b/runtime/src/libc/uvio.c index 83fc039..5fd5ae0 100644 --- a/runtime/src/libc/uvio.c +++ b/runtime/src/libc/uvio.c @@ -458,7 +458,7 @@ wasm_mmap(i32 addr, i32 len, i32 prot, i32 flags, i32 file_descriptor, i32 offse assert(len % WASM_PAGE_SIZE == 0); - i32 result = local_sandbox_member_cache.linear_memory_size; + i32 result = local_sandbox_context_cache.linear_memory_size; for (int i = 0; i < len / WASM_PAGE_SIZE; i++) { expand_memory(); } return result; diff --git a/runtime/src/memory/64bit_nix.c b/runtime/src/memory/64bit_nix.c index aea3075..286ede4 100644 --- a/runtime/src/memory/64bit_nix.c +++ b/runtime/src/memory/64bit_nix.c @@ -29,23 +29,23 @@ expand_memory(void) struct sandbox *sandbox = current_sandbox_get(); // max_pages = 0 => no limit: FIXME - assert((sandbox->sandbox_size + local_sandbox_member_cache.linear_memory_size) / WASM_PAGE_SIZE + assert((sandbox->sandbox_size + local_sandbox_context_cache.linear_memory_size) / WASM_PAGE_SIZE < WASM_MAX_PAGES); // Remap the relevant wasm page to readable - char *mem_as_chars = local_sandbox_member_cache.linear_memory_start; - char *page_address = &mem_as_chars[local_sandbox_member_cache.linear_memory_size]; + char *mem_as_chars = local_sandbox_context_cache.linear_memory_start; + char *page_address = &mem_as_chars[local_sandbox_context_cache.linear_memory_size]; void *map_result = mmap(page_address, WASM_PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); // TODO: Refactor to return RC signifying out-of-mem to caller if (map_result == MAP_FAILED) panic("Mapping of new memory failed"); - if (local_sandbox_member_cache.linear_memory_size > sandbox->linear_memory_max_size) + if (local_sandbox_context_cache.linear_memory_size > sandbox->linear_memory_max_size) panic("expand_memory - Out of Memory!\n"); - local_sandbox_member_cache.linear_memory_size += WASM_PAGE_SIZE; - // local_sandbox_member_cache is "forked state", so update authoritative member - sandbox->linear_memory_size = local_sandbox_member_cache.linear_memory_size; + local_sandbox_context_cache.linear_memory_size += WASM_PAGE_SIZE; + // local_sandbox_context_cache is "forked state", so update authoritative member + sandbox->linear_memory_size = local_sandbox_context_cache.linear_memory_size; } INLINE char * @@ -54,10 +54,10 @@ get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) // Due to how we setup memory for x86, the virtual memory mechanism will catch the error, if bounds < // WASM_PAGE_SIZE assert(bounds_check < WASM_PAGE_SIZE - || (local_sandbox_member_cache.linear_memory_size > bounds_check - && offset <= local_sandbox_member_cache.linear_memory_size - bounds_check)); + || (local_sandbox_context_cache.linear_memory_size > bounds_check + && offset <= local_sandbox_context_cache.linear_memory_size - bounds_check)); - char *mem_as_chars = (char *)local_sandbox_member_cache.linear_memory_start; + char *mem_as_chars = (char *)local_sandbox_context_cache.linear_memory_start; char *address = &mem_as_chars[offset]; return address; diff --git a/runtime/src/memory/common.c b/runtime/src/memory/common.c index 71bd879..7a54df7 100644 --- a/runtime/src/memory/common.c +++ b/runtime/src/memory/common.c @@ -5,8 +5,8 @@ EXPORT void initialize_region(u32 offset, u32 data_count, char *data) { - assert(local_sandbox_member_cache.linear_memory_size >= data_count); - assert(offset < local_sandbox_member_cache.linear_memory_size - data_count); + assert(local_sandbox_context_cache.linear_memory_size >= data_count); + assert(offset < local_sandbox_context_cache.linear_memory_size - data_count); /* FIXME: Hack around segmented and unsegmented access */ memcpy(get_memory_ptr_for_runtime(offset, data_count), data, data_count); @@ -16,14 +16,14 @@ void add_function_to_table(u32 idx, u32 type_id, char *pointer) { assert(idx < INDIRECT_TABLE_SIZE); - assert(local_sandbox_member_cache.module_indirect_table != NULL); + assert(local_sandbox_context_cache.module_indirect_table != NULL); /* TODO: atomic for multiple concurrent invocations? */ - if (local_sandbox_member_cache.module_indirect_table[idx].type_id == type_id - && local_sandbox_member_cache.module_indirect_table[idx].func_pointer == pointer) + 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_member_cache.module_indirect_table[idx] = (struct indirect_table_entry){ + local_sandbox_context_cache.module_indirect_table[idx] = (struct indirect_table_entry){ .type_id = type_id, .func_pointer = pointer }; } diff --git a/runtime/src/module.c b/runtime/src/module.c index 6a26638..ba41637 100644 --- a/runtime/src/module.c +++ b/runtime/src/module.c @@ -154,17 +154,17 @@ module_new(char *name, char *path, i32 argument_count, u32 stack_size, u32 max_m * initialization is complete. * * assumption: This approach depends on module_new only being invoked at program start before preemption is - * enabled. We are check that local_sandbox_member_cache.module_indirect_table is NULL to gain confidence that + * enabled. We are check that local_sandbox_context_cache.module_indirect_table is NULL to gain confidence that * we are not invoking this in a way that clobbers a current module. * * If we want to be able to do this later, we can possibly defer module_initialize_table until the first * invocation */ - assert(local_sandbox_member_cache.module_indirect_table == NULL); - local_sandbox_member_cache.module_indirect_table = module->indirect_table; + assert(local_sandbox_context_cache.module_indirect_table == NULL); + local_sandbox_context_cache.module_indirect_table = module->indirect_table; module_initialize_table(module); - local_sandbox_member_cache.module_indirect_table = NULL; + local_sandbox_context_cache.module_indirect_table = NULL; /* Add the module to the in-memory module DB */ module_database_add(module); diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index 4328242..993d23e 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -24,8 +24,8 @@ sandbox_setup_arguments(struct sandbox *sandbox) i32 argument_count = module_get_argument_count(sandbox->module); /* whatever gregor has, to be able to pass arguments to a module! */ - sandbox->arguments_offset = local_sandbox_member_cache.linear_memory_size; - assert(local_sandbox_member_cache.linear_memory_start == sandbox->linear_memory_start); + sandbox->arguments_offset = local_sandbox_context_cache.linear_memory_size; + assert(local_sandbox_context_cache.linear_memory_start == sandbox->linear_memory_start); expand_memory(); i32 *array_ptr = worker_thread_get_memory_ptr_void(sandbox->arguments_offset, argument_count * sizeof(i32));