From a29f36ca13df5b54608926361abd42596c2b61a4 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Mon, 16 Mar 2020 20:20:48 -0400 Subject: [PATCH] chore: decompose current_sandbox.h --- runtime/include/current_sandbox.h | 190 ++++++++++++++++++++++++++++++ runtime/include/sandbox.h | 186 +---------------------------- runtime/src/libc/uvio.c | 1 + runtime/src/memory/64bit_nix.c | 1 + runtime/src/runtime.c | 1 + runtime/src/sandbox.c | 1 + runtime/src/softint.c | 1 + 7 files changed, 196 insertions(+), 185 deletions(-) create mode 100644 runtime/include/current_sandbox.h diff --git a/runtime/include/current_sandbox.h b/runtime/include/current_sandbox.h new file mode 100644 index 0000000..7d09dfb --- /dev/null +++ b/runtime/include/current_sandbox.h @@ -0,0 +1,190 @@ +#ifndef SFRT_CURRENT_SANDBOX_H +#define SFRT_CURRENT_SANDBOX_H + +#include +#include + +/** + * 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 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. + 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 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 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); +} + +extern http_parser_settings global__http_parser_settings; +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 handle_index index of the sandbox handles 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 handle_index, int file_descriptor) +{ + struct sandbox *sandbox = current_sandbox__get(); + return sandbox__set_file_descriptor(sandbox, handle_index, file_descriptor); +} + +/** + * Get the file descriptor of the sandbox's ith io_handle + * @param handle_index index into the sandbox's handles table + * @returns file descriptor + **/ +static inline int +current_sandbox__get_file_descriptor(int handle_index) +{ + struct sandbox *sandbox = current_sandbox__get(); + return sandbox__get_file_descriptor(sandbox, handle_index); +} + +/** + * Close the sandbox's ith io_handle + * @param handle_index index of the handle to close + **/ +static inline void +current_sandbox__close_file_descriptor(int handle_index) +{ + struct sandbox *sandbox = current_sandbox__get(); + sandbox__close_file_descriptor(sandbox, handle_index); +} + +/** + * Get the Libuv handle located at idx of the sandbox ith io_handle + * @param 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 handle_index) +{ + struct sandbox *sandbox = current_sandbox__get(); + return sandbox__get_libuv_handle(sandbox, 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); +} + + +#endif /* SFRT_CURRENT_SANDBOX_H */ diff --git a/runtime/include/sandbox.h b/runtime/include/sandbox.h index f5521dc..8f8002a 100644 --- a/runtime/include/sandbox.h +++ b/runtime/include/sandbox.h @@ -207,47 +207,7 @@ sandbox__get_libuv_handle(struct sandbox *sandbox, int handle_index) return &sandbox->handles[handle_index].libuv_handle; } -/*************************** - * Current Sandbox * - **************************/ - -/** - * 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 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. - 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); -} void * sandbox_worker_main(void *data); struct sandbox *get_next_sandbox_from_local_run_queue(int interrupt); @@ -262,150 +222,6 @@ void sandbox_response(void); // should have been called with stack allocated and current_sandbox__get() set! void sandbox_main(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 -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 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); -} - -extern http_parser_settings global__http_parser_settings; -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 handle_index index of the sandbox handles 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 handle_index, int file_descriptor) -{ - struct sandbox *sandbox = current_sandbox__get(); - return sandbox__set_file_descriptor(sandbox, handle_index, file_descriptor); -} - -/** - * Get the file descriptor of the sandbox's ith io_handle - * @param handle_index index into the sandbox's handles table - * @returns file descriptor - **/ -static inline int -current_sandbox__get_file_descriptor(int handle_index) -{ - struct sandbox *sandbox = current_sandbox__get(); - return sandbox__get_file_descriptor(sandbox, handle_index); -} - -/** - * Close the sandbox's ith io_handle - * @param handle_index index of the handle to close - **/ -static inline void -current_sandbox__close_file_descriptor(int handle_index) -{ - struct sandbox *sandbox = current_sandbox__get(); - sandbox__close_file_descriptor(sandbox, handle_index); -} - -/** - * Get the Libuv handle located at idx of the sandbox ith io_handle - * @param 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 handle_index) -{ - struct sandbox *sandbox = current_sandbox__get(); - return sandbox__get_libuv_handle(sandbox, 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); -} - +int sandbox__parse_http_request(struct sandbox *sandbox, size_t length); #endif /* SFRT_SANDBOX_H */ diff --git a/runtime/src/libc/uvio.c b/runtime/src/libc/uvio.c index c57360b..f094502 100644 --- a/runtime/src/libc/uvio.c +++ b/runtime/src/libc/uvio.c @@ -2,6 +2,7 @@ #include #include #include +#include // What should we tell the child program its UID and GID are? #define UID 0xFF diff --git a/runtime/src/memory/64bit_nix.c b/runtime/src/memory/64bit_nix.c index 3cae58b..acea3c7 100644 --- a/runtime/src/memory/64bit_nix.c +++ b/runtime/src/memory/64bit_nix.c @@ -1,6 +1,7 @@ /* Code from https://github.com/gwsystems/silverfish/blob/master/runtime/memory/64bit_nix.c */ #include #include +#include #ifdef USE_MEM_VM diff --git a/runtime/src/runtime.c b/runtime/src/runtime.c index 96d143f..0a449d2 100644 --- a/runtime/src/runtime.c +++ b/runtime/src/runtime.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "sandbox_request.h" diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index 6a52815..e04bb69 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -7,6 +7,7 @@ #include #include #include +#include /** * Takes the arguments from the sandbox struct and writes them into the WebAssembly linear memory diff --git a/runtime/src/softint.c b/runtime/src/softint.c index d18b65b..d4d2bd5 100644 --- a/runtime/src/softint.c +++ b/runtime/src/softint.c @@ -12,6 +12,7 @@ #include #include #include +#include /*************************************** * Process Globals