diff --git a/runtime/include/module.h b/runtime/include/module.h index 1ddb9b9..8b829a8 100644 --- a/runtime/include/module.h +++ b/runtime/include/module.h @@ -25,8 +25,8 @@ struct module { u32 reference_count; // ref count how many instances exist here. struct sockaddr_in socket_address; - int socket_descriptor; - int port; + int socket_descriptor; + int port; // unfortunately, using UV for accepting connections is not great! // on_connection, to create a new accepted connection, will have to @@ -39,9 +39,9 @@ struct module { unsigned long max_request_size; unsigned long max_response_size; // Equals the largest of either max_request_size or max_response_size - unsigned long max_request_or_response_size; - int request_header_count; - int response_header_count; + unsigned long max_request_or_response_size; + int request_header_count; + int response_header_count; char request_headers[HTTP_HEADERS_MAX][HTTP_HEADER_MAXSZ]; char request_content_type[HTTP_HEADERVAL_MAXSZ]; char response_content_type[HTTP_HEADERVAL_MAXSZ]; @@ -57,17 +57,11 @@ struct module *module_find_by_socket_descriptor(int sock); static inline void -module_http_info( - struct module *module, - int request_count, - char *request_headers, - char request_content_type[], - int response_count, - char *response_headers, - char response_content_type[]) +module_http_info(struct module *module, int request_count, char *request_headers, char request_content_type[], + int response_count, char *response_headers, char response_content_type[]) { assert(module); - module->request_header_count = request_count; + module->request_header_count = request_count; memcpy(module->request_headers, request_headers, HTTP_HEADER_MAXSZ * HTTP_HEADERS_MAX); strcpy(module->request_content_type, request_content_type); module->response_header_count = response_count; diff --git a/runtime/include/sandbox.h b/runtime/include/sandbox.h index 4c8723d..9316de5 100644 --- a/runtime/include/sandbox.h +++ b/runtime/include/sandbox.h @@ -57,7 +57,7 @@ struct sandbox { void *args; // args from request, must be of module->argument_count size. i32 retval; - struct io_handle handles[SBOX_MAX_OPEN]; + struct io_handle handles[SBOX_MAX_OPEN]; struct sockaddr client; // client requesting connection! int csock; uv_tcp_t cuv; @@ -80,7 +80,7 @@ struct sandbox_request { char * args; int sock; struct sockaddr *addr; - u64 start_time; // cycles + u64 start_time; // cycles }; typedef struct sandbox_request sbox_request_t; @@ -109,19 +109,14 @@ typedef struct sandbox sandbox_t; * @return the new sandbox request **/ static inline sbox_request_t * -sbox_request_alloc( - struct module *module, - char *args, - int sock, - const struct sockaddr *addr, - u64 start_time) +sbox_request_alloc(struct module *module, char *args, int sock, const struct sockaddr *addr, u64 start_time) { sbox_request_t *sandbox_request = malloc(sizeof(sbox_request_t)); assert(sandbox_request); - sandbox_request->module = module; - sandbox_request->args = args; - sandbox_request->sock = sock; - sandbox_request->addr = (struct sockaddr *)addr; + sandbox_request->module = module; + sandbox_request->args = args; + sandbox_request->sock = sock; + sandbox_request->addr = (struct sockaddr *)addr; sandbox_request->start_time = start_time; debuglog("[%p: %s]\n", sandbox_request, sandbox_request->module->name); @@ -193,7 +188,7 @@ sandbox_switch(struct sandbox *next_sandbox) { arch_context_t *next_register_context = next_sandbox == NULL ? NULL : &next_sandbox->ctxt; softint_disable(); - struct sandbox *current_sandbox = sandbox_current(); + struct sandbox *current_sandbox = sandbox_current(); arch_context_t *current_register_context = current_sandbox == NULL ? NULL : ¤t_sandbox->ctxt; sandbox_current_set(next_sandbox); // If the current sandbox we're switching from is in a RETURNED state, add to completion queue @@ -233,7 +228,7 @@ extern pthread_mutex_t global_deque_mutex; /** * Pushes a sandbox request to the global deque - * @para + * @para **/ static inline int sandbox_deque_push(sbox_request_t *sandbox_request) @@ -305,7 +300,7 @@ io_handle_open(int fd) { struct sandbox *sandbox = sandbox_current(); if (fd < 0) return fd; - int i = io_handle_preopen(); + int i = io_handle_preopen(); sandbox->handles[i].fd = fd; // well, per sandbox.. so synchronization necessary! return i; } @@ -330,7 +325,7 @@ io_handle_preopen_set(int idx, int fd) /** * Get the file descriptor of the sandbox's ith io_handle * @param idx index into the sandbox's handles table - * @returns any libuv handle + * @returns any libuv handle **/ static inline int io_handle_fd(int idx) @@ -341,7 +336,7 @@ io_handle_fd(int idx) } /** - * Close the sandbox's ith io_handle + * Close the sandbox's ith io_handle * @param idx index of the handle to close **/ static inline void @@ -353,9 +348,9 @@ io_handle_close(int idx) } /** - * Get the Libuv handle located at idx of the sandbox ith io_handle + * Get the Libuv handle located at idx of the sandbox ith io_handle * @param idx index of the handle containing uvh??? - * @returns any libuv handle + * @returns any libuv handle **/ static inline union uv_any_handle * io_handle_uv_get(int idx) diff --git a/runtime/include/util.h b/runtime/include/util.h index 73ac71e..3776819 100644 --- a/runtime/include/util.h +++ b/runtime/include/util.h @@ -5,6 +5,6 @@ #include /* perhaps move it to module.h or sandbox.h? */ -int util_parse_modules_file_json(char *filename); +int util_parse_modules_file_json(char *filename); #endif /* SFRT_UTIL_H */ diff --git a/runtime/src/libc/uvio.c b/runtime/src/libc/uvio.c index 9f89b26..6671f99 100644 --- a/runtime/src/libc/uvio.c +++ b/runtime/src/libc/uvio.c @@ -105,8 +105,8 @@ wasm_read(i32 filedes, i32 buf_offset, i32 nbyte) { if (filedes == 0) { char * buffer = get_memory_ptr_void(buf_offset, nbyte); - struct sandbox * s = sandbox_current(); - struct http_request *r = &s->http_request; + struct sandbox * s = sandbox_current(); + struct http_request *r = &s->http_request; if (r->bodylen <= 0) return 0; int l = nbyte > r->bodylen ? r->bodylen : nbyte; memcpy(buffer, r->body + r->bodyrlen, l); @@ -116,7 +116,7 @@ wasm_read(i32 filedes, i32 buf_offset, i32 nbyte) } int f = io_handle_fd(filedes); // TODO: read on other file types - uv_fs_t req = UV_FS_REQ_INIT(); + uv_fs_t req = UV_FS_REQ_INIT(); char * buffer = get_memory_ptr_void(buf_offset, nbyte); debuglog("[%p] start[%d:%d, n%d]\n", uv_fs_get_data(&req), filedes, f, nbyte); @@ -137,9 +137,9 @@ wasm_write(i32 fd, i32 buf_offset, i32 buf_size) { if (fd == 1 || fd == 2) { char * buffer = get_memory_ptr_void(buf_offset, buf_size); - struct sandbox *s = sandbox_current(); - int l = s->module->max_response_size - s->rr_data_len; - l = l > buf_size ? buf_size : l; + struct sandbox *s = sandbox_current(); + int l = s->module->max_response_size - s->rr_data_len; + l = l > buf_size ? buf_size : l; if (l == 0) return 0; memcpy(s->req_resp_data + s->rr_data_len, buffer, l); s->rr_data_len += l; @@ -148,7 +148,7 @@ wasm_write(i32 fd, i32 buf_offset, i32 buf_size) } int f = io_handle_fd(fd); // TODO: read on other file types - uv_fs_t req = UV_FS_REQ_INIT(); + uv_fs_t req = UV_FS_REQ_INIT(); char * buffer = get_memory_ptr_void(buf_offset, buf_size); uv_buf_t bufv = uv_buf_init(buffer, buf_size); @@ -324,13 +324,13 @@ wasm_stat(u32 path_str_offset, i32 stat_offset) stat_ptr->st_ctim.tv_sec = stat.st_ctimespec.tv_sec; stat_ptr->st_ctim.tv_nsec = stat.st_ctimespec.tv_nsec; #else - stat_ptr->st_atim.tv_sec = stat.st_atim.tv_sec; + stat_ptr->st_atim.tv_sec = stat.st_atim.tv_sec; stat_ptr->st_atim.tv_nsec = stat.st_atim.tv_nsec; - stat_ptr->st_mtim.tv_sec = stat.st_mtim.tv_sec; + stat_ptr->st_mtim.tv_sec = stat.st_mtim.tv_sec; stat_ptr->st_mtim.tv_nsec = stat.st_mtim.tv_nsec; - stat_ptr->st_ctim.tv_sec = stat.st_ctim.tv_sec; + stat_ptr->st_ctim.tv_sec = stat.st_ctim.tv_sec; stat_ptr->st_ctim.tv_nsec = stat.st_ctim.tv_nsec; #endif @@ -370,13 +370,13 @@ wasm_fstat(i32 filedes, i32 stat_offset) stat_ptr->st_ctim.tv_sec = stat.st_ctimespec.tv_sec; stat_ptr->st_ctim.tv_nsec = stat.st_ctimespec.tv_nsec; #else - stat_ptr->st_atim.tv_sec = stat.st_atim.tv_sec; + stat_ptr->st_atim.tv_sec = stat.st_atim.tv_sec; stat_ptr->st_atim.tv_nsec = stat.st_atim.tv_nsec; - stat_ptr->st_mtim.tv_sec = stat.st_mtim.tv_sec; + stat_ptr->st_mtim.tv_sec = stat.st_mtim.tv_sec; stat_ptr->st_mtim.tv_nsec = stat.st_mtim.tv_nsec; - stat_ptr->st_ctim.tv_sec = stat.st_ctim.tv_sec; + stat_ptr->st_ctim.tv_sec = stat.st_ctim.tv_sec; stat_ptr->st_ctim.tv_nsec = stat.st_ctim.tv_nsec; #endif @@ -416,13 +416,13 @@ wasm_lstat(i32 path_str_offset, i32 stat_offset) stat_ptr->st_ctim.tv_sec = stat.st_ctimespec.tv_sec; stat_ptr->st_ctim.tv_nsec = stat.st_ctimespec.tv_nsec; #else - stat_ptr->st_atim.tv_sec = stat.st_atim.tv_sec; + stat_ptr->st_atim.tv_sec = stat.st_atim.tv_sec; stat_ptr->st_atim.tv_nsec = stat.st_atim.tv_nsec; - stat_ptr->st_mtim.tv_sec = stat.st_mtim.tv_sec; + stat_ptr->st_mtim.tv_sec = stat.st_mtim.tv_sec; stat_ptr->st_mtim.tv_nsec = stat.st_mtim.tv_nsec; - stat_ptr->st_ctim.tv_sec = stat.st_ctim.tv_sec; + stat_ptr->st_ctim.tv_sec = stat.st_ctim.tv_sec; stat_ptr->st_ctim.tv_nsec = stat.st_ctim.tv_nsec; #endif @@ -496,10 +496,10 @@ wasm_readv(i32 fd, i32 iov_offset, i32 iovcnt) { if (fd == 0) { // both 1 and 2 go to client. - int len = 0; - struct wasm_iovec *iov = get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); - struct sandbox *s = sandbox_current(); - struct http_request *r = &s->http_request; + int len = 0; + struct wasm_iovec * iov = get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); + struct sandbox * s = sandbox_current(); + struct http_request *r = &s->http_request; if (r->bodylen <= 0) return 0; for (int i = 0; i < iovcnt; i++) { int l = iov[i].len > r->bodylen ? r->bodylen : iov[i].len; @@ -653,7 +653,7 @@ u32 wasm_getcwd(u32 buf_offset, u32 buf_size) { char *buffer = get_memory_ptr_void(buf_offset, buf_size); - char *res = getcwd(buffer, buf_size); + char *res = getcwd(buffer, buf_size); if (!res) return 0; return buf_offset; @@ -1018,7 +1018,7 @@ i32 wasm_recvfrom(i32 fd, i32 buff_offset, i32 size, i32 flags, i32 sockaddr_offset, i32 socklen_offset) { char * buffer = get_memory_ptr_void(buff_offset, size); - socklen_t *len = get_memory_ptr_void(socklen_offset, sizeof(socklen_t)); + socklen_t *len = get_memory_ptr_void(socklen_offset, sizeof(socklen_t)); // TODO: only support "recv" api for now assert(*len == 0); struct sandbox * c = sandbox_current(); diff --git a/runtime/src/main.c b/runtime/src/main.c index 721d7ba..a6704af 100644 --- a/runtime/src/main.c +++ b/runtime/src/main.c @@ -12,11 +12,11 @@ #include #include -i32 log_file_descriptor = -1; -u32 total_online_processors = 0; -u32 total_worker_processors = 0; -u32 first_worker_processor = 0; -int worker_threads_argument[SBOX_NCORES] = { 0 }; // The worker sets its argument to -1 on error +i32 log_file_descriptor = -1; +u32 total_online_processors = 0; +u32 total_worker_processors = 0; +u32 first_worker_processor = 0; +int worker_threads_argument[SBOX_NCORES] = { 0 }; // The worker sets its argument to -1 on error pthread_t worker_threads[SBOX_NCORES]; /** @@ -44,10 +44,12 @@ usage(char *cmd) } /** - * Sets the process data segment (RLIMIT_DATA) and # file descriptors + * Sets the process data segment (RLIMIT_DATA) and # file descriptors * (RLIMIT_NOFILE) soft limit to its hard limit (see man getrlimit) **/ -void set_resource_limits_to_max(){ +void +set_resource_limits_to_max() +{ struct rlimit resource_limit; if (getrlimit(RLIMIT_DATA, &resource_limit) < 0) { perror("getrlimit RLIMIT_DATA"); @@ -72,7 +74,9 @@ void set_resource_limits_to_max(){ /** * Check the number of cores and the compiler flags and allocate available cores **/ -void allocate_available_cores(){ +void +allocate_available_cores() +{ // Find the number of processors currently online total_online_processors = sysconf(_SC_NPROCESSORS_ONLN); @@ -82,22 +86,24 @@ void allocate_available_cores(){ // SBOX_NCORES can be used as a cap on the number of cores to use // But if there are few cores that SBOX_NCORES, just use what is available u32 max_possible_workers = total_online_processors - 1; - total_worker_processors = (max_possible_workers >= SBOX_NCORES) ? SBOX_NCORES : max_possible_workers; + total_worker_processors = (max_possible_workers >= SBOX_NCORES) ? SBOX_NCORES : max_possible_workers; } else { // If single core, we'll do everything on CPUID 0 - first_worker_processor = 0; + first_worker_processor = 0; total_worker_processors = 1; } - debuglog("Number of cores %u, sandboxing cores %u (start: %u) and module reqs %u\n", total_online_processors, total_worker_processors, - first_worker_processor, MOD_REQ_CORE); + debuglog("Number of cores %u, sandboxing cores %u (start: %u) and module reqs %u\n", total_online_processors, + total_worker_processors, first_worker_processor, MOD_REQ_CORE); } -/** - * If NOSTIO is defined, close stdin, stdout, stderr, and write to logfile named awesome.log. +/** + * If NOSTIO is defined, close stdin, stdout, stderr, and write to logfile named awesome.log. * Otherwise, log to STDOUT * NOSTIO = No Standard Input/Output? **/ -void process_nostio(){ +void +process_nostio() +{ #ifdef NOSTDIO fclose(stdout); fclose(stderr); @@ -115,9 +121,12 @@ void process_nostio(){ /** * Starts all worker threads and sleeps forever on pthread_join, which should never return **/ -void start_worker_threads(){ +void +start_worker_threads() +{ for (int i = 0; i < total_worker_processors; i++) { - int ret = pthread_create(&worker_threads[i], NULL, sandbox_run_func, (void *)&worker_threads_argument[i]); + int ret = pthread_create(&worker_threads[i], NULL, sandbox_run_func, + (void *)&worker_threads_argument[i]); if (ret) { errno = ret; perror("pthread_create"); diff --git a/runtime/src/module.c b/runtime/src/module.c index cc04732..1bd569f 100644 --- a/runtime/src/module.c +++ b/runtime/src/module.c @@ -101,8 +101,9 @@ module_server_init(struct module *module) /** * Module Mega Setup Function - * Creates a new module, invokes tbl_init_fn to initialize the indirect table, adds it to the module DB, and starts listening for HTTP Requests - * + * Creates a new module, invokes tbl_init_fn to initialize the indirect table, adds it to the module DB, and starts + *listening for HTTP Requests + * * @param name * @param path * @param argument_count @@ -114,8 +115,8 @@ module_server_init(struct module *module) * @returns A new module or NULL in case of failure **/ struct module * -module_alloc(char *name, char *path, i32 argument_count, u32 stack_size, u32 max_memory, u32 timeout, int port, int request_size, - int response_size) +module_alloc(char *name, char *path, i32 argument_count, u32 stack_size, u32 max_memory, u32 timeout, int port, + int request_size, int response_size) { struct module *module = (struct module *)malloc(sizeof(struct module)); if (!module) return NULL; @@ -146,28 +147,30 @@ module_alloc(char *name, char *path, i32 argument_count, u32 stack_size, u32 max strncpy(module->name, name, MOD_NAME_MAX); strncpy(module->path, path, MOD_PATH_MAX); - module->argument_count = argument_count; - module->stack_size = round_up_to_page(stack_size == 0 ? WASM_STACK_SIZE : stack_size); - module->max_memory = max_memory == 0 ? ((u64)WASM_PAGE_SIZE * WASM_MAX_PAGES) : max_memory; - module->timeout = timeout; + module->argument_count = argument_count; + module->stack_size = round_up_to_page(stack_size == 0 ? WASM_STACK_SIZE : stack_size); + module->max_memory = max_memory == 0 ? ((u64)WASM_PAGE_SIZE * WASM_MAX_PAGES) : max_memory; + module->timeout = timeout; module->socket_descriptor = -1; - module->port = port; + module->port = port; if (request_size == 0) request_size = MOD_REQ_RESP_DEFAULT; if (response_size == 0) response_size = MOD_REQ_RESP_DEFAULT; - module->max_request_size = request_size; - module->max_response_size = response_size; - module->max_request_or_response_size = round_up_to_page(request_size > response_size ? request_size : response_size); + module->max_request_size = request_size; + module->max_response_size = response_size; + module->max_request_or_response_size = round_up_to_page(request_size > response_size ? request_size + : response_size); // module_indirect_table is a thread-local struct struct indirect_table_entry *cache_tbl = module_indirect_table; - - // assumption: All modules are created at program start before we enable preemption or enable the execution of any worker threads - // We are checking that thread-local module_indirect_table is NULL to prove that we aren't yet preempting - // If we want to be able to do this later, we can possibly defer module_table_init until the first invocation + + // assumption: All modules are created at program start before we enable preemption or enable the execution of + // any worker threads We are checking that thread-local module_indirect_table is NULL to prove that we aren't + // yet preempting If we want to be able to do this later, we can possibly defer module_table_init until the + // first invocation assert(cache_tbl == NULL); - - // TODO: determine why we have to set the module_indirect_table state before calling table init and then restore the existing value - // What is the relationship between these things? + + // TODO: determine why we have to set the module_indirect_table state before calling table init and then restore + // the existing value What is the relationship between these things? module_indirect_table = module->indirect_table; module_table_init(module); module_indirect_table = cache_tbl; diff --git a/runtime/src/runtime.c b/runtime/src/runtime.c index 00e401c..7a59499 100644 --- a/runtime/src/runtime.c +++ b/runtime/src/runtime.c @@ -38,13 +38,14 @@ static inline void sandbox_local_run(struct sandbox *s) { assert(ps_list_singleton_d(s)); - // fprintf(stderr, "(%d,%lu) %s: run %p, %s\n", sched_getcpu(), pthread_self(), __func__, s, s->module->name); + // fprintf(stderr, "(%d,%lu) %s: run %p, %s\n", sched_getcpu(), pthread_self(), __func__, s, + // s->module->name); ps_list_head_append_d(&local_run_queue, s); } /** - * Pulls up to 1..n sandbox requests, allocates them as sandboxes, sets them as runnable and places them on the local runqueue, and then frees the sandbox requests - * The batch size pulled at once is set by SBOX_PULL_MAX + * Pulls up to 1..n sandbox requests, allocates them as sandboxes, sets them as runnable and places them on the local + * runqueue, and then frees the sandbox requests The batch size pulled at once is set by SBOX_PULL_MAX * @return the number of sandbox requests pulled */ static inline int @@ -55,7 +56,9 @@ sandbox_pull(void) while (total_sandboxes_pulled < SBOX_PULL_MAX) { sbox_request_t *sandbox_request; if ((sandbox_request = sandbox_deque_steal()) == NULL) break; - struct sandbox *sandbox = sandbox_alloc(sandbox_request->module, sandbox_request->args, sandbox_request->sock, sandbox_request->addr, sandbox_request->start_time); + struct sandbox *sandbox = sandbox_alloc(sandbox_request->module, sandbox_request->args, + sandbox_request->sock, sandbox_request->addr, + sandbox_request->start_time); assert(sandbox); free(sandbox_request); sandbox->state = SANDBOX_RUNNABLE; @@ -120,7 +123,7 @@ sandbox_schedule(int interrupt) static inline void sandbox_local_free(unsigned int number_to_free) { - for (int i = 0; i < number_to_free; i++){ + for (int i = 0; i < number_to_free; i++) { if (ps_list_head_empty(&local_completion_queue)) break; struct sandbox *s = ps_list_head_first_d(&local_completion_queue, struct sandbox); if (!s) break; @@ -296,21 +299,22 @@ sandbox_exit(void) } /** - * @brief Execution Loop of the listener core, handles HTTP requests, allocates sandbox request objects, and pushes the sandbox object to the global dequeue + * @brief Execution Loop of the listener core, handles HTTP requests, allocates sandbox request objects, and pushes the + * sandbox object to the global dequeue * @param d Unknown * @return NULL - * + * * Used Globals: * epoll_file_descriptor - the epoll file descriptor - * + * */ void * runtime_accept_thdfn(void *d) { - struct epoll_event *epoll_events = (struct epoll_event *)malloc(EPOLL_MAX * sizeof(struct epoll_event)); - int total_requests = 0; + struct epoll_event *epoll_events = (struct epoll_event *)malloc(EPOLL_MAX * sizeof(struct epoll_event)); + int total_requests = 0; while (true) { - int ready = epoll_wait(epoll_file_descriptor, epoll_events, EPOLL_MAX, -1); + int ready = epoll_wait(epoll_file_descriptor, epoll_events, EPOLL_MAX, -1); u64 start_time = rdtsc(); for (int i = 0; i < ready; i++) { if (epoll_events[i].events & EPOLLERR) { @@ -331,7 +335,8 @@ runtime_accept_thdfn(void *d) total_requests++; printf("Received Request %d at %lu\n", total_requests, start_time); - sbox_request_t *sb = sbox_request_alloc(m, m->name, s, (const struct sockaddr *)&client, start_time); + sbox_request_t *sb = sbox_request_alloc(m, m->name, s, (const struct sockaddr *)&client, + start_time); assert(sb); } } diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index af34e96..6ec86fa 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -18,19 +18,19 @@ sandbox_memory_map(struct module *module) unsigned long memory_size = SBOX_MAX_MEM; // 4GB // Why do we add max_request_or_response_size? - unsigned long sandbox_size = sizeof(struct sandbox) + module->max_request_or_response_size; + unsigned long sandbox_size = sizeof(struct sandbox) + module->max_request_or_response_size; unsigned long linear_memory_size = WASM_PAGE_SIZE * WASM_START_PAGES; if (linear_memory_size + sandbox_size > memory_size) return NULL; assert(round_up_to_page(sandbox_size) == sandbox_size); // What does mmap do exactly with fd -1? - void *addr = mmap(NULL, sandbox_size + memory_size + /* guard page */ PAGE_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, - 0); + void *addr = mmap(NULL, sandbox_size + memory_size + /* guard page */ PAGE_SIZE, PROT_NONE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (addr == MAP_FAILED) return NULL; - void *addr_rw = mmap(addr, sandbox_size + linear_memory_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, - 0); + void *addr_rw = mmap(addr, sandbox_size + linear_memory_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); if (addr_rw == MAP_FAILED) { munmap(addr, memory_size + PAGE_SIZE); return NULL; @@ -40,8 +40,8 @@ sandbox_memory_map(struct module *module) // can it include sandbox as well? sandbox->linear_start = (char *)addr + sandbox_size; sandbox->linear_size = linear_memory_size; - sandbox->module = module; - sandbox->sandbox_size = sandbox_size; + sandbox->module = module; + sandbox->sandbox_size = sandbox_size; module_acquire(module); return sandbox; @@ -120,16 +120,16 @@ static inline void sb_alloc_callback(uv_handle_t *h, size_t suggested, uv_buf_t *buf) { struct sandbox *sandbox = h->data; - size_t l = (sandbox->module->max_request_or_response_size - sandbox->rr_data_len); - buf->base = (sandbox->req_resp_data + sandbox->rr_data_len); - buf->len = l > suggested ? suggested : l; + size_t l = (sandbox->module->max_request_or_response_size - sandbox->rr_data_len); + buf->base = (sandbox->req_resp_data + sandbox->rr_data_len); + buf->len = l > suggested ? suggested : l; } static inline int sandbox_client_request_get(void) { struct sandbox *curr = sandbox_current(); - curr->rr_data_len = 0; + curr->rr_data_len = 0; #ifndef USE_HTTP_UVIO int r = 0; r = recv(curr->csock, (curr->req_resp_data), curr->module->max_request_size, 0); @@ -235,8 +235,8 @@ sandbox_entry(void) next_context = NULL; softint_enable(); } - struct module *curr_mod = sandbox_module(curr); - int argument_count = module_argument_count(curr_mod); + struct module *curr_mod = sandbox_module(curr); + int argument_count = module_argument_count(curr_mod); // for stdio int f = io_handle_open(0); assert(f == 0); @@ -256,8 +256,7 @@ sandbox_entry(void) r = uv_tcp_open((uv_tcp_t *)&curr->cuv, curr->csock); assert(r == 0); #endif - if (sandbox_client_request_get() > 0) - { + if (sandbox_client_request_get() > 0) { curr->rr_data_len = rsp_hdr_len; // TODO: do this on first write to body. alloc_linear_memory(); // perhaps only initialized for the first instance? or TODO! @@ -297,7 +296,7 @@ sandbox_alloc(struct module *module, char *args, int sock, const struct sockaddr sandbox->args = (void *)args; sandbox->stack_size = module->stack_size; sandbox->stack_start = mmap(NULL, sandbox->stack_size, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0); + MAP_PRIVATE | MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0); if (sandbox->stack_start == MAP_FAILED) { perror("mmap"); assert(0); diff --git a/runtime/src/softint.c b/runtime/src/softint.c index 4184327..235e354 100644 --- a/runtime/src/softint.c +++ b/runtime/src/softint.c @@ -14,7 +14,7 @@ #include __thread static volatile sig_atomic_t SIGALRM_count = 0; -__thread static volatile sig_atomic_t SIGUSR_count = 0; +__thread static volatile sig_atomic_t SIGUSR_count = 0; __thread volatile sig_atomic_t softint_off = 0; @@ -69,8 +69,8 @@ softint_alarm_schedule(void *user_context_raw) { softint_disable(); // no nesting! - struct sandbox *curr = sandbox_current(); - ucontext_t * user_context = (ucontext_t *)user_context_raw; + struct sandbox *curr = sandbox_current(); + ucontext_t * user_context = (ucontext_t *)user_context_raw; // no sandboxes running..so nothing to preempt..let the "main" scheduler run its course. if (curr == NULL) goto done; @@ -113,8 +113,8 @@ softint_handler(int signal_type, siginfo_t *signal_info, void *user_context_raw) #ifdef PREEMPT_DISABLE assert(0); #else - struct sandbox *curr = sandbox_current(); - ucontext_t * user_context = (ucontext_t *)user_context_raw; + struct sandbox *curr = sandbox_current(); + ucontext_t * user_context = (ucontext_t *)user_context_raw; switch (signal_type) { case SIGALRM: { diff --git a/runtime/src/util.c b/runtime/src/util.c index 733477c..780d46a 100644 --- a/runtime/src/util.c +++ b/runtime/src/util.c @@ -70,7 +70,8 @@ util_parse_modules_file_json(char *file_name) jsmntok_t tokens[MOD_MAX * JSON_ELE_MAX]; // Use Jasmine to parse the JSON - int total_tokens = jsmn_parse(&module_parser, file_buffer, strlen(file_buffer), tokens, sizeof(tokens) / sizeof(tokens[0])); + int total_tokens = jsmn_parse(&module_parser, file_buffer, strlen(file_buffer), tokens, + sizeof(tokens) / sizeof(tokens[0])); if (total_tokens < 0) { debuglog("jsmn_parse: invalid JSON?\n"); return -1; @@ -82,30 +83,31 @@ util_parse_modules_file_json(char *file_name) char module_name[MOD_NAME_MAX] = { 0 }; char module_path[MOD_PATH_MAX] = { 0 }; - char *request_headers = (char *)malloc(HTTP_HEADER_MAXSZ * HTTP_HEADERS_MAX); + char *request_headers = (char *)malloc(HTTP_HEADER_MAXSZ * HTTP_HEADERS_MAX); memset(request_headers, 0, HTTP_HEADER_MAXSZ * HTTP_HEADERS_MAX); char *reponse_headers = (char *)malloc(HTTP_HEADER_MAXSZ * HTTP_HEADERS_MAX); memset(reponse_headers, 0, HTTP_HEADER_MAXSZ * HTTP_HEADERS_MAX); - i32 request_size = 0; - i32 response_size = 0; - i32 argument_count = 0; - u32 port = 0; - i32 is_active = 0; - i32 request_count = 0; - i32 response_count = 0; - int j = 1; - int ntoks = 2 * tokens[i].size; - char request_content_type[HTTP_HEADERVAL_MAXSZ] = { 0 }; - char response_content_type[HTTP_HEADERVAL_MAXSZ] = { 0 }; - - for (; j < ntoks; ) { + i32 request_size = 0; + i32 response_size = 0; + i32 argument_count = 0; + u32 port = 0; + i32 is_active = 0; + i32 request_count = 0; + i32 response_count = 0; + int j = 1; + int ntoks = 2 * tokens[i].size; + char request_content_type[HTTP_HEADERVAL_MAXSZ] = { 0 }; + char response_content_type[HTTP_HEADERVAL_MAXSZ] = { 0 }; + + for (; j < ntoks;) { int ntks = 1; - char key[32] = { 0 }; + char key[32] = { 0 }; char val[256] = { 0 }; sprintf(val, "%.*s", tokens[j + i + 1].end - tokens[j + i + 1].start, file_buffer + tokens[j + i + 1].start); - sprintf(key, "%.*s", tokens[j + i].end - tokens[j + i].start, file_buffer + tokens[j + i].start); + sprintf(key, "%.*s", tokens[j + i].end - tokens[j + i].start, + file_buffer + tokens[j + i].start); if (strcmp(key, "name") == 0) { strcpy(module_name, val); } else if (strcmp(key, "path") == 0) { @@ -160,9 +162,11 @@ util_parse_modules_file_json(char *file_name) if (is_active == 0) continue; // Allocate a module based on the values from the JSON - struct module *module = module_alloc(module_name, module_path, argument_count, 0, 0, 0, port, request_size, response_size); + struct module *module = module_alloc(module_name, module_path, argument_count, 0, 0, 0, port, + request_size, response_size); assert(module); - module_http_info(module, request_count, request_headers, request_content_type, response_count, reponse_headers, response_content_type); + module_http_info(module, request_count, request_headers, request_content_type, response_count, + reponse_headers, response_content_type); module_count++; free(request_headers); free(reponse_headers);