Skip to content

Commit 492c002

Browse files
authored
fix(wasmtime): avoid crash during Nginx reload (#120)
Signed-off-by: spacewander <[email protected]>
1 parent ad0174a commit 492c002

File tree

2 files changed

+83
-12
lines changed

2 files changed

+83
-12
lines changed

src/vm/wasmtime.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323

2424
typedef struct {
25+
wasm_engine_t *vm_engine;
2526
wasmtime_module_t *module;
2627
wasmtime_store_t *store;
2728
wasmtime_context_t *context;
@@ -32,7 +33,6 @@ typedef struct {
3233

3334

3435
static ngx_str_t vm_name = ngx_string("wasmtime");
35-
static wasm_engine_t *vm_engine;
3636
static wasmtime_val_t param_int32[1] = {{ .kind = WASMTIME_I32 }};
3737
static wasmtime_val_t param_int32_int32[2] = {{ .kind = WASMTIME_I32 }, { .kind = WASMTIME_I32 }};
3838
static wasmtime_val_t param_int32_int32_int32[3] = {
@@ -96,24 +96,14 @@ ngx_wasm_wasmtime_init(void)
9696
{
9797
ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "init wasm vm: wasmtime");
9898

99-
vm_engine = wasm_engine_new();
100-
if (vm_engine == NULL) {
101-
return NGX_DECLINED;
102-
}
103-
10499
return NGX_OK;
105100
}
106101

107102

108103
static void
109104
ngx_wasm_wasmtime_cleanup(void)
110105
{
111-
if (vm_engine == NULL) {
112-
return;
113-
}
114-
115106
ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "cleanup wasm vm: wasmtime");
116-
wasm_engine_delete(vm_engine);
117107
}
118108

119109

@@ -122,6 +112,7 @@ ngx_wasm_wasmtime_load(const char *bytecode, size_t size)
122112
{
123113
size_t i;
124114
bool ok;
115+
wasm_engine_t *vm_engine;
125116
wasm_trap_t *trap = NULL;
126117
wasmtime_module_t *module;
127118
wasmtime_store_t *store;
@@ -132,10 +123,16 @@ ngx_wasm_wasmtime_load(const char *bytecode, size_t size)
132123
ngx_wasm_wasmtime_plugin_t *plugin;
133124
wasmtime_extern_t item;
134125

126+
vm_engine = wasm_engine_new();
127+
if (vm_engine == NULL) {
128+
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "failed to new engine");
129+
return NULL;
130+
}
131+
135132
error = wasmtime_module_new(vm_engine, (const uint8_t*) bytecode, size, &module);
136133
if (error != NULL) {
137134
ngx_wasm_wasmtime_report_error(ngx_cycle->log, "failed to new module: ", error, NULL);
138-
return NULL;
135+
goto free_engine;
139136
}
140137

141138
store = wasmtime_store_new(vm_engine, NULL, NULL);
@@ -215,6 +212,7 @@ ngx_wasm_wasmtime_load(const char *bytecode, size_t size)
215212
plugin->memory = item.of.memory;
216213

217214

215+
plugin->vm_engine = vm_engine;
218216
plugin->module = module;
219217
plugin->store = store;
220218
plugin->context = context;
@@ -236,6 +234,9 @@ ngx_wasm_wasmtime_load(const char *bytecode, size_t size)
236234
free_module:
237235
wasmtime_module_delete(module);
238236

237+
free_engine:
238+
wasm_engine_delete(vm_engine);
239+
239240
return NULL;
240241
}
241242

@@ -248,6 +249,7 @@ ngx_wasm_wasmtime_unload(void *data)
248249
wasmtime_module_delete(plugin->module);
249250
wasmtime_store_delete(plugin->store);
250251
wasmtime_linker_delete(plugin->linker);
252+
wasm_engine_delete(plugin->vm_engine);
251253

252254
ngx_free(plugin);
253255

t/hup.t

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
BEGIN {
16+
$ENV{TEST_NGINX_USE_HUP} = 1;
17+
}
18+
19+
use t::WASM 'no_plan';
20+
21+
run_tests();
22+
23+
__DATA__
24+
25+
=== TEST 1: sanity
26+
--- http_config
27+
init_worker_by_lua_block {
28+
local wasm = require("resty.proxy-wasm")
29+
local p = wasm.load("plugin", "t/testdata/log/main.go.wasm")
30+
wasm.on_configure(p, "blah")
31+
}
32+
--- grep_error_log eval
33+
qr/\[\w+\] \d+#\d+: ouch, something is wrong/
34+
--- grep_error_log_out eval
35+
qr/\[emerg\] \d+#\d+: ouch, something is wrong
36+
\[error\] \d+#\d+: ouch, something is wrong
37+
\[warn\] \d+#\d+: ouch, something is wrong
38+
\[info\] \d+#\d+: ouch, something is wrong
39+
/
40+
--- config
41+
location /t {
42+
return 200;
43+
}
44+
--- no_error_log
45+
[alert]
46+
47+
48+
49+
=== TEST 2: after receiving SIGHUP
50+
--- http_config
51+
init_worker_by_lua_block {
52+
local wasm = require("resty.proxy-wasm")
53+
local p = wasm.load("plugin", "t/testdata/log/main.go.wasm")
54+
wasm.on_configure(p, "blah")
55+
}
56+
--- grep_error_log eval
57+
qr/\[\w+\] \d+#\d+: ouch, something is wrong/
58+
--- grep_error_log_out eval
59+
qr/\[emerg\] \d+#\d+: ouch, something is wrong
60+
\[error\] \d+#\d+: ouch, something is wrong
61+
\[warn\] \d+#\d+: ouch, something is wrong
62+
\[info\] \d+#\d+: ouch, something is wrong
63+
/
64+
--- config
65+
location /t {
66+
return 200;
67+
}
68+
--- no_error_log
69+
[alert]

0 commit comments

Comments
 (0)