diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7793f27..86d3f71 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,12 +8,6 @@ on: jobs: test: - strategy: - fail-fast: false - matrix: - vm: - - wasmedge - - wasmtime runs-on: "ubuntu-20.04" env: OPENRESTY_PREFIX: "/usr/local/openresty" @@ -58,13 +52,11 @@ jobs: - name: Install run: | - ./install-wasmedge.sh - wget https://raw.githubusercontent.com/api7/apisix-build-tools/master/build-apisix-base.sh chmod +x build-apisix-base.sh OR_PREFIX=$OPENRESTY_PREFIX CC="clang -fsanitize=address -fcolor-diagnostics -Qunused-arguments" \ cc_opt="-Werror -fsanitize=undefined" \ - ld_opt="-lubsan -Wl,-rpath,$OPENRESTY_PREFIX/wasmedge/lib" \ + ld_opt="-lubsan" \ ./build-apisix-base.sh latest - name: Script @@ -72,4 +64,4 @@ jobs: sudo chown -R runner:root ~/.cache make build.all.testdata export PATH=$OPENRESTY_PREFIX/nginx/sbin:$PATH - WASM_VM=${{ matrix.vm }} prove -I. -Itest-nginx/lib -r t/ + WASM_VM=wasmtime prove -I. -Itest-nginx/lib -r t/ diff --git a/.gitignore b/.gitignore index c6a97ce..a7e1dff 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ wasmtime-*-c-api wasmtime-*-c-api.tar.xz wasmtime-c-api -wasmedge/ *.wasm *.wasm.map *.wat diff --git a/Makefile b/Makefile index ab930fc..70f861e 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,6 @@ RUST_DIR = $(wildcard t/testdata/rust/*) install: $(INSTALL) -m 664 lib/resty/*.lua $(OPENRESTY_PREFIX)/lualib/resty/ cp -r ./wasmtime-c-api $(OPENRESTY_PREFIX)/ - cp -r ./wasmedge $(OPENRESTY_PREFIX)/ .PHONY: build.go.testdata build.go.testdata: diff --git a/README.md b/README.md index e948ae0..167a08c 100644 --- a/README.md +++ b/README.md @@ -34,26 +34,18 @@ export wasmtime_prefix=/path/to/wasm-nginx-module/wasmtime-c-api --with-ld-opt="-Wl,-rpath,${wasmtime_prefix}/lib" \ ``` -* Download WasmEdge with the `./install-wasmedge.sh`. -Remember to add the `$HOME/.wasmedge/lib` to the library search path when you build Nginx, for instance, - -``` -./configure ... \ - --with-ld-opt="-Wl,-rpath,${HOME}/.wasmedge/lib" \ -``` - ## Directives ### wasm_vm -**syntax:** *wasm_vm wasmtime|wasmedge* +**syntax:** *wasm_vm wasmtime* **default:** - **context:** *http* -Select the WASM VM. Currently, only wasmtime and WasmEdge are supported. -If the directive is not set, the WASM VM won't be enabled. +Select the Wasm VM. Currently, only wasmtime is supported. +If the directive is not set, the Wasm VM won't be enabled. ## Methods diff --git a/config b/config index 7c6715e..e4d9115 100644 --- a/config +++ b/config @@ -36,26 +36,6 @@ ngx_module_libs=" \ -L$ngx_addon_dir/wasmtime-c-api/lib -lwasmtime \ " -if [ -d $ngx_addon_dir/wasmedge ]; then - # if wasmedge is installed - wasmedge_path=$ngx_addon_dir/wasmedge - ngx_module_srcs=" \ - $ngx_module_srcs \ - $ngx_addon_dir/src/vm/wasmedge.c \ - " - ngx_module_incs=" \ - $ngx_module_incs \ - $wasmedge_path/include \ - " - ngx_module_libs=" \ - $ngx_module_libs \ - -L$wasmedge_path/lib -lwasmedge_c \ - " - - echo "Build with WasmEdge enabled" - have=NGX_WASM_HAVE_WASMEDGE . auto/have -fi - . auto/module ngx_addon_name=$ngx_module_name diff --git a/gen_wasm_host_api.py b/gen_wasm_host_api.py index f9633c4..6b4da78 100755 --- a/gen_wasm_host_api.py +++ b/gen_wasm_host_api.py @@ -103,31 +103,6 @@ def predefined_macro(vm): wasm_valkind_t param_type[MAX_WASM_API_ARG]; } ngx_wasm_wasmtime_host_api_t; -""" - elif vm == "wasmedge": - vm_def = """ -#define DEFINE_WASM_API(NAME, ARG_CHECK) \\ - static WasmEdge_Result wasmedge_##NAME( \\ - void *Data, \\ - WasmEdge_MemoryInstanceContext *MemCxt, \\ - const WasmEdge_Value *In, \\ - WasmEdge_Value *Out \\ - ) { \\ - ARG_CHECK \\ - Out[0] = WasmEdge_ValueGenI32(res); \\ - return WasmEdge_Result_Success; \\ - } -#define DEFINE_WASM_NAME(NAME, ARG) \\ - {ngx_string(#NAME), wasmedge_##NAME, ARG}, - - -typedef struct { - ngx_str_t name; - WasmEdge_HostFunc_t cb; - int8_t param_num; - enum WasmEdge_ValType param_type[MAX_WASM_API_ARG]; -} ngx_wasm_wasmedge_host_api_t; - """ for i in range(max_wasm_api_arg + 1): @@ -143,8 +118,6 @@ def predefined_macro(vm): param_s = "" if vm == "wasmtime": kind = "WASM_I32" - else: - kind = "WasmEdge_ValType_I32" for j in range(1, i + 1): if j % 5 == 1: param_s += " " @@ -160,8 +133,6 @@ def predefined_macro(vm): for j in range(i): if vm == "wasmtime": vm_def += " int32_t p%d = args[%d].of.i32; \\\n" % (j, j) - elif vm == "wasmedge": - vm_def += " int32_t p%d = WasmEdge_ValueGetI32(In[%d]); \\\n" % (j, j) param_s = ", ".join('p' + str(j) for j in range(i)) vm_def += " int32_t res = NAME(%s);\n" % (param_s) return vm_def @@ -251,15 +222,3 @@ def get_host_apis(src_dir): wasm_api_def=wasmtime_def, max_wasm_api_arg=max_wasm_api_arg, )) - - wasmedge_def = predefined_macro("wasmedge") - wasmedge_def += api_def + "\n\nstatic ngx_wasm_wasmedge_host_api_t host_apis[] = {\n" - wasmedge_def += name_def - with open(os.path.join(src_dir, "ngx_http_wasm_api_wasmedge.h"), 'w') as f: - f.write(s.substitute( - header=header, - vm_header="#include ", - vm_api_header_name="NGX_HTTP_WASM_API_WASMEDGE_H", - wasm_api_def=wasmedge_def, - max_wasm_api_arg=max_wasm_api_arg, - )) diff --git a/install-wasmedge.sh b/install-wasmedge.sh deleted file mode 100755 index 02f1436..0000000 --- a/install-wasmedge.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env bash -# Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -set -euo pipefail -x - -if echo "int main(void) {}" | gcc -o /dev/null -v -x c - &> /dev/stdout| grep collect | tr -s " " "\012" | grep musl; then - # skip if the libc is musl - exit 0 -fi - -curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -e none -p ./wasmedge -v 0.10.0-alpha.1 diff --git a/src/http/ngx_http_wasm_api_wasmedge.h b/src/http/ngx_http_wasm_api_wasmedge.h deleted file mode 100644 index 151c182..0000000 --- a/src/http/ngx_http_wasm_api_wasmedge.h +++ /dev/null @@ -1,339 +0,0 @@ - -/* - * Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -/* Code generated by `./gen_wasm_host_api.py src/http`. DO NOT EDIT. */ -#ifndef NGX_HTTP_WASM_API_WASMEDGE_H -#define NGX_HTTP_WASM_API_WASMEDGE_H - - -#include -#include -#include -#include "proxy_wasm/proxy_wasm_types.h" -#include "http/ngx_http_wasm_api_def.h" - - -#define MAX_WASM_API_ARG 12 - - -#define DEFINE_WASM_API(NAME, ARG_CHECK) \ - static WasmEdge_Result wasmedge_##NAME( \ - void *Data, \ - WasmEdge_MemoryInstanceContext *MemCxt, \ - const WasmEdge_Value *In, \ - WasmEdge_Value *Out \ - ) { \ - ARG_CHECK \ - Out[0] = WasmEdge_ValueGenI32(res); \ - return WasmEdge_Result_Success; \ - } -#define DEFINE_WASM_NAME(NAME, ARG) \ - {ngx_string(#NAME), wasmedge_##NAME, ARG}, - - -typedef struct { - ngx_str_t name; - WasmEdge_HostFunc_t cb; - int8_t param_num; - enum WasmEdge_ValType param_type[MAX_WASM_API_ARG]; -} ngx_wasm_wasmedge_host_api_t; - - -#define DEFINE_WASM_NAME_ARG_VOID \ - 0, {} -#define DEFINE_WASM_API_ARG_CHECK_VOID(NAME) \ - int32_t res = NAME(); - -#define DEFINE_WASM_NAME_ARG_I32_1 \ - 1, { \ - WasmEdge_ValType_I32, } -#define DEFINE_WASM_API_ARG_CHECK_I32_1(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t res = NAME(p0); - -#define DEFINE_WASM_NAME_ARG_I32_2 \ - 2, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, } -#define DEFINE_WASM_API_ARG_CHECK_I32_2(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t res = NAME(p0, p1); - -#define DEFINE_WASM_NAME_ARG_I32_3 \ - 3, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, } -#define DEFINE_WASM_API_ARG_CHECK_I32_3(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t p2 = WasmEdge_ValueGetI32(In[2]); \ - int32_t res = NAME(p0, p1, p2); - -#define DEFINE_WASM_NAME_ARG_I32_4 \ - 4, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, } -#define DEFINE_WASM_API_ARG_CHECK_I32_4(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t p2 = WasmEdge_ValueGetI32(In[2]); \ - int32_t p3 = WasmEdge_ValueGetI32(In[3]); \ - int32_t res = NAME(p0, p1, p2, p3); - -#define DEFINE_WASM_NAME_ARG_I32_5 \ - 5, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ -} -#define DEFINE_WASM_API_ARG_CHECK_I32_5(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t p2 = WasmEdge_ValueGetI32(In[2]); \ - int32_t p3 = WasmEdge_ValueGetI32(In[3]); \ - int32_t p4 = WasmEdge_ValueGetI32(In[4]); \ - int32_t res = NAME(p0, p1, p2, p3, p4); - -#define DEFINE_WASM_NAME_ARG_I32_6 \ - 6, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ - WasmEdge_ValType_I32, } -#define DEFINE_WASM_API_ARG_CHECK_I32_6(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t p2 = WasmEdge_ValueGetI32(In[2]); \ - int32_t p3 = WasmEdge_ValueGetI32(In[3]); \ - int32_t p4 = WasmEdge_ValueGetI32(In[4]); \ - int32_t p5 = WasmEdge_ValueGetI32(In[5]); \ - int32_t res = NAME(p0, p1, p2, p3, p4, p5); - -#define DEFINE_WASM_NAME_ARG_I32_7 \ - 7, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, } -#define DEFINE_WASM_API_ARG_CHECK_I32_7(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t p2 = WasmEdge_ValueGetI32(In[2]); \ - int32_t p3 = WasmEdge_ValueGetI32(In[3]); \ - int32_t p4 = WasmEdge_ValueGetI32(In[4]); \ - int32_t p5 = WasmEdge_ValueGetI32(In[5]); \ - int32_t p6 = WasmEdge_ValueGetI32(In[6]); \ - int32_t res = NAME(p0, p1, p2, p3, p4, p5, p6); - -#define DEFINE_WASM_NAME_ARG_I32_8 \ - 8, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, } -#define DEFINE_WASM_API_ARG_CHECK_I32_8(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t p2 = WasmEdge_ValueGetI32(In[2]); \ - int32_t p3 = WasmEdge_ValueGetI32(In[3]); \ - int32_t p4 = WasmEdge_ValueGetI32(In[4]); \ - int32_t p5 = WasmEdge_ValueGetI32(In[5]); \ - int32_t p6 = WasmEdge_ValueGetI32(In[6]); \ - int32_t p7 = WasmEdge_ValueGetI32(In[7]); \ - int32_t res = NAME(p0, p1, p2, p3, p4, p5, p6, p7); - -#define DEFINE_WASM_NAME_ARG_I32_9 \ - 9, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, } -#define DEFINE_WASM_API_ARG_CHECK_I32_9(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t p2 = WasmEdge_ValueGetI32(In[2]); \ - int32_t p3 = WasmEdge_ValueGetI32(In[3]); \ - int32_t p4 = WasmEdge_ValueGetI32(In[4]); \ - int32_t p5 = WasmEdge_ValueGetI32(In[5]); \ - int32_t p6 = WasmEdge_ValueGetI32(In[6]); \ - int32_t p7 = WasmEdge_ValueGetI32(In[7]); \ - int32_t p8 = WasmEdge_ValueGetI32(In[8]); \ - int32_t res = NAME(p0, p1, p2, p3, p4, p5, p6, p7, p8); - -#define DEFINE_WASM_NAME_ARG_I32_10 \ - 10, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ -} -#define DEFINE_WASM_API_ARG_CHECK_I32_10(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t p2 = WasmEdge_ValueGetI32(In[2]); \ - int32_t p3 = WasmEdge_ValueGetI32(In[3]); \ - int32_t p4 = WasmEdge_ValueGetI32(In[4]); \ - int32_t p5 = WasmEdge_ValueGetI32(In[5]); \ - int32_t p6 = WasmEdge_ValueGetI32(In[6]); \ - int32_t p7 = WasmEdge_ValueGetI32(In[7]); \ - int32_t p8 = WasmEdge_ValueGetI32(In[8]); \ - int32_t p9 = WasmEdge_ValueGetI32(In[9]); \ - int32_t res = NAME(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9); - -#define DEFINE_WASM_NAME_ARG_I32_11 \ - 11, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ - WasmEdge_ValType_I32, } -#define DEFINE_WASM_API_ARG_CHECK_I32_11(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t p2 = WasmEdge_ValueGetI32(In[2]); \ - int32_t p3 = WasmEdge_ValueGetI32(In[3]); \ - int32_t p4 = WasmEdge_ValueGetI32(In[4]); \ - int32_t p5 = WasmEdge_ValueGetI32(In[5]); \ - int32_t p6 = WasmEdge_ValueGetI32(In[6]); \ - int32_t p7 = WasmEdge_ValueGetI32(In[7]); \ - int32_t p8 = WasmEdge_ValueGetI32(In[8]); \ - int32_t p9 = WasmEdge_ValueGetI32(In[9]); \ - int32_t p10 = WasmEdge_ValueGetI32(In[10]); \ - int32_t res = NAME(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); - -#define DEFINE_WASM_NAME_ARG_I32_12 \ - 12, { \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, WasmEdge_ValType_I32, \ - WasmEdge_ValType_I32, WasmEdge_ValType_I32, } -#define DEFINE_WASM_API_ARG_CHECK_I32_12(NAME) \ - int32_t p0 = WasmEdge_ValueGetI32(In[0]); \ - int32_t p1 = WasmEdge_ValueGetI32(In[1]); \ - int32_t p2 = WasmEdge_ValueGetI32(In[2]); \ - int32_t p3 = WasmEdge_ValueGetI32(In[3]); \ - int32_t p4 = WasmEdge_ValueGetI32(In[4]); \ - int32_t p5 = WasmEdge_ValueGetI32(In[5]); \ - int32_t p6 = WasmEdge_ValueGetI32(In[6]); \ - int32_t p7 = WasmEdge_ValueGetI32(In[7]); \ - int32_t p8 = WasmEdge_ValueGetI32(In[8]); \ - int32_t p9 = WasmEdge_ValueGetI32(In[9]); \ - int32_t p10 = WasmEdge_ValueGetI32(In[10]); \ - int32_t p11 = WasmEdge_ValueGetI32(In[11]); \ - int32_t res = NAME(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11); - -DEFINE_WASM_API(proxy_set_effective_context, - DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_set_effective_context)) -DEFINE_WASM_API(proxy_get_property, - DEFINE_WASM_API_ARG_CHECK_I32_4(proxy_get_property)) -DEFINE_WASM_API(proxy_set_property, - DEFINE_WASM_API_ARG_CHECK_I32_4(proxy_set_property)) -DEFINE_WASM_API(proxy_log, - DEFINE_WASM_API_ARG_CHECK_I32_3(proxy_log)) -DEFINE_WASM_API(proxy_get_buffer_bytes, - DEFINE_WASM_API_ARG_CHECK_I32_5(proxy_get_buffer_bytes)) -DEFINE_WASM_API(proxy_set_buffer_bytes, - DEFINE_WASM_API_ARG_CHECK_I32_5(proxy_set_buffer_bytes)) -DEFINE_WASM_API(proxy_send_local_response, - DEFINE_WASM_API_ARG_CHECK_I32_8(proxy_send_local_response)) -DEFINE_WASM_API(proxy_send_http_response, - DEFINE_WASM_API_ARG_CHECK_I32_8(proxy_send_http_response)) -DEFINE_WASM_API(proxy_get_current_time_nanoseconds, - DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_get_current_time_nanoseconds)) -DEFINE_WASM_API(proxy_set_tick_period_milliseconds, - DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_set_tick_period_milliseconds)) -DEFINE_WASM_API(proxy_get_configuration, - DEFINE_WASM_API_ARG_CHECK_I32_2(proxy_get_configuration)) -DEFINE_WASM_API(proxy_get_header_map_pairs, - DEFINE_WASM_API_ARG_CHECK_I32_3(proxy_get_header_map_pairs)) -DEFINE_WASM_API(proxy_set_header_map_pairs, - DEFINE_WASM_API_ARG_CHECK_I32_3(proxy_set_header_map_pairs)) -DEFINE_WASM_API(proxy_get_header_map_value, - DEFINE_WASM_API_ARG_CHECK_I32_5(proxy_get_header_map_value)) -DEFINE_WASM_API(proxy_remove_header_map_value, - DEFINE_WASM_API_ARG_CHECK_I32_3(proxy_remove_header_map_value)) -DEFINE_WASM_API(proxy_replace_header_map_value, - DEFINE_WASM_API_ARG_CHECK_I32_5(proxy_replace_header_map_value)) -DEFINE_WASM_API(proxy_add_header_map_value, - DEFINE_WASM_API_ARG_CHECK_I32_5(proxy_add_header_map_value)) -DEFINE_WASM_API(proxy_get_shared_data, - DEFINE_WASM_API_ARG_CHECK_I32_5(proxy_get_shared_data)) -DEFINE_WASM_API(proxy_set_shared_data, - DEFINE_WASM_API_ARG_CHECK_I32_5(proxy_set_shared_data)) -DEFINE_WASM_API(proxy_register_shared_queue, - DEFINE_WASM_API_ARG_CHECK_I32_3(proxy_register_shared_queue)) -DEFINE_WASM_API(proxy_resolve_shared_queue, - DEFINE_WASM_API_ARG_CHECK_I32_5(proxy_resolve_shared_queue)) -DEFINE_WASM_API(proxy_dequeue_shared_queue, - DEFINE_WASM_API_ARG_CHECK_I32_3(proxy_dequeue_shared_queue)) -DEFINE_WASM_API(proxy_enqueue_shared_queue, - DEFINE_WASM_API_ARG_CHECK_I32_3(proxy_enqueue_shared_queue)) -DEFINE_WASM_API(proxy_continue_request, - DEFINE_WASM_API_ARG_CHECK_VOID(proxy_continue_request)) -DEFINE_WASM_API(proxy_continue_response, - DEFINE_WASM_API_ARG_CHECK_VOID(proxy_continue_response)) -DEFINE_WASM_API(proxy_clear_route_cache, - DEFINE_WASM_API_ARG_CHECK_VOID(proxy_clear_route_cache)) -DEFINE_WASM_API(proxy_continue_stream, - DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_continue_stream)) -DEFINE_WASM_API(proxy_close_stream, - DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_close_stream)) -DEFINE_WASM_API(proxy_http_call, - DEFINE_WASM_API_ARG_CHECK_I32_10(proxy_http_call)) -DEFINE_WASM_API(proxy_grpc_call, - DEFINE_WASM_API_ARG_CHECK_I32_12(proxy_grpc_call)) -DEFINE_WASM_API(proxy_grpc_stream, - DEFINE_WASM_API_ARG_CHECK_I32_9(proxy_grpc_stream)) -DEFINE_WASM_API(proxy_grpc_send, - DEFINE_WASM_API_ARG_CHECK_I32_4(proxy_grpc_send)) -DEFINE_WASM_API(proxy_grpc_cancel, - DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_grpc_cancel)) -DEFINE_WASM_API(proxy_grpc_close, - DEFINE_WASM_API_ARG_CHECK_I32_1(proxy_grpc_close)) -DEFINE_WASM_API(proxy_get_status, - DEFINE_WASM_API_ARG_CHECK_I32_3(proxy_get_status)) -DEFINE_WASM_API(proxy_done, - DEFINE_WASM_API_ARG_CHECK_VOID(proxy_done)) -DEFINE_WASM_API(proxy_call_foreign_function, - DEFINE_WASM_API_ARG_CHECK_I32_6(proxy_call_foreign_function)) - -static ngx_wasm_wasmedge_host_api_t host_apis[] = { - DEFINE_WASM_NAME(proxy_set_effective_context, DEFINE_WASM_NAME_ARG_I32_1) - DEFINE_WASM_NAME(proxy_get_property, DEFINE_WASM_NAME_ARG_I32_4) - DEFINE_WASM_NAME(proxy_set_property, DEFINE_WASM_NAME_ARG_I32_4) - DEFINE_WASM_NAME(proxy_log, DEFINE_WASM_NAME_ARG_I32_3) - DEFINE_WASM_NAME(proxy_get_buffer_bytes, DEFINE_WASM_NAME_ARG_I32_5) - DEFINE_WASM_NAME(proxy_set_buffer_bytes, DEFINE_WASM_NAME_ARG_I32_5) - DEFINE_WASM_NAME(proxy_send_local_response, DEFINE_WASM_NAME_ARG_I32_8) - DEFINE_WASM_NAME(proxy_send_http_response, DEFINE_WASM_NAME_ARG_I32_8) - DEFINE_WASM_NAME(proxy_get_current_time_nanoseconds, DEFINE_WASM_NAME_ARG_I32_1) - DEFINE_WASM_NAME(proxy_set_tick_period_milliseconds, DEFINE_WASM_NAME_ARG_I32_1) - DEFINE_WASM_NAME(proxy_get_configuration, DEFINE_WASM_NAME_ARG_I32_2) - DEFINE_WASM_NAME(proxy_get_header_map_pairs, DEFINE_WASM_NAME_ARG_I32_3) - DEFINE_WASM_NAME(proxy_set_header_map_pairs, DEFINE_WASM_NAME_ARG_I32_3) - DEFINE_WASM_NAME(proxy_get_header_map_value, DEFINE_WASM_NAME_ARG_I32_5) - DEFINE_WASM_NAME(proxy_remove_header_map_value, DEFINE_WASM_NAME_ARG_I32_3) - DEFINE_WASM_NAME(proxy_replace_header_map_value, DEFINE_WASM_NAME_ARG_I32_5) - DEFINE_WASM_NAME(proxy_add_header_map_value, DEFINE_WASM_NAME_ARG_I32_5) - DEFINE_WASM_NAME(proxy_get_shared_data, DEFINE_WASM_NAME_ARG_I32_5) - DEFINE_WASM_NAME(proxy_set_shared_data, DEFINE_WASM_NAME_ARG_I32_5) - DEFINE_WASM_NAME(proxy_register_shared_queue, DEFINE_WASM_NAME_ARG_I32_3) - DEFINE_WASM_NAME(proxy_resolve_shared_queue, DEFINE_WASM_NAME_ARG_I32_5) - DEFINE_WASM_NAME(proxy_dequeue_shared_queue, DEFINE_WASM_NAME_ARG_I32_3) - DEFINE_WASM_NAME(proxy_enqueue_shared_queue, DEFINE_WASM_NAME_ARG_I32_3) - DEFINE_WASM_NAME(proxy_continue_request, DEFINE_WASM_NAME_ARG_VOID) - DEFINE_WASM_NAME(proxy_continue_response, DEFINE_WASM_NAME_ARG_VOID) - DEFINE_WASM_NAME(proxy_clear_route_cache, DEFINE_WASM_NAME_ARG_VOID) - DEFINE_WASM_NAME(proxy_continue_stream, DEFINE_WASM_NAME_ARG_I32_1) - DEFINE_WASM_NAME(proxy_close_stream, DEFINE_WASM_NAME_ARG_I32_1) - DEFINE_WASM_NAME(proxy_http_call, DEFINE_WASM_NAME_ARG_I32_10) - DEFINE_WASM_NAME(proxy_grpc_call, DEFINE_WASM_NAME_ARG_I32_12) - DEFINE_WASM_NAME(proxy_grpc_stream, DEFINE_WASM_NAME_ARG_I32_9) - DEFINE_WASM_NAME(proxy_grpc_send, DEFINE_WASM_NAME_ARG_I32_4) - DEFINE_WASM_NAME(proxy_grpc_cancel, DEFINE_WASM_NAME_ARG_I32_1) - DEFINE_WASM_NAME(proxy_grpc_close, DEFINE_WASM_NAME_ARG_I32_1) - DEFINE_WASM_NAME(proxy_get_status, DEFINE_WASM_NAME_ARG_I32_3) - DEFINE_WASM_NAME(proxy_done, DEFINE_WASM_NAME_ARG_VOID) - DEFINE_WASM_NAME(proxy_call_foreign_function, DEFINE_WASM_NAME_ARG_I32_6) - { ngx_null_string, NULL, 0, {} } -}; - - -#endif diff --git a/src/vm/vm.c b/src/vm/vm.c index 77fbff2..0f8ae9f 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -25,11 +25,6 @@ ngx_wasm_vm_init(ngx_str_t *name) { if (ngx_strcmp(name->data, "wasmtime") == 0) { ngx_wasm_vm = &ngx_wasm_wasmtime_vm; - -#if (NGX_WASM_HAVE_WASMEDGE) - } else if (ngx_strcmp(name->data, "wasmedge") == 0) { - ngx_wasm_vm = &ngx_wasm_wasmedge_vm; -#endif } if (ngx_wasm_vm == NULL) { diff --git a/src/vm/vm.h b/src/vm/vm.h index 789684b..8c67ad5 100644 --- a/src/vm/vm.h +++ b/src/vm/vm.h @@ -59,9 +59,6 @@ typedef struct { extern ngx_wasm_vm_t *ngx_wasm_vm; extern ngx_wasm_vm_t ngx_wasm_wasmtime_vm; -#if (NGX_WASM_HAVE_WASMEDGE) -extern ngx_wasm_vm_t ngx_wasm_wasmedge_vm; -#endif ngx_int_t ngx_wasm_vm_init(ngx_str_t *name); diff --git a/src/vm/wasmedge.c b/src/vm/wasmedge.c deleted file mode 100644 index ae8eb5f..0000000 --- a/src/vm/wasmedge.c +++ /dev/null @@ -1,382 +0,0 @@ -/* - * Copyright 2022 Shenzhen ZhiLiu Technology Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include -#include -#include -#include -#include "vm.h" - - -typedef struct { - WasmEdge_ModuleInstanceContext *module; - WasmEdge_StoreContext *store; - WasmEdge_ExecutorContext *exec; - WasmEdge_MemoryInstanceContext *memory; - /* need to ensure the import objects have the same lifecycle as plugin */ - WasmEdge_ModuleInstanceContext *import; - WasmEdge_ModuleInstanceContext *import_wasi; -} ngx_wasm_wasmedge_plugin_t; - - -static ngx_str_t vm_name = ngx_string("wasmedge"); -static ngx_wasm_wasmedge_plugin_t *cur_plugin; - - -static WasmEdge_FunctionTypeContext * -ngx_http_wasmedge_host_api_func(const ngx_wasm_wasmedge_host_api_t *api) -{ - enum WasmEdge_ValType result[1] = {WasmEdge_ValType_I32}; - - return WasmEdge_FunctionTypeCreate(api->param_type, api->param_num, result, 1); -} - - -static ngx_int_t -ngx_wasm_wasmedge_init(void) -{ - ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "init wasm vm: wasmedge"); - - return NGX_OK; -} - - -static void -ngx_wasm_wasmedge_cleanup(void) -{ - ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "cleanup wasm vm: wasmedge"); -} - - -static void * -ngx_wasm_wasmedge_load(const char *bytecode, size_t size) -{ - ngx_wasm_wasmedge_plugin_t *plugin; - size_t i; - WasmEdge_Result res; - WasmEdge_ASTModuleContext *ast_ctx = NULL; - WasmEdge_StoreContext *store_ctx; - WasmEdge_LoaderContext *loader; - WasmEdge_ValidatorContext *validator; - WasmEdge_ModuleInstanceContext *import; - WasmEdge_ModuleInstanceContext *import_wasi = NULL; - WasmEdge_ModuleInstanceContext *module = NULL; - WasmEdge_ExecutorContext *executor; - WasmEdge_MemoryInstanceContext *memory; - WasmEdge_String s; - - /* Create the configure context */ - store_ctx = WasmEdge_StoreCreate(); - if (store_ctx == NULL) { - goto end; - } - - loader = WasmEdge_LoaderCreate(NULL); - if (loader == NULL) { - goto free_store; - } - - validator = WasmEdge_ValidatorCreate(NULL); - if (validator == NULL) { - goto free_loader; - } - - executor = WasmEdge_ExecutorCreate(NULL, NULL); - if (executor == NULL) { - goto free_validator; - } - - res = WasmEdge_LoaderParseFromBuffer(loader, &ast_ctx, (const uint8_t*) bytecode, size); - if (!WasmEdge_ResultOK(res)) { - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "Loading phase failed: %s", - WasmEdge_ResultGetMessage(res)); - goto free_executor; - } - - res = WasmEdge_ValidatorValidate(validator, ast_ctx); - if (!WasmEdge_ResultOK(res)) { - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "Validation phase failed: %s", - WasmEdge_ResultGetMessage(res)); - goto free_ast; - } - - s = WasmEdge_StringCreateByCString("env"); - import = WasmEdge_ModuleInstanceCreate(s); - WasmEdge_StringDelete(s); - - if (import == NULL) { - goto free_ast; - } - - for (i = 0; host_apis[i].name.len; i++) { - ngx_wasm_wasmedge_host_api_t *api = &host_apis[i]; - WasmEdge_FunctionTypeContext *ft; - WasmEdge_FunctionInstanceContext *f; - - ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, - "define wasm host API %V", &api->name); - - ft = ngx_http_wasmedge_host_api_func(api); - if (ft == NULL) { - goto free_import; - } - - f = WasmEdge_FunctionInstanceCreate(ft, api->cb, NULL, 0); - if (f == NULL) { - goto free_import; - } - - s = WasmEdge_StringCreateByBuffer((const char *)api->name.data, api->name.len); - /* The caller should __NOT__ access or delete the function instance context - * after calling this function. - */ - WasmEdge_ModuleInstanceAddFunction(import, s, f); - WasmEdge_StringDelete(s); - WasmEdge_FunctionTypeDelete(ft); - } - - res = WasmEdge_ExecutorRegisterImport(executor, store_ctx, import); - if (!WasmEdge_ResultOK(res)) { - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "Register imports failed: %s", - WasmEdge_ResultGetMessage(res)); - goto free_import; - } - - // TODO: add env - import_wasi = WasmEdge_ModuleInstanceCreateWASI(NULL, 0, NULL, 0, NULL, 0); - res = WasmEdge_ExecutorRegisterImport(executor, store_ctx, import_wasi); - if (!WasmEdge_ResultOK(res)) { - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "Register wasi imports failed: %s", - WasmEdge_ResultGetMessage(res)); - goto free_import; - } - - res = WasmEdge_ExecutorInstantiate(executor, &module, store_ctx, ast_ctx); - if (!WasmEdge_ResultOK(res)) { - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "Instantiation phase failed: %s", - WasmEdge_ResultGetMessage(res)); - goto free_import; - } - - s = WasmEdge_StringCreateByCString("memory"); - memory = WasmEdge_ModuleInstanceFindMemory(module, s); - WasmEdge_StringDelete(s); - if (memory == NULL) { - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "the wasm plugin doesn't export memory"); - goto free_module; - } - - plugin = ngx_alloc(sizeof(ngx_wasm_wasmedge_plugin_t), ngx_cycle->log); - if (plugin == NULL) { - goto free_module; - } - - plugin->exec = executor; - plugin->module = module; - plugin->store = store_ctx; - plugin->memory = memory; - plugin->import = import; - plugin->import_wasi = import_wasi; - - WasmEdge_ASTModuleDelete(ast_ctx); - WasmEdge_ValidatorDelete(validator); - WasmEdge_LoaderDelete(loader); - - ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0, "loaded wasm plugin"); - - return plugin; - -free_module: - WasmEdge_ModuleInstanceDelete(module); - -free_import: - WasmEdge_ModuleInstanceDelete(import); - - if (import_wasi != NULL) { - WasmEdge_ModuleInstanceDelete(import_wasi); - } - -free_ast: - WasmEdge_ASTModuleDelete(ast_ctx); - -free_executor: - WasmEdge_ExecutorDelete(executor); - -free_validator: - WasmEdge_ValidatorDelete(validator); - -free_loader: - WasmEdge_LoaderDelete(loader); - -free_store: - WasmEdge_StoreDelete(store_ctx); - -end: - return NULL; -} - - -static void -ngx_wasm_wasmedge_unload(void *data) -{ - ngx_wasm_wasmedge_plugin_t *plugin = data; - - WasmEdge_ModuleInstanceDelete(plugin->module); - WasmEdge_ExecutorDelete(plugin->exec); - WasmEdge_StoreDelete(plugin->store); - WasmEdge_ModuleInstanceDelete(plugin->import); - WasmEdge_ModuleInstanceDelete(plugin->import_wasi); - ngx_free(plugin); - - ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0, "unloaded wasm plugin"); -} - - -static bool -ngx_wasm_wasmedge_has(void *data, ngx_str_t *name) -{ - ngx_wasm_wasmedge_plugin_t *plugin = data; - WasmEdge_FunctionInstanceContext *finst; - WasmEdge_String s; - - s = WasmEdge_StringCreateByBuffer((const char *) name->data, name->len); - finst = WasmEdge_ModuleInstanceFindFunction(plugin->module, s); - WasmEdge_StringDelete(s); - - return finst != NULL; -} - - -static ngx_int_t -ngx_wasm_wasmedge_call(void *data, ngx_str_t *name, bool has_result, int param_type, ...) -{ - ngx_wasm_wasmedge_plugin_t *plugin = data; - WasmEdge_FunctionInstanceContext *finst = NULL; - WasmEdge_Result res; - WasmEdge_String s; - ngx_int_t rc; - size_t i; - va_list args; - size_t param_num = 0; - WasmEdge_Value param_list[MAX_WASM_API_ARG]; - WasmEdge_Value results[1]; - - ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, "wasmedge call function %V", name); - - if (plugin == NULL) { - plugin = cur_plugin; - } else { - cur_plugin = plugin; - } - - if (!ngx_wasm_wasmedge_has(plugin, name)) { - ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, - "wasmedge function %V not defined", name); - return NGX_OK; - } - - va_start(args, param_type); - - switch (param_type) { - case NGX_WASM_PARAM_VOID: - break; - - case NGX_WASM_PARAM_I32: - param_num = 1; - break; - - case NGX_WASM_PARAM_I32_I32: - param_num = 2; - break; - - case NGX_WASM_PARAM_I32_I32_I32: - param_num = 3; - break; - - case NGX_WASM_PARAM_I32_I32_I32_I32: - param_num = 4; - break; - - case NGX_WASM_PARAM_I32_I32_I32_I32_I32: - param_num = 5; - break; - - default: - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "unknown param type: %d", param_type); - va_end(args); - return NGX_ERROR; - } - - for (i = 0; i < param_num; i++) { - param_list[i] = WasmEdge_ValueGenI32(va_arg(args, int32_t)); - } - - va_end(args); - - s = WasmEdge_StringCreateByBuffer((const char *) name->data, name->len); - finst = WasmEdge_ModuleInstanceFindFunction(plugin->module, s); - res = WasmEdge_ExecutorInvoke(plugin->exec, finst, - param_list, param_num, results, has_result ? 1 : 0); - WasmEdge_StringDelete(s); - - if (!WasmEdge_ResultOK(res)) { - ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "failed to call function: %s", - WasmEdge_ResultGetMessage(res)); - return NGX_ERROR; - } - - if (!has_result) { - ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, - "wasmedge call function done"); - return NGX_OK; - } - - rc = WasmEdge_ValueGetI32(results[0]); - - ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, - "wasmedge call function result: %d", rc); - - return rc; -} - - -u_char * -ngx_wasm_wasmedge_get_memory(ngx_log_t *log, int32_t addr, int32_t size) -{ - u_char *data; - - /* If the `addr + size` is larger, than the data size in the memory instance, - * this function will return NULL. */ - data = WasmEdge_MemoryInstanceGetPointer(cur_plugin->memory, addr, size); - if (data == NULL) { - ngx_log_error(NGX_LOG_ERR, log, 0, - "wasmedge failed to access memory addr %d with size %d", addr, size); - } - - return data; -} - - -ngx_wasm_vm_t ngx_wasm_wasmedge_vm = { - &vm_name, - ngx_wasm_wasmedge_init, - ngx_wasm_wasmedge_cleanup, - ngx_wasm_wasmedge_load, - ngx_wasm_wasmedge_unload, - ngx_wasm_wasmedge_get_memory, - ngx_wasm_wasmedge_call, - ngx_wasm_wasmedge_has, -}; diff --git a/t/assemblyscript.t b/t/assemblyscript.t index c681f5e..1009492 100644 --- a/t/assemblyscript.t +++ b/t/assemblyscript.t @@ -12,14 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -use t::WASM; - -if ($ENV{"WASM_VM"} eq "wasmedge") { - # err msg: When linking module: "wasi_unstable" , function name: "proc_exit" - plan(skip_all => "wasmedge doesn't support wasi_unstable"); -} else { - plan('no_plan'); -} +use t::WASM 'no_plan'; run_tests();