chore: refactor out u32

main
Sean McBride 4 years ago
parent 407a20d22a
commit 4cb7c355b4

@ -7,8 +7,8 @@
// The below functions are for implementing WASM instructions // The below functions are for implementing WASM instructions
// ROTL and ROTR helper functions // ROTL and ROTR helper functions
INLINE u32 INLINE uint32_t
rotl_u32(u32 n, u32 c_u32) rotl_u32(uint32_t n, uint32_t c_u32)
{ {
// WASM requires a modulus here (usually a single bitwise op, but it means we need no assert) // WASM requires a modulus here (usually a single bitwise op, but it means we need no assert)
unsigned int c = c_u32 % (CHAR_BIT * sizeof(n)); unsigned int c = c_u32 % (CHAR_BIT * sizeof(n));
@ -18,8 +18,8 @@ rotl_u32(u32 n, u32 c_u32)
return (n << c) | (n >> ((-c) & mask)); return (n << c) | (n >> ((-c) & mask));
} }
INLINE u32 INLINE uint32_t
rotr_u32(u32 n, u32 c_u32) rotr_u32(uint32_t n, uint32_t c_u32)
{ {
// WASM requires a modulus here (usually a single bitwise op, but it means we need no assert) // WASM requires a modulus here (usually a single bitwise op, but it means we need no assert)
unsigned int c = c_u32 % (CHAR_BIT * sizeof(n)); unsigned int c = c_u32 % (CHAR_BIT * sizeof(n));
@ -52,15 +52,15 @@ rotr_u64(uint64_t n, uint64_t c_u64)
} }
// Now safe division and remainder // Now safe division and remainder
INLINE u32 INLINE uint32_t
u32_div(u32 a, u32 b) u32_div(uint32_t a, uint32_t b)
{ {
assert(b); assert(b);
return a / b; return a / b;
} }
INLINE u32 INLINE uint32_t
u32_rem(u32 a, u32 b) u32_rem(uint32_t a, uint32_t b)
{ {
assert(b); assert(b);
return a % b; return a % b;
@ -111,11 +111,11 @@ i64_rem(i64 a, i64 b)
// float to integer conversion methods // float to integer conversion methods
// In C, float => int conversions always truncate // In C, float => int conversions always truncate
// If a int2float(int::min_value) <= float <= int2float(int::max_value), it must always be safe to truncate it // If a int2float(int::min_value) <= float <= int2float(int::max_value), it must always be safe to truncate it
u32 uint32_t
u32_trunc_f32(float f) u32_trunc_f32(float f)
{ {
assert(0 <= f && f <= UINT32_MAX); assert(0 <= f && f <= UINT32_MAX);
return (u32)f; return (uint32_t)f;
} }
i32 i32
@ -125,11 +125,11 @@ i32_trunc_f32(float f)
return (i32)f; return (i32)f;
} }
u32 uint32_t
u32_trunc_f64(double f) u32_trunc_f64(double f)
{ {
assert(0 <= f && f <= UINT32_MAX); assert(0 <= f && f <= UINT32_MAX);
return (u32)f; return (uint32_t)f;
} }
i32 i32

@ -151,7 +151,7 @@ set_global_i64(i32 offset, i64 v)
// Table handling functionality // Table handling functionality
INLINE char * INLINE char *
get_function_from_table(u32 idx, u32 type_id) get_function_from_table(uint32_t idx, uint32_t type_id)
{ {
assert(idx < INDIRECT_TABLE_SIZE); assert(idx < INDIRECT_TABLE_SIZE);

@ -9,10 +9,10 @@ struct module {
char path[MODULE_MAX_PATH_LENGTH]; char path[MODULE_MAX_PATH_LENGTH];
void * dynamic_library_handle; /* Handle to the *.so of the serverless function */ void * dynamic_library_handle; /* Handle to the *.so of the serverless function */
i32 argument_count; i32 argument_count;
u32 stack_size; /* a specification? */ uint32_t stack_size; /* a specification? */
uint64_t max_memory; /* perhaps a specification of the module. (max 4GB) */ uint64_t max_memory; /* perhaps a specification of the module. (max 4GB) */
u32 relative_deadline_us; uint32_t relative_deadline_us;
u32 reference_count; /* ref count how many instances exist here. */ uint32_t reference_count; /* ref count how many instances exist here. */
struct indirect_table_entry indirect_table[INDIRECT_TABLE_SIZE]; struct indirect_table_entry indirect_table[INDIRECT_TABLE_SIZE];
struct sockaddr_in socket_address; struct sockaddr_in socket_address;
int socket_descriptor; int socket_descriptor;
@ -186,6 +186,6 @@ module_set_http_info(struct module *module, int request_count, char *request_hea
*******************************/ *******************************/
void module_free(struct module *module); void module_free(struct module *module);
struct module *module_new(char *mod_name, char *mod_path, i32 argument_count, u32 stack_sz, u32 max_heap, struct module *module_new(char *mod_name, char *mod_path, i32 argument_count, uint32_t stack_sz, uint32_t max_heap,
u32 relative_deadline_us, int port, int req_sz, int resp_sz); uint32_t relative_deadline_us, int port, int req_sz, int resp_sz);
int module_new_from_json(char *filename); int module_new_from_json(char *filename);

@ -3,13 +3,13 @@
#include <sys/epoll.h> /* for epoll_create1(), epoll_ctl(), struct epoll_event */ #include <sys/epoll.h> /* for epoll_create1(), epoll_ctl(), struct epoll_event */
#include "types.h" #include "types.h"
extern int runtime_epoll_file_descriptor; extern int runtime_epoll_file_descriptor;
extern u32 runtime_total_worker_processors; extern uint32_t runtime_total_worker_processors;
void alloc_linear_memory(void); void alloc_linear_memory(void);
void expand_memory(void); void expand_memory(void);
INLINE char *get_function_from_table(u32 idx, u32 type_id); INLINE char *get_function_from_table(uint32_t idx, uint32_t type_id);
INLINE char *get_memory_ptr_for_runtime(u32 offset, u32 bounds_check); INLINE char *get_memory_ptr_for_runtime(uint32_t offset, uint32_t bounds_check);
void runtime_initialize(void); void runtime_initialize(void);
void listener_thread_initialize(void); void listener_thread_initialize(void);
void stub_init(i32 offset); void stub_init(i32 offset);

@ -33,14 +33,14 @@ typedef enum
struct sandbox { struct sandbox {
sandbox_state_t state; sandbox_state_t state;
u32 sandbox_size; /* The struct plus enough buffer to hold the request or response (sized off largest) */ uint32_t sandbox_size; /* The struct plus enough buffer to hold the request or response (sized off largest) */
void * linear_memory_start; /* after sandbox struct */ void * linear_memory_start; /* after sandbox struct */
u32 linear_memory_size; /* from after sandbox struct */ uint32_t linear_memory_size; /* from after sandbox struct */
uint64_t linear_memory_max_size; uint64_t linear_memory_max_size;
void *stack_start; void * stack_start;
u32 stack_size; uint32_t stack_size;
arch_context_t ctxt; /* register context for context switch. */ arch_context_t ctxt; /* register context for context switch. */

@ -41,7 +41,6 @@ typedef unsigned char u8;
typedef int16_t i16; typedef int16_t i16;
typedef uint16_t u16; typedef uint16_t u16;
typedef int32_t i32; typedef int32_t i32;
typedef uint32_t u32;
typedef int64_t i64; typedef int64_t i64;
/* FIXME: per-module configuration? */ /* FIXME: per-module configuration? */
@ -56,8 +55,8 @@ macros. The code generator compiles in the starting number of wasm pages, and th
and allocate more than max_pages, we should fault */ and allocate more than max_pages, we should fault */
// TODO: Should this be deleted? // TODO: Should this be deleted?
// extern u32 starting_pages; // extern uint32_t starting_pages;
// extern u32 max_pages; // extern uint32_t max_pages;
/* The code generator also compiles in stubs that populate the linear memory and function table */ /* The code generator also compiles in stubs that populate the linear memory and function table */
void populate_memory(void); void populate_memory(void);
@ -67,15 +66,15 @@ void populate_table(void);
#define INDIRECT_TABLE_SIZE (1 << 10) #define INDIRECT_TABLE_SIZE (1 << 10)
struct indirect_table_entry { struct indirect_table_entry {
u32 type_id; uint32_t type_id;
void *func_pointer; void * func_pointer;
}; };
/* Cache of Frequently Accessed Members used to avoid pointer chasing */ /* Cache of Frequently Accessed Members used to avoid pointer chasing */
struct sandbox_context_cache { struct sandbox_context_cache {
struct indirect_table_entry *module_indirect_table; struct indirect_table_entry *module_indirect_table;
void * linear_memory_start; void * linear_memory_start;
u32 linear_memory_size; uint32_t linear_memory_size;
}; };
extern __thread struct sandbox_context_cache local_sandbox_context_cache; extern __thread struct sandbox_context_cache local_sandbox_context_cache;

@ -15,7 +15,7 @@ void *worker_thread_main(void *return_code);
* @return void pointer to something in WebAssembly linear memory * @return void pointer to something in WebAssembly linear memory
*/ */
static inline void * static inline void *
worker_thread_get_memory_ptr_void(u32 offset, u32 bounds_check) worker_thread_get_memory_ptr_void(uint32_t offset, uint32_t bounds_check)
{ {
return (void *)get_memory_ptr_for_runtime(offset, bounds_check); return (void *)get_memory_ptr_for_runtime(offset, bounds_check);
} }
@ -26,7 +26,7 @@ worker_thread_get_memory_ptr_void(u32 offset, u32 bounds_check)
* @return char at the offset * @return char at the offset
*/ */
static inline char static inline char
worker_thread_get_memory_character(u32 offset) worker_thread_get_memory_character(uint32_t offset)
{ {
return get_memory_ptr_for_runtime(offset, 1)[0]; return get_memory_ptr_for_runtime(offset, 1)[0];
} }
@ -38,7 +38,7 @@ worker_thread_get_memory_character(u32 offset)
* @return pointer to the string or NULL if max_length is reached without finding null-terminator * @return pointer to the string or NULL if max_length is reached without finding null-terminator
*/ */
static inline char * static inline char *
worker_thread_get_memory_string(u32 offset, u32 max_length) worker_thread_get_memory_string(uint32_t offset, uint32_t max_length)
{ {
for (int i = 0; i < max_length; i++) { for (int i = 0; i < max_length; i++) {
if (worker_thread_get_memory_character(offset + i) == '\0') { if (worker_thread_get_memory_character(offset + i) == '\0') {

@ -21,7 +21,7 @@ env___syscall(i32 n, i32 a, i32 b, i32 c, i32 d, i32 e, i32 f)
} }
void void
env___unmapself(u32 base, u32 size) env___unmapself(uint32_t base, uint32_t size)
{ {
/* Just do some no op */ /* Just do some no op */
} }

@ -105,7 +105,7 @@ wasm_fs_callback(uv_fs_t *req)
// We define our own syscall numbers, because WASM uses x86_64 values even on systems that are not x86_64 // We define our own syscall numbers, because WASM uses x86_64 values even on systems that are not x86_64
#define SYS_READ 0 #define SYS_READ 0
u32 uint32_t
wasm_read(i32 filedes, i32 buf_offset, i32 nbyte) wasm_read(i32 filedes, i32 buf_offset, i32 nbyte)
{ {
if (filedes == 0) { if (filedes == 0) {
@ -251,12 +251,12 @@ wasm_close(i32 file_descriptor)
struct wasm_stat { struct wasm_stat {
i64 st_dev; i64 st_dev;
uint64_t st_ino; uint64_t st_ino;
u32 st_nlink; uint32_t st_nlink;
u32 st_mode; uint32_t st_mode;
u32 st_uid; uint32_t st_uid;
u32 st_gid; uint32_t st_gid;
u32 __pad0; uint32_t __pad0;
uint64_t st_rdev; uint64_t st_rdev;
uint64_t st_size; uint64_t st_size;
i32 st_blksize; i32 st_blksize;
@ -298,7 +298,7 @@ struct wasm_stat {
// }; // };
i32 i32
wasm_stat(u32 path_str_offset, i32 stat_offset) wasm_stat(uint32_t path_str_offset, i32 stat_offset)
{ {
char * path = worker_thread_get_memory_string(path_str_offset, MODULE_MAX_PATH_LENGTH); char * path = worker_thread_get_memory_string(path_str_offset, MODULE_MAX_PATH_LENGTH);
struct wasm_stat *stat_ptr = worker_thread_get_memory_ptr_void(stat_offset, sizeof(struct wasm_stat)); struct wasm_stat *stat_ptr = worker_thread_get_memory_ptr_void(stat_offset, sizeof(struct wasm_stat));
@ -448,7 +448,7 @@ wasm_lseek(i32 filedes, i32 file_offset, i32 whence)
} }
#define SYS_MMAP 9 #define SYS_MMAP 9
u32 uint32_t
wasm_mmap(i32 addr, i32 len, i32 prot, i32 flags, i32 file_descriptor, i32 offset) wasm_mmap(i32 addr, i32 len, i32 prot, i32 flags, i32 file_descriptor, i32 offset)
{ {
int d = current_sandbox_get_file_descriptor(file_descriptor); int d = current_sandbox_get_file_descriptor(file_descriptor);
@ -593,10 +593,10 @@ wasm_writev(i32 file_descriptor, i32 iov_offset, i32 iovcnt)
#define SYS_MADVISE 28 #define SYS_MADVISE 28
#define SYS_GETPID 39 #define SYS_GETPID 39
u32 uint32_t
wasm_getpid() wasm_getpid()
{ {
return (u32)getpid(); return (uint32_t)getpid();
} }
@ -616,8 +616,8 @@ wasm_getpid()
#define WF_SETLKW 7 #define WF_SETLKW 7
#define SYS_FCNTL 72 #define SYS_FCNTL 72
u32 uint32_t
wasm_fcntl(u32 file_descriptor, u32 cmd, u32 arg_or_lock_ptr) wasm_fcntl(uint32_t file_descriptor, uint32_t cmd, uint32_t arg_or_lock_ptr)
{ {
int d = current_sandbox_get_file_descriptor(file_descriptor); int d = current_sandbox_get_file_descriptor(file_descriptor);
switch (cmd) { switch (cmd) {
@ -632,8 +632,8 @@ wasm_fcntl(u32 file_descriptor, u32 cmd, u32 arg_or_lock_ptr)
} }
#define SYS_FSYNC 74 #define SYS_FSYNC 74
u32 uint32_t
wasm_fsync(u32 file_descriptor) wasm_fsync(uint32_t file_descriptor)
{ {
int d = current_sandbox_get_file_descriptor(file_descriptor); int d = current_sandbox_get_file_descriptor(file_descriptor);
uv_fs_t req = UV_FS_REQ_INIT(); uv_fs_t req = UV_FS_REQ_INIT();
@ -649,8 +649,8 @@ wasm_fsync(u32 file_descriptor)
} }
#define SYS_GETCWD 79 #define SYS_GETCWD 79
u32 uint32_t
wasm_getcwd(u32 buf_offset, u32 buf_size) wasm_getcwd(uint32_t buf_offset, uint32_t buf_size)
{ {
char *buffer = worker_thread_get_memory_ptr_void(buf_offset, buf_size); char *buffer = worker_thread_get_memory_ptr_void(buf_offset, buf_size);
char *res = getcwd(buffer, buf_size); char *res = getcwd(buffer, buf_size);
@ -660,8 +660,8 @@ wasm_getcwd(u32 buf_offset, u32 buf_size)
} }
#define SYS_UNLINK 87 #define SYS_UNLINK 87
u32 uint32_t
wasm_unlink(u32 path_str_offset) wasm_unlink(uint32_t path_str_offset)
{ {
char * path = worker_thread_get_memory_string(path_str_offset, MODULE_MAX_PATH_LENGTH); char * path = worker_thread_get_memory_string(path_str_offset, MODULE_MAX_PATH_LENGTH);
uv_fs_t req = UV_FS_REQ_INIT(); uv_fs_t req = UV_FS_REQ_INIT();
@ -677,10 +677,10 @@ wasm_unlink(u32 path_str_offset)
} }
#define SYS_GETEUID 107 #define SYS_GETEUID 107
u32 uint32_t
wasm_geteuid() wasm_geteuid()
{ {
return (u32)geteuid(); return (uint32_t)geteuid();
} }
#define SYS_SET_THREAD_AREA 205 #define SYS_SET_THREAD_AREA 205
@ -690,7 +690,7 @@ wasm_geteuid()
#define SYS_GET_TIME 228 #define SYS_GET_TIME 228
struct wasm_time_spec { struct wasm_time_spec {
uint64_t sec; uint64_t sec;
u32 nanosec; uint32_t nanosec;
}; };
i32 i32
@ -734,7 +734,7 @@ wasm_exit_group(i32 status)
#define SYS_FCHOWN 93 #define SYS_FCHOWN 93
i32 i32
wasm_fchown(i32 file_descriptor, u32 owner, u32 group) wasm_fchown(i32 file_descriptor, uint32_t owner, uint32_t group)
{ {
int d = current_sandbox_get_file_descriptor(file_descriptor); int d = current_sandbox_get_file_descriptor(file_descriptor);
uv_fs_t req = UV_FS_REQ_INIT(); uv_fs_t req = UV_FS_REQ_INIT();

@ -20,10 +20,10 @@
i32 runtime_log_file_descriptor = -1; i32 runtime_log_file_descriptor = -1;
#endif #endif
float runtime_processor_speed_MHz = 0; float runtime_processor_speed_MHz = 0;
u32 runtime_total_online_processors = 0; uint32_t runtime_total_online_processors = 0;
u32 runtime_total_worker_processors = 0; uint32_t runtime_total_worker_processors = 0;
u32 runtime_first_worker_processor = 0; uint32_t runtime_first_worker_processor = 0;
int runtime_worker_threads_argument[WORKER_THREAD_CORE_COUNT] = { 0 }; /* The worker sets its argument to -1 on error */ int runtime_worker_threads_argument[WORKER_THREAD_CORE_COUNT] = { 0 }; /* The worker sets its argument to -1 on error */
pthread_t runtime_worker_threads[WORKER_THREAD_CORE_COUNT]; pthread_t runtime_worker_threads[WORKER_THREAD_CORE_COUNT];
@ -80,7 +80,7 @@ runtime_allocate_available_cores()
runtime_first_worker_processor = 1; runtime_first_worker_processor = 1;
/* WORKER_THREAD_CORE_COUNT can be used as a cap on the number of cores to use, but if there are few /* WORKER_THREAD_CORE_COUNT can be used as a cap on the number of cores to use, but if there are few
* cores that WORKER_THREAD_CORE_COUNT, just use what is available */ * cores that WORKER_THREAD_CORE_COUNT, just use what is available */
u32 max_possible_workers = runtime_total_online_processors - 1; uint32_t max_possible_workers = runtime_total_online_processors - 1;
runtime_total_worker_processors = max_possible_workers; runtime_total_worker_processors = max_possible_workers;
if (max_possible_workers >= WORKER_THREAD_CORE_COUNT) if (max_possible_workers >= WORKER_THREAD_CORE_COUNT)
runtime_total_worker_processors = WORKER_THREAD_CORE_COUNT; runtime_total_worker_processors = WORKER_THREAD_CORE_COUNT;

@ -18,7 +18,7 @@ alloc_linear_memory(void)
} }
void void
free_linear_memory(void *base, u32 bound, u32 max) free_linear_memory(void *base, uint32_t bound, uint32_t max)
{ {
// frees on sandbox_free // frees on sandbox_free
} }
@ -49,7 +49,7 @@ expand_memory(void)
} }
INLINE char * INLINE char *
get_memory_ptr_for_runtime(u32 offset, u32 bounds_check) 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 < // Due to how we setup memory for x86, the virtual memory mechanism will catch the error, if bounds <
// WASM_PAGE_SIZE // WASM_PAGE_SIZE

@ -3,7 +3,7 @@
/* Region initialization helper function */ /* Region initialization helper function */
EXPORT void EXPORT void
initialize_region(u32 offset, u32 data_count, char *data) initialize_region(uint32_t offset, uint32_t data_count, char *data)
{ {
assert(local_sandbox_context_cache.linear_memory_size >= data_count); assert(local_sandbox_context_cache.linear_memory_size >= data_count);
assert(offset < local_sandbox_context_cache.linear_memory_size - data_count); assert(offset < local_sandbox_context_cache.linear_memory_size - data_count);
@ -13,7 +13,7 @@ initialize_region(u32 offset, u32 data_count, char *data)
} }
void void
add_function_to_table(u32 idx, u32 type_id, char *pointer) add_function_to_table(uint32_t idx, uint32_t type_id, char *pointer)
{ {
assert(idx < INDIRECT_TABLE_SIZE); assert(idx < INDIRECT_TABLE_SIZE);
assert(local_sandbox_context_cache.module_indirect_table != NULL); assert(local_sandbox_context_cache.module_indirect_table != NULL);

@ -103,8 +103,8 @@ module_free(struct module *module)
*/ */
struct module * struct module *
module_new(char *name, char *path, i32 argument_count, u32 stack_size, u32 max_memory, u32 relative_deadline_us, module_new(char *name, char *path, i32 argument_count, uint32_t stack_size, uint32_t max_memory,
int port, int request_size, int response_size) uint32_t relative_deadline_us, int port, int request_size, int response_size)
{ {
errno = 0; errno = 0;
struct module *module = (struct module *)malloc(sizeof(struct module)); struct module *module = (struct module *)malloc(sizeof(struct module));
@ -316,18 +316,18 @@ module_new_from_json(char *file_name)
} }
memset(reponse_headers, 0, HTTP_MAX_HEADER_LENGTH * HTTP_MAX_HEADER_COUNT); memset(reponse_headers, 0, HTTP_MAX_HEADER_LENGTH * HTTP_MAX_HEADER_COUNT);
i32 request_size = 0; i32 request_size = 0;
i32 response_size = 0; i32 response_size = 0;
i32 argument_count = 0; i32 argument_count = 0;
u32 port = 0; uint32_t port = 0;
u32 relative_deadline_us = 0; uint32_t relative_deadline_us = 0;
bool is_active = false; bool is_active = false;
i32 request_count = 0; i32 request_count = 0;
i32 response_count = 0; i32 response_count = 0;
int j = 1; int j = 1;
int ntoks = 2 * tokens[i].size; int ntoks = 2 * tokens[i].size;
char request_content_type[HTTP_MAX_HEADER_VALUE_LENGTH] = { 0 }; char request_content_type[HTTP_MAX_HEADER_VALUE_LENGTH] = { 0 };
char response_content_type[HTTP_MAX_HEADER_VALUE_LENGTH] = { 0 }; char response_content_type[HTTP_MAX_HEADER_VALUE_LENGTH] = { 0 };
for (; j < ntoks;) { for (; j < ntoks;) {
int ntks = 1; int ntks = 1;

Loading…
Cancel
Save