关于c++:wasmer-运行时

上一篇文章分享了基于wasm的openssl实际,讲述了openssl的MD5算法如何在浏览器中执行。在摸索过程中发现了openssl是能够通过wasm编译后间接run,并且有本人的runtime,这是因为openssl.wasm是通过wasmer编译运行的,这一篇文章分享制作具备运行时的openssl.wasm

概述

  • Wasmer介绍
  • Openssl编译到WASM
  • 总结

一、Wasmer介绍

Wasmer是一个用于在服务器上执行WebAssembly的开源运行时。反对基于WebAssembly的超轻量级容器,该容器能够在任何中央运行,还能够嵌入其余编程语言。其生态包含以下几个局部:

1. Wasmer Runtime

Wasmer Runtime是Wasmer生态其中一个,容许wasm模块能够独立运行。要害性能是使程序可能以任何编程语言运行;使二进制文件可能在Wasmer反对的任何“操作系统”(例如Linux、macOS、Windows和FreeBSD)上不加批改地运行;充当Wasm模块,通过应用程序二进制接口(ABI),如WASI(WebAssembly System Interface)和Emscripten(1.38.43及更早版本)与本机“操作系统”性能交互的平安桥梁。

2. WAPM

WAPM是WebAssembly Package Manager的缩写,为能够独立应用的Wasmer Runtime做的软件包管理器。能够了解为通过Wasmer软件包编译进去的.wasm文件是能够独立运行的,WAPM就是为了治理这些能独立运行的runtime而存在的。如图:


搜寻一个openssl运行时的.wasm。


提供了运行时环境。

整个看上去和docker hub有点像,还有制作方法,咱们能够参考做一个新版的openssl.wasm.

3. WebAssembly.sh

WebAssembly shell程序是一个在线shell程序,您能够在其中拖放WebAssembly模块以进行尝试,还能够执行WAPM中可用的WASI模块的所有命令。在线演示地址:https://webassembly.sh/

4. Wasienv

Wasienv是一个工具,旨在将所有编程语言引入WebAssembly WASI的工具能够将不同的编程语言编译到WebAssembly中,而后在浏览器或服务器中运行程序。

看上去和上一篇咱们应用的Emscripten很像,尽管两者都能把高级语言编译成.wasm文件,然而后者更重视web方面的开发在浏览器中运行(也是第一个生成wasm的工具),前者是解决跨平台的问题,体现的是wasm的可移植性。

这里有个WebAssembly WASI的概念,WASI是WebAssembly System Interface的缩写,是WebAssembly 平台的零碎接口, 由Wasmtime我的项目设计,目标是为WASM设计一套引擎无关(engine-indepent), 面向非Web零碎(non-Web system-oriented)的API规范。因为 WebAssembly 是概念机的汇编语言,所以 WebAssembly 须要一个概念操作系统的零碎接口,而不是任何繁多操作系统的零碎接口。 这样,它就能够在所有不同操作系统中运行。

二、 Openssl编译到WASM

1. 环境筹备

  • Python 3.7.3
  • Pip3 19.3.1
  • Openssl 官网: https://www.openssl.org/source
  • 依赖工具 wasienv

2. 装置wasienv


curl https://raw.githubusercontent.com/wasienv/wasienv/master/install.sh | sh

问题1

我本地无奈解析raw.githubusercontent.com

解决办法

把install.sh复制到本地执行

问题2

执行过程中 pip3 install wasienv --install-option="--install-scripts=$INSTALL_DIRECTORY/bin" --upgrade --user 报错,不反对这种写法

WARNING: Skipping wasienv as it is not installed.
/usr/local/lib/python3.7/site-packages/pip/_internal/commands/install.py:235: UserWarning: Disabling all use of wheels due to the use of --build-option / --global-option / --install-option.
  cmdoptions.check_install_build_global(options)
ERROR: Location-changing options found in --install-option: ['--install-scripts'] from command line. This is unsupported, use pip-level options like --user, --prefix, --root, and --target instead.

解决办法

通过查阅材料,是因为pip3更新到最新版本导致,不反对--install-option=“”显示传参,回退版本

pip install --upgrade pip==19.3.1

问题3

执行过程 curl https://get.wasmer.io -sSfL | sh报错,无奈解析域名

解决办法

浏览器拜访,把内容复制进去,我保留的文件是install2.sh,批改install.sh中的援用。

问题4


执行过程  INSTALL_DIRECTORY/bin/wasienv install-sdk unstable

 File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 659, in urlopen
conn = self._get_conn(timeout=pool_timeout)
 File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 279, in _get_conn
return conn or self._new_conn()
 File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 948, in _new_conn
"Can't connect to HTTPS URL because the SSL module is not available."

解决办法

剖析

这是说python没有ssl模块,对于依赖的包,无奈下载的问题。
通过查阅材料,python ssl模块对于openssl的版本有要求,所以这里尝试降级openssl

# 以后版本
~/Desktop/wasm$ openssl version
LibreSSL 2.8.3
# 装置其余版本openssl,第一步很慢
brew update && brew upgrade
brew uninstall --ignore-dependencies openssl; 
brew install https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb

参考文档


https://www.jianshu.com/p/f8585da77ed9

https://stackoverflow.com/questions/45954528/pip-is-configured-with-locations-that-require-tls-ssl-however-the-ssl-module-in/59280089#59280089

https://stackoverflow.com/questions/35280956/ignoring-ensurepip-failure-pip-7-1-2-requires-ssl-tls-python-3-x-os-x#35282183

#设置brew源
https://www.jianshu.com/p/b26c7bc14440

问题4.1

装置openssl 谬误

Error: Calling Non-checksummed download of openssl formula file from an arbitrary URL is disabled! Use 'brew extract' or 'brew create' and 'brew tap-new' to create a formula file in a tap on GitHub instead.
If reporting this issue please do so at (not Homebrew/brew or Homebrew/core):
https://github.com/tebelorg/Tump/issues/new

解决办法

是openssl版本问题

执行

brew uninstall openssl
brew tap-new $USER/old-openssl
brew extract --version=1.0.2t openssl $USER/old-openssl
brew install openssl@1.0.2t

参考文档:https://github.com/kelabereti…

一顿操作之后,wasienv工具装置实现。

3. openssl 生成 .wasm文件

我通过WAPM找到了openssl编译的脚本,这是一个开源的例子。

git clone https://github.com/wapm-packages/OpenSSL.git

目录构造

build.sh

#!/usr/bin/env sh

# Based on code from https://github.com/TrueBitFoundation/wasm-ports/blob/master/openssl.sh

OPENSSL_VERSION=1.1.1d
PREFIX=`pwd`

DIRECTORY="openssl-${OPENSSL_VERSION}"

if [ ! -d "$DIRECTORY" ]; then
  echo "Download source code"
  wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz
  tar xf openssl-${OPENSSL_VERSION}.tar.gz
fi

cd openssl-${OPENSSL_VERSION}

echo "Configure"
make clean
wasiconfigure ./Configure linux-x32 -no-asm -static -no-sock -no-afalgeng -DOPENSSL_SYS_NETWARE -DSIG_DFL=0 -DSIG_IGN=0 -DHAVE_FORK=0 -DOPENSSL_NO_AFALGENG=1 -DOPENSSL_NO_SPEED=1 || exit $?

cp ../progs.h apps/progs.h

sed -i 's|^CROSS_COMPILE.*$|CROSS_COMPILE=|g' Makefile

echo "Build"
wasimake make -j12 build_generated libssl.a libcrypto.a apps/openssl

rm -rf ${PREFIX}/include
mkdir -p ${PREFIX}/include
cp -R include/openssl ${PREFIX}/include

cp -R apps/openssl.wasm ../

echo "Done"

思路如下:

  1. 从openssl官网上下载对应版本的包。
  2. 通过之前装置的wasienv工具生成Makefile文件。
  3. cp progs.h文件到openssl/apps目录下
  4. 编译生成wasm文件,此时wasm文件在apps/openssl目录下。
  5. 将openssl/include和.wasm文件复制到新的目录下。

4、验证openssl.wasm

~/Desktop/wasm/openssl_demo/OpenSSL$ wasmer run openssl.wasm
OpenSSL> help

Standard commands
asn1parse         ca                cms               crl
crl2pkcs7         dgst              dhparam           dsa
dsaparam          ec                ecparam           enc
engine            errstr            exit              gendsa
genpkey           genrsa            help              list
nseq              ocsp              passwd            pkcs12
pkcs7             pkcs8             pkey              pkeyparam
pkeyutl           prime             rand              rehash
req               rsa               rsautl            sess_id
smime             spkac             srp               ts
verify            version           x509

Message Digest commands (see the `dgst' command for more details)
blake2b512        blake2s256        gost              md4
md5               mdc2              rmd160            sha1
sha224            sha256            sha384            sha512
speed

Cipher commands (see the `enc' command for more details)
aes-128-cbc       aes-128-ecb       aes-192-cbc       aes-192-ecb
aes-256-cbc       aes-256-ecb       base64            bf
bf-cbc            bf-cfb            bf-ecb            bf-ofb
camellia-128-cbc  camellia-128-ecb  camellia-192-cbc  camellia-192-ecb
camellia-256-cbc  camellia-256-ecb  cast              cast-cbc
cast5-cbc         cast5-cfb         cast5-ecb         cast5-ofb
des               des-cbc           des-cfb           des-ecb
des-ede           des-ede-cbc       des-ede-cfb       des-ede-ofb
des-ede3          des-ede3-cbc      des-ede3-cfb      des-ede3-ofb
des-ofb           des3              desx              idea
idea-cbc          idea-cfb          idea-ecb          idea-ofb
rc2               rc2-40-cbc        rc2-64-cbc        rc2-cbc
rc2-cfb           rc2-ecb           rc2-ofb           rc4
rc4-40            seed              seed-cbc          seed-cfb
seed-ecb          seed-ofb

看起来还不错,根本的命令还是有的,生成一个rsa私钥试试。

#!/usr/bin/env sh
wasmer run openssl.wasm --dir=. -- genrsa -des3 -out priv.key 1024 || exit $?
~/Desktop/wasm/openssl_demo/OpenSSL$ cat priv.key
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,6D2827691E261E49

ADR7PRPukSBTEHiWvLxuj8DyU9QtiZIC0ZEQxJMRhx7n0b1J5wnrmIMfwbgtVLru
7vi/zdMqwIRJ7Afq3/g0r0/rWA55CF3SCX+UVSGnMrkOV68MWh2ETFCBD3NqmNG3
XZToct1bON0ES6OTEON+4V3PBunSpeU+6EKE8pTOLBeb6Frt1IQ1FgG8xVGfsCnk
DJKncHzN0D66mFlW0mrluiNvHk/PfBvzXQv2FlTdvctThOj/DK6yQ3m4PtPLYn1d
7qOy1wQt1OuXHnymgkR66Rf6OA2aAJXPxBp7C/aVxEAWWLKNw3zv/9rhlOqkIsEQ
i+tnf58VEBR6HGCDXA4UD83x1Pr9h0l5pcrHcvZYv+bnSiIhZGIVSCx0U9jmgbef
qg+L4wzIaaWaOTKpCtHm/10DWII5H6TO6uQHF8Bjz3bvR77tv/jaKRgrG+F7y4h9
5I+DsQjHhcQvIi2aro2o0DGsaD0qLK4jWbvFRxBb7FBfBEFQ9AWz04tZ9Oco5/Ti
ybMaZyLoCNGauqEQV3vS1OoR9IZ1Dzqra5X+YDRSGxTN+31dSCDYH5ta1VTHJyis
SMDfEisqz6vcIyEf+pWzF+yzhQj9aAqFEbWg+pG0Bhx4GXxhLy0WjnFVlWHxjTjN
hoZYC0vKc9uNvzaewFjHEn3IwnXTaXdknE25uBBc18ceBsaW7uzW7D4iPvjhtOC3
znrwo+VGuNPGJTb7md9Dz1q2fjTrPintPftN9ZqxAjeYx+7NiVZfm71mV88EAXYW
eQMQZmTFTiSyTcotDkyRPkvbDbXEytB4sVbwXiBasKxmrgzgz6UIjA==
-----END RSA PRIVATE KEY-----

三、总结

本篇文章次要介绍了WebAssembly的运行时,以及如何制作一个运行时的openssl.wasm。整体感觉还不错,当初咱们有了运行时的openssl.wasm,并且反对根本的命令,但不晓得性能如何,下一篇文章分享openssl.wasm与原生openssl性能比照。

参考资料

  • Wasmer文档

    • https://docs.wasmer.io/
  • WAPM

    • https://wapm.io/

Netwarps 由国内资深的云计算和分布式技术开发团队组成,该团队在金融、电力、通信及互联网行业有十分丰盛的落地教训。Netwarps 目前在深圳、北京均设立了研发核心,团队规模30+,其中大部分为具备十年以上开发教训的技术人员,别离来自互联网、金融、云计算、区块链以及科研机构等业余畛域。
Netwarps 专一于平安存储技术产品的研发与利用,次要产品有去中心化文件系统(DFS)、去中心化计算平台(DCP),致力于提供基于去中心化网络技术实现的分布式存储和分布式计算平台,具备高可用、低功耗和低网络的技术特点,实用于物联网、工业互联网等场景。
公众号:Netwarps

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理