Skip to content

Commit

Permalink
refactor: wasm_memory
Browse files Browse the repository at this point in the history
  • Loading branch information
bushidocodes committed Dec 10, 2021
1 parent 4b1f991 commit 3cf4580
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 121 deletions.
86 changes: 57 additions & 29 deletions runtime/compiletime/memory_instructions.c
Original file line number Diff line number Diff line change
@@ -1,48 +1,60 @@
#include <assert.h>

#include "wasm_module_instance.h"
#include "current_wasm_module_instance.h"

INLINE uint32_t
instruction_memory_size()
{
return wasm_memory_get_page_count(current_wasm_module_instance.memory);
return (uint32_t)(current_wasm_module_instance.memory.size / WASM_PAGE_SIZE);
}

// All of these are pretty generic
// These functions are equivalent to those in wasm_memory.h, but they minimize pointer dereferencing
INLINE float
get_f32(uint32_t offset)
{
return wasm_memory_get_f32(current_wasm_module_instance.memory, offset);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(float) <= current_wasm_module_instance.memory.size);
return *(float *)&current_wasm_module_instance.memory.buffer[offset];
}

INLINE double
get_f64(uint32_t offset)
{
return wasm_memory_get_f64(current_wasm_module_instance.memory, offset);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(double) <= current_wasm_module_instance.memory.size);
return *(double *)&current_wasm_module_instance.memory.buffer[offset];
}

INLINE int8_t
get_i8(uint32_t offset)
{
return wasm_memory_get_i8(current_wasm_module_instance.memory, offset);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(int8_t) <= current_wasm_module_instance.memory.size);
return *(int8_t *)&current_wasm_module_instance.memory.buffer[offset];
}

INLINE int16_t
get_i16(uint32_t offset)
{
return wasm_memory_get_i16(current_wasm_module_instance.memory, offset);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(int16_t) <= current_wasm_module_instance.memory.size);
return *(int16_t *)&current_wasm_module_instance.memory.buffer[offset];
}

INLINE int32_t
get_i32(uint32_t offset)
{
return wasm_memory_get_i32(current_wasm_module_instance.memory, offset);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(int32_t) <= current_wasm_module_instance.memory.size);
return *(int32_t *)&current_wasm_module_instance.memory.buffer[offset];
}

INLINE int64_t
get_i64(uint32_t offset)
{
return wasm_memory_get_i64(current_wasm_module_instance.memory, offset);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(int64_t) <= current_wasm_module_instance.memory.size);
return *(int64_t *)&current_wasm_module_instance.memory.buffer[offset];
}

INLINE int32_t
Expand All @@ -59,51 +71,65 @@ get_global_i64(uint32_t offset)

// Now setting routines
INLINE void
set_f32(uint32_t offset, float v)
set_f32(uint32_t offset, float value)
{
wasm_memory_set_f32(current_wasm_module_instance.memory, offset, v);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(float) <= current_wasm_module_instance.memory.size);
*(float *)&current_wasm_module_instance.memory.buffer[offset] = value;
}

INLINE void
set_f64(uint32_t offset, double v)
set_f64(uint32_t offset, double value)
{
wasm_memory_set_f64(current_wasm_module_instance.memory, offset, v);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(double) <= current_wasm_module_instance.memory.size);
*(double *)&current_wasm_module_instance.memory.buffer[offset] = value;
}

INLINE void
set_i8(uint32_t offset, int8_t v)
set_i8(uint32_t offset, int8_t value)
{
wasm_memory_set_i8(current_wasm_module_instance.memory, offset, v);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(int8_t) <= current_wasm_module_instance.memory.size);
*(int8_t *)&current_wasm_module_instance.memory.buffer[offset] = value;
}

INLINE void
set_i16(uint32_t offset, int16_t v)
set_i16(uint32_t offset, int16_t value)
{
wasm_memory_set_i16(current_wasm_module_instance.memory, offset, v);
assert(current_wasm_module_instance.memory.buffer != NULL);

assert(offset + sizeof(int16_t) <= current_wasm_module_instance.memory.size);
*(int16_t *)&current_wasm_module_instance.memory.buffer[offset] = value;
}

INLINE void
set_i32(uint32_t offset, int32_t v)
set_i32(uint32_t offset, int32_t value)
{
wasm_memory_set_i32(current_wasm_module_instance.memory, offset, v);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(int32_t) <= current_wasm_module_instance.memory.size);
*(int32_t *)&current_wasm_module_instance.memory.buffer[offset] = value;
}

INLINE void
set_i64(uint32_t offset, int64_t v)
set_i64(uint32_t offset, int64_t value)
{
wasm_memory_set_i64(current_wasm_module_instance.memory, offset, v);
assert(current_wasm_module_instance.memory.buffer != NULL);
assert(offset + sizeof(int64_t) <= current_wasm_module_instance.memory.size);
*(int64_t *)&current_wasm_module_instance.memory.buffer[offset] = value;
}

INLINE void
set_global_i32(uint32_t offset, int32_t v)
set_global_i32(uint32_t offset, int32_t value)
{
set_i32(offset, v);
set_i32(offset, value);
}

INLINE void
set_global_i64(uint32_t offset, int64_t v)
set_global_i64(uint32_t offset, int64_t value)
{
set_i64(offset, v);
set_i64(offset, value);
}

/**
Expand All @@ -115,12 +141,15 @@ set_global_i64(uint32_t offset, int64_t v)
INLINE int32_t
instruction_memory_grow(uint32_t count)
{
int old_page_count = current_wasm_module_instance.memory->size / WASM_PAGE_SIZE;
int old_page_count = current_wasm_module_instance.memory.size / WASM_PAGE_SIZE;

/* Return -1 if we've hit the linear memory max */
int rc = wasm_memory_expand(current_wasm_module_instance.memory, WASM_PAGE_SIZE * count);
int rc = wasm_memory_expand(&current_wasm_module_instance.memory, WASM_PAGE_SIZE * count);
if (unlikely(rc == -1)) return -1;

/* We updated "forked state" in current_wasm_module_instance.memory. We need to write this back to persist */
current_wasm_module_instance_memory_writeback();

#ifdef LOG_SANDBOX_MEMORY_PROFILE
// Cache the runtime of the first N page allocations
for (int i = 0; i < count; i++) {
Expand All @@ -131,13 +160,12 @@ instruction_memory_grow(uint32_t count)
}
}
#endif

return rc;
}


INLINE void
initialize_region(uint32_t offset, uint32_t region_size, uint8_t region[region_size])
{
wasm_memory_initialize_region(current_wasm_module_instance.memory, offset, region_size, region);
wasm_memory_initialize_region(&current_wasm_module_instance.memory, offset, region_size, region);
}
28 changes: 16 additions & 12 deletions runtime/include/current_sandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <threads.h>

#include "sandbox_types.h"
#include "wasm_module_instance.h"
#include "current_wasm_module_instance.h"

/* current sandbox that is active.. */
extern thread_local struct sandbox *worker_thread_current_sandbox;
Expand All @@ -30,16 +30,20 @@ current_sandbox_set(struct sandbox *sandbox)
/* Unpack hierarchy to avoid pointer chasing */
if (sandbox == NULL) {
current_wasm_module_instance = (struct wasm_module_instance){
.memory = NULL,
.table = NULL,
.memory =
(struct wasm_memory){
.size = 0,
.capacity = 0,
.max = 0,
.buffer = NULL,
},
.table = NULL,
};
worker_thread_current_sandbox = NULL;
runtime_worker_threads_deadline[worker_thread_idx] = UINT64_MAX;
} else {
current_wasm_module_instance = (struct wasm_module_instance){
.memory = sandbox->memory,
.table = sandbox->module->indirect_table,
};
memcpy(&current_wasm_module_instance.memory, sandbox->memory, sizeof(struct wasm_memory));
current_wasm_module_instance.table = sandbox->module->indirect_table,
worker_thread_current_sandbox = sandbox;
runtime_worker_threads_deadline[worker_thread_idx] = sandbox->absolute_deadline;
}
Expand All @@ -51,19 +55,19 @@ extern void current_sandbox_sleep();
static inline void *
current_sandbox_get_ptr_void(uint32_t offset, uint32_t bounds_check)
{
assert(current_wasm_module_instance.memory != NULL);
return wasm_memory_get_ptr_void(current_wasm_module_instance.memory, offset, bounds_check);
assert(current_wasm_module_instance.memory.capacity > 0);
return wasm_memory_get_ptr_void(&current_wasm_module_instance.memory, offset, bounds_check);
}

static inline char
current_sandbox_get_char(uint32_t offset)
{
assert(current_wasm_module_instance.memory != NULL);
return wasm_memory_get_char(current_wasm_module_instance.memory, offset);
assert(current_wasm_module_instance.memory.capacity > 0);
return wasm_memory_get_char(&current_wasm_module_instance.memory, offset);
}

static inline char *
current_sandbox_get_string(uint32_t offset, uint32_t size)
{
return wasm_memory_get_string(current_wasm_module_instance.memory, offset, size);
return wasm_memory_get_string(&current_wasm_module_instance.memory, offset, size);
}
7 changes: 7 additions & 0 deletions runtime/include/current_wasm_module_instance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "wasm_module_instance.h"

extern thread_local struct wasm_module_instance current_wasm_module_instance;

extern void current_wasm_module_instance_memory_writeback(void);
2 changes: 1 addition & 1 deletion runtime/include/sandbox_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ sandbox_close_http(struct sandbox *sandbox)
static inline void
sandbox_free_linear_memory(struct sandbox *sandbox)
{
wasm_memory_free(sandbox->memory);
wasm_memory_delete(sandbox->memory);
sandbox->memory = NULL;
}

Expand Down
7 changes: 6 additions & 1 deletion runtime/include/sandbox_setup_arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ sandbox_setup_arguments(struct sandbox *sandbox)
* WASI, so not worth fixing*/

sandbox->arguments_offset = wasm_memory_get_size(sandbox->memory);
int rc = wasm_memory_expand(sandbox->memory, WASM_PAGE_SIZE);

/* Assumption: we can fit the arguments in a single wasm page */
int rc = wasm_memory_expand(sandbox->memory, WASM_PAGE_SIZE);
assert(rc == 0);

/* We have to update our cache here */
memcpy(&current_wasm_module_instance.memory, sandbox->memory, sizeof(struct wasm_memory));

stub_init(sandbox->arguments_offset);
}
Loading

0 comments on commit 3cf4580

Please sign in to comment.