diff options
author | Andrew Clayton <a.clayton@nginx.com> | 2023-10-26 22:48:07 +0100 |
---|---|---|
committer | Andrew Clayton <a.clayton@nginx.com> | 2023-10-26 22:54:34 +0100 |
commit | e3a37a1bb4908b02553096afaeb380dbc85bb7bb (patch) | |
tree | 6457f713cbb2d64b0d6f8839e662733e258c5df2 /wasmtime-wasi-http.c | |
parent | 4904aa341be9c74dec28e6eeea79b707211f2f06 (diff) | |
download | project_blackbird-e3a37a1bb4908b02553096afaeb380dbc85bb7bb.tar.gz project_blackbird-e3a37a1bb4908b02553096afaeb380dbc85bb7bb.tar.bz2 |
Move wasmtime-wasi-http to its own directory
Moved to c/wasmtime-wasi-http
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'wasmtime-wasi-http.c')
-rw-r--r-- | wasmtime-wasi-http.c | 345 |
1 files changed, 0 insertions, 345 deletions
diff --git a/wasmtime-wasi-http.c b/wasmtime-wasi-http.c deleted file mode 100644 index e1b6693..0000000 --- a/wasmtime-wasi-http.c +++ /dev/null @@ -1,345 +0,0 @@ -/* - * 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(¶ms, nr_params, pt); - } else { - wasm_valtype_vec_new_empty(¶ms); - } - - 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(¶ms, &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("Initialising...\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);; -} |