Skip to content

Commit

Permalink
fix(proxy-wasm) load filter from lua - WasmX as module
Browse files Browse the repository at this point in the history
When WasmX is compiled as shared library and loaded into Nginx via
`load_module`, its modules will end up after `ngx_http_lua_module` in
`ngx_cycle->modules` list.

As a result, `ngx_http_lua_init_worker_by_inline` -- which might try to
load wasm filters -- is called before `ngx_wasm_core_init_process` -- which
prepares a wasm vm -- is ever called, causing the load to fail.
  • Loading branch information
casimiro committed Jun 22, 2023
1 parent ba8da72 commit 76ff2fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ if [ "$ngx_module_link" = DYNAMIC ]; then
# add other modules into this bundled one
ngx_wasm_module_MODULES=$ngx_module_order

have=NGX_WASM_AS_MODULE value=1 . auto/define

else
# addon
# bypass auto/make which does not recognize $WASM_ variables
Expand Down
32 changes: 32 additions & 0 deletions src/wasm/ngx_wasm_core_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,38 @@ ngx_wasm_core_create_conf(ngx_conf_t *cf)
static const ngx_str_t runtime_name = ngx_string(NGX_WASM_RUNTIME);
static const ngx_str_t ip = ngx_string(NGX_WASM_DEFAULT_RESOLVER_IP);

#if (NGX_WASM_LUA && NGX_WASM_AS_MODULE)
ngx_uint_t lua_i, wasm_i, tmp_i;
ngx_module_t *lua_module = NULL, *wasm_module = NULL, *tmp;

for (lua_i = 0; cycle->modules[lua_i]; lua_i++) {
lua_module = cycle->modules[lua_i];
if (ngx_str_eq(lua_module->name, -1, "ngx_http_lua_module", -1)) {
break;
}
}

for (wasm_i = lua_i + 1; cycle->modules[wasm_i]; wasm_i++) {
wasm_module = cycle->modules[wasm_i];
if (ngx_str_eq(wasm_module->name, -1, "ngx_wasm_core_module", -1)) {
break;
}
}

/* cycle->modules[wasm_i] is NULL if execution order doesn't need fixing */
if (cycle->modules[wasm_i] && cycle->modules[lua_i]) {
tmp = cycle->modules[lua_i];
tmp_i = cycle->modules[lua_i]->index;

cycle->modules[lua_i]->index = cycle->modules[wasm_i]->index;
cycle->modules[wasm_i]->index = tmp_i;

cycle->modules[lua_i] = cycle->modules[wasm_i];
cycle->modules[wasm_i] = tmp;

}
#endif

wcf = ngx_pcalloc(cycle->pool, sizeof(ngx_wasm_core_conf_t));
if (wcf == NULL) {
return NULL;
Expand Down

0 comments on commit 76ff2fb

Please sign in to comment.