From b510214e373480b2b314c75e887f37989791882e Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Tue, 20 Apr 2021 18:14:08 +0000 Subject: [PATCH] fix: Make module reference count atomic --- runtime/include/module.h | 10 +++++++--- runtime/src/module.c | 13 +++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/runtime/include/module.h b/runtime/include/module.h index 00afec6..87cd934 100644 --- a/runtime/include/module.h +++ b/runtime/include/module.h @@ -51,7 +51,7 @@ struct module { uint64_t max_memory; /* perhaps a specification of the module. (max 4GB) */ uint32_t relative_deadline_us; uint64_t relative_deadline; /* cycles */ - uint32_t reference_count; /* ref count how many instances exist here. */ + _Atomic uint32_t reference_count; /* ref count how many instances exist here. */ struct indirect_table_entry indirect_table[INDIRECT_TABLE_SIZE]; struct sockaddr_in socket_address; int socket_descriptor; @@ -101,7 +101,9 @@ struct module { static inline void module_acquire(struct module *module) { - module->reference_count++; + assert(module->reference_count < UINT32_MAX); + atomic_fetch_add(&module->reference_count, 1); + return; } /** @@ -200,7 +202,9 @@ module_main(struct module *module, int32_t argc, int32_t argv) static inline void module_release(struct module *module) { - module->reference_count--; + assert(module->reference_count > 0); + atomic_fetch_sub(&module->reference_count, 1); + return; } /** diff --git a/runtime/src/module.c b/runtime/src/module.c index 8f0a5ea..02bdcb9 100644 --- a/runtime/src/module.c +++ b/runtime/src/module.c @@ -147,8 +147,12 @@ module_new(char *name, char *path, int32_t argument_count, uint32_t stack_size, memset(module, 0, sizeof(struct module)); + atomic_init(&module->reference_count, 0); + /* Load the dynamic library *.so file with lazy function call binding and deep binding */ - module->dynamic_library_handle = dlopen(path, RTLD_LAZY | RTLD_DEEPBIND); + // TODO: Changed to work with sanitizers. What does RTLD_DEEPBIND do? + // module->dynamic_library_handle = dlopen(path, RTLD_LAZY | RTLD_DEEPBIND); + module->dynamic_library_handle = dlopen(path, RTLD_LAZY); if (module->dynamic_library_handle == NULL) { fprintf(stderr, "Failed to open %s with error: %s\n", path, dlerror()); goto dl_open_error; @@ -193,8 +197,9 @@ module_new(char *name, char *path, int32_t argument_count, uint32_t stack_size, strncpy(module->name, name, MODULE_MAX_NAME_LENGTH); strncpy(module->path, path, MODULE_MAX_PATH_LENGTH); - module->argument_count = argument_count; - module->stack_size = round_up_to_page(stack_size == 0 ? WASM_STACK_SIZE : stack_size); + module->argument_count = argument_count; + module->stack_size = ((uint32_t)(round_up_to_page(stack_size == 0 ? WASM_STACK_SIZE : stack_size))); + debuglog("Stack Size: %u", module->stack_size); module->max_memory = max_memory == 0 ? ((uint64_t)WASM_PAGE_SIZE * WASM_MAX_PAGES) : max_memory; module->socket_descriptor = -1; module->port = port; @@ -209,7 +214,7 @@ module_new(char *name, char *path, int32_t argument_count, uint32_t stack_size, module->relative_deadline = (uint64_t)relative_deadline_us * runtime_processor_speed_MHz; /* Admissions Control */ - uint64_t expected_execution = expected_execution_us * runtime_processor_speed_MHz; + uint64_t expected_execution = (uint64_t)expected_execution_us * runtime_processor_speed_MHz; admissions_info_initialize(&module->admissions_info, admissions_percentile, expected_execution, module->relative_deadline);