summaryrefslogtreecommitdiffhomepage
path: root/src/wasm/nxt_rt_wasmtime.c
diff options
context:
space:
mode:
authorAndrew Clayton <a.clayton@nginx.com>2023-08-08 14:29:54 +0100
committerAndrew Clayton <a.clayton@nginx.com>2023-08-17 13:09:14 +0100
commit6a211e2b7468a2524715b7f00a24aa460481bea7 (patch)
tree31a765fefa0d6c400a0fe0d4fcbea0b700827c4f /src/wasm/nxt_rt_wasmtime.c
parent0c444397366aa07e23573a03e733a0552187eac4 (diff)
downloadunit-6a211e2b7468a2524715b7f00a24aa460481bea7.tar.gz
unit-6a211e2b7468a2524715b7f00a24aa460481bea7.tar.bz2
Wasm: Add the core of initial WebAssembly language module support.
This adds the core of runtime WebAssembly[0] support. Future commits will enable this in the Unit core and expose the configuration. This introduces a new src/wasm directory for storing this source. We are initially using Wasmtime[0] as the WebAssembly runtime, however this has been designed with the ability to use different runtimes in mind. src/wasm/nxt_wasm.[ch] is the main interface to Unit. src/wasm/nxt_rt_wasmtime.c is the Wasmtime runtime support. This is nicely insulated from any knowledge of internal Unit workings. Wasmtime is what loads and runs the Wasm modules. The Wasm modules can export functions Wasmtime can call and Wasmtime can export functions that the module can call. We make use of both. The terminology used is that function exports are what the Wasm module exports and function imports are what the Wasm runtime exports to the module. We currently have four function imports (functions exported by the runtime to be called by the Wasm module). 1) nxt_wasm_get_init_mem_size This allows Wasm modules to get the size of the initially allocated shared memory. This is the size allocated at Unit startup and what the Wasm modules can assume they have access to (in reality this shared memory will likely be larger). The amount of memory allocated at startup is NXT_WASM_MEM_SIZE which as of this commit is 32MiB. We do actually allocate NXT_WASM_MEM_SIZE + NXT_WASM_PAGE_SIZE at startup which is an extra 64KiB (the smallest allocation unit), this is to allow room for the response structure and so module developers can just assume they have the full 32MiB for their actual response. 2) nxt_wasm_send_headers This allows WASM modules to send their headers. 3) nxt_wasm_send_response This allows WASM modules to send their response. 4) nxt_wasm_response_end This allows WASM modules to inform Unit they have finished sending their response. This calls nxt_unit_request_done() Then there are currently up to eight functions that a module can export. Three of which are required. These function can be named anything. I'll use the Unit configuration names to refer to them 1) request_handler The main driving function. This may be called multiple times for a single HTTP request if the request is larger than the shared memory. 2) malloc_handler Used to allocate a chunk of memory at language module startup. This memory is allocated from the WASM modules address space and is what is sued for communicating between the WASM module (the guest) and Unit (the host). 3) free_handler Used to free the memory from above at language module shutdown. Then there are the following optional handlers 1) module_init_handler If set, called at language module startup. 2) module_end_handler If set, called at language module shutdown. 3) request_init_handler If set, called at the start of request. Called only once per HTTP request. 4) request_end_handler If set, called once all of a request has been sent to the WASM module. 5) response_end_handler If set, called at the end of a request, once the WASM module has sent all its headers and data. 32bits We currently support 32bit WASM modules, I.e wasm32-wasi. Newer version of clang, 13+[2], do seem to have support for wasm64 as a target (which uses a LP64 model). However it's not entirely clear if the WASI SDK fully supports[3] this and by extension WASI libc/wasi-sysroot. 64bit support is something than can be explored more thoroughly in the future. As such in structures that are used to communicate between the host and guest we use 32bit ints. Even when a single byte might be enough. This is to avoid issues with structure layout differences between a 64bit host and 32bit guest (I.e WASM module) and the need for various bits of structure padding depending on host architecture. Instead everything is 4-byte aligned. [0]: <https://webassembly.org/> [1]: <https://wasmtime.dev/> [2]: <https://reviews.llvm.org/rG670944fb20b226fc22fa993ab521125f9adbd30a> [3]: <https://github.com/WebAssembly/wasi-sdk/issues/185> Reviewed-by: Alejandro Colomar <alx@nginx.com> Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'src/wasm/nxt_rt_wasmtime.c')
-rw-r--r--src/wasm/nxt_rt_wasmtime.c407
1 files changed, 407 insertions, 0 deletions
diff --git a/src/wasm/nxt_rt_wasmtime.c b/src/wasm/nxt_rt_wasmtime.c
new file mode 100644
index 00000000..07a6165a
--- /dev/null
+++ b/src/wasm/nxt_rt_wasmtime.c
@@ -0,0 +1,407 @@
+/*
+ * Copyright (C) Andrew Clayton
+ * Copyright (C) F5, Inc.
+ */
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdarg.h>
+
+#include <wasm.h>
+#include <wasi.h>
+#include <wasmtime.h>
+
+#include "nxt_wasm.h"
+
+
+typedef struct nxt_wasmtime_ctx_s nxt_wasmtime_ctx_t;
+
+struct nxt_wasmtime_ctx_s {
+ wasm_engine_t *engine;
+ wasmtime_store_t *store;
+ wasmtime_memory_t memory;
+ wasmtime_module_t *module;
+ wasmtime_linker_t *linker;
+ wasmtime_context_t *ctx;
+};
+
+static nxt_wasmtime_ctx_t nxt_wasmtime_ctx;
+
+
+static void
+nxt_wasmtime_err_msg(wasmtime_error_t *error, wasm_trap_t *trap,
+ const char *fmt, ...)
+{
+ va_list args;
+ wasm_byte_vec_t error_message;
+
+ fprintf(stderr, "WASMTIME ERROR: ");
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ fprintf(stderr, "\n");
+
+ if (error == NULL && trap == NULL) {
+ return;
+ }
+
+ if (error != NULL) {
+ wasmtime_error_message(error, &error_message);
+ wasmtime_error_delete(error);
+ } else {
+ wasm_trap_message(trap, &error_message);
+ wasm_trap_delete(trap);
+ }
+ fprintf(stderr, "%.*s\n", (int)error_message.size, error_message.data);
+
+ wasm_byte_vec_delete(&error_message);
+}
+
+
+static wasm_trap_t *
+nxt_wasm_get_init_mem_size(void *env, wasmtime_caller_t *caller,
+ const wasmtime_val_t *args, size_t nargs,
+ wasmtime_val_t *results, size_t nresults)
+{
+ results[0].of.i32 = NXT_WASM_MEM_SIZE;
+
+ return NULL;
+}
+
+
+static wasm_trap_t *
+nxt_wasm_response_end(void *env, wasmtime_caller_t *caller,
+ const wasmtime_val_t *args, size_t nargs,
+ wasmtime_val_t *results, size_t nresults)
+{
+ nxt_wasm_do_response_end(env);
+
+ return NULL;
+}
+
+
+static wasm_trap_t *
+nxt_wasm_send_response(void *env, wasmtime_caller_t *caller,
+ const wasmtime_val_t *args, size_t nargs,
+ wasmtime_val_t *results, size_t nresults)
+{
+ nxt_wasm_do_send_response(env, args[0].of.i32);
+
+ return NULL;
+}
+
+
+static wasm_trap_t *
+nxt_wasm_send_headers(void *env, wasmtime_caller_t *caller,
+ const wasmtime_val_t *args, size_t nargs,
+ wasmtime_val_t *results, size_t nresults)
+{
+ nxt_wasm_do_send_headers(env, args[0].of.i32);
+
+ return NULL;
+}
+
+
+static void
+nxt_wasmtime_execute_hook(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook)
+{
+ const char *name = ctx->fh[hook].func_name;
+ wasm_trap_t *trap = NULL;
+ wasmtime_error_t *error;
+ nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx;
+ const nxt_wasm_func_t *func = &ctx->fh[hook].func;
+
+ if (name == NULL) {
+ return;
+ }
+
+ error = wasmtime_func_call(rt_ctx->ctx, func, NULL, 0, NULL, 0, &trap);
+ if (error != NULL || trap != NULL) {
+ nxt_wasmtime_err_msg(error, trap, "failed to call hook function [%s]",
+ name);
+ }
+}
+
+
+static void
+nxt_wasmtime_execute_request(const nxt_wasm_ctx_t *ctx)
+{
+ int i = 0;
+ wasm_trap_t *trap = NULL;
+ wasmtime_val_t args[1] = { };
+ wasmtime_val_t results[1] = { };
+ wasmtime_error_t *error;
+ nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx;
+ const nxt_wasm_func_t *func = &ctx->fh[NXT_WASM_FH_REQUEST].func;
+
+ args[i].kind = WASMTIME_I32;
+ args[i++].of.i32 = ctx->baddr_off;
+
+ error = wasmtime_func_call(rt_ctx->ctx, func, args, i, results, 1, &trap);
+ if (error != NULL || trap != NULL) {
+ nxt_wasmtime_err_msg(error, trap,
+ "failed to call function [->wasm_request_handler]"
+ );
+ }
+}
+
+
+static void
+nxt_wasmtime_set_function_imports(nxt_wasm_ctx_t *ctx)
+{
+ nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx;
+
+ static const struct {
+ const char *func_name;
+
+ wasmtime_func_callback_t func;
+ wasm_valkind_t params[1];
+ wasm_valkind_t results[1];
+
+ enum {
+ NXT_WASM_FT_0_0,
+ NXT_WASM_FT_1_0,
+ NXT_WASM_FT_0_1,
+ } ft;
+ } import_functions[] = {
+ {
+ .func_name = "nxt_wasm_get_init_mem_size",
+ .func = nxt_wasm_get_init_mem_size,
+ .results = { WASM_I32 },
+ .ft = NXT_WASM_FT_0_1
+ }, {
+ .func_name = "nxt_wasm_response_end",
+ .func = nxt_wasm_response_end,
+ .ft = NXT_WASM_FT_0_0
+ }, {
+ .func_name = "nxt_wasm_send_response",
+ .func = nxt_wasm_send_response,
+ .params = { WASM_I32 },
+ .ft = NXT_WASM_FT_1_0
+ }, {
+ .func_name = "nxt_wasm_send_headers",
+ .func = nxt_wasm_send_headers,
+ .params = { WASM_I32 },
+ .ft = NXT_WASM_FT_1_0
+ },
+
+ { }
+ }, *imf;
+
+ for (imf = import_functions; imf->func_name != NULL; imf++) {
+ wasm_functype_t *func_ty;
+
+ switch (imf->ft) {
+ case NXT_WASM_FT_0_0:
+ func_ty = wasm_functype_new_0_0();
+ break;
+ case NXT_WASM_FT_1_0:
+ func_ty = wasm_functype_new_1_0(wasm_valtype_new(imf->params[0]));
+ break;
+ case NXT_WASM_FT_0_1:
+ func_ty = wasm_functype_new_0_1(wasm_valtype_new(imf->results[0]));
+ break;
+ default:
+ /* Stop GCC complaining about func_ty being used uninitialised */
+ func_ty = NULL;
+ }
+
+ wasmtime_linker_define_func(rt_ctx->linker, "env", 3,
+ imf->func_name, strlen(imf->func_name),
+ func_ty, imf->func, ctx, NULL);
+ wasm_functype_delete(func_ty);
+ }
+}
+
+
+static int
+nxt_wasmtime_get_function_exports(nxt_wasm_ctx_t *ctx)
+{
+ int i;
+ nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx;
+
+ for (i = 0; i < NXT_WASM_FH_NR; i++) {
+ bool ok;
+ wasmtime_extern_t item;
+
+ if (ctx->fh[i].func_name == NULL) {
+ continue;
+ }
+
+ ok = wasmtime_linker_get(rt_ctx->linker, rt_ctx->ctx, "", 0,
+ ctx->fh[i].func_name,
+ strlen(ctx->fh[i].func_name), &item);
+ if (!ok) {
+ nxt_wasmtime_err_msg(NULL, NULL,
+ "couldn't get (%s) export from module",
+ ctx->fh[i].func_name);
+ return -1;
+ }
+ ctx->fh[i].func = item.of.func;
+ }
+
+ return 0;
+}
+
+
+static int
+nxt_wasmtime_wasi_init(const nxt_wasm_ctx_t *ctx)
+{
+ wasi_config_t *wasi_config;
+ wasmtime_error_t *error;
+ nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx;
+
+ wasi_config = wasi_config_new();
+
+ wasi_config_inherit_env(wasi_config);
+ wasi_config_inherit_stdin(wasi_config);
+ wasi_config_inherit_stdout(wasi_config);
+ wasi_config_inherit_stderr(wasi_config);
+
+ error = wasmtime_context_set_wasi(rt_ctx->ctx, wasi_config);
+ if (error != NULL) {
+ nxt_wasmtime_err_msg(error, NULL, "failed to instantiate WASI");
+ return -1;
+ }
+
+ return 0;
+}
+
+
+static int
+nxt_wasmtime_init_memory(nxt_wasm_ctx_t *ctx)
+{
+ int i = 0;
+ bool ok;
+ wasm_trap_t *trap = NULL;
+ wasmtime_val_t args[1] = { };
+ wasmtime_val_t results[1] = { };
+ wasmtime_error_t *error;
+ wasmtime_extern_t item;
+ nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx;
+ const nxt_wasm_func_t *func = &ctx->fh[NXT_WASM_FH_MALLOC].func;
+
+ args[i].kind = WASMTIME_I32;
+ args[i++].of.i32 = NXT_WASM_MEM_SIZE + NXT_WASM_PAGE_SIZE;
+
+ error = wasmtime_func_call(rt_ctx->ctx, func, args, i, results, 1, &trap);
+ if (error != NULL || trap != NULL) {
+ nxt_wasmtime_err_msg(error, trap,
+ "failed to call function [->wasm_malloc_handler]"
+ );
+ return -1;
+ }
+
+ ok = wasmtime_linker_get(rt_ctx->linker, rt_ctx->ctx, "", 0, "memory",
+ strlen("memory"), &item);
+ if (!ok) {
+ nxt_wasmtime_err_msg(NULL, NULL, "couldn't get 'memory' from module\n");
+ return -1;
+ }
+ rt_ctx->memory = item.of.memory;
+
+ ctx->baddr_off = results[0].of.i32;
+ ctx->baddr = wasmtime_memory_data(rt_ctx->ctx, &rt_ctx->memory);
+
+ ctx->baddr += ctx->baddr_off;
+
+ return 0;
+}
+
+
+static int
+nxt_wasmtime_init(nxt_wasm_ctx_t *ctx)
+{
+ int err;
+ FILE *fp;
+ size_t file_size;
+ wasm_byte_vec_t wasm;
+ wasmtime_error_t *error;
+ nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx;
+
+ rt_ctx->engine = wasm_engine_new();
+ rt_ctx->store = wasmtime_store_new(rt_ctx->engine, NULL, NULL);
+ rt_ctx->ctx = wasmtime_store_context(rt_ctx->store);
+
+ rt_ctx->linker = wasmtime_linker_new(rt_ctx->engine);
+ error = wasmtime_linker_define_wasi(rt_ctx->linker);
+ if (error != NULL) {
+ nxt_wasmtime_err_msg(error, NULL, "failed to link wasi");
+ return -1;
+ }
+
+ fp = fopen(ctx->module_path, "r");
+ if (!fp) {
+ nxt_wasmtime_err_msg(NULL, NULL,
+ "error opening file (%s)", ctx->module_path);
+ return -1;
+ }
+ fseek(fp, 0L, SEEK_END);
+ file_size = ftell(fp);
+ wasm_byte_vec_new_uninitialized(&wasm, file_size);
+ fseek(fp, 0L, SEEK_SET);
+ if (fread(wasm.data, file_size, 1, fp) != 1) {
+ nxt_wasmtime_err_msg(NULL, NULL, "error loading module");
+ fclose(fp);
+ return -1;
+ }
+ fclose(fp);
+
+ error = wasmtime_module_new(rt_ctx->engine, (uint8_t *)wasm.data, wasm.size,
+ &rt_ctx->module);
+ if (!rt_ctx->module) {
+ nxt_wasmtime_err_msg(error, NULL, "failed to compile module");
+ return -1;
+ }
+ wasm_byte_vec_delete(&wasm);
+
+ nxt_wasmtime_set_function_imports(ctx);
+
+ nxt_wasmtime_wasi_init(ctx);
+
+ error = wasmtime_linker_module(rt_ctx->linker, rt_ctx->ctx, "", 0,
+ rt_ctx->module);
+ if (error != NULL) {
+ nxt_wasmtime_err_msg(error, NULL, "failed to instantiate");
+ return -1;
+ }
+
+ err = nxt_wasmtime_get_function_exports(ctx);
+ if (err) {
+ return -1;
+ }
+
+ err = nxt_wasmtime_init_memory(ctx);
+ if (err) {
+ return -1;
+ }
+
+ return 0;
+}
+
+
+static void
+nxt_wasmtime_destroy(const nxt_wasm_ctx_t *ctx)
+{
+ int i = 0;
+ wasmtime_val_t args[1] = { };
+ nxt_wasmtime_ctx_t *rt_ctx = &nxt_wasmtime_ctx;
+ const nxt_wasm_func_t *func = &ctx->fh[NXT_WASM_FH_FREE].func;
+
+ args[i].kind = WASMTIME_I32;
+ args[i++].of.i32 = ctx->baddr_off;
+
+ wasmtime_func_call(rt_ctx->ctx, func, args, i, NULL, 0, NULL);
+
+ wasmtime_module_delete(rt_ctx->module);
+ wasmtime_store_delete(rt_ctx->store);
+ wasm_engine_delete(rt_ctx->engine);
+}
+
+
+const nxt_wasm_operations_t nxt_wasm_ops = {
+ .init = nxt_wasmtime_init,
+ .destroy = nxt_wasmtime_destroy,
+ .exec_request = nxt_wasmtime_execute_request,
+ .exec_hook = nxt_wasmtime_execute_hook,
+};