Compare commits
12 Commits
Author | SHA1 | Date |
---|---|---|
|
87903f2c41 | 4 years ago |
|
d014641efd | 4 years ago |
|
65c363d93d | 4 years ago |
|
6b043666f6 | 4 years ago |
|
ef09fcc3f1 | 4 years ago |
|
7e46f58d0f | 4 years ago |
|
25493641b3 | 4 years ago |
|
18f36172e3 | 4 years ago |
|
fc3325366f | 4 years ago |
|
a6689c4823 | 4 years ago |
|
29af1d7fa5 | 4 years ago |
|
a9d3b3199f | 4 years ago |
@ -0,0 +1,6 @@
|
||||
A WebAssembly module instance is statically linked with the backing functions implementing the wasm32 ABI, yielding a *.so file that SLEdge can execute. This ensures that the instance is able to aggressively inline and optimize this code.
|
||||
|
||||
They are broken into instruction types as on https://webassembly.github.io/spec/core/exec/instructions.html. They depend on common headers for the WebAssembly types located in the WebAssembly instance struct. These are located in runtime/include/common.
|
||||
|
||||
The stubs correspond to awsm/src/codegen/runtime_stubs.rs
|
||||
|
@ -1,182 +0,0 @@
|
||||
#include <assert.h>
|
||||
#include "types.h"
|
||||
|
||||
uint32_t
|
||||
instruction_memory_size()
|
||||
{
|
||||
return local_sandbox_context_cache.memory.size / WASM_PAGE_SIZE;
|
||||
}
|
||||
|
||||
// All of these are pretty generic
|
||||
INLINE float
|
||||
get_f32(uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(float) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
return *(float *)address;
|
||||
}
|
||||
|
||||
INLINE double
|
||||
get_f64(uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(double) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
return *(double *)address;
|
||||
}
|
||||
|
||||
INLINE int8_t
|
||||
get_i8(uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(int8_t) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
return *(int8_t *)address;
|
||||
}
|
||||
|
||||
INLINE int16_t
|
||||
get_i16(uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(int16_t) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
return *(int16_t *)address;
|
||||
}
|
||||
|
||||
INLINE int32_t
|
||||
get_i32(uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(int32_t) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
return *(int32_t *)address;
|
||||
}
|
||||
|
||||
INLINE int64_t
|
||||
get_i64(uint32_t offset)
|
||||
{
|
||||
assert(offset + sizeof(int64_t) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
return *(int64_t *)address;
|
||||
}
|
||||
|
||||
INLINE int32_t
|
||||
get_global_i32(uint32_t offset)
|
||||
{
|
||||
return get_i32(offset);
|
||||
}
|
||||
|
||||
INLINE int64_t
|
||||
get_global_i64(uint32_t offset)
|
||||
{
|
||||
return get_i64(offset);
|
||||
}
|
||||
|
||||
// Now setting routines
|
||||
INLINE void
|
||||
set_f32(uint32_t offset, float v)
|
||||
{
|
||||
assert(offset + sizeof(float) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
*(float *)address = v;
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_f64(uint32_t offset, double v)
|
||||
{
|
||||
assert(offset + sizeof(double) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
*(double *)address = v;
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_i8(uint32_t offset, int8_t v)
|
||||
{
|
||||
assert(offset + sizeof(int8_t) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
*(int8_t *)address = v;
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_i16(uint32_t offset, int16_t v)
|
||||
{
|
||||
assert(offset + sizeof(int16_t) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
*(int16_t *)address = v;
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_i32(uint32_t offset, int32_t v)
|
||||
{
|
||||
assert(offset + sizeof(int32_t) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
*(int32_t *)address = v;
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_i64(uint32_t offset, int64_t v)
|
||||
{
|
||||
assert(offset + sizeof(int64_t) <= local_sandbox_context_cache.memory.size);
|
||||
|
||||
char *mem_as_chars = (char *)local_sandbox_context_cache.memory.start;
|
||||
void *address = &mem_as_chars[offset];
|
||||
|
||||
*(int64_t *)address = v;
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_global_i32(uint32_t offset, int32_t v)
|
||||
{
|
||||
set_i32(offset, v);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_global_i64(uint32_t offset, int64_t v)
|
||||
{
|
||||
set_i64(offset, v);
|
||||
}
|
||||
|
||||
// Table handling functionality
|
||||
// INLINE char *
|
||||
// get_function_from_table(uint32_t idx, uint32_t type_id)
|
||||
// {
|
||||
// assert(idx < INDIRECT_TABLE_SIZE);
|
||||
|
||||
// struct indirect_table_entry f = local_sandbox_context_cache.module_indirect_table[idx];
|
||||
|
||||
// // FIXME: Commented out function type check because of gocr
|
||||
// // assert(f.type_id == type_id);
|
||||
|
||||
// assert(f.func_pointer);
|
||||
|
||||
// return f.func_pointer;
|
||||
// }
|
@ -0,0 +1,144 @@
|
||||
#include <assert.h>
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "wasm_store.h"
|
||||
|
||||
extern thread_local struct wasm_module_instance current_wasm_module_instance;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
INLINE uint32_t
|
||||
instruction_memory_size()
|
||||
{
|
||||
return wasm_memory_get_page_count(current_wasm_module_instance.memory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
INLINE int32_t
|
||||
instruction_memory_grow(uint32_t count)
|
||||
{
|
||||
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);
|
||||
if (unlikely(rc == -1)) return -1;
|
||||
|
||||
#ifdef LOG_SANDBOX_MEMORY_PROFILE
|
||||
// Cache the runtime of the first N page allocations
|
||||
for (int i = 0; i < count; i++) {
|
||||
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
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
INLINE float
|
||||
get_f32(uint32_t offset)
|
||||
{
|
||||
return wasm_memory_get_float(current_wasm_module_instance.memory, offset);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_f32(uint32_t offset, float v)
|
||||
{
|
||||
wasm_memory_set_float(current_wasm_module_instance.memory, offset, v);
|
||||
}
|
||||
|
||||
INLINE double
|
||||
get_f64(uint32_t offset)
|
||||
{
|
||||
return wasm_memory_get_double(current_wasm_module_instance.memory, offset);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_f64(uint32_t offset, double v)
|
||||
{
|
||||
wasm_memory_set_double(current_wasm_module_instance.memory, offset, v);
|
||||
}
|
||||
|
||||
INLINE int8_t
|
||||
get_i8(uint32_t offset)
|
||||
{
|
||||
return wasm_memory_get_int8(current_wasm_module_instance.memory, offset);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_i8(uint32_t offset, int8_t v)
|
||||
{
|
||||
wasm_memory_set_int8(current_wasm_module_instance.memory, offset, v);
|
||||
}
|
||||
|
||||
INLINE int16_t
|
||||
get_i16(uint32_t offset)
|
||||
{
|
||||
return wasm_memory_get_int16(current_wasm_module_instance.memory, offset);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_i16(uint32_t offset, int16_t v)
|
||||
{
|
||||
wasm_memory_set_int16(current_wasm_module_instance.memory, offset, v);
|
||||
}
|
||||
|
||||
INLINE int32_t
|
||||
get_i32(uint32_t offset)
|
||||
{
|
||||
return wasm_memory_get_int32(current_wasm_module_instance.memory, offset);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_i32(uint32_t offset, int32_t v)
|
||||
{
|
||||
wasm_memory_set_int32(current_wasm_module_instance.memory, offset, v);
|
||||
}
|
||||
|
||||
INLINE int64_t
|
||||
get_i64(uint32_t offset)
|
||||
{
|
||||
return wasm_memory_get_int64(current_wasm_module_instance.memory, offset);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_i64(uint32_t offset, int64_t v)
|
||||
{
|
||||
wasm_memory_set_int64(current_wasm_module_instance.memory, offset, v);
|
||||
}
|
||||
|
||||
INLINE int32_t
|
||||
get_global_i32(uint32_t offset)
|
||||
{
|
||||
return get_i32(offset);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_global_i32(uint32_t offset, int32_t v)
|
||||
{
|
||||
set_i32(offset, v);
|
||||
}
|
||||
|
||||
INLINE int64_t
|
||||
get_global_i64(uint32_t offset)
|
||||
{
|
||||
return get_i64(offset);
|
||||
}
|
||||
|
||||
INLINE void
|
||||
set_global_i64(uint32_t offset, int64_t v)
|
||||
{
|
||||
set_i64(offset, v);
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <types.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#define CHAR_BIT 8
|
||||
|
@ -0,0 +1,23 @@
|
||||
|
||||
#include "types.h"
|
||||
#include "wasm_store.h"
|
||||
|
||||
/* This file contains the stub functions that the aWsm compiler expects
|
||||
* This corresponds to awsm/src/codegen/runtime_stubs.rs
|
||||
* This should be linked with the *.bc file generated by aWsm in order to compile a module as a *.so
|
||||
*/
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return wasm_table_get(current_wasm_module_instance.table, idx, type_id);
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
This folder includes headers used by the compiletime and runtime components.
|
||||
|
||||
Changes in here break our ABI and require *.so module to be recompiled.
|
@ -0,0 +1,303 @@
|
||||
#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[region_size])
|
||||
{
|
||||
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 char *
|
||||
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 };
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#define WASM_PAGE_SIZE (1024 * 64) /* 64KB */
|
||||
#define WASM_MEMORY_PAGES_MAX (1 << 16) /* 32,768 Pages ~4GB */
|
||||
#define WASM_STACK_SIZE (1 << 19) /* 512KB */
|
||||
|
||||
/* Wasm initialization functions generated by the compiler */
|
||||
#define AWSM_ABI_INITIALIZE_GLOBALS "populate_globals"
|
||||
#define AWSM_ABI_INITIALIZE_MEMORY "populate_memory"
|
||||
#define AWSM_ABI_INITIALIZE_TABLE "populate_table"
|
||||
#define AWSM_ABI_INITIALIZE_LIBC "wasmf___init_libc"
|
||||
#define AWSM_ABI_ENTRYPOINT "wasmf_main"
|
||||
|
||||
#define AWSM_ABI_STARTING_PAGES "starting_pages"
|
||||
#define AWSM_ABI_MAX_PAGES "max_pages"
|
||||
|
||||
/* functions in the module to lookup and call per sandbox. */
|
||||
typedef int32_t (*awsm_abi_entrypoint_fn_t)(int32_t a, int32_t b);
|
||||
typedef void (*awsm_abi_init_globals_fn_t)(void);
|
||||
typedef void (*awsm_abi_init_mem_fn_t)(void);
|
||||
typedef void (*awsm_abi_init_tbl_fn_t)(void);
|
||||
typedef void (*awsm_abi_init_libc_fn_t)(int32_t, int32_t);
|
@ -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;
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "generic_thread.h"
|
||||
#include "lock.h"
|
||||
|
||||
struct pool {
|
||||
bool use_lock;
|
||||
lock_t lock;
|
||||
ssize_t top;
|
||||
size_t capacity;
|
||||
void * buffer[];
|
||||
};
|
||||
|
||||
static inline bool
|
||||
pool_is_empty(struct pool *self)
|
||||
{
|
||||
return self->top == -1;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
pool_is_full(struct pool *self)
|
||||
{
|
||||
return self->top + 1 == self->capacity;
|
||||
}
|
||||
|
||||
static inline void *
|
||||
pool_allocate_object_nolock(struct pool *self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(!self->use_lock || LOCK_IS_LOCKED(&self->lock));
|
||||
|
||||
void *result = NULL;
|
||||
|
||||
if (pool_is_empty(self)) return result;
|
||||
|
||||
result = self->buffer[self->top--];
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline void *
|
||||
pool_allocate_object(struct pool *self)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(self->use_lock);
|
||||
|
||||
void *result = NULL;
|
||||
|
||||
if (pool_is_empty(self)) return result;
|
||||
|
||||
LOCK_LOCK(&self->lock);
|
||||
if (pool_is_empty(self)) {
|
||||
LOCK_UNLOCK(&self->lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
result = self->buffer[self->top--];
|
||||
assert(result != NULL);
|
||||
LOCK_UNLOCK(&self->lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline int
|
||||
pool_free_object_nolock(struct pool *self, void *obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(obj != NULL);
|
||||
assert(!self->use_lock || LOCK_IS_LOCKED(&self->lock));
|
||||
|
||||
if (pool_is_full(self)) return -1;
|
||||
|
||||
self->buffer[++self->top] = obj;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
pool_free_object(struct pool *self, void *obj)
|
||||
{
|
||||
assert(self != NULL);
|
||||
assert(obj != NULL);
|
||||
assert(self->use_lock);
|
||||
|
||||
if (pool_is_full(self)) return -1;
|
||||
|
||||
LOCK_LOCK(&self->lock);
|
||||
if (pool_is_full(self)) {
|
||||
LOCK_UNLOCK(&self->lock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
self->buffer[++self->top] = obj;
|
||||
LOCK_UNLOCK(&self->lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline struct pool *
|
||||
pool_init(size_t capacity, bool use_lock)
|
||||
{
|
||||
struct pool *self = (struct pool *)calloc(1, sizeof(struct pool) + capacity * sizeof(void *));
|
||||
self->top = -1;
|
||||
self->capacity = capacity;
|
||||
self->use_lock = use_lock;
|
||||
|
||||
if (use_lock) LOCK_INIT(&self->lock);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
static inline void
|
||||
pool_free(struct pool *self)
|
||||
{
|
||||
while (!pool_is_empty(self)) free(pool_allocate_object(self));
|
||||
|
||||
free(self);
|
||||
}
|
@ -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;
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* FIXME: per-module configuration? Issue #101 */
|
||||
#define WASM_PAGE_SIZE (1024 * 64) /* 64KB */
|
||||
#define WASM_MEMORY_PAGES_INITIAL (1 << 8) /* 256 Pages ~16MB */
|
||||
#define WASM_MEMORY_PAGES_MAX (1 << 15) /* 32,768 Pages ~4GB */
|
||||
|
||||
#define WASM_STACK_SIZE (1 << 19) /* 512KB */
|
||||
|
||||
/* bytes, not wasm pages */
|
||||
struct wasm_memory {
|
||||
void * start;
|
||||
uint32_t size;
|
||||
uint64_t max;
|
||||
};
|
@ -0,0 +1,3 @@
|
||||
#include "pool.h"
|
||||
|
||||
struct pool **runtime_linear_memory_pools;
|
@ -1,118 +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;
|
||||
}
|
||||
|
||||
/*
|
||||
* Table handling functionality
|
||||
* This was moved from compiletime in order to place the
|
||||
* function in the callstack in GDB. It can be moved back
|
||||
* to runtime/compiletime/memory/64bit_nix.c to remove the
|
||||
* additional function call
|
||||
*/
|
||||
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;
|
||||
}
|
@ -1,38 +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);
|
||||
}
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
*.wasm
|
||||
*.bc
|
||||
*.so
|
@ -1,60 +1,67 @@
|
||||
include Makefile.inc
|
||||
CC=clang # Source -> Native
|
||||
WASMCC=wasm32-unknown-unknown-wasm-clang # Source -> WebAssembly
|
||||
|
||||
TESTS=fibonacci empty empty
|
||||
OPTFLAGS=-O0 -g
|
||||
TMP_DIR=tmp/
|
||||
|
||||
TESTSRT=$(TESTS:%=%_rt)
|
||||
# same stack-size for all
|
||||
WASMLINKFLAGS=-Wl,-z,stack-size=524288,--allow-undefined,--no-threads,--stack-first,--no-entry,--export-all,--export=main,--export=dummy
|
||||
WASMCFLAGS=${WASMLINKFLAGS} -nostartfiles
|
||||
|
||||
.PHONY: all
|
||||
all: rttests tinyekf cifar10 gocr sod
|
||||
@echo "Test Compilation done!"
|
||||
# aWsm Compiler Runtime (WebAssembly -> LLVM Bitcode)
|
||||
AWSM_NAME=awsm
|
||||
AWSM_BASE_DIR=../../${AWSM_NAME}
|
||||
DUMMY=${AWSM_BASE_DIR}/code_benches/dummy.c
|
||||
|
||||
# for SLEdge
|
||||
SLEDGE_BASE_DIR=../../
|
||||
SLEDGE_RT_DIR=${SLEDGE_BASE_DIR}/runtime/
|
||||
SLEDGE_COMPILETIME_INC=${SLEDGE_RT_DIR}/include/common
|
||||
SLEDGE_BIN_DIR=${SLEDGE_RT_DIR}/bin/
|
||||
SLEDGE_COMPILETIME_SRC=${SLEDGE_RT_DIR}/compiletime/*.c
|
||||
|
||||
ALL=fibonacci empty license_plate_detection resize_image gocr cifar10
|
||||
ALL_COPY_DEST=$(ALL:%=../../runtime/bin/%_wasm.so)
|
||||
|
||||
.PHONY: rttests
|
||||
rttests: $(TESTSRT)
|
||||
.PHONY: all
|
||||
all: $(ALL_COPY_DEST)
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@echo "Cleaning Test Applications"
|
||||
@rm -rf ${TMP_DIR}
|
||||
@make clean -C ./CMSIS_5_NN/ -f Makefile
|
||||
@make clean -C ./gocr/src/ -f wasm.mk
|
||||
@make clean -C ./sod/
|
||||
@make clean -C ./TinyEKF/extras/c/ -f wasm.mk
|
||||
@rm -f ../../runtime/bin/*.so
|
||||
|
||||
cifar10.wasm:
|
||||
@make cifar10.wasm -C ./CMSIS_5_NN/ -f Makefile
|
||||
@cp ./CMSIS_5_NN/cifar10.wasm cifar10.wasm
|
||||
|
||||
gocr.wasm:
|
||||
@make gocr.wasm -C ./gocr/src/ -f wasm.mk
|
||||
@cp ./gocr/src/gocr.wasm gocr.wasm
|
||||
|
||||
resize_image.wasm:
|
||||
@make dir resize_image.wasm -C ./sod/
|
||||
@cp ./sod/bin/resize_image.wasm resize_image.wasm
|
||||
|
||||
license_plate_detection.wasm:
|
||||
@make dir license_plate_detection.wasm -C ./sod/
|
||||
@cp ./sod/bin/license_plate_detection.wasm license_plate_detection.wasm
|
||||
|
||||
gps_ekf_fn.wasm:
|
||||
@make gps_ekf_fn.wasm -C ./TinyEKF/extras/c/ -f wasm.mk
|
||||
@cp ./TinyEKF/extras/c/gps_ekf_fn.wasm gps_ekf_fn.wasm
|
||||
|
||||
%.wasm: %.c $(DUMMY)
|
||||
${WASMCC} ${WASMCFLAGS} ${OPTFLAGS} $^ -o $@
|
||||
|
||||
%.bc: %.wasm
|
||||
${AWSM_NAME} --inline-constant-globals --runtime-globals $^ -o $@
|
||||
|
||||
.PHONY: tinyekf
|
||||
tinyekf:
|
||||
@echo "Making and Installing tinyekf"
|
||||
@make gps_ekf_fn.so -C ./TinyEKF/extras/c/ -f wasm.mk
|
||||
@cp ./TinyEKF/extras/c/gps_ekf_fn.so ${SLEDGE_BIN_DIR}/ekf_wasm.so
|
||||
|
||||
.PHONY: cifar10
|
||||
cifar10:
|
||||
@echo "Making and Installing cifar10"
|
||||
@make cifar10.so -C ./CMSIS_5_NN/ -f Makefile
|
||||
@cp ./CMSIS_5_NN/cifar10.so ${SLEDGE_BIN_DIR}/cifar10_wasm.so
|
||||
|
||||
.PHONY: gocr
|
||||
gocr:
|
||||
@echo "Making and Installing gocr"
|
||||
@make gocr.so -C ./gocr/src/ -f wasm.mk
|
||||
@cp ./gocr/src/gocr.so ${SLEDGE_BIN_DIR}/gocr_wasm.so
|
||||
|
||||
.PHONY: sod
|
||||
sod:
|
||||
@echo "Making and Installing license_plate_detection and image_resize"
|
||||
@make dir samples.so -C ./sod/
|
||||
@cp ./sod/bin/license_plate_detection.so ${SLEDGE_BIN_DIR}/lpd_wasm.so
|
||||
@cp ./sod/bin/resize_image.so ${SLEDGE_BIN_DIR}/resize_wasm.so
|
||||
|
||||
%_rt:
|
||||
@mkdir -p ${TMP_DIR}
|
||||
# Compile the wasm file
|
||||
@echo "Compiling $(@:%_rt=%)"
|
||||
${WASMCC} ${$(@:%_rt=%)_CFLAGS} ${WASMCFLAGS} ${OPTFLAGS} $(@:%_rt=%)/*.c $(AWSM_DUMMY) -o ${TMP_DIR}/$(@:%_rt=%).wasm
|
||||
|
||||
# Compile the *.bc file
|
||||
|
||||
${AWSM_NAME} --inline-constant-globals --runtime-globals ${TMP_DIR}/$(@:%_rt=%).wasm -o ${TMP_DIR}/$(@:%_rt=%).bc
|
||||
# Compile the *.so file
|
||||
${CC} --shared -fPIC ${OPTFLAGS} -I${SLEDGE_RT_INC} -D${USE_MEM} ${TMP_DIR}/$(@:%_rt=%).bc ${SLEDGE_MEMC} ${SLEDGE_WASMISA} -o ${TMP_DIR}/$(@:%_rt=%)_wasm.so
|
||||
# Copy the *.so file to the binary directory
|
||||
@cp ${TMP_DIR}/$(@:%_rt=%)_wasm.so ${SLEDGE_BIN_DIR}
|
||||
# @rm -rf ${TMP_DIR}
|
||||
%_wasm.so: %.bc ${SLEDGE_COMPILETIME_SRC}
|
||||
${CC} --shared -fPIC ${OPTFLAGS} -I${SLEDGE_COMPILETIME_INC} $^ -o $@
|
||||
|
||||
../../runtime/bin/%.so: %.so
|
||||
cp $^ $@
|
||||
|
@ -1,42 +0,0 @@
|
||||
ARCH := $(shell uname -m)
|
||||
|
||||
CC=clang # Source -> Native
|
||||
WASMCC=wasm32-unknown-unknown-wasm-clang # Source -> WebAssembly
|
||||
|
||||
OPTFLAGS=-O3 -flto
|
||||
MEMC_64=64bit_nix.c
|
||||
# MEMC_NO=no_protection.c
|
||||
# MEMC_GEN=generic.c
|
||||
# MEMC_MPX=mpx.c
|
||||
# MEMC_SEG=segmented.c
|
||||
TMP_DIR=tmp/
|
||||
|
||||
# same stack-size for all
|
||||
WASMLINKFLAGS=-Wl,-z,stack-size=524288,--allow-undefined,--no-threads,--stack-first,--no-entry,--export-all,--export=main,--export=dummy
|
||||
WASMCFLAGS=${WASMLINKFLAGS} -nostartfiles
|
||||
|
||||
# aWsm Compiler Runtime (WebAssembly -> LLVM Bitcode)
|
||||
AWSM_NAME=awsm
|
||||
AWSM_BASE_DIR=../../${AWSM_NAME}
|
||||
AWSM_RT_DIR=${AWSM_BASE_DIR}/runtime/
|
||||
AWSM_RT_MEM=${AWSM_RT_DIR}/memory/
|
||||
AWSM_RT_LIBC=${AWSM_RT_DIR}/libc/libc_backing.c
|
||||
AWSM_RT_RT=${AWSM_RT_DIR}/runtime.c
|
||||
AWSM_RT_ENV=${AWSM_RT_DIR}/libc/env.c
|
||||
# Is seems like the arch specific code no longer exists
|
||||
# AWSM_RT_ENV=${AWSM_RT_DIR}/libc/env.c ${AWSM_RT_DIR}/libc/env_${ARCH}.c
|
||||
AWSM_MEMC=${AWSM_RT_MEM}/${MEMC_64}
|
||||
AWSM_DUMMY=${AWSM_BASE_DIR}/code_benches/dummy.c
|
||||
|
||||
# for SLEdge
|
||||
SLEDGE_BASE_DIR=../../
|
||||
SLEDGE_RT_DIR=${SLEDGE_BASE_DIR}/runtime/
|
||||
SLEDGE_RT_INC=${SLEDGE_RT_DIR}/include/
|
||||
SLEDGE_BIN_DIR=${SLEDGE_RT_DIR}/bin/
|
||||
SLEDGE_WASMISA=${SLEDGE_RT_DIR}/compiletime/instr.c
|
||||
|
||||
USE_MEM=USE_MEM_VM
|
||||
|
||||
ifeq ($(USE_MEM),USE_MEM_VM)
|
||||
SLEDGE_MEMC=${SLEDGE_RT_DIR}/compiletime/memory/${MEMC_64}
|
||||
endif
|
@ -1 +1 @@
|
||||
Subproject commit fe4d4600a3b02f313b74892990595354dfc98913
|
||||
Subproject commit 92a8aa9a5e0aed719836558825fa839b1b1efb0b
|
@ -1,44 +0,0 @@
|
||||
#ifndef GET_TIME_H
|
||||
#define GET_TIME_H
|
||||
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef WASM
|
||||
#ifndef CPU_FREQ
|
||||
#define CPU_FREQ 1000
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static unsigned long long
|
||||
get_time()
|
||||
{
|
||||
#if 0
|
||||
unsigned long long int ret = 0;
|
||||
unsigned int cycles_lo;
|
||||
unsigned int cycles_hi;
|
||||
__asm__ volatile ("rdtsc" : "=a" (cycles_lo), "=d" (cycles_hi));
|
||||
ret = (unsigned long long int)cycles_hi << 32 | cycles_lo;
|
||||
|
||||
return ret;
|
||||
#else
|
||||
struct timeval Tp;
|
||||
int stat;
|
||||
stat = gettimeofday(&Tp, NULL);
|
||||
if (stat != 0) printf("Error return from gettimeofday: %d", stat);
|
||||
return (Tp.tv_sec * 1000000 + Tp.tv_usec);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void
|
||||
print_time(unsigned long long s, unsigned long long e)
|
||||
{
|
||||
#if 0
|
||||
printf("%llu cycs, %llu us\n", e - s, (e - s) / CPU_FREQ);
|
||||
#else
|
||||
fprintf(stdout, "%llu us\n", e - s);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* GET_TIME_H */
|
@ -1,32 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
ITERS=$3
|
||||
|
||||
# before running this benchmark,
|
||||
# copy fibonacci to fibonacci_native.out
|
||||
|
||||
testeach() {
|
||||
tmp_cnt=${ITERS}
|
||||
exe_relpath=$1
|
||||
|
||||
echo "${exe_relpath} ($2) for ${tmp_cnt}"
|
||||
|
||||
while [ ${tmp_cnt} -gt 0 ]; do
|
||||
bench=$(echo $2 | $exe_relpath 2> /dev/null)
|
||||
tmp_cnt=$((tmp_cnt - 1))
|
||||
echo "$bench"
|
||||
done
|
||||
|
||||
echo "Done!"
|
||||
}
|
||||
|
||||
MAXNUM=$2
|
||||
|
||||
tmp1_cnt=${MAXNUM}
|
||||
|
||||
while [ ${tmp1_cnt} -gt 28 ]; do
|
||||
testeach ./fibonacci_$1.out ${tmp1_cnt}
|
||||
tmp1_cnt=$((tmp1_cnt - 1))
|
||||
done
|
||||
|
||||
echo "All done!"
|
Loading…
Reference in new issue