Skip to content

Commit

Permalink
feat: honor the ABI version (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander committed Oct 26, 2021
1 parent 497bc6b commit 801edf2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/http/ngx_http_wasm_ctx.h
Expand Up @@ -6,9 +6,16 @@
#include "ngx_http_wasm_state.h"


#define PROXY_WASM_ABI_VER_010 0
#define PROXY_WASM_ABI_VER_020 1
#define PROXY_WASM_ABI_VER_021 2
#define PROXY_WASM_ABI_VER_MAX 99


typedef struct {
void *plugin;
uint32_t cur_ctx_id;
uint32_t abi_version;
ngx_str_t name;
ngx_http_wasm_state_t *state;
ngx_queue_t occupied;
Expand Down
33 changes: 28 additions & 5 deletions src/http/ngx_http_wasm_module.c
Expand Up @@ -17,6 +17,12 @@ static bool ngx_http_wasm_vm_inited = false;


static ngx_str_t plugin_start = ngx_string("_start");
static ngx_str_t abi_versions[] = {
ngx_string("proxy_abi_version_0_1_0"),
ngx_string("proxy_abi_version_0_2_0"),
ngx_string("proxy_abi_version_0_2_1"),
ngx_null_string,
};
static ngx_str_t proxy_on_context_create = ngx_string("proxy_on_context_create");
static ngx_str_t proxy_on_configure = ngx_string("proxy_on_configure");
static ngx_str_t proxy_on_done = ngx_string("proxy_on_done");
Expand Down Expand Up @@ -166,7 +172,8 @@ ngx_http_wasm_load_plugin(const char *name, size_t name_len,
const char *bytecode, size_t size)
{
void *plugin;
ngx_int_t rc;
ngx_int_t rc, i;
bool found;
ngx_http_wasm_plugin_t *hw_plugin;

if (!ngx_http_wasm_vm_inited) {
Expand Down Expand Up @@ -195,6 +202,15 @@ ngx_http_wasm_load_plugin(const char *name, size_t name_len,
hw_plugin->cur_ctx_id = 0;
hw_plugin->plugin = plugin;

/* assumed using latest ABI by default */
hw_plugin->abi_version = PROXY_WASM_ABI_VER_MAX;
for (i = 0; abi_versions[i].len != 0; i++) {
found = ngx_wasm_vm.has(plugin, &abi_versions[i]);
if (found) {
hw_plugin->abi_version = i;
}
}

hw_plugin->name.len = name_len;
hw_plugin->name.data = (u_char *) (hw_plugin + 1);
ngx_memcpy(hw_plugin->name.data, name, name_len);
Expand Down Expand Up @@ -591,10 +607,17 @@ ngx_http_wasm_on_http(ngx_http_wasm_plugin_ctx_t *hwp_ctx, ngx_http_request_t *r
return NGX_DECLINED;
}

rc = ngx_wasm_vm.call(hwp_ctx->hw_plugin->plugin,
&proxy_on_request_headers,
true, NGX_WASM_PARAM_I32_I32_I32, http_ctx->id,
0, 1);
if (hwp_ctx->hw_plugin->abi_version == PROXY_WASM_ABI_VER_010) {
rc = ngx_wasm_vm.call(hwp_ctx->hw_plugin->plugin,
&proxy_on_request_headers,
true, NGX_WASM_PARAM_I32_I32, http_ctx->id, 0);
} else {
rc = ngx_wasm_vm.call(hwp_ctx->hw_plugin->plugin,
&proxy_on_request_headers,
true, NGX_WASM_PARAM_I32_I32_I32, http_ctx->id,
0, 1);
}

ngx_http_wasm_set_state(NULL);

if (rc < 0) {
Expand Down
7 changes: 7 additions & 0 deletions src/vm/vm.h
Expand Up @@ -32,8 +32,15 @@ typedef struct {
*/
int32_t (*malloc)(ngx_log_t *log, int32_t size);

/*
* call run a function exported from the plugin.
*/
ngx_int_t (*call)(void *plugin, ngx_str_t *name, bool has_result,
int param_type, ...);
/*
* has check if a function is exported from the plugin.
*/
bool (*has)(void *plugin, ngx_str_t *name);
} ngx_wasm_vm_t;


Expand Down
12 changes: 12 additions & 0 deletions src/vm/wasmtime.c
Expand Up @@ -288,6 +288,17 @@ ngx_wasm_wasmtime_call(void *data, ngx_str_t *name, bool has_result, int param_t
}


static bool
ngx_wasm_wasmtime_has(void *data, ngx_str_t *name)
{
ngx_wasm_wasmtime_plugin_t *plugin = data;
wasmtime_extern_t func;

return wasmtime_instance_export_get(plugin->context, &plugin->instance,
(const char *) name->data, name->len, &func);
}


u_char *
ngx_wasm_wasmtime_get_memory(ngx_log_t *log, int32_t addr, int32_t size)
{
Expand Down Expand Up @@ -349,4 +360,5 @@ ngx_wasm_vm_t ngx_wasm_vm = {
ngx_wasm_wasmtime_get_memory,
ngx_wasm_wasmtime_malloc,
ngx_wasm_wasmtime_call,
ngx_wasm_wasmtime_has,
};

0 comments on commit 801edf2

Please sign in to comment.