From 4178e4f9bccf61e824de1eb3d1edd61fed9925f3 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 16 Mar 2020 14:55:06 -0400 Subject: [PATCH] chore: partial sandbox cleanup --- runtime/include/http_api.h | 12 ++-- runtime/include/sandbox.h | 98 ++++++++++++++----------------- runtime/src/libc/uvio.c | 104 ++++++++++++++++----------------- runtime/src/memory/64bit_nix.c | 6 +- runtime/src/runtime.c | 12 ++-- runtime/src/sandbox.c | 41 +++++++------ runtime/src/softint.c | 8 +-- 7 files changed, 134 insertions(+), 147 deletions(-) diff --git a/runtime/include/http_api.h b/runtime/include/http_api.h index bed34fb..d387137 100644 --- a/runtime/include/http_api.h +++ b/runtime/include/http_api.h @@ -16,7 +16,7 @@ int sandbox__get_http_request_body(struct sandbox *sandbox, char **body); static inline int current_sandbox__get_http_request_body(char **body) { - return sandbox__get_http_request_body(get_current_sandbox(), body); + return sandbox__get_http_request_body(current_sandbox__get(), body); } /*************************************************** @@ -36,7 +36,7 @@ int sandbox__vectorize_http_response(struct sandbox *sandbox); static inline int current_sandbox__set_http_response_header(char *header, int length) { - return sandbox__set_http_response_header(get_current_sandbox(), header, length); + return sandbox__set_http_response_header(current_sandbox__get(), header, length); } /** @@ -48,7 +48,7 @@ current_sandbox__set_http_response_header(char *header, int length) static inline int current_sandbox__set_http_response_body(char *body, int length) { - return sandbox__set_http_response_body(get_current_sandbox(), body, length); + return sandbox__set_http_response_body(current_sandbox__get(), body, length); } /** @@ -60,7 +60,7 @@ current_sandbox__set_http_response_body(char *body, int length) static inline int current_sandbox__set_http_response_status(char *status, int length) { - return sandbox__set_http_response_status(get_current_sandbox(), status, length); + return sandbox__set_http_response_status(current_sandbox__get(), status, length); } /** @@ -70,7 +70,7 @@ current_sandbox__set_http_response_status(char *status, int length) static inline int current_sandbox__vectorize_http_response(void) { - return sandbox__vectorize_http_response(get_current_sandbox()); + return sandbox__vectorize_http_response(current_sandbox__get()); } /*********************************************************************** @@ -90,7 +90,7 @@ int sandbox__parse_http_request(struct sandbox *sandbox, size_t l); static inline int current_sandbox__parse_http_request(size_t length) { - return sandbox__parse_http_request(get_current_sandbox(), length); + return sandbox__parse_http_request(current_sandbox__get(), length); } #endif /* SRFT_HTTP_API_H */ diff --git a/runtime/include/sandbox.h b/runtime/include/sandbox.h index 39fa83e..1f66480 100644 --- a/runtime/include/sandbox.h +++ b/runtime/include/sandbox.h @@ -78,15 +78,16 @@ struct sandbox { } PAGE_ALIGNED; // a runtime resource, malloc on this! -struct sandbox *allocate_sandbox(struct module *module, char *arguments, int socket_descriptor, const struct sockaddr *socket_address, u64 start_time); +struct sandbox *sandbox__allocate(struct module *module, char *arguments, int socket_descriptor, const struct sockaddr *socket_address, u64 start_time); // should free stack and heap resources.. also any I/O handles. -void free_sandbox(struct sandbox *sandbox); +void sandbox__free(struct sandbox *sandbox); extern __thread struct sandbox *current_sandbox; // next_sandbox only used in SIGUSR1 extern __thread arch_context_t *next_context; typedef struct sandbox sandbox_t; +extern void add_sandbox_to_completion_queue(struct sandbox *sandbox); /** @@ -94,7 +95,7 @@ typedef struct sandbox sandbox_t; * @returns the current sandbox executing on this thread **/ static inline struct sandbox * -get_current_sandbox(void) +current_sandbox__get(void) { return current_sandbox; } @@ -104,7 +105,7 @@ get_current_sandbox(void) * @param sandbox the sandbox we are setting this thread to run **/ static inline void -set_current_sandbox(struct sandbox *sandbox) +current_sandbox__set(struct sandbox *sandbox) { // FIXME: critical-section. current_sandbox = sandbox; @@ -116,60 +117,27 @@ set_current_sandbox(struct sandbox *sandbox) module_indirect_table = sandbox->module->indirect_table; } -/** - * Check that the current_sandbox struct matches the rest of the thread local state about the executing sandbox - * This includes lmbase, lmbound, and module_indirect_table - */ -static inline void -check_current_sandbox(void) -{ - struct sandbox *sandbox = get_current_sandbox(); - assert(sandbox && sandbox->linear_memory_start == sandbox_lmbase && sandbox->linear_memory_size == sandbox_lmbound); - assert(sandbox->module->indirect_table == module_indirect_table); -} - /** * Given a sandbox, returns the module that sandbox is executing * @param sandbox the sandbox whose module we want * @return the module of the provided sandbox */ static inline struct module * -get_sandbox_module(struct sandbox *sandbox) +sandbox__get_module(struct sandbox *sandbox) { if (!sandbox) return NULL; return sandbox->module; } -extern void add_sandbox_to_completion_queue(struct sandbox *sandbox); - -/** - * @brief Switches to the next sandbox, placing the current sandbox of the completion queue if in RETURNED state - * @param next The Sandbox Context to switch to or NULL - * @return void - */ -static inline void -switch_to_sandbox(struct sandbox *next_sandbox) -{ - arch_context_t *next_register_context = next_sandbox == NULL ? NULL : &next_sandbox->ctxt; - softint__disable(); - struct sandbox *current_sandbox = get_current_sandbox(); - arch_context_t *current_register_context = current_sandbox == NULL ? NULL : ¤t_sandbox->ctxt; - set_current_sandbox(next_sandbox); - // If the current sandbox we're switching from is in a RETURNED state, add to completion queue - if (current_sandbox && current_sandbox->state == RETURNED) add_sandbox_to_completion_queue(current_sandbox); - next_context = next_register_context; - arch_context_switch(current_register_context, next_register_context); - softint__enable(); -} /** * Getter for the arguments of the current sandbox * @return the arguments of the current sandbox */ static inline char * -get_current_sandbox_arguments(void) +current_sandbox__get_arguments(void) { - struct sandbox *sandbox = get_current_sandbox(); + struct sandbox *sandbox = current_sandbox__get(); return (char *)sandbox->arguments; } @@ -183,18 +151,18 @@ void sandbox_block_http(void); void sandbox_response(void); // should be the entry-point for each sandbox so it can do per-sandbox mem/etc init. -// should have been called with stack allocated and get_current_sandbox() set! +// should have been called with stack allocated and current_sandbox__get() set! void sandbox_main(void); -void exit_current_sandbox(void); +void current_sandbox__exit(void); /** * Initializes and returns an IO handle on the current sandbox ready for use * @return index of handle we preopened or -1 if all handles are exhausted **/ static inline int -initialize_io_handle_in_current_sandbox(void) +current_sandbox__initialize_io_handle(void) { - struct sandbox *sandbox = get_current_sandbox(); + struct sandbox *sandbox = current_sandbox__get(); int handle_index; for (handle_index = 0; handle_index < SBOX_MAX_OPEN; handle_index++) { if (sandbox->handles[handle_index].file_descriptor < 0) break; @@ -211,11 +179,11 @@ initialize_io_handle_in_current_sandbox(void) * @return index of handle we preopened or -1 if all handles are exhausted **/ static inline int -initialize_io_handle_and_set_file_descriptor_in_current_sandbox(int file_descriptor) +current_sandbox__initialize_io_handle_and_set_file_descriptor(int file_descriptor) { - struct sandbox *sandbox = get_current_sandbox(); + struct sandbox *sandbox = current_sandbox__get(); if (file_descriptor < 0) return file_descriptor; - int handle_index = initialize_io_handle_in_current_sandbox(); + int handle_index = current_sandbox__initialize_io_handle(); if (handle_index != -1) sandbox->handles[handle_index].file_descriptor = file_descriptor; // well, per sandbox.. so synchronization necessary! return handle_index; } @@ -228,9 +196,9 @@ initialize_io_handle_and_set_file_descriptor_in_current_sandbox(int file_descrip * @returns the index that was set or -1 in case of error **/ static inline int -set_current_sandbox_file_descriptor(int handle_index, int file_descriptor) +current_sandbox__set_file_descriptor(int handle_index, int file_descriptor) { - struct sandbox *sandbox = get_current_sandbox(); + struct sandbox *sandbox = current_sandbox__get(); if (handle_index >= SBOX_MAX_OPEN || handle_index < 0) return -1; if (file_descriptor < 0 || sandbox->handles[handle_index].file_descriptor != SBOX_PREOPEN_MAGIC) return -1; sandbox->handles[handle_index].file_descriptor = file_descriptor; @@ -243,9 +211,9 @@ set_current_sandbox_file_descriptor(int handle_index, int file_descriptor) * @returns file descriptor **/ static inline int -get_current_sandbox_file_descriptor(int handle_index) +current_sandbox__get_file_descriptor(int handle_index) { - struct sandbox *sandbox = get_current_sandbox(); + struct sandbox *sandbox = current_sandbox__get(); if (handle_index >= SBOX_MAX_OPEN || handle_index < 0) return -1; return sandbox->handles[handle_index].file_descriptor; } @@ -255,9 +223,9 @@ get_current_sandbox_file_descriptor(int handle_index) * @param handle_index index of the handle to close **/ static inline void -close_current_sandbox_file_descriptor(int handle_index) +current_sandbox__close_file_descriptor(int handle_index) { - struct sandbox *sandbox = get_current_sandbox(); + struct sandbox *sandbox = current_sandbox__get(); if (handle_index >= SBOX_MAX_OPEN || handle_index < 0) return; // TODO: Do we actually need to call some sort of close function here? sandbox->handles[handle_index].file_descriptor = -1; @@ -269,11 +237,31 @@ close_current_sandbox_file_descriptor(int handle_index) * @returns any libuv handle **/ static inline union uv_any_handle * -get_current_sandbox_libuv_handle(int handle_index) +current_sandbox__get_libuv_handle(int handle_index) { - struct sandbox *sandbox = get_current_sandbox(); + struct sandbox *sandbox = current_sandbox__get(); if (handle_index >= SBOX_MAX_OPEN || handle_index < 0) return NULL; return &sandbox->handles[handle_index].libuv_handle; } +/** + * @brief Switches to the next sandbox, placing the current sandbox of the completion queue if in RETURNED state + * @param next The Sandbox Context to switch to or NULL + * @return void + */ +static inline void +switch_to_sandbox(struct sandbox *next_sandbox) +{ + arch_context_t *next_register_context = next_sandbox == NULL ? NULL : &next_sandbox->ctxt; + softint__disable(); + struct sandbox *current_sandbox = current_sandbox__get(); + arch_context_t *current_register_context = current_sandbox == NULL ? NULL : ¤t_sandbox->ctxt; + current_sandbox__set(next_sandbox); + // If the current sandbox we're switching from is in a RETURNED state, add to completion queue + if (current_sandbox && current_sandbox->state == RETURNED) add_sandbox_to_completion_queue(current_sandbox); + next_context = next_register_context; + arch_context_switch(current_register_context, next_register_context); + softint__enable(); +} + #endif /* SFRT_SANDBOX_H */ diff --git a/runtime/src/libc/uvio.c b/runtime/src/libc/uvio.c index 0ef4d46..ffdec32 100644 --- a/runtime/src/libc/uvio.c +++ b/runtime/src/libc/uvio.c @@ -33,7 +33,7 @@ void stub_init(i32 offset) { // What program name will we put in the auxiliary vectors - char *program_name = get_current_sandbox()->module->name; + char *program_name = current_sandbox__get()->module->name; // Copy the program name into WASM accessible memory i32 program_name_offset = offset; strcpy(get_memory_ptr_for_runtime(offset, sizeof(program_name)), program_name); @@ -64,7 +64,7 @@ stub_init(i32 offset) i32 env_vec_offset = offset; memcpy(get_memory_ptr_for_runtime(env_vec_offset, sizeof(env_vec)), env_vec, sizeof(env_vec)); - module__initialize_libc(get_current_sandbox()->module, env_vec_offset, program_name_offset); + module__initialize_libc(current_sandbox__get()->module, env_vec_offset, program_name_offset); } // Emulated syscall implementations @@ -88,7 +88,7 @@ uv_fs_get_type(uv_fs_t *req) #define UV_FS_REQ_INIT() \ { \ - .data = get_current_sandbox(), .result = 0 \ + .data = current_sandbox__get(), .result = 0 \ } static void @@ -105,7 +105,7 @@ wasm_read(i32 filedes, i32 buf_offset, i32 nbyte) { if (filedes == 0) { char * buffer = get_memory_ptr_void(buf_offset, nbyte); - struct sandbox * s = get_current_sandbox(); + struct sandbox * s = current_sandbox__get(); struct http_request *r = &s->http_request; if (r->body_length <= 0) return 0; int l = nbyte > r->body_length ? r->body_length : nbyte; @@ -114,7 +114,7 @@ wasm_read(i32 filedes, i32 buf_offset, i32 nbyte) r->body_length -= l; return l; } - int f = get_current_sandbox_file_descriptor(filedes); + int f = current_sandbox__get_file_descriptor(filedes); // TODO: read on other file types uv_fs_t req = UV_FS_REQ_INIT(); char * buffer = get_memory_ptr_void(buf_offset, nbyte); @@ -137,7 +137,7 @@ wasm_write(i32 file_descriptor, i32 buf_offset, i32 buf_size) { if (file_descriptor == 1 || file_descriptor == 2) { char * buffer = get_memory_ptr_void(buf_offset, buf_size); - struct sandbox *s = get_current_sandbox(); + struct sandbox *s = current_sandbox__get(); int l = s->module->max_response_size - s->request_response_data_length; l = l > buf_size ? buf_size : l; if (l == 0) return 0; @@ -146,7 +146,7 @@ wasm_write(i32 file_descriptor, i32 buf_offset, i32 buf_size) return l; } - int f = get_current_sandbox_file_descriptor(file_descriptor); + int f = current_sandbox__get_file_descriptor(file_descriptor); // TODO: read on other file types uv_fs_t req = UV_FS_REQ_INIT(); char * buffer = get_memory_ptr_void(buf_offset, buf_size); @@ -184,7 +184,7 @@ wasm_open(i32 path_off, i32 flags, i32 mode) uv_fs_t req = UV_FS_REQ_INIT(); char * path = get_memory_string(path_off); - int iofd = initialize_io_handle_in_current_sandbox(); + int iofd = current_sandbox__initialize_io_handle(); if (iofd < 0) return -1; i32 modified_flags = 0; if (flags & WO_RDONLY) modified_flags |= O_RDONLY; @@ -202,9 +202,9 @@ wasm_open(i32 path_off, i32 flags, i32 mode) debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); uv_fs_req_cleanup(&req); if (ret < 0) - close_current_sandbox_file_descriptor(iofd); + current_sandbox__close_file_descriptor(iofd); else - set_current_sandbox_file_descriptor(iofd, ret); + current_sandbox__set_file_descriptor(iofd, ret); return iofd; } @@ -214,9 +214,9 @@ i32 wasm_close(i32 file_descriptor) { if (file_descriptor >= 0 && file_descriptor <= 2) { return 0; } - struct sandbox * c = get_current_sandbox(); - int d = get_current_sandbox_file_descriptor(file_descriptor); - union uv_any_handle *h = get_current_sandbox_libuv_handle(file_descriptor); + struct sandbox * c = current_sandbox__get(); + int d = current_sandbox__get_file_descriptor(file_descriptor); + union uv_any_handle *h = current_sandbox__get_libuv_handle(file_descriptor); uv_handle_type type = ((uv_handle_t *)h)->type; debuglog("[%p] [%d,%d]\n", c, file_descriptor, d); @@ -238,7 +238,7 @@ wasm_close(i32 file_descriptor) int ret = uv_fs_get_result(&req); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); uv_fs_req_cleanup(&req); - if (ret == 0) close_current_sandbox_file_descriptor(file_descriptor); + if (ret == 0) current_sandbox__close_file_descriptor(file_descriptor); return ret; } @@ -344,7 +344,7 @@ wasm_fstat(i32 filedes, i32 stat_offset) struct wasm_stat *stat_ptr = get_memory_ptr_void(stat_offset, sizeof(struct wasm_stat)); struct stat stat; - int d = get_current_sandbox_file_descriptor(filedes); + int d = current_sandbox__get_file_descriptor(filedes); i32 res = fstat(d, &stat); if (res == -1) return -errno; @@ -434,7 +434,7 @@ wasm_lstat(i32 path_str_offset, i32 stat_offset) i32 wasm_lseek(i32 filedes, i32 file_offset, i32 whence) { - int d = get_current_sandbox_file_descriptor(filedes); + int d = current_sandbox__get_file_descriptor(filedes); i32 res = (i32)lseek(d, file_offset, whence); if (res == -1) return -errno; @@ -446,7 +446,7 @@ wasm_lseek(i32 filedes, i32 file_offset, i32 whence) u32 wasm_mmap(i32 addr, i32 len, i32 prot, i32 flags, i32 file_descriptor, i32 offset) { - int d = get_current_sandbox_file_descriptor(file_descriptor); + int d = current_sandbox__get_file_descriptor(file_descriptor); if (file_descriptor >= 0) assert(d >= 0); if (addr != 0) { printf("parameter void *addr is not supported!\n"); @@ -478,7 +478,7 @@ wasm_mmap(i32 addr, i32 len, i32 prot, i32 flags, i32 file_descriptor, i32 offse i32 wasm_ioctl(i32 file_descriptor, i32 request, i32 data_offet) { - // int d = get_current_sandbox_file_descriptor(file_descriptor); + // int d = current_sandbox__get_file_descriptor(file_descriptor); // musl libc does some ioctls to stdout, so just allow these to silently go through // FIXME: The above is idiotic // assert(d == 1); @@ -498,7 +498,7 @@ wasm_readv(i32 file_descriptor, i32 iov_offset, i32 iovcnt) // 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 = get_current_sandbox(); + struct sandbox * s = current_sandbox__get(); struct http_request *r = &s->http_request; if (r->body_length <= 0) return 0; for (int i = 0; i < iovcnt; i++) { @@ -516,7 +516,7 @@ wasm_readv(i32 file_descriptor, i32 iov_offset, i32 iovcnt) } // TODO: read on other file types int gret = 0; - int d = get_current_sandbox_file_descriptor(file_descriptor); + int d = current_sandbox__get_file_descriptor(file_descriptor); struct wasm_iovec *iov = get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); for (int i = 0; i < iovcnt; i += RDWR_VEC_MAX) { @@ -547,7 +547,7 @@ wasm_readv(i32 file_descriptor, i32 iov_offset, i32 iovcnt) i32 wasm_writev(i32 file_descriptor, i32 iov_offset, i32 iovcnt) { - struct sandbox *c = get_current_sandbox(); + struct sandbox *c = current_sandbox__get(); if (file_descriptor == 1 || file_descriptor == 2) { // both 1 and 2 go to client. int len = 0; @@ -562,7 +562,7 @@ wasm_writev(i32 file_descriptor, i32 iov_offset, i32 iovcnt) return len; } // TODO: read on other file types - int d = get_current_sandbox_file_descriptor(file_descriptor); + int d = current_sandbox__get_file_descriptor(file_descriptor); int gret = 0; struct wasm_iovec *iov = get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); @@ -619,7 +619,7 @@ wasm_getpid() u32 wasm_fcntl(u32 file_descriptor, u32 cmd, u32 arg_or_lock_ptr) { - int d = get_current_sandbox_file_descriptor(file_descriptor); + int d = current_sandbox__get_file_descriptor(file_descriptor); switch (cmd) { case WF_SETFD: // return fcntl(d, F_SETFD, arg_or_lock_ptr); @@ -635,7 +635,7 @@ wasm_fcntl(u32 file_descriptor, u32 cmd, u32 arg_or_lock_ptr) u32 wasm_fsync(u32 file_descriptor) { - int d = get_current_sandbox_file_descriptor(file_descriptor); + int d = current_sandbox__get_file_descriptor(file_descriptor); uv_fs_t req = UV_FS_REQ_INIT(); debuglog("[%p] start[%d,%d]\n", uv_fs_get_data(&req), file_descriptor, d); uv_fs_fsync(get_thread_libuv_handle(), &req, d, wasm_fs_callback); @@ -735,7 +735,7 @@ wasm_exit_group(i32 status) i32 wasm_fchown(i32 file_descriptor, u32 owner, u32 group) { - int d = get_current_sandbox_file_descriptor(file_descriptor); + int d = current_sandbox__get_file_descriptor(file_descriptor); uv_fs_t req = UV_FS_REQ_INIT(); debuglog("[%p] start[%d,%d]\n", uv_fs_get_data(&req), file_descriptor, d); uv_fs_fchown(get_thread_libuv_handle(), &req, d, owner, group, wasm_fs_callback); @@ -777,12 +777,12 @@ wasm_connect_callback(uv_connect_t *req, int status) i32 wasm_socket(i32 domain, i32 type, i32 protocol) { - struct sandbox *c = get_current_sandbox(); - int pfd = initialize_io_handle_in_current_sandbox(), file_descriptor = -1; + struct sandbox *c = current_sandbox__get(); + int pfd = current_sandbox__initialize_io_handle(), file_descriptor = -1; // TODO: use domain, type and protocol in libuv? debuglog("[%p] => %d %d %d\n", c, domain, type, protocol); if (pfd < 0) return pfd; - union uv_any_handle *h = get_current_sandbox_libuv_handle(pfd); + union uv_any_handle *h = current_sandbox__get_libuv_handle(pfd); if (type & SOCK_DGRAM) { uv_udp_t *uh = (uv_udp_t *)h; @@ -804,10 +804,10 @@ wasm_socket(i32 domain, i32 type, i32 protocol) i32 wasm_connect(i32 sockfd, i32 sockaddr_offset, i32 addrlen) { - struct sandbox *c = get_current_sandbox(); - int file_descriptor = get_current_sandbox_file_descriptor(sockfd); + struct sandbox *c = current_sandbox__get(); + int file_descriptor = current_sandbox__get_file_descriptor(sockfd); debuglog("[%p] [%d, %d]\n", c, sockfd, file_descriptor); - union uv_any_handle *h = get_current_sandbox_libuv_handle(sockfd); + union uv_any_handle *h = current_sandbox__get_libuv_handle(sockfd); uv_handle_type t = ((uv_handle_t *)h)->type; if (t == UV_TCP) { @@ -835,27 +835,27 @@ wasm_accept(i32 sockfd, i32 sockaddr_offset, i32 addrlen_offset) // what do we do with the sockaddr TODO: ???? socklen_t * addrlen = get_memory_ptr_void(addrlen_offset, sizeof(socklen_t)); struct sockaddr * socket_address = get_memory_ptr_void(sockaddr_offset, *addrlen); - union uv_any_handle *s = get_current_sandbox_libuv_handle(sockfd); - int cfd = initialize_io_handle_in_current_sandbox(); + union uv_any_handle *s = current_sandbox__get_libuv_handle(sockfd); + int cfd = current_sandbox__initialize_io_handle(); if (cfd < 0) return -1; - struct sandbox *c = get_current_sandbox(); - debuglog("[%p] [%d, %d]\n", c, sockfd, get_current_sandbox_file_descriptor(sockfd)); + struct sandbox *c = current_sandbox__get(); + debuglog("[%p] [%d, %d]\n", c, sockfd, current_sandbox__get_file_descriptor(sockfd)); // assert so we can look into whether we need to implement UDP or others.. assert(((uv_handle_t *)s)->type == UV_TCP); - union uv_any_handle *h = get_current_sandbox_libuv_handle(cfd); + union uv_any_handle *h = current_sandbox__get_libuv_handle(cfd); uv_tcp_init(get_thread_libuv_handle(), (uv_tcp_t *)h); debuglog("[%p] tcp init %d\n", c, cfd); int r = uv_accept((uv_stream_t *)s, (uv_stream_t *)h); if (r < 0) return r; // TODO: if accept fails, what do we do with the preopened handle? - // if (r < 0) close_current_sandbox_file_descriptor(cfd); + // if (r < 0) current_sandbox__close_file_descriptor(cfd); // we've to also remove it from the runtime loop?? int r2 = -1, f = -1; r2 = uv_fileno((uv_handle_t *)h, &f); if (r2 < 0 || f < 0) assert(0); - set_current_sandbox_file_descriptor(cfd, f); + current_sandbox__set_file_descriptor(cfd, f); debuglog("[%p] done[%d,%d]\n", c, cfd, f); return cfd; @@ -864,10 +864,10 @@ wasm_accept(i32 sockfd, i32 sockaddr_offset, i32 addrlen_offset) i32 wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen) { - struct sandbox *c = get_current_sandbox(); - int file_descriptor = get_current_sandbox_file_descriptor(sockfd); + struct sandbox *c = current_sandbox__get(); + int file_descriptor = current_sandbox__get_file_descriptor(sockfd); debuglog("[%p] [%d,%d]\n", c, sockfd, file_descriptor); - union uv_any_handle *h = get_current_sandbox_libuv_handle(sockfd); + union uv_any_handle *h = current_sandbox__get_libuv_handle(sockfd); uv_handle_type t = ((uv_handle_t *)h)->type; if (t == UV_TCP) { @@ -877,7 +877,7 @@ wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen) int r2 = -1, f = -1; r2 = uv_fileno((uv_handle_t *)h, &f); debuglog("[%p] [%d,%d]\n", c, f, file_descriptor); - set_current_sandbox_file_descriptor(sockfd, f); + current_sandbox__set_file_descriptor(sockfd, f); } return r1; } else if (t == UV_UDP) { @@ -887,7 +887,7 @@ wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen) int r2 = -1, f = -1; r2 = uv_fileno((uv_handle_t *)h, &f); debuglog("[%p] [%d,%d]\n", c, f, file_descriptor); - set_current_sandbox_file_descriptor(sockfd, f); + current_sandbox__set_file_descriptor(sockfd, f); } return r1; } @@ -899,10 +899,10 @@ wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen) i32 wasm_listen(i32 sockfd, i32 backlog) { - struct sandbox * c = get_current_sandbox(); - union uv_any_handle *h = get_current_sandbox_libuv_handle(sockfd); + struct sandbox * c = current_sandbox__get(); + union uv_any_handle *h = current_sandbox__get_libuv_handle(sockfd); assert(c == (struct sandbox *)(((uv_tcp_t *)h)->data)); - debuglog("[%p] [%d,%d]\n", c, sockfd, get_current_sandbox_file_descriptor(sockfd)); + debuglog("[%p] [%d,%d]\n", c, sockfd, current_sandbox__get_file_descriptor(sockfd)); uv_handle_type t = ((uv_handle_t *)h)->type; // assert so we can look into whether we need to implement UDP or others.. @@ -970,10 +970,10 @@ wasm_sendto(i32 file_descriptor, i32 buff_offset, i32 len, i32 flags, i32 sockad char *buffer = get_memory_ptr_void(buff_offset, len); // TODO: only support "send" api for now assert(sockaddr_len == 0); - struct sandbox * c = get_current_sandbox(); - union uv_any_handle *h = get_current_sandbox_libuv_handle(file_descriptor); + struct sandbox * c = current_sandbox__get(); + union uv_any_handle *h = current_sandbox__get_libuv_handle(file_descriptor); uv_handle_type t = ((uv_handle_t *)h)->type; - debuglog("[%p] [%d,%d]\n", c, file_descriptor, get_current_sandbox_file_descriptor(file_descriptor)); + debuglog("[%p] [%d,%d]\n", c, file_descriptor, current_sandbox__get_file_descriptor(file_descriptor)); if (t == UV_TCP) { uv_write_t req = { @@ -1021,10 +1021,10 @@ wasm_recvfrom(i32 file_descriptor, i32 buff_offset, i32 size, i32 flags, i32 soc 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 = get_current_sandbox(); - union uv_any_handle *h = get_current_sandbox_libuv_handle(file_descriptor); + struct sandbox * c = current_sandbox__get(); + union uv_any_handle *h = current_sandbox__get_libuv_handle(file_descriptor); uv_handle_type t = ((uv_handle_t *)h)->type; - debuglog("[%p] [%d,%d]\n", c, file_descriptor, get_current_sandbox_file_descriptor(file_descriptor)); + debuglog("[%p] [%d,%d]\n", c, file_descriptor, current_sandbox__get_file_descriptor(file_descriptor)); // uv stream API are not simple wrappers on read/write.. // and there will only be one system call pending.. diff --git a/runtime/src/memory/64bit_nix.c b/runtime/src/memory/64bit_nix.c index 7d72625..3cae58b 100644 --- a/runtime/src/memory/64bit_nix.c +++ b/runtime/src/memory/64bit_nix.c @@ -11,19 +11,19 @@ void alloc_linear_memory(void) { - // mmaped memory in allocate_sandbox. + // mmaped memory in sandbox__allocate. } void free_linear_memory(void *base, u32 bound, u32 max) { - // frees on free_sandbox + // frees on sandbox__free } void expand_memory(void) { - struct sandbox *sandbox = get_current_sandbox(); + struct sandbox *sandbox = current_sandbox__get(); // max_pages = 0 => no limit: FIXME assert((sandbox->sandbox_size + sandbox_lmbound) / WASM_PAGE_SIZE < WASM_MAX_PAGES); diff --git a/runtime/src/runtime.c b/runtime/src/runtime.c index c467b15..5d7d26c 100644 --- a/runtime/src/runtime.c +++ b/runtime/src/runtime.c @@ -189,7 +189,7 @@ block_current_sandbox(void) { assert(in_callback == 0); softint__disable(); - struct sandbox *current_sandbox = get_current_sandbox(); + struct sandbox *current_sandbox = current_sandbox__get(); ps_list_rem_d(current_sandbox); current_sandbox->state = BLOCKED; struct sandbox *next_sandbox = get_next_sandbox_from_local_run_queue(0); @@ -246,7 +246,7 @@ pull_sandbox_requests_from_global_runqueue(void) sandbox_request_t *sandbox_request; if ((sandbox_request = steal_sandbox_request_from_global_dequeue()) == NULL) break; // Actually allocate the sandbox for the requests that we've pulled - struct sandbox *sandbox = allocate_sandbox(sandbox_request->module, sandbox_request->arguments, + struct sandbox *sandbox = sandbox__allocate(sandbox_request->module, sandbox_request->arguments, sandbox_request->socket_descriptor, sandbox_request->socket_address, sandbox_request->start_time); assert(sandbox); @@ -354,7 +354,7 @@ free_sandboxes_from_completion_queue(unsigned int number_to_free) struct sandbox *sandbox = ps_list_head_first_d(&local_completion_queue, struct sandbox); if (!sandbox) break; ps_list_rem_d(sandbox); - free_sandbox(sandbox); + sandbox__free(sandbox); } } @@ -366,7 +366,7 @@ free_sandboxes_from_completion_queue(unsigned int number_to_free) struct sandbox * worker_thread_single_loop(void) { - assert(get_current_sandbox() == NULL); + assert(current_sandbox__get() == NULL); // Try to free one sandbox from the completion queue free_sandboxes_from_completion_queue(1); // Execute libuv callbacks @@ -422,9 +422,9 @@ worker_thread_main(void *return_code) * TODO: Does this belong in sandbox.c? **/ void -exit_current_sandbox(void) +current_sandbox__exit(void) { - struct sandbox *current_sandbox = get_current_sandbox(); + struct sandbox *current_sandbox = current_sandbox__get(); assert(current_sandbox); softint__disable(); remove_sandbox_from_local_run_queue(current_sandbox); diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index 49119e4..85b3665 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -138,10 +138,10 @@ allocate_sandbox_memory(struct module *module) * @param argument_count **/ static inline void -setup_sandbox_arguments(i32 argument_count) +current_sandbox__setup_arguments(i32 argument_count) { - struct sandbox *curr = get_current_sandbox(); - char * arguments = get_current_sandbox_arguments(); + struct sandbox *curr = current_sandbox__get(); + char * arguments = current_sandbox__get_arguments(); // whatever gregor has, to be able to pass arguments to a module! curr->arguments_offset = sandbox_lmbound; @@ -169,9 +169,9 @@ setup_sandbox_arguments(i32 argument_count) * @return 1 on success, 0 if no context, < 0 on failure. **/ static inline int -receive_and_parse_current_sandbox_client_request(void) +current_sandbox__receive_and_parse_client_request(void) { - struct sandbox *curr = get_current_sandbox(); + struct sandbox *curr = current_sandbox__get(); curr->request_response_data_length = 0; #ifndef USE_HTTP_UVIO int r = 0; @@ -206,10 +206,10 @@ receive_and_parse_current_sandbox_client_request(void) * @return RC. -1 on Failure **/ static inline int -build_and_send_current_sandbox_client_response(void) +current_sandbox__build_and_send_client_response(void) { int sndsz = 0; - struct sandbox *curr = get_current_sandbox(); + struct sandbox *curr = current_sandbox__get(); int response_header_length = strlen(HTTP_RESP_200OK) + strlen(HTTP_RESP_CONTTYPE) + strlen(HTTP_RESP_CONTLEN); int body_length = curr->request_response_data_length - response_header_length; @@ -273,7 +273,7 @@ done: void sandbox_main(void) { - struct sandbox *current_sandbox = get_current_sandbox(); + struct sandbox *current_sandbox = current_sandbox__get(); // FIXME: is this right? this is the first time this sandbox is running.. so it wont // return to switch_to_sandbox() api.. // we'd potentially do what we'd in switch_to_sandbox() api here for cleanup.. @@ -282,18 +282,18 @@ sandbox_main(void) next_context = NULL; softint__enable(); } - struct module *current_module = get_sandbox_module(current_sandbox); + struct module *current_module = sandbox__get_module(current_sandbox); int argument_count = module__get_argument_count(current_module); // for stdio // Try to initialize file descriptors 0, 1, and 2 as io handles 0, 1, 2 // We need to check that we get what we expect, as these IO handles may theoretically have been taken // TODO: why do the file descriptors have to match the io handles? - int f = initialize_io_handle_and_set_file_descriptor_in_current_sandbox(0); + int f = current_sandbox__initialize_io_handle_and_set_file_descriptor(0); assert(f == 0); - f = initialize_io_handle_and_set_file_descriptor_in_current_sandbox(1); + f = current_sandbox__initialize_io_handle_and_set_file_descriptor(1); assert(f == 1); - f = initialize_io_handle_and_set_file_descriptor_in_current_sandbox(2); + f = current_sandbox__initialize_io_handle_and_set_file_descriptor(2); assert(f == 2); // Initialize the HTTP-Parser for a request @@ -320,7 +320,7 @@ sandbox_main(void) #endif // If the HTTP Request returns 1, we've successfully received and parsed the HTTP request, so execute it! - if (receive_and_parse_current_sandbox_client_request() > 0) { + if (current_sandbox__receive_and_parse_client_request() > 0) { // current_sandbox->request_response_data_length = response_header_length; @@ -331,13 +331,13 @@ sandbox_main(void) module__initialize_memory(current_module); // Copy the arguments into the WebAssembly sandbox - setup_sandbox_arguments(argument_count); + current_sandbox__setup_arguments(argument_count); // Executing the function within the WebAssembly sandbox current_sandbox->return_value = module__main(current_module, argument_count, current_sandbox->arguments_offset); // Retrieve the result from the WebAssembly sandbox, construct the HTTP response, and send to client - build_and_send_current_sandbox_client_response(); + current_sandbox__build_and_send_client_response(); } // Cleanup connection and exit sandbox @@ -348,11 +348,11 @@ sandbox_main(void) #else close(current_sandbox->client_socket_descriptor); #endif - exit_current_sandbox(); + current_sandbox__exit(); } struct sandbox * -allocate_sandbox(struct module *module, char *arguments, int socket_descriptor, const struct sockaddr *socket_address, u64 start_time) +sandbox__allocate(struct module *module, char *arguments, int socket_descriptor, const struct sockaddr *socket_address, u64 start_time) { if (!module__is_valid(module)) return NULL; @@ -384,12 +384,10 @@ allocate_sandbox(struct module *module, char *arguments, int socket_descriptor, } void -free_sandbox(struct sandbox *sandbox) +sandbox__free(struct sandbox *sandbox) { - int ret; - // you have to context switch away to free a sandbox. - if (!sandbox || sandbox == get_current_sandbox()) return; + if (!sandbox || sandbox == current_sandbox__get()) return; // again sandbox should be done and waiting for the parent. if (sandbox->state != RETURNED) return; @@ -405,6 +403,7 @@ free_sandbox(struct sandbox *sandbox) // depending on the memory type // free_linear_memory(sandbox->linear_memory_start, sandbox->linear_memory_size, sandbox->linear_memory_max_size); + int ret; // mmaped memory includes sandbox structure in there. ret = munmap(sandbox, sz); if (ret) perror("munmap sandbox"); diff --git a/runtime/src/softint.c b/runtime/src/softint.c index 9d8bcda..d18b65b 100644 --- a/runtime/src/softint.c +++ b/runtime/src/softint.c @@ -54,7 +54,7 @@ softint__handle_signals(int signal_type, siginfo_t *signal_info, void *user_cont #ifdef PREEMPT_DISABLE assert(0); #else - struct sandbox *curr = get_current_sandbox(); + struct sandbox *curr = current_sandbox__get(); ucontext_t * user_context = (ucontext_t *)user_context_raw; switch (signal_type) { @@ -118,7 +118,7 @@ softint__schedule_alarm(void *user_context_raw) { softint__disable(); // no nesting! - struct sandbox *curr = get_current_sandbox(); + struct sandbox *curr = current_sandbox__get(); ucontext_t * user_context = (ucontext_t *)user_context_raw; // no sandboxes running..so nothing to preempt..let the "main" scheduler run its course. @@ -131,8 +131,8 @@ softint__schedule_alarm(void *user_context_raw) // save the current sandbox, state from user_context! arch_mcontext_save(&curr->ctxt, &user_context->uc_mcontext); - // set_current_sandbox on it. restore through *user_context.. - set_current_sandbox(next); + // current_sandbox__set on it. restore through *user_context.. + current_sandbox__set(next); if (arch_mcontext_restore(&user_context->uc_mcontext, &next->ctxt)) goto skip; // reset if SIGALRM happens before SIGUSR1 and if don't preempt..OR