summaryrefslogtreecommitdiff
path: root/wasmtime-wasi-http.c
diff options
context:
space:
mode:
authorAndrew Clayton <a.clayton@nginx.com>2023-10-17 12:53:18 +0100
committerAndrew Clayton <a.clayton@nginx.com>2023-10-17 12:53:18 +0100
commit4af8e80018a387e0d54be3c3cf902ee12186df00 (patch)
tree045e1dbf3a9e2b66e162c7ae68d4b42b90658742 /wasmtime-wasi-http.c
downloadproject_blackbird-4af8e80018a387e0d54be3c3cf902ee12186df00.tar.gz
project_blackbird-4af8e80018a387e0d54be3c3cf902ee12186df00.tar.bz2
Initial commit
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to '')
-rw-r--r--wasmtime-wasi-http.c345
1 files changed, 345 insertions, 0 deletions
diff --git a/wasmtime-wasi-http.c b/wasmtime-wasi-http.c
new file mode 100644
index 0000000..8b8ddff
--- /dev/null
+++ b/wasmtime-wasi-http.c
@@ -0,0 +1,345 @@
+/*
+ * Copyright (C) Andrew Clayton
+ * Copyright (C) F5, Inc.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <wasm.h>
+#include <wasi.h>
+#include <wasmtime.h>
+
+static void exit_with_error(const char *message, wasmtime_error_t *error,
+ wasm_trap_t *trap)
+{
+ wasm_byte_vec_t error_message;
+
+ fprintf(stderr, "error: %s\n", message);
+ 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);
+
+ exit(EXIT_FAILURE);
+}
+
+static wasm_trap_t *dummy_func(void *env, wasmtime_caller_t *caller,
+ const wasmtime_val_t *args, size_t nargs,
+ wasmtime_val_t *results, size_t nresults)
+{
+ (void)env;
+ (void)caller;
+ (void)args;
+ (void)nargs;
+ (void)results;
+ (void)nresults;
+
+ return NULL;
+}
+
+static wasm_functype_t *create_wasm_func(const wasm_valkind_t *pk,
+ size_t nr_params,
+ const wasm_valkind_t *rk,
+ size_t nr_results)
+{
+ wasm_valtype_t *pt[14];
+ wasm_valtype_t *rt[1];
+ wasm_valtype_vec_t params;
+ wasm_valtype_vec_t results;
+
+ if (nr_params > 0) {
+ for (size_t i = 0; i < nr_params; i++)
+ pt[i] = wasm_valtype_new(pk[i]);
+ wasm_valtype_vec_new(&params, nr_params, pt);
+ } else {
+ wasm_valtype_vec_new_empty(&params);
+ }
+
+ if (nr_results > 0) {
+ for (size_t i = 0; i < nr_results; i++)
+ rt[i] = wasm_valtype_new(rk[i]);
+ wasm_valtype_vec_new(&results, nr_results, rt);
+ } else {
+ wasm_valtype_vec_new_empty(&results);
+ }
+
+ return wasm_functype_new(&params, &results);
+}
+
+static inline size_t rg(const char *s)
+{
+ return strlen(s);
+}
+
+/*
+ * Argument counting macros gleamed from
+ * https://gist.github.com/christophercrouzet/db9bbb07d427381253a91210e7ddc68d
+ */
+#define ARG( \
+ _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, ...) \
+ _14
+#define COUNT_ARGS(...) \
+ ARG(__VA_ARGS__, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,)
+
+#define SET_VALS(type, ...) \
+ .type = { __VA_ARGS__ }, \
+ .nr_##type = COUNT_ARGS(__VA_ARGS__)
+
+#define SET_FUNC(function, function_name, module) \
+ .func_name = function_name, \
+ .func_namelen = strlen(function_name), \
+ .mod_name = module, \
+ .mod_namelen = strlen(module), \
+ .func = dummy_func
+
+static void set_function_imports(wasmtime_linker_t *linker)
+{
+ static const struct {
+ const char *func_name;
+ int func_namelen;
+ const char *mod_name;
+ int mod_namelen;
+
+ wasmtime_func_callback_t func;
+
+ wasm_valkind_t params[14];
+ size_t nr_params;
+ wasm_valkind_t results[1];
+ size_t nr_results;
+ } import_functions[] = {
+ {
+ SET_FUNC(poll_oneoff, "poll-oneoff", "poll2"),
+ SET_VALS(params, WASM_I32, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(read, "read", "streams2"),
+ SET_VALS(params, WASM_I32, WASM_I64, WASM_I32),
+ }, {
+ SET_FUNC(blocking_read, "blocking-read", "streams2"),
+ SET_VALS(params, WASM_I32, WASM_I64, WASM_I32),
+ }, {
+ SET_FUNC(subscribe_to_input_stream,
+ "subscribe-to-input-stream", "streams2"),
+ SET_VALS(params, WASM_I32),
+ SET_VALS(results, WASM_I32),
+ }, {
+ SET_FUNC(write, "write", "streams2"),
+ SET_VALS(params,
+ WASM_I32, WASM_I32, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(blocking_write, "blocking-write", "streams2"),
+ SET_VALS(params,
+ WASM_I32, WASM_I32, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(subscribe_to_output_stream,
+ "subscribe-to-output-stream", "streams2"),
+ SET_VALS(params, WASM_I32),
+ SET_VALS(results, WASM_I32),
+ }, {
+ SET_FUNC(handle, "handle", "default-outgoing-HTTP2"),
+ SET_VALS(params,
+ WASM_I32, WASM_I32, WASM_I32, WASM_I32,
+ WASM_I32, WASM_I32, WASM_I32, WASM_I32),
+ SET_VALS(results, WASM_I32),
+ }, {
+ SET_FUNC(new_fields, "new-fields", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ SET_VALS(results, WASM_I32),
+ }, {
+ SET_FUNC(fields_entries, "fields-entries", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(finish_incoming_stream,
+ "finish-incoming-stream", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(finish_outgoing_stream,
+ "finish-outgoing-stream", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(incoming_request_method,
+ "incoming-request-method", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(incoming_request_path_with_query,
+ "incoming-request-path-with-query", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(incoming_request_headers,
+ "incoming-request-headers", "types2"),
+ SET_VALS(params, WASM_I32),
+ SET_VALS(results, WASM_I32),
+ }, {
+ SET_FUNC(incoming_request_consume,
+ "incoming-request-consume", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(new_outgoing_request, "new-outgoing-request",
+ "types2"),
+ SET_VALS(params,
+ WASM_I32, WASM_I32, WASM_I32, WASM_I32,
+ WASM_I32, WASM_I32, WASM_I32, WASM_I32,
+ WASM_I32, WASM_I32, WASM_I32, WASM_I32,
+ WASM_I32, WASM_I32),
+ SET_VALS(results, WASM_I32),
+ }, {
+ SET_FUNC(outgoing_request_write,
+ "outgoing-request-write", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(set_response_outparam,
+ "set-response-outparam", "types2"),
+ SET_VALS(params,
+ WASM_I32, WASM_I32, WASM_I32, WASM_I32,
+ WASM_I32),
+ SET_VALS(results, WASM_I32),
+ }, {
+ SET_FUNC(incoming_response_status,
+ "incoming-response-status", "types2"),
+ SET_VALS(params, WASM_I32),
+ SET_VALS(results, WASM_I32),
+ }, {
+ SET_FUNC(incoming_response_headers,
+ "incoming-response-headers", "types2"),
+ SET_VALS(params, WASM_I32),
+ SET_VALS(results, WASM_I32),
+ }, {
+ SET_FUNC(incoming_response_consume,
+ "incoming-response-consume", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(new_outgoing_response,
+ "new-outgoing-response", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ SET_VALS(results, WASM_I32),
+ }, {
+ SET_FUNC(outgoing_response_write,
+ "outgoing-response-write", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(future_incoming_response-get,
+ "future-incoming-response-get", "types2"),
+ SET_VALS(params, WASM_I32, WASM_I32),
+ }, {
+ SET_FUNC(listen_to_fauture_incoming_response,
+ "listen-to-future-incoming-response",
+ "types2"),
+ SET_VALS(params, WASM_I32),
+ SET_VALS(results, WASM_I32),
+ },
+
+ { }
+ }, *imf;
+
+ for (imf = import_functions; imf->func_name != NULL; imf++) {
+ wasm_functype_t *func_ty;
+
+ printf(" [%s::%s]\n", imf->mod_name, imf->func_name);
+
+ func_ty = create_wasm_func(imf->params, imf->nr_params,
+ imf->results, imf->nr_results);
+ wasmtime_linker_define_func(linker,
+ imf->mod_name, imf->mod_namelen,
+ imf->func_name, imf->func_namelen,
+ func_ty, imf->func, NULL, NULL);
+ wasm_functype_delete(func_ty);
+ }
+}
+
+int main(void)
+{
+ wasm_engine_t *engine;
+ wasmtime_store_t *store;
+ wasmtime_context_t *context;
+ wasmtime_module_t *module = NULL;
+ wasmtime_linker_t *linker;
+ wasmtime_error_t *error;
+ wasm_byte_vec_t wasm;
+ wasmtime_extern_t item;
+ wasi_config_t *wasi_config;
+ FILE *file;
+ size_t file_size;
+ bool ok;
+
+ printf("Initializing...\n");
+ engine = wasm_engine_new();
+ store = wasmtime_store_new(engine, NULL, NULL);
+ context = wasmtime_store_context(store);
+
+ linker = wasmtime_linker_new(engine);
+ error = wasmtime_linker_define_wasi(linker);
+ if (error != NULL)
+ exit_with_error("failed to link wasi", error, NULL);
+
+ printf("Loading binary...\n");
+ file = fopen(WASM_MODULE, "r");
+ if (!file) {
+ printf("> Error loading file!\n");
+ exit(EXIT_FAILURE);
+ }
+ fseek(file, 0L, SEEK_END);
+ file_size = ftell(file);
+ wasm_byte_vec_new_uninitialized(&wasm, file_size);
+ fseek(file, 0L, SEEK_SET);
+ if (fread(wasm.data, file_size, 1, file) != 1) {
+ printf("> Error loading module!\n");
+ exit(EXIT_FAILURE);
+ }
+ fclose(file);
+
+ printf("Compiling module...\n");
+ error = wasmtime_module_new(engine, (uint8_t*)wasm.data, wasm.size,
+ &module);
+ if (!module)
+ exit_with_error("failed to compile module", error, NULL);
+ wasm_byte_vec_delete(&wasm);
+
+ printf("Setting function imports...\n");
+ set_function_imports(linker);
+
+ printf("Initialising WASI...\n");
+ 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(context, wasi_config);
+ if (error)
+ exit_with_error("failed to initialise WASI", error, NULL);
+
+ printf("Instantiating module...\n");
+ error = wasmtime_linker_module(linker, context, "", 0, module);
+ if (error != NULL)
+ exit_with_error("failed to instantiate module", error, NULL);
+
+ printf("Getting function exports...\n");
+ ok = wasmtime_linker_get(linker, context, "", 0, "HTTP#handle", 11,
+ &item);
+ if (!ok) {
+ printf("> Error getting function export [HTTP#handle]\n");
+ exit(EXIT_FAILURE);
+ }
+ printf(" [HTTP#handle]\n");
+
+ ok = wasmtime_linker_get(linker, context, "", 0, "cabi_realloc", 12,
+ &item);
+ if (!ok) {
+ printf("> Error getting function export [cabi_realloc]\n");
+ exit(EXIT_FAILURE);
+ }
+ printf(" [cabi_realloc]\n");
+
+ printf("Shutting down...\n");
+ wasmtime_module_delete(module);
+ wasmtime_store_delete(store);
+ wasm_engine_delete(engine);
+
+ printf("Done.\n");
+
+ exit(EXIT_SUCCESS);;
+}