Skip to content

Commit 1d3a962

Browse files
authored
refactor: move malloc method out of Wasm VM (#91)
1 parent 5c72ab1 commit 1d3a962

File tree

6 files changed

+78
-44
lines changed

6 files changed

+78
-44
lines changed

config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ngx_module_srcs=" \
2020
$ngx_addon_dir/src/http/ngx_http_wasm_call.c \
2121
$ngx_addon_dir/src/http/ngx_http_wasm_state.c \
2222
$ngx_addon_dir/src/proxy_wasm/proxy_wasm_map.c \
23+
$ngx_addon_dir/src/proxy_wasm/proxy_wasm_memory.c \
2324
$ngx_addon_dir/src/vm/wasmtime.c \
2425
$ngx_addon_dir/src/vm/vm.c \
2526
"

src/http/ngx_http_wasm_api.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "ngx_http_wasm_call.h"
2424
#include "ngx_http_wasm_ctx.h"
2525
#include "proxy_wasm/proxy_wasm_map.h"
26+
#include "proxy_wasm/proxy_wasm_memory.h"
2627

2728

2829
typedef struct {
@@ -201,7 +202,7 @@ ngx_http_wasm_copy_to_wasm(ngx_log_t *log, const u_char *data, int32_t len,
201202
return PROXY_RESULT_OK;
202203
}
203204

204-
buf_addr = ngx_wasm_vm.malloc(log, len);
205+
buf_addr = proxy_wasm_memory_alloc(log, len);
205206
if (buf_addr == 0) {
206207
ngx_log_error(NGX_LOG_ERR, log, 0, "no memory");
207208
return PROXY_RESULT_INTERNAL_FAILURE;
@@ -233,7 +234,7 @@ ngx_http_wasm_get_buf_to_write(ngx_log_t *log, int32_t len,
233234
int32_t *p;
234235
u_char *buf;
235236

236-
buf_addr = ngx_wasm_vm.malloc(log, len);
237+
buf_addr = proxy_wasm_memory_alloc(log, len);
237238
if (buf_addr == 0) {
238239
ngx_log_error(NGX_LOG_ERR, log, 0, "no memory");
239240
return NULL;

src/proxy_wasm/proxy_wasm_memory.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
#include "vm/vm.h"
18+
#include "proxy_wasm_memory.h"
19+
20+
21+
static ngx_str_t proxy_on_memory_allocate = ngx_string("proxy_on_memory_allocate");
22+
static ngx_str_t exported_malloc = ngx_string("malloc");
23+
24+
25+
int32_t proxy_wasm_memory_alloc(ngx_log_t *log, int32_t size)
26+
{
27+
int32_t addr;
28+
29+
addr = ngx_wasm_vm.call(NULL, &proxy_on_memory_allocate, true, NGX_WASM_PARAM_I32, size);
30+
if (addr == 0) {
31+
addr = ngx_wasm_vm.call(NULL, &exported_malloc, true, NGX_WASM_PARAM_I32, size);
32+
}
33+
34+
if (addr == 0) {
35+
ngx_log_error(NGX_LOG_ERR, log, 0, "failed to malloc");
36+
}
37+
38+
return addr;
39+
}

src/proxy_wasm/proxy_wasm_memory.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
#ifndef PROXY_WASM_MEMORY_H
18+
#define PROXY_WASM_MEMORY_H
19+
#include <ngx_core.h>
20+
21+
22+
/*
23+
* malloc allocates memory in WASM, and then return the address of the allocated
24+
* memory.
25+
*/
26+
int32_t proxy_wasm_memory_alloc(ngx_log_t *log, int32_t size);
27+
28+
29+
#endif // PROXY_WASM_MEMORY_H

src/vm/vm.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ typedef struct {
4444
* It returns NULL if addr + size is out of bound.
4545
*/
4646
u_char *(*get_memory)(ngx_log_t *log, int32_t addr, int32_t size);
47-
/*
48-
* malloc allocates memory in WASM, and then return the address of the allocated
49-
* memory.
50-
*/
51-
int32_t (*malloc)(ngx_log_t *log, int32_t size);
5247

5348
/*
5449
* call run a function exported from the plugin.

src/vm/wasmtime.c

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ ngx_wasm_wasmtime_call(void *data, ngx_str_t *name, bool has_result, int param_t
244244

245245
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, "wasmtime call function %V", name);
246246

247+
if (plugin == NULL) {
248+
plugin = cur_plugin;
249+
} else {
250+
cur_plugin = plugin;
251+
}
252+
247253
found = wasmtime_instance_export_get(plugin->context, &plugin->instance,
248254
(const char *) name->data, name->len, &func);
249255
if (!found) {
@@ -252,8 +258,6 @@ ngx_wasm_wasmtime_call(void *data, ngx_str_t *name, bool has_result, int param_t
252258
return NGX_OK;
253259
}
254260

255-
cur_plugin = plugin;
256-
257261
va_start(args, param_type);
258262

259263
switch (param_type) {
@@ -360,48 +364,13 @@ ngx_wasm_wasmtime_get_memory(ngx_log_t *log, int32_t addr, int32_t size)
360364
}
361365

362366

363-
int32_t
364-
ngx_wasm_wasmtime_malloc(ngx_log_t *log, int32_t size)
365-
{
366-
wasmtime_extern_t func;
367-
wasm_trap_t *trap = NULL;
368-
wasmtime_error_t *error;
369-
wasmtime_val_t params[1];
370-
wasmtime_val_t results[1];
371-
bool found;
372-
373-
found = wasmtime_instance_export_get(cur_plugin->context, &cur_plugin->instance,
374-
"proxy_on_memory_allocate", 24, &func);
375-
if (!found) {
376-
found = wasmtime_instance_export_get(cur_plugin->context, &cur_plugin->instance,
377-
"malloc", 6, &func);
378-
if (!found) {
379-
ngx_log_error(NGX_LOG_ERR, log, 0, "can't find malloc in the WASM plugin");
380-
return 0;
381-
}
382-
}
383-
384-
params[0].kind = WASMTIME_I32;
385-
params[0].of.i32 = size;
386-
387-
error = wasmtime_func_call(cur_plugin->context, &func.of.func, params, 1, results, 1, &trap);
388-
if (error != NULL || trap != NULL) {
389-
ngx_wasm_wasmtime_report_error(log, "failed to malloc: ", error, trap);
390-
return 0;
391-
}
392-
393-
return results[0].of.i64;
394-
}
395-
396-
397367
ngx_wasm_vm_t ngx_wasm_vm = {
398368
&vm_name,
399369
ngx_wasm_wasmtime_init,
400370
ngx_wasm_wasmtime_cleanup,
401371
ngx_wasm_wasmtime_load,
402372
ngx_wasm_wasmtime_unload,
403373
ngx_wasm_wasmtime_get_memory,
404-
ngx_wasm_wasmtime_malloc,
405374
ngx_wasm_wasmtime_call,
406375
ngx_wasm_wasmtime_has,
407376
};

0 commit comments

Comments
 (0)