chore: de-dunder current_sandbox

master
Sean McBride 5 years ago
parent 0271f4a48d
commit d64ca75c4e

@ -11,7 +11,7 @@ extern http_parser_settings runtime__http_parser_settings;
* @returns the current sandbox executing on this thread * @returns the current sandbox executing on this thread
**/ **/
static inline struct sandbox * static inline struct sandbox *
current_sandbox__get(void) current_sandbox_get(void)
{ {
return worker_thread__current_sandbox; return worker_thread__current_sandbox;
} }
@ -21,7 +21,7 @@ current_sandbox__get(void)
* @param sandbox the sandbox we are setting this thread to run * @param sandbox the sandbox we are setting this thread to run
**/ **/
static inline void static inline void
current_sandbox__set(struct sandbox *sandbox) current_sandbox_set(struct sandbox *sandbox)
{ {
// FIXME: critical-section. // FIXME: critical-section.
worker_thread__current_sandbox = sandbox; worker_thread__current_sandbox = sandbox;
@ -38,9 +38,9 @@ current_sandbox__set(struct sandbox *sandbox)
* @return the arguments of the current sandbox * @return the arguments of the current sandbox
*/ */
static inline char * static inline char *
current_sandbox__get_arguments(void) current_sandbox_get_arguments(void)
{ {
struct sandbox *sandbox = current_sandbox__get(); struct sandbox *sandbox = current_sandbox_get();
return sandbox__get_arguments(sandbox); return sandbox__get_arguments(sandbox);
} }
@ -49,9 +49,9 @@ current_sandbox__get_arguments(void)
* @return index of handle we preopened or -1 if all io_handles are exhausted * @return index of handle we preopened or -1 if all io_handles are exhausted
**/ **/
static inline int static inline int
current_sandbox__initialize_io_handle(void) current_sandbox_initialize_io_handle(void)
{ {
struct sandbox *sandbox = current_sandbox__get(); struct sandbox *sandbox = current_sandbox_get();
return sandbox__initialize_io_handle(sandbox); return sandbox__initialize_io_handle(sandbox);
} }
@ -61,9 +61,9 @@ current_sandbox__initialize_io_handle(void)
* @return index of handle we preopened or -1 if all io_handles are exhausted * @return index of handle we preopened or -1 if all io_handles are exhausted
**/ **/
static inline int static inline int
current_sandbox__initialize_io_handle_and_set_file_descriptor(int file_descriptor) current_sandbox_initialize_io_handle_and_set_file_descriptor(int file_descriptor)
{ {
struct sandbox *sandbox = current_sandbox__get(); struct sandbox *sandbox = current_sandbox_get();
return sandbox__initialize_io_handle_and_set_file_descriptor(sandbox, file_descriptor); return sandbox__initialize_io_handle_and_set_file_descriptor(sandbox, file_descriptor);
} }
@ -75,9 +75,9 @@ int sandbox__parse_http_request(struct sandbox *sandbox, size_t l);
* @returns 0 * @returns 0
**/ **/
static inline int static inline int
current_sandbox__parse_http_request(size_t length) current_sandbox_parse_http_request(size_t length)
{ {
return sandbox__parse_http_request(current_sandbox__get(), length); return sandbox__parse_http_request(current_sandbox_get(), length);
} }
/** /**
@ -88,9 +88,9 @@ current_sandbox__parse_http_request(size_t length)
* @returns the index that was set or -1 in case of error * @returns the index that was set or -1 in case of error
**/ **/
static inline int static inline int
current_sandbox__set_file_descriptor(int io_handle_index, int file_descriptor) current_sandbox_set_file_descriptor(int io_handle_index, int file_descriptor)
{ {
struct sandbox *sandbox = current_sandbox__get(); struct sandbox *sandbox = current_sandbox_get();
return sandbox__set_file_descriptor(sandbox, io_handle_index, file_descriptor); return sandbox__set_file_descriptor(sandbox, io_handle_index, file_descriptor);
} }
@ -100,9 +100,9 @@ current_sandbox__set_file_descriptor(int io_handle_index, int file_descriptor)
* @returns file descriptor * @returns file descriptor
**/ **/
static inline int static inline int
current_sandbox__get_file_descriptor(int io_handle_index) current_sandbox_get_file_descriptor(int io_handle_index)
{ {
struct sandbox *sandbox = current_sandbox__get(); struct sandbox *sandbox = current_sandbox_get();
return sandbox__get_file_descriptor(sandbox, io_handle_index); return sandbox__get_file_descriptor(sandbox, io_handle_index);
} }
@ -111,9 +111,9 @@ current_sandbox__get_file_descriptor(int io_handle_index)
* @param io_handle_index index of the handle to close * @param io_handle_index index of the handle to close
**/ **/
static inline void static inline void
current_sandbox__close_file_descriptor(int io_handle_index) current_sandbox_close_file_descriptor(int io_handle_index)
{ {
struct sandbox *sandbox = current_sandbox__get(); struct sandbox *sandbox = current_sandbox_get();
sandbox__close_file_descriptor(sandbox, io_handle_index); sandbox__close_file_descriptor(sandbox, io_handle_index);
} }
@ -123,9 +123,9 @@ current_sandbox__close_file_descriptor(int io_handle_index)
* @returns any libuv handle * @returns any libuv handle
**/ **/
static inline union uv_any_handle * static inline union uv_any_handle *
current_sandbox__get_libuv_handle(int io_handle_index) current_sandbox_get_libuv_handle(int io_handle_index)
{ {
struct sandbox *sandbox = current_sandbox__get(); struct sandbox *sandbox = current_sandbox_get();
return sandbox__get_libuv_handle(sandbox, io_handle_index); return sandbox__get_libuv_handle(sandbox, io_handle_index);
} }
@ -135,9 +135,9 @@ current_sandbox__get_libuv_handle(int io_handle_index)
* @returns the length of the http_request's body * @returns the length of the http_request's body
**/ **/
static inline int static inline int
current_sandbox__get_http_request_body(char **body) current_sandbox_get_http_request_body(char **body)
{ {
return http_request__get_body(&current_sandbox__get()->http_request, body); return http_request__get_body(&current_sandbox_get()->http_request, body);
} }
@ -148,9 +148,9 @@ current_sandbox__get_http_request_body(char **body)
* @returns 0 (abends program in case of error) * @returns 0 (abends program in case of error)
**/ **/
static inline int static inline int
current_sandbox__set_http_response_header(char *header, int length) current_sandbox_set_http_response_header(char *header, int length)
{ {
return http_response__set_header(&current_sandbox__get()->http_response, header, length); return http_response__set_header(&current_sandbox_get()->http_response, header, length);
} }
/** /**
@ -160,9 +160,9 @@ current_sandbox__set_http_response_header(char *header, int length)
* @returns 0 (abends program in case of error) * @returns 0 (abends program in case of error)
**/ **/
static inline int static inline int
current_sandbox__set_http_response_body(char *body, int length) current_sandbox_set_http_response_body(char *body, int length)
{ {
return http_response__set_body(&current_sandbox__get()->http_response, body, length); return http_response__set_body(&current_sandbox_get()->http_response, body, length);
} }
/** /**
@ -172,9 +172,9 @@ current_sandbox__set_http_response_body(char *body, int length)
* @returns 0 (abends program in case of error) * @returns 0 (abends program in case of error)
**/ **/
static inline int static inline int
current_sandbox__set_http_response_status(char *status, int length) current_sandbox_set_http_response_status(char *status, int length)
{ {
return http_response__set_status(&current_sandbox__get()->http_response, status, length); return http_response__set_status(&current_sandbox_get()->http_response, status, length);
} }
/** /**
@ -182,9 +182,9 @@ current_sandbox__set_http_response_status(char *status, int length)
* @returns the number of buffers used to store the HTTP Response * @returns the number of buffers used to store the HTTP Response
**/ **/
static inline int static inline int
current_sandbox__vectorize_http_response(void) current_sandbox_vectorize_http_response(void)
{ {
return http_response__encode_as_vector(&current_sandbox__get()->http_response); return http_response__encode_as_vector(&current_sandbox_get()->http_response);
} }

@ -34,7 +34,7 @@ void
stub_init(i32 offset) stub_init(i32 offset)
{ {
// What program name will we put in the auxiliary vectors // What program name will we put in the auxiliary vectors
char *program_name = current_sandbox__get()->module->name; char *program_name = current_sandbox_get()->module->name;
// Copy the program name into WASM accessible memory // Copy the program name into WASM accessible memory
i32 program_name_offset = offset; i32 program_name_offset = offset;
strcpy(get_memory_ptr_for_runtime(offset, sizeof(program_name)), program_name); strcpy(get_memory_ptr_for_runtime(offset, sizeof(program_name)), program_name);
@ -65,7 +65,7 @@ stub_init(i32 offset)
i32 env_vec_offset = offset; i32 env_vec_offset = offset;
memcpy(get_memory_ptr_for_runtime(env_vec_offset, sizeof(env_vec)), env_vec, sizeof(env_vec)); memcpy(get_memory_ptr_for_runtime(env_vec_offset, sizeof(env_vec)), env_vec, sizeof(env_vec));
module__initialize_libc(current_sandbox__get()->module, env_vec_offset, program_name_offset); module__initialize_libc(current_sandbox_get()->module, env_vec_offset, program_name_offset);
} }
// Emulated syscall implementations // Emulated syscall implementations
@ -87,9 +87,9 @@ uv_fs_get_type(uv_fs_t *req)
return req->fs_type; return req->fs_type;
} }
#define UV_FS_REQ_INIT() \ #define UV_FS_REQ_INIT() \
{ \ { \
.data = current_sandbox__get(), .result = 0 \ .data = current_sandbox_get(), .result = 0 \
} }
static void static void
@ -106,7 +106,7 @@ wasm_read(i32 filedes, i32 buf_offset, i32 nbyte)
{ {
if (filedes == 0) { if (filedes == 0) {
char * buffer = worker_thread__get_memory_ptr_void(buf_offset, nbyte); char * buffer = worker_thread__get_memory_ptr_void(buf_offset, nbyte);
struct sandbox * s = current_sandbox__get(); struct sandbox * s = current_sandbox_get();
struct http_request *r = &s->http_request; struct http_request *r = &s->http_request;
if (r->body_length <= 0) return 0; if (r->body_length <= 0) return 0;
int l = nbyte > r->body_length ? r->body_length : nbyte; int l = nbyte > r->body_length ? r->body_length : nbyte;
@ -115,7 +115,7 @@ wasm_read(i32 filedes, i32 buf_offset, i32 nbyte)
r->body_length -= l; r->body_length -= l;
return l; return l;
} }
int f = current_sandbox__get_file_descriptor(filedes); int f = current_sandbox_get_file_descriptor(filedes);
// TODO: read on other file types // TODO: read on other file types
uv_fs_t req = UV_FS_REQ_INIT(); uv_fs_t req = UV_FS_REQ_INIT();
char * buffer = worker_thread__get_memory_ptr_void(buf_offset, nbyte); char * buffer = worker_thread__get_memory_ptr_void(buf_offset, nbyte);
@ -138,7 +138,7 @@ wasm_write(i32 file_descriptor, i32 buf_offset, i32 buf_size)
{ {
if (file_descriptor == 1 || file_descriptor == 2) { if (file_descriptor == 1 || file_descriptor == 2) {
char * buffer = worker_thread__get_memory_ptr_void(buf_offset, buf_size); char * buffer = worker_thread__get_memory_ptr_void(buf_offset, buf_size);
struct sandbox *s = current_sandbox__get(); struct sandbox *s = current_sandbox_get();
int l = s->module->max_response_size - s->request_response_data_length; int l = s->module->max_response_size - s->request_response_data_length;
l = l > buf_size ? buf_size : l; l = l > buf_size ? buf_size : l;
if (l == 0) return 0; if (l == 0) return 0;
@ -147,7 +147,7 @@ wasm_write(i32 file_descriptor, i32 buf_offset, i32 buf_size)
return l; return l;
} }
int f = current_sandbox__get_file_descriptor(file_descriptor); int f = current_sandbox_get_file_descriptor(file_descriptor);
// TODO: read on other file types // TODO: read on other file types
uv_fs_t req = UV_FS_REQ_INIT(); uv_fs_t req = UV_FS_REQ_INIT();
char * buffer = worker_thread__get_memory_ptr_void(buf_offset, buf_size); char * buffer = worker_thread__get_memory_ptr_void(buf_offset, buf_size);
@ -185,7 +185,7 @@ wasm_open(i32 path_off, i32 flags, i32 mode)
uv_fs_t req = UV_FS_REQ_INIT(); uv_fs_t req = UV_FS_REQ_INIT();
char * path = worker_thread__get_memory_string(path_off, MODULE__MAX_PATH_LENGTH); char * path = worker_thread__get_memory_string(path_off, MODULE__MAX_PATH_LENGTH);
int iofd = current_sandbox__initialize_io_handle(); int iofd = current_sandbox_initialize_io_handle();
if (iofd < 0) return -1; if (iofd < 0) return -1;
i32 modified_flags = 0; i32 modified_flags = 0;
if (flags & WO_RDONLY) modified_flags |= O_RDONLY; if (flags & WO_RDONLY) modified_flags |= O_RDONLY;
@ -203,9 +203,9 @@ wasm_open(i32 path_off, i32 flags, i32 mode)
debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret);
uv_fs_req_cleanup(&req); uv_fs_req_cleanup(&req);
if (ret < 0) if (ret < 0)
current_sandbox__close_file_descriptor(iofd); current_sandbox_close_file_descriptor(iofd);
else else
current_sandbox__set_file_descriptor(iofd, ret); current_sandbox_set_file_descriptor(iofd, ret);
return iofd; return iofd;
} }
@ -215,9 +215,9 @@ i32
wasm_close(i32 file_descriptor) wasm_close(i32 file_descriptor)
{ {
if (file_descriptor >= 0 && file_descriptor <= 2) { return 0; } if (file_descriptor >= 0 && file_descriptor <= 2) { return 0; }
struct sandbox * c = current_sandbox__get(); struct sandbox * c = current_sandbox_get();
int d = current_sandbox__get_file_descriptor(file_descriptor); int d = current_sandbox_get_file_descriptor(file_descriptor);
union uv_any_handle *h = current_sandbox__get_libuv_handle(file_descriptor); union uv_any_handle *h = current_sandbox_get_libuv_handle(file_descriptor);
uv_handle_type type = ((uv_handle_t *)h)->type; uv_handle_type type = ((uv_handle_t *)h)->type;
debuglog("[%p] [%d,%d]\n", c, file_descriptor, d); debuglog("[%p] [%d,%d]\n", c, file_descriptor, d);
@ -239,7 +239,7 @@ wasm_close(i32 file_descriptor)
int ret = uv_fs_get_result(&req); int ret = uv_fs_get_result(&req);
debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret);
uv_fs_req_cleanup(&req); uv_fs_req_cleanup(&req);
if (ret == 0) current_sandbox__close_file_descriptor(file_descriptor); if (ret == 0) current_sandbox_close_file_descriptor(file_descriptor);
return ret; return ret;
} }
@ -345,7 +345,7 @@ wasm_fstat(i32 filedes, i32 stat_offset)
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));
struct stat stat; struct stat stat;
int d = current_sandbox__get_file_descriptor(filedes); int d = current_sandbox_get_file_descriptor(filedes);
i32 res = fstat(d, &stat); i32 res = fstat(d, &stat);
if (res == -1) return -errno; if (res == -1) return -errno;
@ -435,7 +435,7 @@ wasm_lstat(i32 path_str_offset, i32 stat_offset)
i32 i32
wasm_lseek(i32 filedes, i32 file_offset, i32 whence) wasm_lseek(i32 filedes, i32 file_offset, i32 whence)
{ {
int d = current_sandbox__get_file_descriptor(filedes); int d = current_sandbox_get_file_descriptor(filedes);
i32 res = (i32)lseek(d, file_offset, whence); i32 res = (i32)lseek(d, file_offset, whence);
if (res == -1) return -errno; if (res == -1) return -errno;
@ -447,7 +447,7 @@ wasm_lseek(i32 filedes, i32 file_offset, i32 whence)
u32 u32
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);
if (file_descriptor >= 0) assert(d >= 0); if (file_descriptor >= 0) assert(d >= 0);
if (addr != 0) { if (addr != 0) {
printf("parameter void *addr is not supported!\n"); printf("parameter void *addr is not supported!\n");
@ -479,7 +479,7 @@ wasm_mmap(i32 addr, i32 len, i32 prot, i32 flags, i32 file_descriptor, i32 offse
i32 i32
wasm_ioctl(i32 file_descriptor, i32 request, i32 data_offet) wasm_ioctl(i32 file_descriptor, i32 request, i32 data_offet)
{ {
// int d = current_sandbox__get_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 // musl libc does some ioctls to stdout, so just allow these to silently go through
// FIXME: The above is idiotic // FIXME: The above is idiotic
// assert(d == 1); // assert(d == 1);
@ -500,7 +500,7 @@ wasm_readv(i32 file_descriptor, i32 iov_offset, i32 iovcnt)
int len = 0; int len = 0;
struct wasm_iovec * iov = worker_thread__get_memory_ptr_void(iov_offset, struct wasm_iovec * iov = worker_thread__get_memory_ptr_void(iov_offset,
iovcnt * sizeof(struct wasm_iovec)); iovcnt * sizeof(struct wasm_iovec));
struct sandbox * s = current_sandbox__get(); struct sandbox * s = current_sandbox_get();
struct http_request *r = &s->http_request; struct http_request *r = &s->http_request;
if (r->body_length <= 0) return 0; if (r->body_length <= 0) return 0;
for (int i = 0; i < iovcnt; i++) { for (int i = 0; i < iovcnt; i++) {
@ -518,7 +518,7 @@ wasm_readv(i32 file_descriptor, i32 iov_offset, i32 iovcnt)
} }
// TODO: read on other file types // TODO: read on other file types
int gret = 0; int gret = 0;
int d = current_sandbox__get_file_descriptor(file_descriptor); int d = current_sandbox_get_file_descriptor(file_descriptor);
struct wasm_iovec *iov = worker_thread__get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); struct wasm_iovec *iov = worker_thread__get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec));
for (int i = 0; i < iovcnt; i += RUNTIME__READ_WRITE_VECTOR_LENGTH) { for (int i = 0; i < iovcnt; i += RUNTIME__READ_WRITE_VECTOR_LENGTH) {
@ -550,7 +550,7 @@ wasm_readv(i32 file_descriptor, i32 iov_offset, i32 iovcnt)
i32 i32
wasm_writev(i32 file_descriptor, i32 iov_offset, i32 iovcnt) wasm_writev(i32 file_descriptor, i32 iov_offset, i32 iovcnt)
{ {
struct sandbox *c = current_sandbox__get(); struct sandbox *c = current_sandbox_get();
if (file_descriptor == 1 || file_descriptor == 2) { if (file_descriptor == 1 || file_descriptor == 2) {
// both 1 and 2 go to client. // both 1 and 2 go to client.
int len = 0; int len = 0;
@ -566,7 +566,7 @@ wasm_writev(i32 file_descriptor, i32 iov_offset, i32 iovcnt)
return len; return len;
} }
// TODO: read on other file types // TODO: read on other file types
int d = current_sandbox__get_file_descriptor(file_descriptor); int d = current_sandbox_get_file_descriptor(file_descriptor);
int gret = 0; int gret = 0;
struct wasm_iovec *iov = worker_thread__get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); struct wasm_iovec *iov = worker_thread__get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec));
@ -624,7 +624,7 @@ wasm_getpid()
u32 u32
wasm_fcntl(u32 file_descriptor, u32 cmd, u32 arg_or_lock_ptr) wasm_fcntl(u32 file_descriptor, u32 cmd, u32 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) {
case WF_SETFD: case WF_SETFD:
// return fcntl(d, F_SETFD, arg_or_lock_ptr); // return fcntl(d, F_SETFD, arg_or_lock_ptr);
@ -640,7 +640,7 @@ wasm_fcntl(u32 file_descriptor, u32 cmd, u32 arg_or_lock_ptr)
u32 u32
wasm_fsync(u32 file_descriptor) wasm_fsync(u32 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();
debuglog("[%p] start[%d,%d]\n", uv_fs_get_data(&req), file_descriptor, d); debuglog("[%p] start[%d,%d]\n", uv_fs_get_data(&req), file_descriptor, d);
uv_fs_fsync(worker_thread__get_thread_libuv_handle(), &req, d, wasm_fs_callback); uv_fs_fsync(worker_thread__get_thread_libuv_handle(), &req, d, wasm_fs_callback);
@ -741,7 +741,7 @@ wasm_exit_group(i32 status)
i32 i32
wasm_fchown(i32 file_descriptor, u32 owner, u32 group) wasm_fchown(i32 file_descriptor, u32 owner, u32 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();
debuglog("[%p] start[%d,%d]\n", uv_fs_get_data(&req), file_descriptor, d); debuglog("[%p] start[%d,%d]\n", uv_fs_get_data(&req), file_descriptor, d);
uv_fs_fchown(worker_thread__get_thread_libuv_handle(), &req, d, owner, group, wasm_fs_callback); uv_fs_fchown(worker_thread__get_thread_libuv_handle(), &req, d, owner, group, wasm_fs_callback);
@ -783,12 +783,12 @@ wasm_connect_callback(uv_connect_t *req, int status)
i32 i32
wasm_socket(i32 domain, i32 type, i32 protocol) wasm_socket(i32 domain, i32 type, i32 protocol)
{ {
struct sandbox *c = current_sandbox__get(); struct sandbox *c = current_sandbox_get();
int pfd = current_sandbox__initialize_io_handle(), file_descriptor = -1; int pfd = current_sandbox_initialize_io_handle(), file_descriptor = -1;
// TODO: use domain, type and protocol in libuv? // TODO: use domain, type and protocol in libuv?
debuglog("[%p] => %d %d %d\n", c, domain, type, protocol); debuglog("[%p] => %d %d %d\n", c, domain, type, protocol);
if (pfd < 0) return pfd; if (pfd < 0) return pfd;
union uv_any_handle *h = current_sandbox__get_libuv_handle(pfd); union uv_any_handle *h = current_sandbox_get_libuv_handle(pfd);
if (type & SOCK_DGRAM) { if (type & SOCK_DGRAM) {
uv_udp_t *uh = (uv_udp_t *)h; uv_udp_t *uh = (uv_udp_t *)h;
@ -810,10 +810,10 @@ wasm_socket(i32 domain, i32 type, i32 protocol)
i32 i32
wasm_connect(i32 sockfd, i32 sockaddr_offset, i32 addrlen) wasm_connect(i32 sockfd, i32 sockaddr_offset, i32 addrlen)
{ {
struct sandbox *c = current_sandbox__get(); struct sandbox *c = current_sandbox_get();
int file_descriptor = current_sandbox__get_file_descriptor(sockfd); int file_descriptor = current_sandbox_get_file_descriptor(sockfd);
debuglog("[%p] [%d, %d]\n", c, sockfd, file_descriptor); debuglog("[%p] [%d, %d]\n", c, sockfd, file_descriptor);
union uv_any_handle *h = current_sandbox__get_libuv_handle(sockfd); union uv_any_handle *h = current_sandbox_get_libuv_handle(sockfd);
uv_handle_type t = ((uv_handle_t *)h)->type; uv_handle_type t = ((uv_handle_t *)h)->type;
if (t == UV_TCP) { if (t == UV_TCP) {
@ -842,27 +842,27 @@ wasm_accept(i32 sockfd, i32 sockaddr_offset, i32 addrlen_offset)
// what do we do with the sockaddr TODO: ???? // what do we do with the sockaddr TODO: ????
socklen_t * addrlen = worker_thread__get_memory_ptr_void(addrlen_offset, sizeof(socklen_t)); socklen_t * addrlen = worker_thread__get_memory_ptr_void(addrlen_offset, sizeof(socklen_t));
struct sockaddr * socket_address = worker_thread__get_memory_ptr_void(sockaddr_offset, *addrlen); struct sockaddr * socket_address = worker_thread__get_memory_ptr_void(sockaddr_offset, *addrlen);
union uv_any_handle *s = current_sandbox__get_libuv_handle(sockfd); union uv_any_handle *s = current_sandbox_get_libuv_handle(sockfd);
int cfd = current_sandbox__initialize_io_handle(); int cfd = current_sandbox_initialize_io_handle();
if (cfd < 0) return -1; if (cfd < 0) return -1;
struct sandbox *c = current_sandbox__get(); struct sandbox *c = current_sandbox_get();
debuglog("[%p] [%d, %d]\n", c, sockfd, current_sandbox__get_file_descriptor(sockfd)); 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 so we can look into whether we need to implement UDP or others..
assert(((uv_handle_t *)s)->type == UV_TCP); assert(((uv_handle_t *)s)->type == UV_TCP);
union uv_any_handle *h = current_sandbox__get_libuv_handle(cfd); union uv_any_handle *h = current_sandbox_get_libuv_handle(cfd);
uv_tcp_init(worker_thread__get_thread_libuv_handle(), (uv_tcp_t *)h); uv_tcp_init(worker_thread__get_thread_libuv_handle(), (uv_tcp_t *)h);
debuglog("[%p] tcp init %d\n", c, cfd); debuglog("[%p] tcp init %d\n", c, cfd);
int r = uv_accept((uv_stream_t *)s, (uv_stream_t *)h); int r = uv_accept((uv_stream_t *)s, (uv_stream_t *)h);
if (r < 0) return r; if (r < 0) return r;
// TODO: if accept fails, what do we do with the preopened handle? // TODO: if accept fails, what do we do with the preopened handle?
// if (r < 0) current_sandbox__close_file_descriptor(cfd); // if (r < 0) current_sandbox_close_file_descriptor(cfd);
// we've to also remove it from the runtime loop?? // we've to also remove it from the runtime loop??
int r2 = -1, f = -1; int r2 = -1, f = -1;
r2 = uv_fileno((uv_handle_t *)h, &f); r2 = uv_fileno((uv_handle_t *)h, &f);
if (r2 < 0 || f < 0) assert(0); if (r2 < 0 || f < 0) assert(0);
current_sandbox__set_file_descriptor(cfd, f); current_sandbox_set_file_descriptor(cfd, f);
debuglog("[%p] done[%d,%d]\n", c, cfd, f); debuglog("[%p] done[%d,%d]\n", c, cfd, f);
return cfd; return cfd;
@ -871,10 +871,10 @@ wasm_accept(i32 sockfd, i32 sockaddr_offset, i32 addrlen_offset)
i32 i32
wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen) wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen)
{ {
struct sandbox *c = current_sandbox__get(); struct sandbox *c = current_sandbox_get();
int file_descriptor = current_sandbox__get_file_descriptor(sockfd); int file_descriptor = current_sandbox_get_file_descriptor(sockfd);
debuglog("[%p] [%d,%d]\n", c, sockfd, file_descriptor); debuglog("[%p] [%d,%d]\n", c, sockfd, file_descriptor);
union uv_any_handle *h = current_sandbox__get_libuv_handle(sockfd); union uv_any_handle *h = current_sandbox_get_libuv_handle(sockfd);
uv_handle_type t = ((uv_handle_t *)h)->type; uv_handle_type t = ((uv_handle_t *)h)->type;
if (t == UV_TCP) { if (t == UV_TCP) {
@ -885,7 +885,7 @@ wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen)
int r2 = -1, f = -1; int r2 = -1, f = -1;
r2 = uv_fileno((uv_handle_t *)h, &f); r2 = uv_fileno((uv_handle_t *)h, &f);
debuglog("[%p] [%d,%d]\n", c, f, file_descriptor); debuglog("[%p] [%d,%d]\n", c, f, file_descriptor);
current_sandbox__set_file_descriptor(sockfd, f); current_sandbox_set_file_descriptor(sockfd, f);
} }
return r1; return r1;
} else if (t == UV_UDP) { } else if (t == UV_UDP) {
@ -896,7 +896,7 @@ wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen)
int r2 = -1, f = -1; int r2 = -1, f = -1;
r2 = uv_fileno((uv_handle_t *)h, &f); r2 = uv_fileno((uv_handle_t *)h, &f);
debuglog("[%p] [%d,%d]\n", c, f, file_descriptor); debuglog("[%p] [%d,%d]\n", c, f, file_descriptor);
current_sandbox__set_file_descriptor(sockfd, f); current_sandbox_set_file_descriptor(sockfd, f);
} }
return r1; return r1;
} }
@ -908,10 +908,10 @@ wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen)
i32 i32
wasm_listen(i32 sockfd, i32 backlog) wasm_listen(i32 sockfd, i32 backlog)
{ {
struct sandbox * c = current_sandbox__get(); struct sandbox * c = current_sandbox_get();
union uv_any_handle *h = current_sandbox__get_libuv_handle(sockfd); union uv_any_handle *h = current_sandbox_get_libuv_handle(sockfd);
assert(c == (struct sandbox *)(((uv_tcp_t *)h)->data)); assert(c == (struct sandbox *)(((uv_tcp_t *)h)->data));
debuglog("[%p] [%d,%d]\n", c, sockfd, current_sandbox__get_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; uv_handle_type t = ((uv_handle_t *)h)->type;
// assert so we can look into whether we need to implement UDP or others.. // assert so we can look into whether we need to implement UDP or others..
@ -980,10 +980,10 @@ wasm_sendto(i32 file_descriptor, i32 buff_offset, i32 len, i32 flags, i32 sockad
char *buffer = worker_thread__get_memory_ptr_void(buff_offset, len); char *buffer = worker_thread__get_memory_ptr_void(buff_offset, len);
// TODO: only support "send" api for now // TODO: only support "send" api for now
assert(sockaddr_len == 0); assert(sockaddr_len == 0);
struct sandbox * c = current_sandbox__get(); struct sandbox * c = current_sandbox_get();
union uv_any_handle *h = current_sandbox__get_libuv_handle(file_descriptor); union uv_any_handle *h = current_sandbox_get_libuv_handle(file_descriptor);
uv_handle_type t = ((uv_handle_t *)h)->type; uv_handle_type t = ((uv_handle_t *)h)->type;
debuglog("[%p] [%d,%d]\n", c, file_descriptor, current_sandbox__get_file_descriptor(file_descriptor)); debuglog("[%p] [%d,%d]\n", c, file_descriptor, current_sandbox_get_file_descriptor(file_descriptor));
if (t == UV_TCP) { if (t == UV_TCP) {
uv_write_t req = { uv_write_t req = {
@ -1031,10 +1031,10 @@ wasm_recvfrom(i32 file_descriptor, i32 buff_offset, i32 size, i32 flags, i32 soc
socklen_t *len = worker_thread__get_memory_ptr_void(socklen_offset, sizeof(socklen_t)); socklen_t *len = worker_thread__get_memory_ptr_void(socklen_offset, sizeof(socklen_t));
// TODO: only support "recv" api for now // TODO: only support "recv" api for now
assert(*len == 0); assert(*len == 0);
struct sandbox * c = current_sandbox__get(); struct sandbox * c = current_sandbox_get();
union uv_any_handle *h = current_sandbox__get_libuv_handle(file_descriptor); union uv_any_handle *h = current_sandbox_get_libuv_handle(file_descriptor);
uv_handle_type t = ((uv_handle_t *)h)->type; uv_handle_type t = ((uv_handle_t *)h)->type;
debuglog("[%p] [%d,%d]\n", c, file_descriptor, current_sandbox__get_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.. // uv stream API are not simple wrappers on read/write..
// and there will only be one system call pending.. // and there will only be one system call pending..

@ -24,7 +24,7 @@ free_linear_memory(void *base, u32 bound, u32 max)
void void
expand_memory(void) expand_memory(void)
{ {
struct sandbox *sandbox = current_sandbox__get(); struct sandbox *sandbox = current_sandbox_get();
// max_pages = 0 => no limit: FIXME // max_pages = 0 => no limit: FIXME
assert((sandbox->sandbox_size + sandbox_lmbound) / WASM_PAGE_SIZE < WASM_MAX_PAGES); assert((sandbox->sandbox_size + sandbox_lmbound) / WASM_PAGE_SIZE < WASM_MAX_PAGES);

@ -180,9 +180,9 @@ worker_thread__switch_to_sandbox(struct sandbox *next_sandbox)
{ {
arch_context_t *next_register_context = next_sandbox == NULL ? NULL : &next_sandbox->ctxt; arch_context_t *next_register_context = next_sandbox == NULL ? NULL : &next_sandbox->ctxt;
software_interrupt__disable(); software_interrupt__disable();
struct sandbox *current_sandbox = current_sandbox__get(); struct sandbox *current_sandbox = current_sandbox_get();
arch_context_t *current_register_context = current_sandbox == NULL ? NULL : &current_sandbox->ctxt; arch_context_t *current_register_context = current_sandbox == NULL ? NULL : &current_sandbox->ctxt;
current_sandbox__set(next_sandbox); current_sandbox_set(next_sandbox);
// If the current sandbox we're switching from is in a RETURNED state, add to completion queue // If the current sandbox we're switching from is in a RETURNED state, add to completion queue
if (current_sandbox && current_sandbox->state == RETURNED) if (current_sandbox && current_sandbox->state == RETURNED)
worker_thread__push_sandbox_to_completion_queue(current_sandbox); worker_thread__push_sandbox_to_completion_queue(current_sandbox);
@ -219,7 +219,7 @@ worker_thread__block_current_sandbox(void)
{ {
assert(worker_thread__is_in_callback == 0); assert(worker_thread__is_in_callback == 0);
software_interrupt__disable(); software_interrupt__disable();
struct sandbox *current_sandbox = current_sandbox__get(); struct sandbox *current_sandbox = current_sandbox_get();
ps_list_rem_d(current_sandbox); ps_list_rem_d(current_sandbox);
current_sandbox->state = BLOCKED; current_sandbox->state = BLOCKED;
struct sandbox *next_sandbox = worker_thread__get_next_sandbox(0); struct sandbox *next_sandbox = worker_thread__get_next_sandbox(0);
@ -399,7 +399,7 @@ worker_thread__pop_and_free_n_sandboxes_from_completion_queue(unsigned int numbe
static inline struct sandbox * static inline struct sandbox *
worker_thread__execute_runtime_maintenance_and_get_next_sandbox(void) worker_thread__execute_runtime_maintenance_and_get_next_sandbox(void)
{ {
assert(current_sandbox__get() == NULL); assert(current_sandbox_get() == NULL);
// Try to free one sandbox from the completion queue // Try to free one sandbox from the completion queue
worker_thread__pop_and_free_n_sandboxes_from_completion_queue(1); worker_thread__pop_and_free_n_sandboxes_from_completion_queue(1);
// Execute libuv callbacks // Execute libuv callbacks
@ -455,7 +455,7 @@ worker_thread__main(void *return_code)
void void
worker_thread__exit_current_sandbox(void) worker_thread__exit_current_sandbox(void)
{ {
struct sandbox *current_sandbox = current_sandbox__get(); struct sandbox *current_sandbox = current_sandbox_get();
assert(current_sandbox); assert(current_sandbox);
software_interrupt__disable(); software_interrupt__disable();
worker_thread__pop_sandbox_from_run_queue(current_sandbox); worker_thread__pop_sandbox_from_run_queue(current_sandbox);

@ -15,10 +15,10 @@
* @param argument_count * @param argument_count
**/ **/
static inline void static inline void
current_sandbox__setup_arguments(i32 argument_count) current_sandbox_setup_arguments(i32 argument_count)
{ {
struct sandbox *curr = current_sandbox__get(); struct sandbox *curr = current_sandbox_get();
char * arguments = current_sandbox__get_arguments(); char * arguments = current_sandbox_get_arguments();
// whatever gregor has, to be able to pass arguments to a module! // whatever gregor has, to be able to pass arguments to a module!
curr->arguments_offset = sandbox_lmbound; curr->arguments_offset = sandbox_lmbound;
@ -65,9 +65,9 @@ sandbox__parse_http_request(struct sandbox *sandbox, size_t length)
* @return 1 on success, 0 if no context, < 0 on failure. * @return 1 on success, 0 if no context, < 0 on failure.
**/ **/
static inline int static inline int
current_sandbox__receive_and_parse_client_request(void) current_sandbox_receive_and_parse_client_request(void)
{ {
struct sandbox *curr = current_sandbox__get(); struct sandbox *curr = current_sandbox_get();
curr->request_response_data_length = 0; curr->request_response_data_length = 0;
#ifndef USE_HTTP_UVIO #ifndef USE_HTTP_UVIO
int r = 0; int r = 0;
@ -77,7 +77,7 @@ current_sandbox__receive_and_parse_client_request(void)
return r; return r;
} }
while (r > 0) { while (r > 0) {
if (current_sandbox__parse_http_request(r) != 0) return -1; if (current_sandbox_parse_http_request(r) != 0) return -1;
curr->request_response_data_length += r; curr->request_response_data_length += r;
struct http_request *rh = &curr->http_request; struct http_request *rh = &curr->http_request;
if (rh->message_end) break; if (rh->message_end) break;
@ -105,10 +105,10 @@ current_sandbox__receive_and_parse_client_request(void)
* @return RC. -1 on Failure * @return RC. -1 on Failure
**/ **/
static inline int static inline int
current_sandbox__build_and_send_client_response(void) current_sandbox_build_and_send_client_response(void)
{ {
int sndsz = 0; int sndsz = 0;
struct sandbox *curr = current_sandbox__get(); struct sandbox *curr = current_sandbox_get();
int response_header_length = strlen(HTTP__RESPONSE_200_OK) + strlen(HTTP__RESPONSE_CONTENT_TYPE) int response_header_length = strlen(HTTP__RESPONSE_200_OK) + strlen(HTTP__RESPONSE_CONTENT_TYPE)
+ strlen(HTTP__RESPONSE_CONTENT_LENGTH); + strlen(HTTP__RESPONSE_CONTENT_LENGTH);
int body_length = curr->request_response_data_length - response_header_length; int body_length = curr->request_response_data_length - response_header_length;
@ -175,9 +175,9 @@ done:
*cleanup *cleanup
**/ **/
void void
current_sandbox__main(void) current_sandbox_main(void)
{ {
struct sandbox *current_sandbox = current_sandbox__get(); struct sandbox *current_sandbox = current_sandbox_get();
// FIXME: is this right? this is the first time this sandbox is running.. so it wont // FIXME: is this right? this is the first time this sandbox is running.. so it wont
// return to worker_thread__switch_to_sandbox() api.. // return to worker_thread__switch_to_sandbox() api..
// we'd potentially do what we'd in worker_thread__switch_to_sandbox() api here for cleanup.. // we'd potentially do what we'd in worker_thread__switch_to_sandbox() api here for cleanup..
@ -193,11 +193,11 @@ current_sandbox__main(void)
// Try to initialize file descriptors 0, 1, and 2 as io handles 0, 1, 2 // 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 // 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? // TODO: why do the file descriptors have to match the io handles?
int f = current_sandbox__initialize_io_handle_and_set_file_descriptor(0); int f = current_sandbox_initialize_io_handle_and_set_file_descriptor(0);
assert(f == 0); assert(f == 0);
f = current_sandbox__initialize_io_handle_and_set_file_descriptor(1); f = current_sandbox_initialize_io_handle_and_set_file_descriptor(1);
assert(f == 1); assert(f == 1);
f = current_sandbox__initialize_io_handle_and_set_file_descriptor(2); f = current_sandbox_initialize_io_handle_and_set_file_descriptor(2);
assert(f == 2); assert(f == 2);
// Initialize the HTTP-Parser for a request // Initialize the HTTP-Parser for a request
@ -226,7 +226,7 @@ current_sandbox__main(void)
#endif #endif
// If the HTTP Request returns 1, we've successfully received and parsed the HTTP request, so execute it! // If the HTTP Request returns 1, we've successfully received and parsed the HTTP request, so execute it!
if (current_sandbox__receive_and_parse_client_request() > 0) { if (current_sandbox_receive_and_parse_client_request() > 0) {
// //
current_sandbox->request_response_data_length = response_header_length; current_sandbox->request_response_data_length = response_header_length;
@ -236,14 +236,14 @@ current_sandbox__main(void)
module__initialize_memory(current_module); module__initialize_memory(current_module);
// Copy the arguments into the WebAssembly sandbox // Copy the arguments into the WebAssembly sandbox
current_sandbox__setup_arguments(argument_count); current_sandbox_setup_arguments(argument_count);
// Executing the function within the WebAssembly sandbox // Executing the function within the WebAssembly sandbox
current_sandbox->return_value = module__main(current_module, argument_count, current_sandbox->return_value = module__main(current_module, argument_count,
current_sandbox->arguments_offset); current_sandbox->arguments_offset);
// Retrieve the result from the WebAssembly sandbox, construct the HTTP response, and send to client // Retrieve the result from the WebAssembly sandbox, construct the HTTP response, and send to client
current_sandbox__build_and_send_client_response(); current_sandbox_build_and_send_client_response();
} }
// Cleanup connection and exit sandbox // Cleanup connection and exit sandbox
@ -326,7 +326,7 @@ sandbox__allocate(struct module *module, char *arguments, int socket_descriptor,
ps_list_init_d(sandbox); ps_list_init_d(sandbox);
// Setup the sandbox's context, stack, and instruction pointer // Setup the sandbox's context, stack, and instruction pointer
arch_context_init(&sandbox->ctxt, (reg_t)current_sandbox__main, arch_context_init(&sandbox->ctxt, (reg_t)current_sandbox_main,
(reg_t)(sandbox->stack_start + sandbox->stack_size)); (reg_t)(sandbox->stack_start + sandbox->stack_size));
return sandbox; return sandbox;
} }
@ -339,7 +339,7 @@ void
sandbox__free(struct sandbox *sandbox) sandbox__free(struct sandbox *sandbox)
{ {
// you have to context switch away to free a sandbox. // you have to context switch away to free a sandbox.
if (!sandbox || sandbox == current_sandbox__get()) return; if (!sandbox || sandbox == current_sandbox_get()) return;
// again sandbox should be done and waiting for the parent. // again sandbox should be done and waiting for the parent.
if (sandbox->state != RETURNED) return; if (sandbox->state != RETURNED) return;

@ -55,7 +55,7 @@ software_interrupt__handle_signals(int signal_type, siginfo_t *signal_info, void
#ifdef PREEMPT_DISABLE #ifdef PREEMPT_DISABLE
assert(0); assert(0);
#else #else
struct sandbox *curr = current_sandbox__get(); struct sandbox *curr = current_sandbox_get();
ucontext_t * user_context = (ucontext_t *)user_context_raw; ucontext_t * user_context = (ucontext_t *)user_context_raw;
switch (signal_type) { switch (signal_type) {
@ -119,7 +119,7 @@ software_interrupt__schedule_alarm(void *user_context_raw)
{ {
software_interrupt__disable(); // no nesting! software_interrupt__disable(); // no nesting!
struct sandbox *curr = current_sandbox__get(); struct sandbox *curr = current_sandbox_get();
ucontext_t * user_context = (ucontext_t *)user_context_raw; ucontext_t * user_context = (ucontext_t *)user_context_raw;
// no sandboxes running..so nothing to preempt..let the "main" scheduler run its course. // no sandboxes running..so nothing to preempt..let the "main" scheduler run its course.
@ -132,8 +132,8 @@ software_interrupt__schedule_alarm(void *user_context_raw)
// save the current sandbox, state from user_context! // save the current sandbox, state from user_context!
arch_mcontext_save(&curr->ctxt, &user_context->uc_mcontext); arch_mcontext_save(&curr->ctxt, &user_context->uc_mcontext);
// current_sandbox__set on it. restore through *user_context.. // current_sandbox_set on it. restore through *user_context..
current_sandbox__set(next); current_sandbox_set(next);
if (arch_mcontext_restore(&user_context->uc_mcontext, &next->ctxt)) goto skip; if (arch_mcontext_restore(&user_context->uc_mcontext, &next->ctxt)) goto skip;
// reset if SIGALRM happens before SIGUSR1 and if don't preempt..OR // reset if SIGALRM happens before SIGUSR1 and if don't preempt..OR

Loading…
Cancel
Save