parent
5efee76cae
commit
f4ede43b52
@ -1,42 +1,17 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "wasm_store.h"
|
||||
|
||||
extern thread_local struct wasm_module_instance current_wasm_module_instance;
|
||||
|
||||
INLINE 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_table_set(current_wasm_module_instance.table, idx, type_id, pointer);
|
||||
}
|
||||
|
||||
/* char * is used as a generic pointer to a function pointer */
|
||||
INLINE 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_table_get(current_wasm_module_instance.table, idx, type_id);
|
||||
}
|
||||
|
@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "current_sandbox.h"
|
||||
#include "http.h"
|
||||
#include "http_total.h"
|
||||
#include "likely.h"
|
||||
#include "sandbox_types.h"
|
||||
#include "scheduler.h"
|
||||
#include "panic.h"
|
||||
|
||||
/**
|
||||
* Sends Response Back to Client
|
||||
* @return RC. -1 on Failure
|
||||
*/
|
||||
static inline int
|
||||
current_sandbox_send_response()
|
||||
{
|
||||
struct sandbox *sandbox = current_sandbox_get();
|
||||
assert(sandbox != NULL);
|
||||
|
||||
int rc;
|
||||
ssize_t sent = 0;
|
||||
|
||||
/* Determine values to template into our HTTP response */
|
||||
ssize_t response_body_size = sandbox->response.length;
|
||||
char * module_content_type = sandbox->module->response_content_type;
|
||||
const char *content_type = strlen(module_content_type) > 0 ? module_content_type : "text/plain";
|
||||
|
||||
/* Capture Timekeeping data for end-to-end latency */
|
||||
uint64_t end_time = __getcycles();
|
||||
sandbox->total_time = end_time - sandbox->timestamp_of.request_arrival;
|
||||
|
||||
/* Generate and send HTTP Response Headers */
|
||||
ssize_t response_header_size = http_response_200_size(content_type, response_body_size);
|
||||
char response_header_buffer[response_header_size + 1];
|
||||
rc = http_header_200_build(response_header_buffer, content_type, response_body_size);
|
||||
if (rc <= 0) {
|
||||
perror("sprintf");
|
||||
goto err;
|
||||
}
|
||||
client_socket_send(sandbox->client_socket_descriptor, response_header_buffer, response_header_size,
|
||||
current_sandbox_sleep);
|
||||
|
||||
/* Send HTTP Response Body */
|
||||
client_socket_send(sandbox->client_socket_descriptor, sandbox->response.base, response_body_size,
|
||||
current_sandbox_sleep);
|
||||
|
||||
http_total_increment_2xx();
|
||||
rc = 0;
|
||||
|
||||
done:
|
||||
return rc;
|
||||
err:
|
||||
debuglog("Error sending to client: %s", strerror(errno));
|
||||
rc = -1;
|
||||
goto done;
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "current_sandbox.h"
|
||||
#include "http.h"
|
||||
#include "http_total.h"
|
||||
#include "likely.h"
|
||||
#include "sandbox_types.h"
|
||||
#include "scheduler.h"
|
||||
#include "panic.h"
|
||||
|
||||
/**
|
||||
* Sends Response Back to Client
|
||||
* @return RC. -1 on Failure
|
||||
*/
|
||||
static inline int
|
||||
sandbox_send_response(struct sandbox *sandbox)
|
||||
{
|
||||
assert(sandbox != NULL);
|
||||
/* Assumption: The HTTP Request Buffer immediately precedes the HTTP Response Buffer,
|
||||
* meaning that when we prepend, we are overwritting the tail of the HTTP request buffer */
|
||||
assert(sandbox->request.base + sandbox->module->max_request_size == sandbox->response.base);
|
||||
|
||||
int rc;
|
||||
|
||||
/* Determine values to template into our HTTP response */
|
||||
ssize_t response_body_size = sandbox->response.length;
|
||||
char content_length[20] = { 0 };
|
||||
sprintf(content_length, "%zu", response_body_size);
|
||||
char *module_content_type = sandbox->module->response_content_type;
|
||||
char *content_type = strlen(module_content_type) > 0 ? module_content_type : "text/plain";
|
||||
|
||||
/* Prepend HTTP Response Headers */
|
||||
size_t response_header_size = http_response_200_size(content_type, content_length);
|
||||
char * response_header = sandbox->response.base - response_header_size;
|
||||
rc = http_response_200(response_header, content_type, content_length);
|
||||
if (rc < 0) goto err;
|
||||
|
||||
/* Capture Timekeeping data for end-to-end latency */
|
||||
uint64_t end_time = __getcycles();
|
||||
sandbox->total_time = end_time - sandbox->timestamp_of.request_arrival;
|
||||
|
||||
/* Send HTTP Response */
|
||||
int sent = 0;
|
||||
size_t response_size = response_header_size + response_body_size;
|
||||
while (sent < response_size) {
|
||||
rc = write(sandbox->client_socket_descriptor, response_header, response_size - sent);
|
||||
if (rc < 0) {
|
||||
if (errno == EAGAIN)
|
||||
current_sandbox_sleep();
|
||||
else {
|
||||
perror("write");
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
sent += rc;
|
||||
}
|
||||
|
||||
http_total_increment_2xx();
|
||||
rc = 0;
|
||||
|
||||
done:
|
||||
return rc;
|
||||
err:
|
||||
rc = -1;
|
||||
goto done;
|
||||
}
|
@ -0,0 +1,302 @@
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "types.h" /* PAGE_SIZE */
|
||||
#include "wasm_types.h"
|
||||
|
||||
#define wasm_memory_MAX (size_t) UINT32_MAX + 1
|
||||
|
||||
struct wasm_memory {
|
||||
size_t size; /* Initial Size in bytes */
|
||||
size_t capacity; /* Size backed by actual pages */
|
||||
size_t max; /* Soft cap in bytes. Defaults to 4GB */
|
||||
uint8_t data[];
|
||||
};
|
||||
|
||||
static inline struct wasm_memory *
|
||||
wasm_memory_allocate(size_t initial, size_t max)
|
||||
{
|
||||
assert(initial > 0);
|
||||
assert(initial <= (size_t)UINT32_MAX + 1);
|
||||
assert(max > 0);
|
||||
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;
|
||||
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,
|
||||
strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
struct wasm_memory *self = (struct wasm_memory *)temp;
|
||||
|
||||
/* Set the struct and initial pages to read / write */
|
||||
size_t size_to_read_write = sizeof(struct wasm_memory) + initial;
|
||||
|
||||
int rc = mprotect(self, size_to_read_write, PROT_READ | PROT_WRITE);
|
||||
if (rc != 0) {
|
||||
perror("wasm_memory_allocate - prot r/w failed");
|
||||
munmap(self, size_to_alloc);
|
||||
assert(0);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
self->size = initial;
|
||||
self->capacity = initial;
|
||||
self->max = max;
|
||||
return self;
|
||||
}
|
||||
|
||||
static inline void
|
||||
wasm_memory_free(struct wasm_memory *self)
|
||||
{
|
||||
size_t size_to_free = sizeof(struct wasm_memory) + self->max + /* guard page */ PAGE_SIZE;
|
||||
munmap(self, size_to_free);
|
||||
}
|
||||
|
||||
static inline void
|
||||
wasm_memory_wipe(struct wasm_memory *self)
|
||||
{
|
||||
memset(self->data, 0, self->size);
|
||||
}
|
||||
|
||||
static inline int
|
||||
wasm_memory_expand(struct wasm_memory *self, size_t size_to_expand)
|
||||
{
|
||||
size_t target_size = self->size + size_to_expand;
|
||||
if (unlikely(target_size > self->max)) {
|
||||
fprintf(stderr, "wasm_memory_expand - Out of Memory!. %lu out of %lu\n", self->size, self->max);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (target_size > self->capacity) {
|
||||
int rc = mprotect(self, sizeof(struct wasm_memory) + target_size, PROT_READ | PROT_WRITE);
|
||||
if (rc != 0) {
|
||||
perror("wasm_memory_expand mprotect");
|
||||
return -1;
|
||||
}
|
||||
|
||||
self->capacity = target_size;
|
||||
}
|
||||
|
||||
self->size = target_size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates WASM offsets into runtime VM pointers
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @param bounds_check the size of the thing we are pointing to
|
||||
* @return void pointer to something in WebAssembly linear memory
|
||||
*/
|
||||
static inline void *
|
||||
wasm_memory_get_ptr_void(struct wasm_memory *self, uint32_t offset, uint32_t size)
|
||||
{
|
||||
assert(offset + size <= self->size);
|
||||
return (void *)&self->data[offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an ASCII character from WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return char at the offset
|
||||
*/
|
||||
static inline char
|
||||
wasm_memory_get_char(struct wasm_memory *self, uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(char) <= self->size);
|
||||
return (char)self->data[offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an float from WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return float at the offset
|
||||
*/
|
||||
static inline float
|
||||
wasm_memory_get_float(struct wasm_memory *self, uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(float) <= self->size);
|
||||
return *(float *)&self->data[offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a double from WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return double at the offset
|
||||
*/
|
||||
static inline double
|
||||
wasm_memory_get_double(struct wasm_memory *self, uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(double) <= self->size);
|
||||
return *(double *)&self->data[offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a int8_t from WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return int8_t at the offset
|
||||
*/
|
||||
static inline int8_t
|
||||
wasm_memory_get_int8(struct wasm_memory *self, uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(int8_t) <= self->size);
|
||||
return (int8_t)self->data[offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a int16_t from WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return int16_t at the offset
|
||||
*/
|
||||
static inline int16_t
|
||||
wasm_memory_get_int16(struct wasm_memory *self, uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(int16_t) <= self->size);
|
||||
return *(int16_t *)&self->data[offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a int32_t from WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return int32_t at the offset
|
||||
*/
|
||||
static inline int32_t
|
||||
wasm_memory_get_int32(struct wasm_memory *self, uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(int32_t) <= self->size);
|
||||
return *(int32_t *)&self->data[offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a int32_t from WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return int32_t at the offset
|
||||
*/
|
||||
static inline int64_t
|
||||
wasm_memory_get_int64(struct wasm_memory *self, uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(int64_t) <= self->size);
|
||||
return *(int64_t *)&self->data[offset];
|
||||
}
|
||||
|
||||
static inline uint32_t
|
||||
wasm_memory_get_page_count(struct wasm_memory *self)
|
||||
{
|
||||
return (uint32_t)(self->size / WASM_PAGE_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a null-terminated String from WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @param size the maximum expected length in characters
|
||||
* @return pointer to the string or NULL if max_length is reached without finding null-terminator
|
||||
*/
|
||||
static inline char *
|
||||
wasm_memory_get_string(struct wasm_memory *self, uint32_t offset, uint32_t size)
|
||||
{
|
||||
assert(offset + (sizeof(char) * size) <= self->size);
|
||||
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
if (self->data[offset + i] == '\0') return (char *)&self->data[offset];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a double to WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return double at the offset
|
||||
*/
|
||||
static inline void
|
||||
wasm_memory_set_double(struct wasm_memory *self, uint32_t offset, double value)
|
||||
{
|
||||
assert(offset + sizeof(double) <= self->size);
|
||||
*(double *)&self->data[offset] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a float to WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return float at the offset
|
||||
*/
|
||||
static inline void
|
||||
wasm_memory_set_float(struct wasm_memory *self, uint32_t offset, float value)
|
||||
{
|
||||
assert(offset + sizeof(float) <= self->size);
|
||||
*(float *)&self->data[offset] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a int8_t to WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return int8_t at the offset
|
||||
*/
|
||||
static inline void
|
||||
wasm_memory_set_int8(struct wasm_memory *self, uint32_t offset, int8_t value)
|
||||
{
|
||||
assert(offset + sizeof(int8_t) <= self->size);
|
||||
self->data[offset] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a int16_t to WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return int16_t at the offset
|
||||
*/
|
||||
static inline void
|
||||
wasm_memory_set_int16(struct wasm_memory *self, uint32_t offset, int16_t value)
|
||||
{
|
||||
assert(offset + sizeof(int16_t) <= self->size);
|
||||
*(int16_t *)&self->data[offset] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a int32_t to WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return int32_t at the offset
|
||||
*/
|
||||
static inline void
|
||||
wasm_memory_set_int32(struct wasm_memory *self, uint32_t offset, int32_t value)
|
||||
{
|
||||
assert(offset + sizeof(int32_t) <= self->size);
|
||||
*(int32_t *)&self->data[offset] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a int64_t to WebAssembly linear memory
|
||||
* @param offset an offset into the WebAssembly linear memory
|
||||
* @return int64_t at the offset
|
||||
*/
|
||||
static inline void
|
||||
wasm_memory_set_int64(struct wasm_memory *self, uint64_t offset, int64_t value)
|
||||
{
|
||||
assert(offset + sizeof(int64_t) <= self->size);
|
||||
*(int32_t *)&self->data[offset] = value;
|
||||
}
|
||||
|
||||
static inline void
|
||||
wasm_memory_set_size(struct wasm_memory *self, size_t size)
|
||||
{
|
||||
self->size = size;
|
||||
}
|
||||
|
||||
static inline size_t
|
||||
wasm_memory_get_size(struct wasm_memory *self)
|
||||
{
|
||||
return self->size;
|
||||
}
|
||||
|
||||
static inline void
|
||||
wasm_memory_initialize_region(struct wasm_memory *self, uint32_t offset, uint32_t region_size, uint8_t region[])
|
||||
{
|
||||
assert((size_t)offset + region_size <= self->size);
|
||||
memcpy(&self->data[offset], region, region_size);
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "wasm_memory.h"
|
||||
#include "wasm_table.h"
|
||||
|
||||
/* This structure is the runtime representation of the unique state of a module instance
|
||||
* Currently this is not spec-compliant, as it only supports a single table and a single memory and it excludes many
|
||||
* entities https://webassembly.github.io/spec/core/exec/runtime.html#module-instances
|
||||
*/
|
||||
struct wasm_module_instance {
|
||||
struct wasm_memory *memory;
|
||||
struct wasm_table * table;
|
||||
};
|
||||
|
||||
extern thread_local struct wasm_module_instance current_wasm_module_instance;
|
@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* memory also provides the table access functions */
|
||||
#define INDIRECT_TABLE_SIZE (1 << 10)
|
||||
|
||||
struct wasm_table_entry {
|
||||
uint32_t type_id;
|
||||
void * func_pointer;
|
||||
};
|
||||
|
||||
struct wasm_table {
|
||||
uint32_t length;
|
||||
uint32_t capacity;
|
||||
struct wasm_table_entry data[];
|
||||
};
|
||||
|
||||
static inline struct wasm_table *
|
||||
wasm_table_allocate(size_t capacity)
|
||||
{
|
||||
struct wasm_table *self = (struct wasm_table *)malloc(sizeof(struct wasm_table)
|
||||
+ capacity * sizeof(struct wasm_table_entry));
|
||||
|
||||
self->capacity = capacity;
|
||||
self->length = 0;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
static inline void
|
||||
wasm_table_free(struct wasm_table *self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
free(self);
|
||||
}
|
||||
|
||||
static inline void *
|
||||
wasm_table_get(struct wasm_table *self, uint32_t idx, uint32_t type_id)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(idx < self->capacity);
|
||||
|
||||
struct wasm_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_table_set(struct wasm_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_table_entry){ .type_id = type_id, .func_pointer = pointer };
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
#include "current_sandbox.h"
|
||||
#include "panic.h"
|
||||
#include "runtime.h"
|
||||
#include "sandbox_types.h"
|
||||
#include "types.h"
|
||||
|
||||
#include <sys/mman.h>
|
||||
|
||||
/**
|
||||
* @brief Expand the linear memory of the active WebAssembly sandbox by a single page
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int
|
||||
expand_memory(void)
|
||||
{
|
||||
struct sandbox *sandbox = current_sandbox_get();
|
||||
|
||||
assert(sandbox->state == SANDBOX_RUNNING_USER || sandbox->state == SANDBOX_RUNNING_SYS);
|
||||
assert(local_sandbox_context_cache.memory.size % WASM_PAGE_SIZE == 0);
|
||||
|
||||
/* Return -1 if we've hit the linear memory max */
|
||||
if (unlikely(local_sandbox_context_cache.memory.size + WASM_PAGE_SIZE
|
||||
>= local_sandbox_context_cache.memory.max)) {
|
||||
debuglog("expand_memory - Out of Memory!. %u out of %lu\n", local_sandbox_context_cache.memory.size,
|
||||
local_sandbox_context_cache.memory.max);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Remap the relevant wasm page to readable
|
||||
char *mem_as_chars = local_sandbox_context_cache.memory.start;
|
||||
char *page_address = &mem_as_chars[local_sandbox_context_cache.memory.size];
|
||||
void *map_result = mmap(page_address, WASM_PAGE_SIZE, PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
|
||||
if (map_result == MAP_FAILED) {
|
||||
debuglog("Mapping of new memory failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
local_sandbox_context_cache.memory.size += WASM_PAGE_SIZE;
|
||||
|
||||
#ifdef LOG_SANDBOX_MEMORY_PROFILE
|
||||
// Cache the runtime of the first N page allocations
|
||||
if (likely(sandbox->timestamp_of.page_allocations_size < SANDBOX_PAGE_ALLOCATION_TIMESTAMP_COUNT)) {
|
||||
sandbox->timestamp_of.page_allocations[sandbox->timestamp_of.page_allocations_size++] =
|
||||
sandbox->duration_of_state.running
|
||||
+ (uint32_t)(__getcycles() - sandbox->timestamp_of.last_state_change);
|
||||
}
|
||||
#endif
|
||||
|
||||
// local_sandbox_context_cache is "forked state", so update authoritative member
|
||||
sandbox->memory.size = local_sandbox_context_cache.memory.size;
|
||||
return 0;
|
||||
}
|
||||
|
||||
INLINE char *
|
||||
get_memory_ptr_for_runtime(uint32_t offset, uint32_t 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_context_cache.memory.size > bounds_check
|
||||
&& offset <= local_sandbox_context_cache.memory.size - bounds_check));
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
char *address = &mem_as_chars[offset];
|
||||
|
||||
return address;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Stub that implements the WebAssembly memory.grow instruction
|
||||
*
|
||||
* @param count number of pages to grow the WebAssembly linear memory by
|
||||
* @return The previous size of the linear memory in pages or -1 if enough memory cannot be allocated
|
||||
*/
|
||||
int32_t
|
||||
instruction_memory_grow(uint32_t count)
|
||||
{
|
||||
int rc = local_sandbox_context_cache.memory.size / WASM_PAGE_SIZE;
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (unlikely(expand_memory() != 0)) {
|
||||
rc = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "runtime.h"
|
||||
#include "types.h"
|
||||
|
||||
/* Region initialization helper function */
|
||||
EXPORT void
|
||||
initialize_region(uint32_t offset, uint32_t data_count, char *data)
|
||||
{
|
||||
assert(local_sandbox_context_cache.memory.size >= data_count);
|
||||
assert(offset < local_sandbox_context_cache.memory.size - data_count);
|
||||
|
||||
memcpy(get_memory_ptr_for_runtime(offset, data_count), data, data_count);
|
||||
}
|
||||
|
||||
/* If we are using runtime globals, we need to populate them */
|
||||
WEAK void
|
||||
populate_globals()
|
||||
{
|
||||
assert(0); /* FIXME: is this used in WASM as dynamic modules? Issue #105. */
|
||||
}
|
Loading…
Reference in new issue