From dafa257273d8891c64c4b5269b9ef40796cc35ca Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Thu, 16 Apr 2020 15:52:43 -0400 Subject: [PATCH] chore: encapsulate current_sandbox --- runtime/include/current_sandbox.h | 198 +++--------------------------- runtime/include/sandbox.h | 1 - runtime/src/current_sandbox.c | 185 ++++++++++++++++++++++++++++ runtime/src/runtime.c | 3 - runtime/src/sandbox.c | 2 + 5 files changed, 202 insertions(+), 187 deletions(-) create mode 100644 runtime/src/current_sandbox.c diff --git a/runtime/include/current_sandbox.h b/runtime/include/current_sandbox.h index 79c9a3a..44a0a65 100644 --- a/runtime/include/current_sandbox.h +++ b/runtime/include/current_sandbox.h @@ -4,188 +4,20 @@ #include "sandbox.h" #include "types.h" -extern http_parser_settings runtime_http_parser_settings; - -/** - * Getter for the current sandbox executing on this thread - * @returns the current sandbox executing on this thread - **/ -static inline struct sandbox * -current_sandbox_get(void) -{ - return worker_thread_current_sandbox; -} - -/** - * Setter for the current sandbox executing on this thread - * @param sandbox the sandbox we are setting this thread to run - **/ -static inline void -current_sandbox_set(struct sandbox *sandbox) -{ - // FIXME: critical-section. - worker_thread_current_sandbox = sandbox; - if (sandbox == NULL) return; - - // Thread Local State about the Current Sandbox - sandbox_lmbase = sandbox->linear_memory_start; - sandbox_lmbound = sandbox->linear_memory_size; - module_indirect_table = sandbox->module->indirect_table; -} - -/** - * Getter for the arguments of the current sandbox - * @return the arguments of the current sandbox - */ -static inline char * -current_sandbox_get_arguments(void) -{ - struct sandbox *sandbox = current_sandbox_get(); - return sandbox_get_arguments(sandbox); -} - -/** - * Initializes and returns an IO handle on the current sandbox ready for use - * @return index of handle we preopened or -1 if all io_handles are exhausted - **/ -static inline int -current_sandbox_initialize_io_handle(void) -{ - struct sandbox *sandbox = current_sandbox_get(); - return sandbox_initialize_io_handle(sandbox); -} - -/** - * Initializes and returns an IO handle on the current sandbox ready for use - * @param file_descriptor what we'll set on the IO handle after initialization - * @return index of handle we preopened or -1 if all io_handles are exhausted - **/ -static inline int -current_sandbox_initialize_io_handle_and_set_file_descriptor(int file_descriptor) -{ - struct sandbox *sandbox = current_sandbox_get(); - return sandbox_initialize_io_handle_and_set_file_descriptor(sandbox, file_descriptor); -} - -int sandbox_parse_http_request(struct sandbox *sandbox, size_t l); - -/** - * Parse the current sandbox's request_response_data up to length - * @param length - * @returns 0 - **/ -static inline int -current_sandbox_parse_http_request(size_t length) -{ - return sandbox_parse_http_request(current_sandbox_get(), length); -} - -/** - * Sets the file descriptor of the sandbox's ith io_handle - * Returns error condition if the file_descriptor to set does not contain sandbox preopen magin - * @param io_handle_index index of the sandbox io_handle we want to set - * @param file_descriptor the file descripter we want to set it to - * @returns the index that was set or -1 in case of error - **/ -static inline int -current_sandbox_set_file_descriptor(int io_handle_index, int file_descriptor) -{ - struct sandbox *sandbox = current_sandbox_get(); - return sandbox_set_file_descriptor(sandbox, io_handle_index, file_descriptor); -} - -/** - * Get the file descriptor of the sandbox's ith io_handle - * @param io_handle_index index into the sandbox's io_handles table - * @returns file descriptor - **/ -static inline int -current_sandbox_get_file_descriptor(int io_handle_index) -{ - struct sandbox *sandbox = current_sandbox_get(); - return sandbox_get_file_descriptor(sandbox, io_handle_index); -} - -/** - * Close the sandbox's ith io_handle - * @param io_handle_index index of the handle to close - **/ -static inline void -current_sandbox_close_file_descriptor(int io_handle_index) -{ - struct sandbox *sandbox = current_sandbox_get(); - sandbox_close_file_descriptor(sandbox, io_handle_index); -} - -/** - * Get the Libuv handle located at idx of the sandbox ith io_handle - * @param io_handle_index index of the handle containing libuv_handle??? - * @returns any libuv handle - **/ -static inline union uv_any_handle * -current_sandbox_get_libuv_handle(int io_handle_index) -{ - struct sandbox *sandbox = current_sandbox_get(); - return sandbox_get_libuv_handle(sandbox, io_handle_index); -} - -/** - * Gets the HTTP Request body from the current sandbox - * @param body pointer that we'll assign to the http_request body - * @returns the length of the http_request's body - **/ -static inline int -current_sandbox_get_http_request_body(char **body) -{ - return http_request_get_body(¤t_sandbox_get()->http_request, body); -} - - -/** - * Set an HTTP Response Header on the current sandbox - * @param header string of the header that we want to set - * @param length the length of the header string - * @returns 0 (abends program in case of error) - **/ -static inline int -current_sandbox_set_http_response_header(char *header, int length) -{ - return http_response_set_header(¤t_sandbox_get()->http_response, header, length); -} - -/** - * Set an HTTP Response Body on the current sandbox - * @param body string of the body that we want to set - * @param length the length of the body string - * @returns 0 (abends program in case of error) - **/ -static inline int -current_sandbox_set_http_response_body(char *body, int length) -{ - return http_response_set_body(¤t_sandbox_get()->http_response, body, length); -} - -/** - * Set an HTTP Response Status on the current sandbox - * @param status string of the status we want to set - * @param length the length of the status - * @returns 0 (abends program in case of error) - **/ -static inline int -current_sandbox_set_http_response_status(char *status, int length) -{ - return http_response_set_status(¤t_sandbox_get()->http_response, status, length); -} - -/** - * Encode the current sandbox's HTTP Response as an array of buffers - * @returns the number of buffers used to store the HTTP Response - **/ -static inline int -current_sandbox_vectorize_http_response(void) -{ - return http_response_encode_as_vector(¤t_sandbox_get()->http_response); -} - +void current_sandbox_close_file_descriptor(int io_handle_index); +struct sandbox * current_sandbox_get(void); +char * current_sandbox_get_arguments(void); +int current_sandbox_get_file_descriptor(int io_handle_index); +int current_sandbox_get_http_request_body(char **body); +union uv_any_handle *current_sandbox_get_libuv_handle(int io_handle_index); +int current_sandbox_initialize_io_handle(void); +int current_sandbox_initialize_io_handle_and_set_file_descriptor(int file_descriptor); +int current_sandbox_parse_http_request(size_t length); +void current_sandbox_set(struct sandbox *sandbox); +int current_sandbox_set_file_descriptor(int io_handle_index, int file_descriptor); +int current_sandbox_set_http_response_header(char *header, int length); +int current_sandbox_set_http_response_body(char *body, int length); +int current_sandbox_set_http_response_status(char *status, int length); +int current_sandbox_vectorize_http_response(void); #endif /* SFRT_CURRENT_SANDBOX_H */ diff --git a/runtime/include/sandbox.h b/runtime/include/sandbox.h index 1ac3101..d58e212 100644 --- a/runtime/include/sandbox.h +++ b/runtime/include/sandbox.h @@ -82,7 +82,6 @@ typedef struct sandbox sandbox_t; **************************/ -extern __thread struct sandbox *worker_thread_current_sandbox; extern __thread arch_context_t *worker_thread_next_context; extern void worker_thread_block_current_sandbox(void); diff --git a/runtime/src/current_sandbox.c b/runtime/src/current_sandbox.c new file mode 100644 index 0000000..3338935 --- /dev/null +++ b/runtime/src/current_sandbox.c @@ -0,0 +1,185 @@ +#include "current_sandbox.h" + +// current sandbox that is active.. +static __thread sandbox_t *worker_thread_current_sandbox = NULL; + +/** + * Getter for the current sandbox executing on this thread + * @returns the current sandbox executing on this thread + **/ +struct sandbox * +current_sandbox_get(void) +{ + return worker_thread_current_sandbox; +} + +/** + * Setter for the current sandbox executing on this thread + * @param sandbox the sandbox we are setting this thread to run + **/ +void +current_sandbox_set(struct sandbox *sandbox) +{ + // FIXME: critical-section. + worker_thread_current_sandbox = sandbox; + if (sandbox == NULL) return; + + // Thread Local State about the Current Sandbox + sandbox_lmbase = sandbox->linear_memory_start; + sandbox_lmbound = sandbox->linear_memory_size; + module_indirect_table = sandbox->module->indirect_table; +} + +/** + * Getter for the arguments of the current sandbox + * @return the arguments of the current sandbox + */ +char * +current_sandbox_get_arguments(void) +{ + struct sandbox *sandbox = current_sandbox_get(); + return sandbox_get_arguments(sandbox); +} + +/** + * Initializes and returns an IO handle on the current sandbox ready for use + * @return index of handle we preopened or -1 if all io_handles are exhausted + **/ +int +current_sandbox_initialize_io_handle(void) +{ + struct sandbox *sandbox = current_sandbox_get(); + return sandbox_initialize_io_handle(sandbox); +} + +/** + * Initializes and returns an IO handle on the current sandbox ready for use + * @param file_descriptor what we'll set on the IO handle after initialization + * @return index of handle we preopened or -1 if all io_handles are exhausted + **/ +int +current_sandbox_initialize_io_handle_and_set_file_descriptor(int file_descriptor) +{ + struct sandbox *sandbox = current_sandbox_get(); + return sandbox_initialize_io_handle_and_set_file_descriptor(sandbox, file_descriptor); +} + +int sandbox_parse_http_request(struct sandbox *sandbox, size_t l); + +/** + * Parse the current sandbox's request_response_data up to length + * @param length + * @returns 0 + **/ +int +current_sandbox_parse_http_request(size_t length) +{ + return sandbox_parse_http_request(current_sandbox_get(), length); +} + +/** + * Sets the file descriptor of the sandbox's ith io_handle + * Returns error condition if the file_descriptor to set does not contain sandbox preopen magin + * @param io_handle_index index of the sandbox io_handle we want to set + * @param file_descriptor the file descripter we want to set it to + * @returns the index that was set or -1 in case of error + **/ +int +current_sandbox_set_file_descriptor(int io_handle_index, int file_descriptor) +{ + struct sandbox *sandbox = current_sandbox_get(); + return sandbox_set_file_descriptor(sandbox, io_handle_index, file_descriptor); +} + +/** + * Get the file descriptor of the sandbox's ith io_handle + * @param io_handle_index index into the sandbox's io_handles table + * @returns file descriptor + **/ +int +current_sandbox_get_file_descriptor(int io_handle_index) +{ + struct sandbox *sandbox = current_sandbox_get(); + return sandbox_get_file_descriptor(sandbox, io_handle_index); +} + +/** + * Close the sandbox's ith io_handle + * @param io_handle_index index of the handle to close + **/ +void +current_sandbox_close_file_descriptor(int io_handle_index) +{ + struct sandbox *sandbox = current_sandbox_get(); + sandbox_close_file_descriptor(sandbox, io_handle_index); +} + +/** + * Get the Libuv handle located at idx of the sandbox ith io_handle + * @param io_handle_index index of the handle containing libuv_handle??? + * @returns any libuv handle + **/ +union uv_any_handle * +current_sandbox_get_libuv_handle(int io_handle_index) +{ + struct sandbox *sandbox = current_sandbox_get(); + return sandbox_get_libuv_handle(sandbox, io_handle_index); +} + +/** + * Gets the HTTP Request body from the current sandbox + * @param body pointer that we'll assign to the http_request body + * @returns the length of the http_request's body + **/ +int +current_sandbox_get_http_request_body(char **body) +{ + return http_request_get_body(¤t_sandbox_get()->http_request, body); +} + + +/** + * Set an HTTP Response Header on the current sandbox + * @param header string of the header that we want to set + * @param length the length of the header string + * @returns 0 (abends program in case of error) + **/ +int +current_sandbox_set_http_response_header(char *header, int length) +{ + return http_response_set_header(¤t_sandbox_get()->http_response, header, length); +} + +/** + * Set an HTTP Response Body on the current sandbox + * @param body string of the body that we want to set + * @param length the length of the body string + * @returns 0 (abends program in case of error) + **/ +int +current_sandbox_set_http_response_body(char *body, int length) +{ + return http_response_set_body(¤t_sandbox_get()->http_response, body, length); +} + +/** + * Set an HTTP Response Status on the current sandbox + * @param status string of the status we want to set + * @param length the length of the status + * @returns 0 (abends program in case of error) + **/ +int +current_sandbox_set_http_response_status(char *status, int length) +{ + return http_response_set_status(¤t_sandbox_get()->http_response, status, length); +} + +/** + * Encode the current sandbox's HTTP Response as an array of buffers + * @returns the number of buffers used to store the HTTP Response + **/ +int +current_sandbox_vectorize_http_response(void) +{ + return http_response_encode_as_vector(¤t_sandbox_get()->http_response); +} diff --git a/runtime/src/runtime.c b/runtime/src/runtime.c index fdb853c..8ee694d 100644 --- a/runtime/src/runtime.c +++ b/runtime/src/runtime.c @@ -143,9 +143,6 @@ listener_thread_initialize(void) __thread static struct ps_list_head worker_thread_completion_queue; -// current sandbox that is active.. -__thread sandbox_t *worker_thread_current_sandbox = NULL; - // context pointer to switch to when this thread gets a SIGUSR1 __thread arch_context_t *worker_thread_next_context = NULL; diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index 7e856e1..90c1694 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -8,6 +8,8 @@ #include #include +extern http_parser_settings runtime_http_parser_settings; + /** * Takes the arguments from the sandbox struct and writes them into the WebAssembly linear memory * TODO: why do we have to pass argument count explicitly? Can't we just get this off the sandbox?