Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI] fix(*) ensure fake requests filter chains are freed #421

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/common/lua/ngx_wasm_lua_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ ngx_http_wasm_ffi_set_property(ngx_http_request_t *r,
return NGX_ERROR;
}

pwctx = ngx_http_proxy_wasm.get_context(rctx);
pwctx = ngx_proxy_wasm_ctx(NULL, 0,
NGX_PROXY_WASM_ISOLATION_STREAM,
&ngx_http_proxy_wasm,
rctx);
if (pwctx == NULL) {
return NGX_ERROR;
}
Expand All @@ -215,7 +218,10 @@ ngx_http_wasm_ffi_get_property(ngx_http_request_t *r,
return NGX_ERROR;
}

pwctx = ngx_http_proxy_wasm.get_context(rctx);
pwctx = ngx_proxy_wasm_ctx(NULL, 0,
NGX_PROXY_WASM_ISOLATION_STREAM,
&ngx_http_proxy_wasm,
rctx);
if (pwctx == NULL) {
return NGX_ERROR;
}
Expand Down
22 changes: 13 additions & 9 deletions src/common/proxy_wasm/ngx_proxy_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ ngx_proxy_wasm_ctx_alloc(ngx_pool_t *pool)
return NULL;
}

pwctx->pool = pool;
pwctx->pool = ngx_create_pool(512, pool->log);
if (pwctx->pool == NULL) {
return NULL;
}

pwctx->parent_pool = pool;

ngx_rbtree_init(&pwctx->host_props_tree, &pwctx->host_props_sentinel,
ngx_str_rbtree_insert_value);
Expand Down Expand Up @@ -294,20 +299,17 @@ ngx_proxy_wasm_ctx(ngx_uint_t *filter_ids, size_t nfilters,
return NULL;
}

pool = pwctx->pool;
log = pwctx->log;

switch (pwctx->isolation) {
case NGX_PROXY_WASM_ISOLATION_NONE:
pwstore = filter->store;
pool = filter->pool;
log = pwctx->log;
break;
case NGX_PROXY_WASM_ISOLATION_STREAM:
pwstore = &pwctx->store;
pool = pwstore->pool;
log = pwctx->log;
break;
case NGX_PROXY_WASM_ISOLATION_FILTER:
pool = pwctx->pool;
log = pwctx->log;
break;
default:
ngx_proxy_wasm_log_error(NGX_LOG_WASM_NYI, pwctx->log, 0,
Expand Down Expand Up @@ -399,6 +401,7 @@ ngx_proxy_wasm_ctx_destroy(ngx_proxy_wasm_ctx_t *pwctx)

ngx_proxy_wasm_store_destroy(&pwctx->store);

#if 0
if (pwctx->authority.data) {
ngx_pfree(pwctx->pool, pwctx->authority.data);
}
Expand Down Expand Up @@ -442,10 +445,11 @@ ngx_proxy_wasm_ctx_destroy(ngx_proxy_wasm_ctx_t *pwctx)
if (pwctx->response_status.data) {
ngx_pfree(pwctx->pool, pwctx->response_status.data);
}
#endif

ngx_array_destroy(&pwctx->pwexecs);
ngx_destroy_pool(pwctx->pool);

ngx_pfree(pwctx->pool, pwctx);
ngx_pfree(pwctx->parent_pool, pwctx);
}


Expand Down
1 change: 1 addition & 0 deletions src/common/proxy_wasm/ngx_proxy_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ struct ngx_proxy_wasm_ctx_s {
ngx_proxy_wasm_context_type_e type;
ngx_log_t *log;
ngx_pool_t *pool;
ngx_pool_t *parent_pool;
void *data;

/* control */
Expand Down
20 changes: 20 additions & 0 deletions src/http/proxy_wasm/ngx_http_proxy_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,23 @@ ngx_http_proxy_wasm_ecode(ngx_proxy_wasm_err_e ecode)
}


static void
ngx_http_proxy_wasm_ctx_cleanup_handler(void *data)
{
ngx_proxy_wasm_ctx_t *pwctx = data;

ngx_proxy_wasm_ctx_destroy(pwctx);
}


static ngx_proxy_wasm_ctx_t *
ngx_http_proxy_wasm_ctx(void *data)
{
ngx_proxy_wasm_ctx_t *pwctx;
ngx_http_wasm_req_ctx_t *rctx = data;
ngx_http_request_t *r = rctx->r;
ngx_http_wasm_loc_conf_t *loc;
ngx_pool_cleanup_t *cln;

loc = ngx_http_get_module_loc_conf(r, ngx_http_wasm_module);

Expand All @@ -374,6 +384,16 @@ ngx_http_proxy_wasm_ctx(void *data)

/* for on_request_body retrieval */
rctx->data = pwctx;

if (rctx->fake_request) {
cln = ngx_pool_cleanup_add(pwctx->parent_pool, 0);
if (cln == NULL) {
return NULL;
}

cln->handler = ngx_http_proxy_wasm_ctx_cleanup_handler;
cln->data = pwctx;
}
}

return pwctx;
Expand Down
35 changes: 27 additions & 8 deletions src/wasm/wrt/ngx_wrt_wasmtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <ngx_wasi.h>


#define NGX_WRT_WASMTIME_STACK_ARGS 10
#define NGX_WRT_WASMTIME_STACK_RETS 10


typedef void (*wasmtime_config_set_int_pt)(wasm_config_t *config,
uint64_t value);
typedef void (*wasmtime_config_set_bool_pt)(wasm_config_t *config, bool value);
Expand Down Expand Up @@ -611,6 +615,9 @@ ngx_wasmtime_call(ngx_wrt_instance_t *instance, ngx_str_t *func_name,
wasmtime_extern_t item;
wasmtime_func_t *func;
wasmtime_val_t *wargs = NULL, *wrets = NULL;
wasmtime_val_t swargs[NGX_WRT_WASMTIME_STACK_ARGS];
wasmtime_val_t swrets[NGX_WRT_WASMTIME_STACK_RETS];


if (!wasmtime_instance_export_get(instance->store->context,
&instance->instance,
Expand All @@ -624,16 +631,28 @@ ngx_wasmtime_call(ngx_wrt_instance_t *instance, ngx_str_t *func_name,

func = &item.of.func;

wargs = ngx_pcalloc(instance->pool, sizeof(wasmtime_val_t) * args->size);
if (wargs == NULL) {
goto done;
if (args->size <= NGX_WRT_WASMTIME_STACK_ARGS) {
wargs = &swargs[0];

} else {
wargs = ngx_pcalloc(instance->pool,
sizeof(wasmtime_val_t) * args->size);
if (wargs == NULL) {
goto done;
}
}

ngx_wasm_valvec2wasmtime(wargs, args);

wrets = ngx_pcalloc(instance->pool, sizeof(wasmtime_val_t) * rets->size);
if (wrets == NULL) {
goto done;
if (rets->size <= NGX_WRT_WASMTIME_STACK_RETS) {
wrets = &swrets[0];

} else {
wrets = ngx_pcalloc(instance->pool,
sizeof(wasmtime_val_t) * rets->size);
if (wrets == NULL) {
goto done;
}
}

err->res = wasmtime_func_call(instance->store->context,
Expand All @@ -652,11 +671,11 @@ ngx_wasmtime_call(ngx_wrt_instance_t *instance, ngx_str_t *func_name,

done:

if (wargs) {
if (args->size > NGX_WRT_WASMTIME_STACK_ARGS && wargs) {
ngx_pfree(instance->pool, wargs);
}

if (wrets) {
if (rets->size > NGX_WRT_WASMTIME_STACK_RETS && wrets) {
ngx_pfree(instance->pool, wrets);
}

Expand Down