chore: sandbox member cache->sandbox context cache

main
Sean McBride 4 years ago
parent fbea46b84f
commit 58a38016b1

@ -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);

@ -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;

@ -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,

@ -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;

@ -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;

@ -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
};
}

@ -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);

@ -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));

Loading…
Cancel
Save