diff --git a/.vscode/settings.json b/.vscode/settings.json index f186870..3228639 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -121,7 +121,8 @@ "stdint.h": "c", "scheduler_options.h": "c", "route_config_parse.h": "c", - "route.h": "c" + "route.h": "c", + "pool.h": "c" }, "files.exclude": { "**/.git": true, diff --git a/runtime/include/http_router.h b/runtime/include/http_router.h index b7165b1..b7be83d 100644 --- a/runtime/include/http_router.h +++ b/runtime/include/http_router.h @@ -43,10 +43,6 @@ http_router_add_route(struct http_router *router, struct route_config *config, s .response_content_type = config->http_resp_content_type }; - /* Move strings from config */ - config->route = NULL; - config->http_resp_content_type = NULL; - /* Admissions Control */ uint64_t expected_execution = (uint64_t)config->expected_execution_us * runtime_processor_speed_MHz; admissions_info_initialize(&router->routes[router->routes_length].admissions_info, diff --git a/runtime/include/module.h b/runtime/include/module.h index 40d3ae4..4a19cd5 100644 --- a/runtime/include/module.h +++ b/runtime/include/module.h @@ -42,7 +42,7 @@ struct module { struct sledge_abi__wasm_table *indirect_table; struct module_pools pools[MAX_WORKER_THREADS]; -}; +} __attribute__((aligned(CACHE_PAD))); /******************************** * Public Methods from module.c * diff --git a/runtime/include/route_config.h b/runtime/include/route_config.h index 09bd15b..3a2dc6b 100644 --- a/runtime/include/route_config.h +++ b/runtime/include/route_config.h @@ -32,9 +32,12 @@ struct route_config { static inline void route_config_deinit(struct route_config *config) { - free(config->path); - free(config->route); - free(config->http_resp_content_type); + /* ownership of the route and http_resp_content_type strings was moved during http_router_add_route */ + assert(config->route == NULL); + assert(config->http_resp_content_type == NULL); + + /* ownership of the path stringswas moved during module_alloc */ + assert(config->path == NULL); } static inline void diff --git a/runtime/include/tenant_functions.h b/runtime/include/tenant_functions.h index fe3fb40..e79b78d 100644 --- a/runtime/include/tenant_functions.h +++ b/runtime/include/tenant_functions.h @@ -39,10 +39,16 @@ tenant_alloc(struct tenant_config *config) module_database_init(&tenant->module_db); for (int i = 0; i < config->routes_len; i++) { - /* Resolve module */ - struct module *module = module_database_find_by_path(&tenant->module_db, config->routes[i].path); + /* Resolve module, Ownership of path moves here */ + struct module *module = module_database_find_by_path(&tenant->module_db, config->routes[i].path); + config->routes[i].path = NULL; + assert(module != NULL); + + /* Ownership of config's route and http_resp_content_type move here */ http_router_add_route(&tenant->router, &config->routes[i], module); + config->routes[i].route = NULL; + config->routes[i].http_resp_content_type = NULL; } return tenant; diff --git a/runtime/include/types.h b/runtime/include/types.h index f2b30a9..e61800e 100644 --- a/runtime/include/types.h +++ b/runtime/include/types.h @@ -11,12 +11,16 @@ #define CACHE_PAD (CACHE_LINE * 2) /* For this family of macros, do NOT pass zero as the pow2 */ -#define round_to_pow2(x, pow2) (((unsigned long)(x)) & (~((pow2)-1))) -#define round_up_to_pow2(x, pow2) (round_to_pow2(((unsigned long)(x)) + (pow2)-1, (pow2))) +#define round_to_pow2(x, pow2) (((unsigned long)(x)) & (~(((unsigned long)(pow2)) - 1))) +#define round_up_to_pow2(x, pow2) \ + (round_to_pow2(((unsigned long)(x)) + ((unsigned long)(pow2)) - 1, (unsigned long)((pow2)))) #define round_to_page(x) round_to_pow2(x, PAGE_SIZE) #define round_up_to_page(x) round_up_to_pow2(x, PAGE_SIZE) +#define round_to_cache_pad(x) round_to_pow2((unsigned long)(x), CACHE_PAD) +#define round_up_to_cache_pad(x) round_up_to_pow2((unsigned long)(x), CACHE_PAD) + #define EXPORT __attribute__((visibility("default"))) #define IMPORT __attribute__((visibility("default"))) #define INLINE __attribute__((always_inline)) diff --git a/runtime/include/wasm_memory.h b/runtime/include/wasm_memory.h index 45e48b1..6d92605 100644 --- a/runtime/include/wasm_memory.h +++ b/runtime/include/wasm_memory.h @@ -56,7 +56,7 @@ static INLINE void wasm_memory_set_f64(struct wasm_memory *wasm_memory, uint3 static INLINE struct wasm_memory * wasm_memory_alloc(uint64_t initial, uint64_t max) { - struct wasm_memory *wasm_memory = aligned_alloc(4096, sizeof(struct wasm_memory)); + struct wasm_memory *wasm_memory = aligned_alloc(4096, round_up_to_page(sizeof(struct wasm_memory))); if (wasm_memory == NULL) return wasm_memory; int rc = wasm_memory_init(wasm_memory, initial, max); diff --git a/runtime/include/wasm_stack.h b/runtime/include/wasm_stack.h index 9252b85..db9d1db 100644 --- a/runtime/include/wasm_stack.h +++ b/runtime/include/wasm_stack.h @@ -120,10 +120,9 @@ wasm_stack_reinit(struct wasm_stack *wasm_stack) { assert(wasm_stack != NULL); assert(wasm_stack->buffer != NULL); - - wasm_stack->low = wasm_stack->buffer + /* guard page */ PAGE_SIZE; + assert(wasm_stack->low == wasm_stack->buffer + /* guard page */ PAGE_SIZE); + assert(wasm_stack->high == wasm_stack->low + wasm_stack->capacity); memset(wasm_stack->low, 0, wasm_stack->capacity); ps_list_init_d(wasm_stack); - wasm_stack->high = wasm_stack->low + wasm_stack->capacity; } diff --git a/runtime/src/local_completion_queue.c b/runtime/src/local_completion_queue.c index 0dda8e3..588e2e8 100644 --- a/runtime/src/local_completion_queue.c +++ b/runtime/src/local_completion_queue.c @@ -3,7 +3,7 @@ #include "local_completion_queue.h" #include "sandbox_functions.h" -thread_local static struct ps_list_head local_completion_queue; +thread_local static struct ps_list_head local_completion_queue __attribute__((aligned(16))); void diff --git a/runtime/src/module.c b/runtime/src/module.c index 5924dcd..6b3f2f3 100644 --- a/runtime/src/module.c +++ b/runtime/src/module.c @@ -94,14 +94,19 @@ module_free(struct module *module) */ struct module * -module_alloc(char *path) +module_alloc(IN char *path) { - struct module *module = (struct module *)calloc(1, sizeof(struct module)); + size_t alignment = (size_t)CACHE_PAD; + size_t size_to_alloc = (size_t)round_to_cache_pad(sizeof(struct module)); + + struct module *module = aligned_alloc(alignment, size_to_alloc); if (!module) { fprintf(stderr, "Failed to allocate module: %s\n", strerror(errno)); goto err; }; + memset(module, 0, size_to_alloc); + int rc = module_init(module, path); if (rc < 0) goto init_err; @@ -109,6 +114,7 @@ done: return module; init_err: + free(path); free(module); err: module = NULL; diff --git a/tests/gocr/hyde/Makefile b/tests/gocr/hyde/Makefile index c9fae24..aee733e 100644 --- a/tests/gocr/hyde/Makefile +++ b/tests/gocr/hyde/Makefile @@ -12,6 +12,9 @@ run: debug: SLEDGE_DISABLE_PREEMPTION=true SLEDGE_NWORKERS=1 LD_LIBRARY_PATH=${SLEDGE_BINARY_DIR} gdb ${SLEDGE_BINARY_DIR}/sledgert --eval-command="run spec.json" +valgrind: + SLEDGE_DISABLE_PREEMPTION=true SLEDGE_NWORKERS=1 LD_LIBRARY_PATH=${SLEDGE_BINARY_DIR} valgrind --leak-check=full --max-stackframe=11150456 --run-libc-freeres=no --run-cxx-freeres=no ${SLEDGE_BINARY_DIR}/sledgert spec.json + client: curl -H 'Expect:' -H "Content-Type: text/plain" --data-binary "@hyde.pnm" "${HOSTNAME}:10000/gocr"