diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..ee5839968a2bf86c93283efc09d40fd050b7cfa2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,127 @@ + 木兰宽松许可证, 第2版 + + 木兰宽松许可证, 第2版 + 2020年1月 http://license.coscl.org.cn/MulanPSL2 + + + 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束: + + 0. 定义 + + “软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 + + “贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 + + “贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 + + “法人实体”是指提交贡献的机构及其“关联实体”。 + + “关联实体”是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 + + 1. 授予版权许可 + + 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。 + + 2. 授予专利许可 + + 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。 + + 3. 无商标许可 + + “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。 + + 4. 分发限制 + + 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 + + 5. 免责声明与责任限制 + + “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。 + + 6. 语言 + “本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。 + + 条款结束 + + 如何将木兰宽松许可证,第2版,应用到您的软件 + + 如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步: + + 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字; + + 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中; + + 3, 请将如下声明文本放入每个源文件的头部注释中。 + + Copyright (c) [Year] [name of copyright holder] + [Software Name] is licensed under Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. + + + Mulan Permissive Software License,Version 2 + + Mulan Permissive Software License,Version 2 (Mulan PSL v2) + January 2020 http://license.coscl.org.cn/MulanPSL2 + + Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions: + + 0. Definition + + Software means the program and related documents which are licensed under this License and comprise all Contribution(s). + + Contribution means the copyrightable work licensed by a particular Contributor under this License. + + Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. + + Legal Entity means the entity making a Contribution and all its Affiliates. + + Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. + + 1. Grant of Copyright License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. + + 2. Grant of Patent License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. + + 3. No Trademark License + + No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4. + + 4. Distribution Restriction + + You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. + + 5. Disclaimer of Warranty and Limitation of Liability + + THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + + 6. Language + + THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL. + + END OF THE TERMS AND CONDITIONS + + How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software + + To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps: + + i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; + + ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package; + + iii Attach the statement to the appropriate annotated syntax at the beginning of each source file. + + + Copyright (c) [Year] [name of copyright holder] + [Software Name] is licensed under Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. diff --git a/README.en.md b/README.en.md deleted file mode 100644 index b8d5a5555f1aa462a0eb83f64351c903bfd59e63..0000000000000000000000000000000000000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# openGauss-wasm - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md index a3feccd08ee71cebe878f9cdaf40b3be35f2e929..12bc5a8acb316d363e89369b01ec2e1de1d0393d 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,236 @@ -# openGauss-wasm +A complete and mature WebAssembly runtime for openGauss based on [Wasmtime](https://wasmtime.dev/). +It's an original way to extend your favorite database capabilities. -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} +> Note This project is inspired by [wasmer-postgres](https://github.com/wasmerio/wasmer-postgres) -#### 软件架构 -软件架构说明 +Features: + * **Easy to use**: The `wasmtime` API mimics the standard WebAssembly API, + * **Fast**: `wasmtime` executes the WebAssembly modules as fast as + possible, close to **native speed**, + * **Safe**: All calls to WebAssembly will be fast, but more + importantly, completely safe and sandboxed. -#### 安装教程 +> Note: The project is still in heavy development. This is a +0.1.0 version. Some API are missing and are under implementation. But +it's fun to play with it. -1. xxxx -2. xxxx -3. xxxx +# Installation -#### 使用说明 +The project comes in two parts: -1. xxxx -2. xxxx -3. xxxx + 1. A shared library, and + 2. A PL/pgSQL extension. + +To compile the former, the wasmtime-c-api header files are required and be placed alongside the "openGauss-server" code. +You can download the header file from [here](https://github.com/bytecodealliance/wasmtime/releases). -#### 参与贡献 +After that, run `CREATE EXTENSION wasm_executor` in a +openGauss shell. Two new functions will appear: `wasm_new_instance` and `wasm_new_instance_wat`; They must be +called with the absolute path to the shared library. It looks like +this: -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request +```shell +$ # Build the shared library. +$ make +$ # Install the extension in the Postgres opengauss +$ make install -#### 特技 +$ # Activate and initialize the extension. +$ gsql -d postgres -c 'CREATE EXTENSION wasm_executor' +``` -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +And you are ready to go! + + +# Usage & documentation + +Consider the `examples/sum.rs` program: + +```rust +#[no_mangle] +pub extern fn sum(x: i32, y: i32) -> i32 { + x + y +} +``` + +Once compiled to WebAssembly, one obtains a similar WebAssembly binary +to `examples/sum.wasm`. To use the `sum` exported function, first, +create a new instance of the WebAssembly module, and second, +call the `sum` function. + +To instantiate a WebAssembly module, the `wasm_new_instance` function +must be used. It has two arguments: + + 1. The absolute path to the WebAssembly module, and + 2. A namespace used to prefix exported functions in SQL. + +For instance, calling +`wasm_new_instance('/path/to/sum.wasm', 'wasm')` will create the +`wasm_sum` function that is a direct call to the `sum` exported function +of the WebAssembly instance. Thus: + +```sql +-- New instance of the `sum.wasm` WebAssembly module. +SELECT wasm_new_instance('/absolute/path/to/sum.wasm', 'wasm'); + +-- Call a WebAssembly exported function! +SELECT wasm_sum(1, 2); + +-- wasm_sum +-- -------- +-- 3 +-- (1 row) +``` + +Isn't it awesome? Calling Rust from openGauss through WebAssembly! + +Let's inspect a little bit further the `wasm_sum` function: + +```sql +\x +\df+ wasm_sum +Schema | public +Name | wasm_sum +Result data type | integer +Argument data types | integer, integer +Type | normal +Volatility | volatile +Parallel | unsafe +Owner | ... +Language | plpgsql +Source code | ... +Description | +fencedmode | f +propackage | f +prokind | f +``` + +The openGauss `wasm_sum` signature is `(integer, integer) -> integer`, +which maps the Rust `sum` signature `(i32, i32) -> i32`. + +So far, only the WebAssembly types `i32` and `i64` are +supported; they respectively map to `integer` and `bigint` +in openGauss. Floats are partly implemented for the moment. + +# Quickstart + +To get your hands on openGauss with wasm, we recommend using the Docker image. +Download the docker image firstlly. + +```shell +docker pull heguofeng/opengauss-wasm:1.0.0 +``` +Then run it. +```shell +docker run -it heguofeng/opengauss-wasm:1.0.0 bash +``` +And enjoy it. + + +## Inspect a WebAssembly instance + +The extension provides two ways to initilize a WebAssembly instance. As you can +see from the functions name show above, one way is to use `wasm_new_instance` from +.wasm file compiled from other languages, the other way is to use `wasm_new_instance_wat` +from .wat file, which is the text format of wasm. + +And, the extension provides two tables, gathered together in +the `wasm` foreign schema: + + * `wasm.instances` is a table with the `id` and `wasm_file` columns, + respectively for the instance ID, and the path of the WebAssembly + module, + * `wasm.exported_functions` is a table with the `instanceid`, + `funcname`, `inputs` and `output` columns, respectively for the + instance ID of the exported function, its name, its input types + (already formatted for Postgres), and its output types (already + formatted for Postgres). + +Let's see: + +```sql +-- Select all WebAssembly instances. +SELECT * FROM wasm.instances; + +-- id | wasm_file +-- ---------------+------------------------------- +-- 2785875771 | /absolute/path/to/sum.wasm +-- 3780612139 | /absolute/path/to/gcd.wat +-- (1 row) + +-- Select all exported functions for a specific instance. +SELECT + funcname, + inputs, + outputs +FROM + wasm.exported_functions +WHERE + instanceid = 2785875771; + +-- name | inputs | outputs +-- --------+-----------------+--------- +-- wasm_sum | integer,integer | integer +-- (1 row) +``` + +# Benchmarks + +Benchmarks are useless most of the time, but it shows that WebAssembly +can be a credible alternative to procedural languages such as +PL/pgSQL. Please, don't take those numbers for granted, it can change +at any time, but it shows promising results: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
BenchmarkRuntimeTime (ms)Ratio
Fibonacci (n = 50)openGauss-wasm-executor0.765
PL/pgSQL1.714
Fibonacci (n = 500)openGauss-wasm-executor0.794
PL/pgSQL9.74612×
Fibonacci (n = 5000)openGauss-wasm-executor0.820
PL/pgSQL92.720113×
+ +# License + +The entire project is under the MulanPSL2 License. Please read [the `LICENSE` file][license]. + +[license]: http://license.coscl.org.cn/MulanPSL2/ diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..4681e584885dea430be0876f748521267bc4baf3 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,55 @@ +FROM openeuler/openeuler:22.03-lts +MAINTAINER openGauss-ml@openguass.org +LABEL MAIL="heguofeng@huawei.com" \ + CREATE_DATE="2022-10" \ + GAUSS_SERVER="openGauss-3.1.0" + +RUN \ + sed -i "s/gpgcheck=1/gpgcheck=0/g" /etc/yum.repos.d/openEuler.repo && \ + sed -i "s#http://repo.openeuler.org#https://mirrors.huaweicloud.com/openeuler#g" /etc/yum.repos.d/openEuler.repo && \ + yum install -y -q net-tools wget sudo shadow vim bc util-linux git && \ + useradd opengauss && \ + sed -i "101i opengauss ALL=(ALL) NOPASSWD:ALL" /etc/sudoers && \ + sed -i "s/TMOUT=300/TMOUT=0/g" /etc/bashrc && \ + cp -rf /usr/share/zoneinfo/Asia/Beijing /etc/localtime && \ + yum install -y python -q tzdata cmake libaio-devel ncurses-devel pam-devel libffi-devel libtool \ + libtool-ltdl openssl-devel bison flex glibc-devel patch unzip openeuler-lsb make automake xz && \ + yum clean all + +WORKDIR /home/opengauss +COPY --chown=opengauss:opengauss . /home/opengauss + +USER opengauss:opengauss + +RUN cp -f /home/opengauss/bashrc /home/opengauss/.bashrc && \ + cd /home/opengauss && \ + if [ "`uname -m`" == "x86_64" ]; then \ + wget -q https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.1.0/binarylibs/openGauss-third_party_binarylibs_openEuler_x86_64.tar.gz -O openGauss_third.tar.gz && \ + wget -q https://github.com/bytecodealliance/wasmtime/releases/download/v1.0.1/wasmtime-v1.0.1-x86_64-linux-c-api.tar.xz -O wasmtime.tar.xz; \ + else \ + wget -q https://opengauss.obs.cn-south-1.myhuaweicloud.com/3.1.0/binarylibs/openGauss-third_party_binarylibs_openEuler_arm.tar.gz -O openGauss_third.tar.gz && \ + wget -q https://github.com/bytecodealliance/wasmtime/releases/download/v1.0.1/wasmtime-v1.0.1-aarch64-linux-c-api.tar.xz -O wasmtime.tar.xz; \ + fi && \ + tar -xf openGauss_third.tar.gz && mv openGauss-third_party_binarylibs* binarylibs && rm -f openGauss_third.tar.gz && \ + tar -xf wasmtime.tar.xz && mv wasmtime-v1.0.1* wasmtime && rm -rf wasmtime.tar.xz && rm -rf /home/opengauss/wasmtime/lib/libwasmtime.a && \ + source /home/opengauss/.bashrc && git clone https://gitee.com/Nelson-He/openGauss-server.git && cd openGauss-server && \ + ./configure --gcc-version=7.3.0 CC=g++ CFLAGS='-O2' \ + --prefix=$GAUSSHOME --3rd=$BINARYLIBS \ + --enable-cassert --enable-thread-safety \ + --without-readline --without-zlib && \ + make -sj 8 && make install -sj 8 && \ + cd /home/opengauss && git clone https://gitee.com/Nelson-He/openGauss-wasm.git && \ + cp -rf openGauss-wasm/wasm openGauss-server/contrib && \ + cd openGauss-server/contrib/wasm && make && make install && \ + cd /home/opengauss && \ + chmod +x /home/opengauss/entrypoint.sh && \ + cp /home/opengauss/openGauss-server/contrib/wasm/examples/* /home/opengauss && \ + sudo rm -rf /home/opengauss/binarylibs && \ + sudo rm -rf /home/opengauss/openGauss-server && \ + sudo rm -rf /home/opengauss/openGauss-wasm && \ + sudo yum remove -y make cmake git && \ + sudo yum clean all + +ENTRYPOINT ["/bin/bash", "/home/opengauss/entrypoint.sh"] + +CMD ["bash"] diff --git a/docker/bashrc b/docker/bashrc new file mode 100644 index 0000000000000000000000000000000000000000..81365070c87f6f4d44484fac6dc21182030d837b --- /dev/null +++ b/docker/bashrc @@ -0,0 +1,13 @@ +# Source default setting +[ -f /etc/bashrc ] && . /etc/bashrc + +# User environment PATH +PATH="$HOME/.local/bin:$HOME/bin:$PATH" +export GAUSSHOME="/home/opengauss/openGauss/install" +export BINARYLIBS="/home/opengauss/binarylibs" +export GCC_PATH="$BINARYLIBS/buildtools/gcc7.3/" +export CC="$GCC_PATH/gcc/bin/gcc" +export CXX="$GCC_PATH/gcc/bin/g++" +export LD_LIBRARY_PATH="$GAUSSHOME/lib:$GCC_PATH/gcc/lib64:$GCC_PATH/isl/lib:$GCC_PATH/mpc/lib/:$GCC_PATH/mpfr/lib/:$GCC_PATH/gmp/lib/:$LD_LIBRARY_PATH" +export PATH="$GAUSSHOME/bin:$GCC_PATH/gcc/bin:$PATH" +export PGPORT=5433 diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 0000000000000000000000000000000000000000..120eabc4af74752899c2e4ee3ffa62d47e506e3f --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Exit on error, real CI +set -e + +echo "Initing openGauss..." + +source /home/opengauss/.bashrc + +source ~/.bashrc && gs_initdb -D /home/opengauss/openGauss/data/ -w "openGauss2022" -E utf8 --nodename=datanode +sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /home/opengauss/openGauss/data/postgresql.conf +sed -i "s/#port = 5432/port = 5433/g" /home/opengauss/openGauss/data/postgresql.conf && \ + +echo "Starting openGauss..." +gs_ctl start -D /home/opengauss/openGauss/data -Z single_node + +echo "Ready!" +if [[ ! -z $@ ]]; then + echo + echo "To connect to the database: " + echo " gsql -d postgres " + echo + $@ +fi diff --git a/wasm/Makefile b/wasm/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..4f45fb399c4ea9d696fc9fdbbb13c62fbeea2e37 --- /dev/null +++ b/wasm/Makefile @@ -0,0 +1,36 @@ +# contrib/wasm/Makefile + +MODULE_big = wasm_executor +OBJS= wasm_executor.o + +EXTENSION = wasm_executor +DATA = wasm_executor--1.0.sql + +SHLIB_LINK_INTERNAL = $(libpq) +SHLIB_LINK += -lwasmtime + +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = contrib/wasm +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +override CPPFLAGS := $(filter-out -fPIE, $(CPPFLAGS)) -fPIC -I${top_builddir}/../wasmtime/include +override LDFLAGS += -L${top_builddir}/../wasmtime/lib + +install:install-libwasmtime install-data + +.PHONY: install-data +install-data: installdirs + $(INSTALL_DATA) $(addprefix $(srcdir)/, $(DATA)) '$(DESTDIR)$(datadir)/extension/' + +installdirs: + $(MKDIR_P) '$(DESTDIR)$(datadir)/extension' + +install-libwasmtime: + @cp ${top_builddir}/../wasmtime/lib/libwasmtime.so $(DESTDIR)$(libdir) + +endif diff --git a/wasm/benchmarks/fib.rs b/wasm/benchmarks/fib.rs new file mode 100644 index 0000000000000000000000000000000000000000..d566ccf648ea822e74487d2d920912a568479cdf --- /dev/null +++ b/wasm/benchmarks/fib.rs @@ -0,0 +1,20 @@ +#[no_mangle] +pub extern "C" fn fibonacci(n: u32) -> u64 { + if n <= 0 { + 0 + } else if n == 1 { + 1 + } else { + let mut accumulator = 0; + let mut last = 0; + let mut current = 1; + + for _i in 1..n { + accumulator = last + current; + last = current; + current = accumulator; + } + + accumulator + } +} diff --git a/wasm/benchmarks/fib.sql b/wasm/benchmarks/fib.sql new file mode 100644 index 0000000000000000000000000000000000000000..86ad7676208c43b3c8c6f0856c009122676f5b10 --- /dev/null +++ b/wasm/benchmarks/fib.sql @@ -0,0 +1,18 @@ +CREATE OR REPLACE FUNCTION fibonacci (n integer) RETURNS decimal AS $$ +DECLARE + counter bigint := 1; + i decimal := 0; + j decimal := 1; +BEGIN + IF (n < 1) THEN + RETURN 0; + END IF; + + WHILE counter <= n LOOP + counter := counter + 1; + SELECT j, i + j INTO i, j; + END LOOP; + + RETURN i; +END; +$$ LANGUAGE plpgsql; diff --git a/wasm/benchmarks/fib.wasm b/wasm/benchmarks/fib.wasm new file mode 100644 index 0000000000000000000000000000000000000000..cbbd88bb8d4f6c82376deb46084f043ff8b4b80e Binary files /dev/null and b/wasm/benchmarks/fib.wasm differ diff --git a/wasm/examples/fib.wat b/wasm/examples/fib.wat new file mode 100644 index 0000000000000000000000000000000000000000..42a5a8b797149dce9495a471ea1c02d43fc3784b --- /dev/null +++ b/wasm/examples/fib.wat @@ -0,0 +1,39 @@ +(module + (type (;0;) (func (param i64) (result i64))) + (func $fib (type 0) (param i64) (result i64) + (local i64) + i64.const 0 + local.set 1 + block ;; label = @1 + local.get 0 + i64.const 2 + i64.lt_u + br_if 0 (;@1;) + i64.const 0 + local.set 1 + loop ;; label = @2 + local.get 0 + i64.const -1 + i64.add + call $fib + local.get 1 + i64.add + local.set 1 + local.get 0 + i64.const -2 + i64.add + local.tee 0 + i64.const 1 + i64.gt_u + br_if 0 (;@2;) + end + end + local.get 0 + local.get 1 + i64.add) + (memory (;0;) 16) + (global $__stack_pointer (mut i32) (i32.const 1048576)) + (global (;1;) i32 (i32.const 1048576)) + (global (;2;) i32 (i32.const 1048576)) + (export "memory" (memory 0)) + (export "fib" (func $fib))) diff --git a/wasm/examples/gcd.wat b/wasm/examples/gcd.wat new file mode 100644 index 0000000000000000000000000000000000000000..3c31742912c1849e74c145ac509a1e1ba39f3acb --- /dev/null +++ b/wasm/examples/gcd.wat @@ -0,0 +1,28 @@ +(module + (func $gcd (param i32 i32) (result i32) + (local i32) + block ;; label = @1 + block ;; label = @2 + local.get 0 + br_if 0 (;@2;) + local.get 1 + local.set 2 + br 1 (;@1;) + end + loop ;; label = @2 + local.get 1 + local.get 0 + local.tee 2 + i32.rem_u + local.set 0 + local.get 2 + local.set 1 + local.get 0 + br_if 0 (;@2;) + end + end + local.get 2 + ) + (export "gcd" (func $gcd)) +) + diff --git a/wasm/examples/sum.rs b/wasm/examples/sum.rs new file mode 100644 index 0000000000000000000000000000000000000000..b0052b60fe55f0e5fe7700d48cee70d85cf1c1ef --- /dev/null +++ b/wasm/examples/sum.rs @@ -0,0 +1,4 @@ +#[no_mangle] +pub extern fn sum(x: i32, y: i32) -> i32 { + x + y +} diff --git a/wasm/examples/sum.wasm b/wasm/examples/sum.wasm new file mode 100644 index 0000000000000000000000000000000000000000..6893929024a5d9d3ba501e4e6b2914c94c5c8445 Binary files /dev/null and b/wasm/examples/sum.wasm differ diff --git a/wasm/wasm_executor--1.0.sql b/wasm/wasm_executor--1.0.sql new file mode 100644 index 0000000000000000000000000000000000000000..ebaccd18edff30050a2e7eab394fd2fac9e59d58 --- /dev/null +++ b/wasm/wasm_executor--1.0.sql @@ -0,0 +1,240 @@ +/* contrib/wasm/wasm_executor--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION wasm_executor" to load this file. \quit + +DROP SCHEMA IF EXISTS wasm CASCADE; +CREATE SCHEMA wasm; + +CREATE TABLE wasm.instances( + id bigint, + wasm_file text +); + +CREATE TABLE wasm.exported_functions( + instanceid bigint, + funcname text, + inputs text, + outputs text +); + +CREATE FUNCTION wasm_get_instances( + OUT id bigint, + OUT wasm_file text +) +RETURNS SETOF record +AS 'MODULE_PATHNAME', 'wasm_get_instances' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_get_exported_functions( + IN id bigint, + OUT funcname text, + OUT inputs text, + OUT outputs text +) +RETURNS SETOF record +AS 'MODULE_PATHNAME', 'wasm_get_exported_functions' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_create_new_instance(text) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_create_instance' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_create_new_instance_wat(text) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_create_instance_wat' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_0(text, text) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_0' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_1(text, text, int8) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_1' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_2(text, text, int8, int8) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_2' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_3(text, text, int8, int8, int8) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_3' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_4(text, text, int8, int8, int8, int8) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_4' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_5(text, text, int8, int8, int8, int8, int8) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_5' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_6(text, text, int8, int8, int8, int8, int8, int8) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_6' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_7(text, text, int8, int8, int8, int8, int8, int8, int8) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_7' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_8(text, text, int8, int8, int8, int8, int8, int8, int8, int8) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_8' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_9(text, text, int8, int8, int8, int8, int8, int8, int8, int8, int8) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_9' +LANGUAGE C STRICT; + +CREATE FUNCTION wasm_invoke_function_10(text, text, int8, int8, int8, int8, int8, int8, int8, int8, int8, int8) +RETURNS int8 +AS 'MODULE_PATHNAME', 'wasm_invoke_function_10' +LANGUAGE C STRICT; + +CREATE OR REPLACE FUNCTION wasm_new_instance(module_pathname text, namespace text) RETURNS text AS $$ +DECLARE + current_instance_id int8; + exported_function RECORD; + exported_function_generated_inputs text; + exported_function_generated_outputs text; +BEGIN + -- Create a new instance, and stores its ID in `current_instance_id`. + SELECT wasm_create_new_instance(module_pathname) INTO STRICT current_instance_id; + + -- Insert the wasm information to gloable table + INSERT INTO wasm.instances SELECT id, wasm_file FROM wasm_get_instances() WHERE id = current_instance_id; + INSERT INTO wasm.exported_functions SELECT current_instance_id, funcname, inputs, outputs FROM wasm_get_exported_functions(current_instance_id); + + -- Generate functions for each exported functions from the WebAssembly instance. + FOR + exported_function + IN + SELECT + funcname, + inputs, + CASE + WHEN length(inputs) = 0 THEN 0 + ELSE array_length(regexp_split_to_array(inputs, ','), 1) + END AS input_arity, + outputs + FROM + (SELECT * FROM wasm_get_exported_functions(current_instance_id)) + LOOP + IF exported_function.input_arity > 10 THEN + RAISE EXCEPTION 'WebAssembly exported function `%` has an arity greater than 10, which is not supported yet.', exported_function.funcname; + END IF; + + exported_function_generated_inputs := ''; + exported_function_generated_outputs := ''; + + FOR nth IN 1..exported_function.input_arity LOOP + exported_function_generated_inputs := exported_function_generated_inputs || format(', CAST($%s AS int8)', nth); + END LOOP; + + IF length(exported_function.outputs) > 0 THEN + exported_function_generated_outputs := exported_function.outputs; + ELSE + exported_function_generated_outputs := 'integer'; + END IF; + + EXECUTE format( + 'CREATE OR REPLACE FUNCTION %I_%I(%3$s) RETURNS %5$s AS $F$' || + 'DECLARE' || + ' output %5$s;' || + 'BEGIN' || + ' SELECT wasm_invoke_function_%4$s(%6$L, %2$L%7$s) INTO STRICT output;' || + ' RETURN output;' || + 'END;' || + '$F$ LANGUAGE plpgsql;', + namespace, -- 1 + exported_function.funcname, -- 2 + exported_function.inputs, -- 3 + exported_function.input_arity, -- 4 + exported_function_generated_outputs, -- 5 + current_instance_id, -- 6 + exported_function_generated_inputs -- 7 + ); + END LOOP; + + RETURN current_instance_id; +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION wasm_new_instance_wat(module_pathname text, namespace text) RETURNS text AS $$ +DECLARE + current_instance_id int8; + exported_function RECORD; + exported_function_generated_inputs text; + exported_function_generated_outputs text; +BEGIN + -- Create a new instance, and stores its ID in `current_instance_id`. + SELECT wasm_create_new_instance_wat(module_pathname) INTO STRICT current_instance_id; + + -- Insert the wasm information to gloable table + INSERT INTO wasm.instances SELECT id, wasm_file FROM wasm_get_instances() WHERE id = current_instance_id; + INSERT INTO wasm.exported_functions SELECT current_instance_id, funcname, inputs, outputs FROM wasm_get_exported_functions(current_instance_id); + + -- Generate functions for each exported functions from the WebAssembly instance. + FOR + exported_function + IN + SELECT + funcname, + inputs, + CASE + WHEN length(inputs) = 0 THEN 0 + ELSE array_length(regexp_split_to_array(inputs, ','), 1) + END AS input_arity, + outputs + FROM + (SELECT * FROM wasm_get_exported_functions(current_instance_id)) + LOOP + IF exported_function.input_arity > 10 THEN + RAISE EXCEPTION 'WebAssembly exported function `%` has an arity greater than 10, which is not supported yet.', exported_function.funcname; + END IF; + + exported_function_generated_inputs := ''; + exported_function_generated_outputs := ''; + + FOR nth IN 1..exported_function.input_arity LOOP + exported_function_generated_inputs := exported_function_generated_inputs || format(', CAST($%s AS int8)', nth); + END LOOP; + + IF length(exported_function.outputs) > 0 THEN + exported_function_generated_outputs := exported_function.outputs; + ELSE + exported_function_generated_outputs := 'integer'; + END IF; + + EXECUTE format( + 'CREATE OR REPLACE FUNCTION %I_%I(%3$s) RETURNS %5$s AS $F$' || + 'DECLARE' || + ' output %5$s;' || + 'BEGIN' || + ' SELECT wasm_invoke_function_%4$s(%6$L, %2$L%7$s) INTO STRICT output;' || + ' RETURN output;' || + 'END;' || + '$F$ LANGUAGE plpgsql;', + namespace, -- 1 + exported_function.funcname, -- 2 + exported_function.inputs, -- 3 + exported_function.input_arity, -- 4 + exported_function_generated_outputs, -- 5 + current_instance_id, -- 6 + exported_function_generated_inputs -- 7 + ); + END LOOP; + + RETURN current_instance_id; +END; +$$ LANGUAGE plpgsql; \ No newline at end of file diff --git a/wasm/wasm_executor.control b/wasm/wasm_executor.control new file mode 100755 index 0000000000000000000000000000000000000000..35e09a0305107ff0cbc14e11654858d0368e8ec2 --- /dev/null +++ b/wasm/wasm_executor.control @@ -0,0 +1,5 @@ +# wasm_executor extension +comment = 'wasm runtime executor for opengauss based on wasmtime' +default_version = '1.0' +module_pathname = '$libdir/wasm_executor' +relocatable = true diff --git a/wasm/wasm_executor.cpp b/wasm/wasm_executor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73b412698929010ec6e0b8c7757048e48294a970 --- /dev/null +++ b/wasm/wasm_executor.cpp @@ -0,0 +1,657 @@ +#include "postgres.h" +#include "knl/knl_variable.h" +#include "utils/builtins.h" +#include "access/hash.h" +#include "miscadmin.h" +#include "funcapi.h" +#include +#include +#include + +#include "wasm.h" +#include "wasmtime.h" + +PG_MODULE_MAGIC; + +extern "C" Datum wasm_create_instance_wat(PG_FUNCTION_ARGS); +extern "C" Datum wasm_create_instance(PG_FUNCTION_ARGS); +extern "C" Datum wasm_get_instances(PG_FUNCTION_ARGS); +extern "C" Datum wasm_get_exported_functions(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_0(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_1(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_2(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_3(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_4(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_5(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_6(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_7(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_8(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_9(PG_FUNCTION_ARGS); +extern "C" Datum wasm_invoke_function_10(PG_FUNCTION_ARGS); + +typedef struct WasmInstInfo { + wasm_engine_t *wasm_engine; + + wasmtime_store_t *wasm_store; + + wasmtime_module_t *wasm_module; + + wasmtime_instance_t instance; + + std::string wasm_file; +} WasmInstInfo; + +typedef struct TupleInstanceState { + TupleDesc tupd; + std::map::iterator currindex; + std::map::iterator lastindex; +} TupleInstanceState; + +typedef struct WasmFuncInfo { + std::string funcname; + std::string inputs; + std::string outputs; +} WasmFuncInfo; + +typedef struct TupleFuncState { + TupleDesc tupd; + std::vector::iterator currindex; + std::vector::iterator lastindex; +} TupleFuncState; + +// Store the wasm instance info globally +static std::map instances; + +// Store the wasm exported function info globally +static std::map*> exported_functions; + +static WasmInstInfo* find_instance(int64 instanceid) +{ + std::map::iterator itor = instances.begin(); + while (itor != instances.end()) { + if (itor->first == instanceid) { + return itor->second; + } + itor++; + } + elog(DEBUG1, "wasm_executor: not find instance info for instanceid %ld", instanceid); + return NULL; +} + +static std::vector* find_exported_func_list(int64 instanceid) +{ + std::map*>::iterator itor = exported_functions.begin(); + while (itor != exported_functions.end()) { + if (itor->first == instanceid) { + return itor->second; + } + itor++; + } + elog(DEBUG1, "wasm_executor: not find exported func info for instanceid %ld", instanceid); + return NULL; +} + +static int64 generate_uuid(Datum input) +{ + Datum uuid = DirectFunctionCall1(hashtext, input); + return DatumGetInt64(uuid); +} + +static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) +{ + wasm_byte_vec_t error_message; + if (error != NULL) { + wasmtime_error_message(error, &error_message); + } else { + wasm_trap_message(trap, &error_message); + } + char *messaga_info = pstrdup(error_message.data); + wasm_byte_vec_delete(&error_message); + + ereport(ERROR, (errmsg("wasm_executor: %s:%s", message, messaga_info))); +} + +static int64 wasm_invoke_function(char *instanceid_str, char* funcname, std::vector &args) +{ + int64 instanceid = atol(instanceid_str); + WasmInstInfo* instanceinfo = find_instance(instanceid); + if (instanceinfo == NULL) { + ereport(ERROR, (errmsg("wasm_executor: instance with id %ld is not find", instanceid))); + } + + wasmtime_extern_t wasm_extern; + wasmtime_context_t* context = wasmtime_store_context(instanceinfo->wasm_store); + int funcnamelen = strlen(funcname); + bool ok = wasmtime_instance_export_get(context, &instanceinfo->instance, funcname, funcnamelen, &wasm_extern); + if (!ok || wasm_extern.kind != WASMTIME_EXTERN_FUNC) { + ereport(ERROR, (errmsg("wasm_executor: not find the exported function with name(%s) and namelen(%d)", + funcname, funcnamelen))); + } + + wasmtime_func_t wasm_func = wasm_extern.of.func; + wasm_functype_t* wasm_functype = wasmtime_func_type(context, &wasm_func); + + const wasm_valtype_vec_t* wasm_params = wasm_functype_params(wasm_functype); + if (wasm_params->size != args.size()) { + ereport(ERROR, (errmsg("wasm_executor: function parameters not matched"))); + } + + wasmtime_val_t call_params[args.size()]; + for (unsigned int i = 0; i < wasm_params->size; ++i) { + if (wasm_valtype_kind(wasm_params->data[i]) == WASM_I32) { + call_params[i].kind = WASMTIME_I32; + call_params[i].of.i32 = args[i]; + } else if (wasm_valtype_kind(wasm_params->data[i]) == WASM_I64) { + call_params[i].kind = WASMTIME_I64; + call_params[i].of.i64 = args[i]; + } else { + ereport(ERROR, (errmsg("wasm_executor: not support the value type(%d) for now", wasm_valtype_kind(wasm_params->data[i])))); + } + } + + wasmtime_val_t results[1]; + wasm_trap_t *wasm_trap = NULL; + wasmtime_error_t *error_msg = wasmtime_func_call(context, &wasm_func, call_params, args.size(), results, 1, &wasm_trap); + if (error_msg != NULL || wasm_trap != NULL) { + exit_with_error("failed to call function", error_msg, wasm_trap); + } + + int64 ret_val = 0; + if (results[0].kind == WASMTIME_I32) { + ret_val = results[0].of.i32; + } else if (results[0].kind == WASMTIME_I64) { + ret_val = results[0].of.i64; + } else { + ereport(ERROR, (errmsg("wasm_executor: the function(%s) return type(%d) not supported", funcname, results[0].kind))); + } + + return ret_val; +} + +static void wasm_export_funcs_query(int64 instanceid, TupleFuncState* inter_call_data) +{ + WasmInstInfo* instanceinfo = find_instance(instanceid); + if (instanceinfo == NULL) { + ereport(ERROR, (errmsg("wasm_executor: instance with id %ld is not find", instanceid))); + } + + std::vector* functions = find_exported_func_list(instanceid); + if (functions != NULL) { + inter_call_data->currindex = functions->begin(); + inter_call_data->lastindex = functions->end(); + elog(DEBUG1, "wasm_executor:find exported func info for instanceid %ld", instanceid); + return; + } + + functions = new(std::nothrow)std::vector; + exported_functions.insert(std::pair*>(instanceid, functions)); + + wasmtime_context_t* context = wasmtime_store_context(instanceinfo->wasm_store); + wasmtime_instance_t& instance = instanceinfo->instance; + + char *export_name = NULL; + size_t namelen; + wasmtime_extern_t wasm_extern; + int index = 0; + while (wasmtime_instance_export_nth(context, &instance, index, &export_name, &namelen, &wasm_extern)) { + if (wasm_extern.kind == WASMTIME_EXTERN_FUNC) { + wasmtime_func_t wasm_func = wasm_extern.of.func; + wasm_functype_t* wasm_functype = wasmtime_func_type(context, &wasm_func); + WasmFuncInfo *funcinfo = new(std::nothrow)WasmFuncInfo(); + + const wasm_valtype_vec_t* wasm_results = wasm_functype_results(wasm_functype); + if (wasm_results->size != 1) { + ereport(ERROR, (errmsg("wasm_executor: only support the functions who will return exactly one result value for now"))); + } + //TODO:support more data types + if (wasm_valtype_kind(wasm_results->data[0]) == WASM_I32) { + funcinfo->outputs = "integer"; + } else if (wasm_valtype_kind(wasm_results->data[0]) == WASM_I64) { + funcinfo->outputs = "bigint"; + } else { + ereport(ERROR, (errmsg("wasm_executor: not support the value type(%d) for now", wasm_valtype_kind(wasm_results->data[0])))); + } + + const wasm_valtype_vec_t* wasm_params = wasm_functype_params(wasm_functype); + if (wasm_params->size > 10) { + ereport(ERROR, (errmsg("wasm_executor: do not support function with 10 more parameters"))); + } + + for (unsigned int i = 0; i < wasm_params->size; ++i) { + if (wasm_valtype_kind(wasm_params->data[i]) == WASM_I32) { + funcinfo->inputs += "integer,"; + } else if (wasm_valtype_kind(wasm_params->data[i]) == WASM_I64) { + funcinfo->inputs += "bigint,"; + } else { + ereport(ERROR, (errmsg("wasm_executor: not support the value type(%d) for now", wasm_valtype_kind(wasm_params->data[i])))); + } + } + if (funcinfo->inputs.length() > 0) { + funcinfo->inputs.pop_back(); + } + + funcinfo->funcname = std::string(export_name, namelen); + functions->push_back(funcinfo); + } + index++; + } + + inter_call_data->currindex = functions->begin(); + inter_call_data->lastindex = functions->end(); + elog(DEBUG1, "wasm_executor:init exported func info for instanceid %ld", instanceid); +} + +PG_FUNCTION_INFO_V1(wasm_create_instance_wat); +Datum wasm_create_instance_wat(PG_FUNCTION_ARGS) +{ + int64 uuid = generate_uuid(PG_GETARG_DATUM(0)); + text *arg = PG_GETARG_TEXT_P(0); + char* filepath = text_to_cstring(arg); + canonicalize_path(filepath); + + if (!superuser()) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errmsg("wasm_executor: must be system admin to create wasm instance")))); + + WasmInstInfo *instinfo = find_instance(uuid); + if (instinfo != NULL) { + ereport(NOTICE, (errmsg("wasm_executor: instance already created for %s", filepath))); + return UInt32GetDatum(uuid); + } + + instinfo = new (std::nothrow)WasmInstInfo(); + + instinfo->wasm_engine = wasm_engine_new(); + if (instinfo->wasm_engine == NULL) { + ereport(ERROR, (errmsg("wasm_executor: unable to create new wasm engine"))); + } + instinfo->wasm_store = wasmtime_store_new(instinfo->wasm_engine, NULL, NULL); + if (instinfo->wasm_store == NULL) { + ereport(ERROR, (errmsg("wasm_executor: unable to create new wasmtime storage"))); + } + wasmtime_context_t *context = wasmtime_store_context(instinfo->wasm_store); + + wasm_byte_vec_t wat_bytes; + FILE *file = fopen(filepath, "r"); + if (file == NULL) { + ereport(ERROR, (errmsg("wasm_executor: unable to open file %s", filepath))); + } + + fseek(file, 0L, SEEK_END); + size_t file_size = ftell(file); + wasm_byte_vec_new_uninitialized(&wat_bytes, file_size); + fseek(file, 0L, SEEK_SET); + int ret = fread(wat_bytes.data, file_size, 1, file); + fclose(file); + if (ret != 1) { + ereport(ERROR, (errmsg("wasm_executor: failed to load moude from %s", filepath))); + } + + wasm_byte_vec_t wasm_bytes; + wasmtime_error_t *error_msg = wasmtime_wat2wasm(wat_bytes.data, wat_bytes.size, &wasm_bytes); + if (error_msg != NULL) { + exit_with_error("failed to parse wat", error_msg, NULL); + } + wasm_byte_vec_delete(&wat_bytes); + + error_msg = wasmtime_module_new(instinfo->wasm_engine, (uint8 *)wasm_bytes.data, wasm_bytes.size, &instinfo->wasm_module); + if (instinfo->wasm_module == NULL) { + exit_with_error("failed to compile module", error_msg, NULL); + } + wasm_byte_vec_delete(&wasm_bytes); + + wasm_trap_t *wasm_trap = NULL; + error_msg = wasmtime_instance_new(context, instinfo->wasm_module, NULL, 0, &instinfo->instance, &wasm_trap); + if (error_msg != NULL || wasm_trap != NULL) { + exit_with_error("failed to create wasm instance", error_msg, wasm_trap); + } + + instinfo->wasm_file = filepath; + instances.insert(std::pair(uuid, instinfo)); + + return Int64GetDatum(uuid); +} + +PG_FUNCTION_INFO_V1(wasm_create_instance); +Datum wasm_create_instance(PG_FUNCTION_ARGS) +{ + int64 uuid = generate_uuid(PG_GETARG_DATUM(0)); + text *arg = PG_GETARG_TEXT_P(0); + char* filepath = text_to_cstring(arg); + canonicalize_path(filepath); + + if (!superuser()) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errmsg("wasm_executor: must be system admin to create wasm instance")))); + + WasmInstInfo *instinfo = find_instance(uuid); + if (instinfo != NULL) { + ereport(NOTICE, (errmsg("wasm_executor: instance already created for %s", filepath))); + return UInt32GetDatum(uuid); + } + + instinfo = new (std::nothrow)WasmInstInfo(); + + instinfo->wasm_engine = wasm_engine_new(); + if (instinfo->wasm_engine == NULL) { + ereport(ERROR, (errmsg("wasm_executor: unable to create new wasm engine"))); + } + instinfo->wasm_store = wasmtime_store_new(instinfo->wasm_engine, NULL, NULL); + if (instinfo->wasm_store == NULL) { + ereport(ERROR, (errmsg("wasm_executor: unable to create new wasmtime storage"))); + } + wasmtime_context_t *context = wasmtime_store_context(instinfo->wasm_store); + + wasm_byte_vec_t wasm_bytes; + FILE *file = fopen(filepath, "rb"); + if (file == NULL) { + ereport(ERROR, (errmsg("wasm_executor: unable to open file %s", filepath))); + } + + fseek(file, 0L, SEEK_END); + size_t file_size = ftell(file); + wasm_byte_vec_new_uninitialized(&wasm_bytes, file_size); + fseek(file, 0L, SEEK_SET); + int ret = fread(wasm_bytes.data, file_size, 1, file); + fclose(file); + if (ret != 1) { + ereport(ERROR, (errmsg("wasm_executor: failed to load moude from %s", filepath))); + } + + wasmtime_error_t *error_msg = wasmtime_module_new(instinfo->wasm_engine, (uint8 *)wasm_bytes.data, wasm_bytes.size, &instinfo->wasm_module); + if (instinfo->wasm_module == NULL) { + exit_with_error("failed to compile module", error_msg, NULL); + } + wasm_byte_vec_delete(&wasm_bytes); + + wasm_trap_t *wasm_trap = NULL; + error_msg = wasmtime_instance_new(context, instinfo->wasm_module, NULL, 0, &instinfo->instance, &wasm_trap); + if (error_msg != NULL || wasm_trap != NULL) { + exit_with_error("failed to create wasm instance", error_msg, wasm_trap); + } + + instinfo->wasm_file = filepath; + instances.insert(std::pair(uuid, instinfo)); + + return Int64GetDatum(uuid); +} + +PG_FUNCTION_INFO_V1(wasm_get_instances); +Datum wasm_get_instances(PG_FUNCTION_ARGS) +{ + FuncCallContext* fctx = NULL; + TupleInstanceState* inter_call_data = NULL; + if (SRF_IS_FIRSTCALL()) { + TupleDesc tupdesc; + MemoryContext mctx; + + fctx = SRF_FIRSTCALL_INIT(); + mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx); + inter_call_data = (TupleInstanceState*)palloc(sizeof(TupleInstanceState)); + + /* Build a tuple descriptor for our result type */ + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "wasm_executor: return type must be a row type"); + + inter_call_data->tupd = tupdesc; + inter_call_data->currindex = instances.begin(); + inter_call_data->lastindex = instances.end(); + + fctx->user_fctx = inter_call_data; + MemoryContextSwitchTo(mctx); + } + + fctx = SRF_PERCALL_SETUP(); + inter_call_data = (TupleInstanceState*)(fctx->user_fctx); + + if (inter_call_data->currindex != inter_call_data->lastindex) { + HeapTuple resultTuple; + Datum result; + Datum values[2]; + bool nulls[2]; + + errno_t rc = memset_s(nulls, sizeof(nulls), 0, sizeof(nulls)); + securec_check_c(rc, "\0", "\0"); + + WasmInstInfo *instanceinfo = inter_call_data->currindex->second; + values[0] = Int64GetDatum(inter_call_data->currindex->first); + values[1] = CStringGetTextDatum(instanceinfo->wasm_file.c_str()); + + /* Build and return the result tuple. */ + resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls); + result = HeapTupleGetDatum(resultTuple); + + inter_call_data->currindex++; + SRF_RETURN_NEXT(fctx, result); + } else { + SRF_RETURN_DONE(fctx); + } +} + +PG_FUNCTION_INFO_V1(wasm_get_exported_functions); +Datum wasm_get_exported_functions(PG_FUNCTION_ARGS) +{ + int64 instanceid = PG_GETARG_INT64(0); + FuncCallContext* fctx = NULL; + TupleFuncState* inter_call_data = NULL; + if (SRF_IS_FIRSTCALL()) { + TupleDesc tupdesc; + MemoryContext mctx; + + fctx = SRF_FIRSTCALL_INIT(); + mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx); + inter_call_data = (TupleFuncState*)palloc(sizeof(TupleFuncState)); + + /* Build a tuple descriptor for our result type */ + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "wasm_executor: return type must be a row type"); + + inter_call_data->tupd = tupdesc; + wasm_export_funcs_query(instanceid, inter_call_data); + + fctx->user_fctx = inter_call_data; + MemoryContextSwitchTo(mctx); + } + fctx = SRF_PERCALL_SETUP(); + inter_call_data = (TupleFuncState*)(fctx->user_fctx); + + if (inter_call_data->currindex != inter_call_data->lastindex) { + HeapTuple resultTuple; + Datum result; + Datum values[3]; + bool nulls[3]; + + errno_t rc = memset_s(nulls, sizeof(nulls), 0, sizeof(nulls)); + securec_check_c(rc, "\0", "\0"); + + WasmFuncInfo *funcinfo = *inter_call_data->currindex; + values[0] = CStringGetTextDatum(funcinfo->funcname.c_str()); + values[1] = CStringGetTextDatum(funcinfo->inputs.c_str()); + values[2] = CStringGetTextDatum(funcinfo->outputs.c_str()); + + /* Build and return the result tuple. */ + resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls); + result = HeapTupleGetDatum(resultTuple); + inter_call_data->currindex++; + + SRF_RETURN_NEXT(fctx, result); + } else { + SRF_RETURN_DONE(fctx); + } +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_0); +Datum wasm_invoke_function_0(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_1); +Datum wasm_invoke_function_1(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + params.push_back(PG_GETARG_INT64(2)); + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_2); +Datum wasm_invoke_function_2(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + params.push_back(PG_GETARG_INT64(2)); + params.push_back(PG_GETARG_INT64(3)); + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_3); +Datum wasm_invoke_function_3(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + params.push_back(PG_GETARG_INT64(2)); + params.push_back(PG_GETARG_INT64(3)); + params.push_back(PG_GETARG_INT64(4)); + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_4); +Datum wasm_invoke_function_4(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + params.push_back(PG_GETARG_INT64(2)); + params.push_back(PG_GETARG_INT64(3)); + params.push_back(PG_GETARG_INT64(4)); + params.push_back(PG_GETARG_INT64(5)); + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_5); +Datum wasm_invoke_function_5(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + params.push_back(PG_GETARG_INT64(2)); + params.push_back(PG_GETARG_INT64(3)); + params.push_back(PG_GETARG_INT64(4)); + params.push_back(PG_GETARG_INT64(5)); + params.push_back(PG_GETARG_INT64(6)); + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_6); +Datum wasm_invoke_function_6(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + params.push_back(PG_GETARG_INT64(2)); + params.push_back(PG_GETARG_INT64(3)); + params.push_back(PG_GETARG_INT64(4)); + params.push_back(PG_GETARG_INT64(5)); + params.push_back(PG_GETARG_INT64(6)); + params.push_back(PG_GETARG_INT64(7)); + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_7); +Datum wasm_invoke_function_7(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + params.push_back(PG_GETARG_INT64(2)); + params.push_back(PG_GETARG_INT64(3)); + params.push_back(PG_GETARG_INT64(4)); + params.push_back(PG_GETARG_INT64(5)); + params.push_back(PG_GETARG_INT64(6)); + params.push_back(PG_GETARG_INT64(7)); + params.push_back(PG_GETARG_INT64(8)); + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_8); +Datum wasm_invoke_function_8(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + params.push_back(PG_GETARG_INT64(2)); + params.push_back(PG_GETARG_INT64(3)); + params.push_back(PG_GETARG_INT64(4)); + params.push_back(PG_GETARG_INT64(5)); + params.push_back(PG_GETARG_INT64(6)); + params.push_back(PG_GETARG_INT64(7)); + params.push_back(PG_GETARG_INT64(8)); + params.push_back(PG_GETARG_INT64(9)); + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_9); +Datum wasm_invoke_function_9(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + params.push_back(PG_GETARG_INT64(2)); + params.push_back(PG_GETARG_INT64(3)); + params.push_back(PG_GETARG_INT64(4)); + params.push_back(PG_GETARG_INT64(5)); + params.push_back(PG_GETARG_INT64(6)); + params.push_back(PG_GETARG_INT64(7)); + params.push_back(PG_GETARG_INT64(8)); + params.push_back(PG_GETARG_INT64(9)); + params.push_back(PG_GETARG_INT64(10)); + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +} + +PG_FUNCTION_INFO_V1(wasm_invoke_function_10); +Datum wasm_invoke_function_10(PG_FUNCTION_ARGS) +{ + char* instanceid = TextDatumGetCString(PG_GETARG_DATUM(0)); + char* funcname = TextDatumGetCString(PG_GETARG_DATUM(1)); + std::vector params; + + params.push_back(PG_GETARG_INT64(2)); + params.push_back(PG_GETARG_INT64(3)); + params.push_back(PG_GETARG_INT64(4)); + params.push_back(PG_GETARG_INT64(5)); + params.push_back(PG_GETARG_INT64(6)); + params.push_back(PG_GETARG_INT64(7)); + params.push_back(PG_GETARG_INT64(8)); + params.push_back(PG_GETARG_INT64(9)); + params.push_back(PG_GETARG_INT64(10)); + params.push_back(PG_GETARG_INT64(11)); + int64 result = wasm_invoke_function(instanceid, funcname, params); + return Int64GetDatum(result); +}