diff --git a/runtime/include/sandbox_functions.h b/runtime/include/sandbox_functions.h index bc9dda3..3a2940f 100644 --- a/runtime/include/sandbox_functions.h +++ b/runtime/include/sandbox_functions.h @@ -12,7 +12,7 @@ * Public API * **************************/ -struct sandbox *sandbox_allocate(struct sandbox_request *sandbox_request); +struct sandbox *sandbox_new(struct sandbox_request *sandbox_request); void sandbox_free(struct sandbox *sandbox); void sandbox_main(struct sandbox *sandbox); void sandbox_switch_to(struct sandbox *next_sandbox); diff --git a/runtime/include/sandbox_set_as_initialized.h b/runtime/include/sandbox_set_as_initialized.h index 484fa52..c229238 100644 --- a/runtime/include/sandbox_set_as_initialized.h +++ b/runtime/include/sandbox_set_as_initialized.h @@ -38,15 +38,12 @@ sandbox_set_as_initialized(struct sandbox *sandbox, struct sandbox_request *sand /* Copy the socket descriptor and address of the client invocation */ memcpy(&sandbox->client_address, &sandbox_request->socket_address, sizeof(struct sockaddr)); - - /* Initialize the sandbox's context, stack, and instruction pointer */ - /* stack.start points to the bottom of the usable stack, so add stack_size to get to top */ - arch_context_init(&sandbox->ctxt, (reg_t)current_sandbox_start, - (reg_t)sandbox->stack.start + sandbox->stack.size); - /* Initialize Parsec control structures */ ps_list_init_d(sandbox); + /* Allocations require the module to be set */ + sandbox->module = sandbox_request->module; + module_acquire(sandbox->module); /* State Change Bookkeeping */ sandbox->duration_of_state[SANDBOX_ALLOCATED] = now - allocation_timestamp; @@ -54,4 +51,17 @@ sandbox_set_as_initialized(struct sandbox *sandbox, struct sandbox_request *sand sandbox->timestamp_of.last_state_change = allocation_timestamp; sandbox_state_history_append(sandbox, SANDBOX_INITIALIZED); runtime_sandbox_total_increment(SANDBOX_INITIALIZED); + +#ifdef LOG_STATE_CHANGES + sandbox->state_history_count = 0; + sandbox->state_history[sandbox->state_history_count++] = SANDBOX_ALLOCATED; + memset(&sandbox->state_history, 0, SANDBOX_STATE_HISTORY_CAPACITY * sizeof(sandbox_state_t)); +#endif +} + + +static inline void +sandbox_init(struct sandbox *sandbox, struct sandbox_request *sandbox_request, uint64_t allocation_timestamp) +{ + sandbox_set_as_initialized(sandbox, sandbox_request, allocation_timestamp); } diff --git a/runtime/include/scheduler.h b/runtime/include/scheduler.h index 549f70a..9f2c129 100644 --- a/runtime/include/scheduler.h +++ b/runtime/include/scheduler.h @@ -86,7 +86,7 @@ scheduler_edf_get_next() if (global_request_scheduler_remove_if_earlier(&request, local_deadline) == 0) { assert(request != NULL); assert(request->absolute_deadline < local_deadline); - struct sandbox *global = sandbox_allocate(request); + struct sandbox *global = sandbox_new(request); if (!global) goto err_allocate; assert(global->state == SANDBOX_INITIALIZED); @@ -115,7 +115,7 @@ scheduler_fifo_get_next() /* If the local runqueue is empty, pull from global request scheduler */ if (global_request_scheduler_remove(&sandbox_request) < 0) goto err; - sandbox = sandbox_allocate(sandbox_request); + sandbox = sandbox_new(sandbox_request); if (!sandbox) goto err_allocate; sandbox_set_as_runnable(sandbox, SANDBOX_INITIALIZED); diff --git a/runtime/include/wasm_memory.h b/runtime/include/wasm_memory.h index d56ff32..d5a6fd5 100644 --- a/runtime/include/wasm_memory.h +++ b/runtime/include/wasm_memory.h @@ -10,7 +10,7 @@ #include "types.h" /* PAGE_SIZE */ #include "wasm_types.h" -#define wasm_memory_MAX (size_t) UINT32_MAX + 1 +#define WASM_MEMORY_MAX (size_t) UINT32_MAX + 1 struct wasm_memory { size_t size; /* Initial Size in bytes */ @@ -28,7 +28,7 @@ wasm_memory_allocate(size_t initial, size_t max) assert(max <= (size_t)UINT32_MAX + 1); /* Allocate contiguous virtual addresses for struct, full linear memory, and guard page */ - size_t size_to_alloc = sizeof(struct wasm_memory) + wasm_memory_MAX + /* guard page */ PAGE_SIZE; + size_t size_to_alloc = sizeof(struct wasm_memory) + WASM_MEMORY_MAX + /* guard page */ PAGE_SIZE; void * temp = mmap(NULL, size_to_alloc, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (temp == MAP_FAILED) { fprintf(stderr, "wasm_memory_allocate - allocation failed, (size: %lu) %s\n", size_to_alloc, diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index 5f498b7..15684d6 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -87,6 +87,17 @@ sandbox_allocate_http_buffers(struct sandbox *self) return 0; } +static inline struct sandbox * +sandbox_allocate(void) +{ + struct sandbox *sandbox = NULL; + size_t page_aligned_sandbox_size = round_up_to_page(sizeof(struct sandbox)); + sandbox = calloc(1, page_aligned_sandbox_size); + sandbox->state = SANDBOX_ALLOCATED; + return sandbox; +} + + /** * Allocates a new sandbox from a sandbox request * Frees the sandbox request on success @@ -94,7 +105,7 @@ sandbox_allocate_http_buffers(struct sandbox *self) * @returns sandbox * on success, NULL on error */ struct sandbox * -sandbox_allocate(struct sandbox_request *sandbox_request) +sandbox_new(struct sandbox_request *sandbox_request) { /* Validate Arguments */ assert(sandbox_request != NULL); @@ -104,13 +115,12 @@ sandbox_allocate(struct sandbox_request *sandbox_request) int rc; - struct sandbox *sandbox = NULL; - size_t page_aligned_sandbox_size = round_up_to_page(sizeof(struct sandbox)); - sandbox = calloc(1, page_aligned_sandbox_size); + struct sandbox *sandbox = sandbox_allocate(); if (sandbox == NULL) goto err_struct_allocation_failed; - /* Set state to initializing */ - sandbox_set_as_initialized(sandbox, sandbox_request, now); + sandbox_init(sandbox, sandbox_request, now); + + free(sandbox_request); if (sandbox_allocate_http_buffers(sandbox)) { error_message = "failed to allocate http buffers"; @@ -128,18 +138,12 @@ sandbox_allocate(struct sandbox_request *sandbox_request) error_message = "failed to allocate sandbox stack"; goto err_stack_allocation_failed; } - sandbox->state = SANDBOX_ALLOCATED; - -#ifdef LOG_STATE_CHANGES - sandbox->state_history_count = 0; - sandbox->state_history[sandbox->state_history_count++] = SANDBOX_ALLOCATED; - memset(&sandbox->state_history, 0, SANDBOX_STATE_HISTORY_CAPACITY * sizeof(sandbox_state_t)); -#endif - /* Set state to initializing */ - sandbox_set_as_initialized(sandbox, sandbox_request, now); + /* Initialize the sandbox's context, stack, and instruction pointer */ + /* stack.start points to the bottom of the usable stack, so add stack_size to get to top */ + arch_context_init(&sandbox->ctxt, (reg_t)current_sandbox_start, + (reg_t)sandbox->stack.start + sandbox->stack.size); - free(sandbox_request); done: return sandbox; err_stack_allocation_failed: @@ -195,7 +199,7 @@ sandbox_free(struct sandbox *sandbox) */ /* Linear Memory and Guard Page should already have been munmaped and set to NULL */ - assert(sandbox->memory->data == NULL); + assert(sandbox->memory == NULL); errno = 0; unsigned long size_to_unmap = round_up_to_page(sizeof(struct sandbox)) + sandbox->module->max_request_size