Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2020-01-27 21:06:08 +08:00
commit 923e8ae603
4 changed files with 255 additions and 262 deletions

View File

@ -0,0 +1,87 @@
[#]: collector: (lujun9972)
[#]: translator: (laingke)
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-11823-1.html)
[#]: subject: (Why everyone is talking about WebAssembly)
[#]: via: (https://opensource.com/article/20/1/webassembly)
[#]: author: (Mike Bursell https://opensource.com/users/mikecamel)
为什么每个人都在谈论 WebAssembly
======
> 了解有关在 Web 浏览器中运行任何代码的最新方法的更多信息。
![](https://img.linux.net.cn/data/attachment/album/202001/27/125343ch0hxdfbzibrihfn.jpg)
如果你还没有听说过 [WebAssembly][2]那么你很快就会知道。这是业界最保密的秘密之一但它无处不在。所有主流的浏览器都支持它并且它也将在服务器端使用。它很快它能用于游戏编程。这是主要的国际网络标准组织万维网联盟W3C的一个开放标准。
你可能会说:“哇,这听起来像是我应该学习编程的东西!”你可能是对的,但也是错的。你不需要用 WebAssembly 编程。让我们花一些时间来学习这种通常被缩写为“Wasm”的技术。
### 它从哪里来?
大约十年前,人们越来越认识到,广泛使用的 JavaScript 不够快速无法满足许多目的。JavaScript 无疑是成功和方便的。它可以在任何浏览器中运行,并启用了今天我们认为理所当然的动态网页类型。但这是一种高级语言,在设计时并没有考虑到计算密集型工作负载。
然而,尽管负责主流 web 浏览器的工程师们对性能问题的看法大体一致,但他们对如何解决这个问题却意见不一。出现了两个阵营,谷歌开始了它的<ruby>原生客户端<rt>Native Client</rt></ruby>项目,后来又推出了<ruby>可移植原生客户端<rt>Portable Native Client</rt></ruby>变体,着重于允许用 C/C++ 编写的游戏和其它软件在 Chrome 的一个安全隔间中运行。与此同时Mozilla 赢得了微软对 asm.js 的支持。该方法更新了浏览器,因此它可以非常快速地运行 JavaScript 指令的低级子集(有另一个项目可以将 C/C++ 代码转换为这些指令)。
由于这两个阵营都没有得到广泛采用,各方在 2015 年同意围绕一种称为 WebAssembly 的新标准,以 asm.js 所采用的基本方法为基础,联合起来。[如 CNET 的 Stephen Shankland 当时所写][3],“在当今的 Web 上,浏览器的 JavaScript 将这些指令转换为机器代码。但是,通过 WebAssembly程序员可以在此过程的早期阶段完成很多工作从而生成介于两种状态之间的程序。这使浏览器摆脱了创建机器代码的繁琐工作但也实现了 Web 的承诺 —— 该软件将在具有浏览器的任何设备上运行,而无需考虑基础硬件的细节。”
在 2017 年Mozilla 宣布了它的最小可行的产品MVP并使其脱离预览版阶段。到该年年底所有主流的浏览器都采用了它。[2019 年 12 月][4]WebAssembly 工作组发布了三个 W3C 推荐的 WebAssembly 规范。
WebAssembly 定义了一种可执行程序的可移植二进制代码格式、相应的文本汇编语言以及用于促进此类程序与其宿主环境之间的交互接口。WebAssembly 代码在低级虚拟机中运行这个可运行于许多微处理器之上的虚拟机可模仿这些处理器的功能。通过即时JIT编译或解释WebAssembly 引擎可以以近乎原生平台编译代码的速度执行。
### 为什么现在感兴趣?
当然,最近对 WebAssembly 感兴趣的部分原因是最初希望在浏览器中运行更多计算密集型代码。尤其是笔记本电脑用户,越来越多的时间都花在浏览器上(或者,对于 Chromebook 用户来说,基本上是所有时间)。这种趋势已经迫切需要消除在浏览器中运行各种应用程序的障碍。这些障碍之一通常是性能的某些方面,这正是 WebAssembly 及其前身最初旨在解决的问题。
但是WebAssembly 并不仅仅适用于浏览器。在 2019 年,[Mozilla 宣布了一个名为 WASI][5]<ruby>WebAssembly 系统接口<rt>WebAssembly System Interface</rt></ruby>)的项目,以标准化 WebAssembly 代码如何与浏览器上下文之外的操作系统进行交互。通过将浏览器对 WebAssembly 和 WASI 的支持结合在一起,编译后的二进制文件将能够以接近原生的速度,跨不同的设备和操作系统在浏览器内外运行。
WebAssembly 的低开销立即使它可以在浏览器之外使用,但这无疑是赌注;显然,还有其它不会引入性能瓶颈的运行应用程序的方法。为什么要专门使用 WebAssembly
一个重要的原因是它的可移植性。如今,像 C++ 和 Rust 这样的广泛使用的编译语言可能是与 WebAssembly 关联最紧密的语言。但是,[各种各样的其他语言][6]可以编译为 WebAssembly 或拥有它们的 WebAssembly 虚拟机。此外,尽管 WebAssembly 为其执行环境[假定了某些先决条件][7]但它被设计为在各种操作系统和指令集体系结构上有效执行。因此WebAssembly 代码可以使用多种语言编写,并可以在多种操作系统和处理器类型上运行。
另一个 WebAssembly 优势源于这样一个事实:代码在虚拟机中运行。因此,每个 WebAssembly 模块都在沙盒环境中执行,并使用故障隔离技术将其与宿主机运行时环境分开。这意味着,对于其它部分而言,应用程序独立于其宿主机环境的其余部分执行,如果不调用适当的 API就无法摆脱沙箱。
### WebAssembly 现状
这一切在实践中意味着什么?
如今在运作中的 WebAssembly 的一个例子是 [Enarx][8]。
Enarx 是一个提供硬件独立性的项目,可使用<ruby>受信任的执行环境<rt>Trusted Execution Environments</rt></ruby>TEE保护应用程序的安全。Enarx 使你可以安全地将编译为 WebAssembly 的应用程序始终交付到云服务商,并远程执行它。正如 Red Hat 安全工程师 [Nathaniel McCallum 指出的那样][9]:“我们这样做的方式是,我们将你的应用程序作为输入,并使用远程硬件执行认证过程。我们使用加密技术验证了远程硬件实际上是它声称的硬件。最终的结果不仅是我们对硬件的信任度提高了;它也是一个会话密钥,我们可以使用它将加密的代码和数据传递到我们刚刚要求加密验证的环境中。”
另一个例子是 OPA<ruby>开放策略代理<rt>Open Policy Agent</rt></ruby>,它[发布][10]于 2019 年 11 月,你可以[编译][11]他们的策略定义语言 Rego 为 WebAssembly。Rego 允许你编写逻辑来搜索和组合来自不同来源的 JSON/YAML 数据,以询问诸如“是否允许使用此 API”之类的问题。
OPA 已被用于支持策略的软件,包括但不限于 Kubernetes。使用 OPA 之类的工具来简化策略[被认为是在各种不同环境中正确保护 Kubernetes 部署的重要步骤][12]。WebAssembly 的可移植性和内置的安全功能非常适合这些工具。
我们的最后一个例子是 [Unity][13]。还记得我们在文章开头提到过 WebAssembly 可用于游戏吗?好吧,跨平台游戏引擎 Unity 是 WebAssembly 的较早采用者,它提供了在浏览器中运行的 Wasm 的首个演示品,并且自 2018 年 8 月以来,[已将 WebAssembly][14]用作 Unity WebGL 构建目标的输出目标。
这些只是 WebAssembly 已经开始产生影响的几种方式。你可以在 <https://webassembly.org/> 上查找更多信息并了解 Wasm 的所有最新信息。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/1/webassembly
作者:[Mike Bursell][a]
选题:[lujun9972][b]
译者:[laingke](https://github.com/laingke)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/mikecamel
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/browser_web_internet_website.png?itok=g5B_Bw62 (Digital creative of a browser on the internet)
[2]: https://opensource.com/article/19/8/webassembly-speed-code-reuse
[3]: https://www.cnet.com/news/the-secret-alliance-that-could-give-the-web-a-massive-speed-boost/
[4]: https://www.w3.org/blog/news/archives/8123
[5]: https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/
[6]: https://github.com/appcypher/awesome-wasm-langs
[7]: https://webassembly.org/docs/portability/
[8]: https://enarx.io
[9]: https://enterprisersproject.com/article/2019/9/application-security-4-facts-confidential-computing-consortium
[10]: https://blog.openpolicyagent.org/tagged/webassembly
[11]: https://github.com/open-policy-agent/opa/tree/master/wasm
[12]: https://enterprisersproject.com/article/2019/11/kubernetes-reality-check-3-takeaways-kubecon
[13]: https://opensource.com/article/20/1/www.unity.com
[14]: https://blogs.unity3d.com/2018/08/15/webassembly-is-here/

View File

@ -1,176 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Introducing the guide to inter-process communication in Linux)
[#]: via: (https://opensource.com/article/20/1/inter-process-communication-linux)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
Introducing the guide to inter-process communication in Linux
======
This free eBook gives seasoned and occasional coders insight into the
core concepts and mechanisms of inter-process communication (IPC) in
Linux.
![Inter-process Communication in Linux][1]
Getting one software process to talk to another software process is a delicate balancing act. It can be a vital function for an application, though, so it's a problem any programmer embarking on a complex project has to solve. Whether your application needs to kick off a job being handled by someone else's software; to monitor an action being performed by a peripheral or over a network; or to detect a signal from some other source, when your software relies on something outside of its own code to know what to do next or when to do it, you need to think about inter-process communication (IPC).
The Unix operating system accounted for this long ago, possibly because of an early expectation that software would originate from diverse sources. In the same tradition, Linux provides many of the same interfaces for IPC and some new ones. The Linux kernel features several IPC methods, and the [util-linux package][2] contains the **ipcmk**, **ipcrm**, **ipcs**, and **lsipc** commands for monitoring and managing IPC messages.
### Show IPC information
Before experimenting with IPC, you should know what IPC facilities are already on your system. The **lsipc** command provides that information.
```
RESOURCE DESCRIPTION               LIMIT  USED  USE%
MSGMNI   Number of message queues  32000     0 0.00%
MSGMAX   Max size of message (byt.. 8192     -     -
MSGMNB   Default max size of queue 16384     -     -
SHMMNI   Shared memory segments     4096    79 1.93%
SHMALL   Shared memory pages       184[...] 25452 0.00%
SHMMAX   Max size of shared memory 18446744073692774399
SHMMIN   Min size of shared memory     1     -     -
SEMMNI   Number of semaphore ident 32000     0 0.00%
SEMMNS   Total number of semaphore 1024000.. 0 0.00%
SEMMSL   Max semaphores per semap  32000     -     -
SEMOPM   Max number of operations p  500     -     -
SEMVMX   Semaphore max value       32767     -     -
```
You may notice that this sample listing includes three different types of IPC mechanisms, each available in the Linux kernel: messages (MSG), shared memory (SHM), and semaphores (SEM). You can view current activity in each of those subsystems with the **ipcs** command:
```
$ ipcs
\------ Message Queues Creators/Owners ---
msqid     perms     cuid      cgid  [...]
\------ Shared Memory Segment Creators/Owners
shmid     perms    cuid    cgid  [...]
557056    700      seth    users [...]
3571713   700      seth    users [...]
2654210   600      seth    users [...]
2457603   700      seth    users [...]
\------ Semaphore Arrays Creators/Owners ---
semid     perms     cuid      cgid  [...]
```
This shows that there currently are no messages or semaphore arrays, but a number of shared memory segments are in use.
There's a simple example you can perform on your system so you can see one of these systems at work. It involves some C code, so you must have build tools on your system. The names of the packages you must install to be able to build from source code vary depending on your distro, so refer to your documentation for specifics. For example, on Debian-based distributions, you can learn about build requirements on the [BuildingTutorial][3] section of the wiki, and on Fedora-based distributions, refer to the [Installing software from source][4] section of the docs.
### Create a message queue
Your system has a default message queue already, but you can create your own using the **ipcmk** command:
```
$ ipcmk --queue
Message queue id: 32764
```
Write a simple IPC message sender, hard-coding in the queue ID for simplicity:
```
#include &lt;sys/ipc.h&gt;
#include &lt;sys/msg.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
struct msgbuffer {
  char text[24];
} message;
int main() {
    int msqid = 32764;
    strcpy(message.text,"opensource.com");
    msgsnd(msqid, &amp;message, sizeof(message), 0);
    printf("Message: %s\n",message.text);
    printf("Queue: %d\n",msqid);
    return 0;
        }
```
Compile the application and run it:
```
$ gcc msgsend.c -o msg.bin
$ ./msg.bin
Message: opensource.com
Queue: 32769
```
You just sent a message to your message queue. You can verify that with the **ipcs** command, using the **\--queue** option to limit output to the message queue:
```
$ ipcs -q
\------ Message Queues --------
key        msqid   owner  perms  used-bytes  messages
0x7b341ab9 0       seth   666    0          0
0x72bd8410 32764   seth   644    24         1
```
You can also retrieve those messages with:
```
#include &lt;sys/ipc.h&gt;
#include &lt;sys/msg.h&gt;
#include &lt;stdio.h&gt;
struct msgbuffer {
    char text[24];
} message;
int main() {
    int msqid = 32764;
    msgrcv(msqid, &amp;message, sizeof(message),0,0);
    printf("\nQueue: %d\n",msqid);
    printf("Got this message: %s\n", message.text);
    msgctl(msqid,IPC_RMID,NULL);
    return 0;
```
Compile and run with:
```
$ gcc get.c -o get.bin
$ ./get.bin
Queue: 32764
Got this message: opensource.com
```
### Download [the eBook][5]
This is just one example of the lessons available in Marty Kalin's [A guide to inter-process communication in Linux][5], the latest free (and Creative Commons) downloadable eBook from Opensource.com. In just a few short lessons, you will learn about POSIX methods of IPC from message queues, shared memory and semaphores, sockets, signals, and much more. Sit down with Marty's book, and you'll emerge a better-informed programmer. But it isn't just for seasoned coders—if all you ever write are shell scripts, there's plenty of practical knowledge about pipes (named and unnamed) and shared files, as well as important concepts you need to know when you use a shared file or an external message queue.
If you're interested in making great software that's written to be dynamic and system-aware, you need to know about IPC. Let [this book][5] be your guide.
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/1/inter-process-communication-linux
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coverimage_inter-process_communication_linux_520x292.png?itok=hPoen7oI (Inter-process Communication in Linux)
[2]: https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/
[3]: https://wiki.debian.org/BuildingTutorial
[4]: https://docs.pagure.org/docs-fedora/installing-software-from-source.html
[5]: https://opensource.com/downloads/guide-inter-process-communication-linux

View File

@ -0,0 +1,168 @@
[#]: collector: (lujun9972)
[#]: translator: (laingke)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Introducing the guide to inter-process communication in Linux)
[#]: via: (https://opensource.com/article/20/1/inter-process-communication-linux)
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
Linux 进程间通信介绍指南
======
这本免费的电子书使经验丰富的程序员更深入了解 Linux 中进程间通信IPC的核心概念和机制。
![Inter-process Communication in Linux][1]
让一个软件过程与另一个软件过程进行对话是一个微妙的平衡行为。但是它对于应用程序而言可能是至关重要的功能因此这是任何从事复杂项目的程序员都必须解决的问题。你的应用程序是否需要启动由其它软件处理的工作监视外设或网络上正在执行的操作或者检测来自其它来源的信号当你的软件需要依赖其自身代码之外的东西来知道下一步做什么或什么时候做时你就需要考虑进程间通信IPC
Unix 操作系统在很久以前就说明了这一点这可能是因为人们早就期望软件会来自各种来源。按照相同的传统Linux 为 IPC 和一些新接口提供了许多相同的接口。Linux 内核具有几个 IPC 方法,[util-linux 包][2]包含 `ipcmk`、`ipcrm`、`ipcs` 和 `lsipc` 命令,用于监视和管理 IPC 消息。
### 显示进程间通信信息
在尝试 IPC 之前,你应该知道系统上已经有哪些 IPC 工具。`lsipc` 命令提供了该信息。
```
RESOURCE DESCRIPTION LIMIT USED USE%
MSGMNI Number of message queues 32000 0 0.00%
MSGMAX Max size of message (byt.. 8192 - -
MSGMNB Default max size of queue 16384 - -
SHMMNI Shared memory segments 4096 79 1.93%
SHMALL Shared memory pages 184[...] 25452 0.00%
SHMMAX Max size of shared memory 18446744073692774399
SHMMIN Min size of shared memory 1 - -
SEMMNI Number of semaphore ident 32000 0 0.00%
SEMMNS Total number of semaphore 1024000.. 0 0.00%
SEMMSL Max semaphores per semap 32000 - -
SEMOPM Max number of operations p 500 - -
SEMVMX Semaphore max value 32767 - -
```
你可能注意到,这个示例清单包含三种不同类型的 IPC 机制,每种机制在 Linux 内核中都是可用的消息MSG、共享内存SHM和信号量SEM。你可以用 `ipcs` 命令查看每个子系统的当前活动:
```
$ ipcs
\------ Message Queues Creators/Owners ---
msqid perms cuid cgid [...]
\------ Shared Memory Segment Creators/Owners
shmid perms cuid cgid [...]
557056 700 seth users [...]
3571713 700 seth users [...]
2654210 600 seth users [...]
2457603 700 seth users [...]
\------ Semaphore Arrays Creators/Owners ---
semid perms cuid cgid [...]
```
这表明当前没有消息或信号量数组,但是使用了一些共享内存段。
你可以在系统上执行一个简单的示例,这样就可以看到其中一个系统正在工作。它涉及到一些 C 代码,所以你必须在系统上有构建工具。必须安装才能从源代码构建的软件包的名称取决于发行版,因此请参考文档以获取详细信息。例如,在基于 Debian 的发行版上,你可以在 wiki 的[构建教程][3]部分了解构建需求,而在基于 Fedora 的发行版上,你可以参考文档的[从源代码安装软件][4]部分。
### 创建一个消息队列
你的系统已经有一个默认的消息队列,但是你可以使用 `ipcmk` 命令创建你自己的消息队列:
```
$ ipcmk --queue
Message queue id: 32764
```
编写一个简单的 IPC 消息发送器,为了简单,在队列 ID 中硬编码:
```
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
struct msgbuffer {
char text[24];
} message;
int main() {
int msqid = 32764;
strcpy(message.text,"opensource.com");
msgsnd(msqid, &message, sizeof(message), 0);
printf("Message: %s\n",message.text);
printf("Queue: %d\n",msqid);
return 0;
}
```
编译应用程序并运行:
```
$ gcc msgsend.c -o msg.bin
$ ./msg.bin
Message: opensource.com
Queue: 32769
```
你刚刚向消息队列发送了一条消息。你可以使用 `ipcs` 命令验证这一点,使用 `\——queue` 选项将输出限制到消息队列:
```
$ ipcs -q
\------ Message Queues --------
key msqid owner perms used-bytes messages
0x7b341ab9 0 seth 666 0 0
0x72bd8410 32764 seth 644 24 1
```
你也可以检索这些讯息:
```
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
struct msgbuffer {
char text[24];
} message;
int main() {
int msqid = 32764;
msgrcv(msqid, &amp;message, sizeof(message),0,0);
printf("\nQueue: %d\n",msqid);
printf("Got this message: %s\n", message.text);
msgctl(msqid,IPC_RMID,NULL);
return 0;
```
编译并运行:
```
$ gcc get.c -o get.bin
$ ./get.bin
Queue: 32764
Got this message: opensource.com
```
### 下载[电子书][5]
这只是 Marty Kalin 的 [Linux 内进程间通信指南][5]中课程的一个例子,这是可从 Opensource.com 下载的最新免费(和知识共享)电子书。在短短的几节课中,你将从消息队列、共享内存和信号量、套接字、信号等中了解 IPC 的 POSIX 方法。认真阅读 Marty 的书,您将成为一个消息灵通的程序员。而这不仅适用于经验丰富的编码人员,如果你编写的只是 shell 脚本,那么你将拥有有关管道(命名和未命名)和共享文件的大量实践知识,以及使用共享文件或外部消息队列时需要了解的重要概念。
如果你对制作具有动态和系统意识的优秀软件感兴趣,那么你需要了解 IPC。让[这本书][5]做你的向导。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/1/inter-process-communication-linux
作者:[Seth Kenlon][a]
选题:[lujun9972][b]
译者:[laingke](https://github.com/laingke)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/coverimage_inter-process_communication_linux_520x292.png?itok=hPoen7oI (Inter-process Communication in Linux)
[2]: https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/
[3]: https://wiki.debian.org/BuildingTutorial
[4]: https://docs.pagure.org/docs-fedora/installing-software-from-source.html
[5]: https://opensource.com/downloads/guide-inter-process-communication-linux

View File

@ -1,86 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (laingke)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Why everyone is talking about WebAssembly)
[#]: via: (https://opensource.com/article/20/1/webassembly)
[#]: author: (Mike Bursell https://opensource.com/users/mikecamel)
为什么每个人都在谈论 WebAssembly
======
了解有关在 Web 浏览器中运行任何代码的最新方法的更多信息。
![Digital creative of a browser on the internet][1]
如果你还没有听说过 [WebAssembly][2]那么你很快就会知道。这是业界保存最完好的秘密之一但无处不在。所有主流的浏览器都支持它并且它也将在服务器端使用。它很快。它被用于游戏。这是一个开放的万维网联盟W3C主要的国际网络标准组织。它与平台无关可以在 LinuxMac 和 Windows 上运行。
你可能会说:“哇,这听起来像是我应该学习编程的东西!” 你可能是对的,但你也可能是错的。你不需要用 WebAssembly 编码。让我们花一些时间来学习通常被缩写为“Wasm”的技术。
### 它从哪里来?
大约十年前,人们越来越认识到,广泛使用的 JavaScript 不够快速无法满足许多目的。JavaScript 无疑是成功和方便的。它可以在任何浏览器中运行,并启用了今天我们认为理所当然的动态网页类型。但这是一种高级语言,在设计时并没有考虑到计算密集型工作负载。
然而,尽管负责主流 web 浏览器的工程师们对性能问题的看法大体一致,但他们对如何解决这个问题却意见不一。两个阵营出现。谷歌开始了它的<ruby>原生客户端<rt>Native Client</rt></ruby>项目,后来又推出了<ruby>可移植原生客户端<rt>Portable Native Client</rt></ruby>变体,着重于允许用 C/C++ 编写的游戏和其它软件在 Chrome 的一个安全隔间中运行。与此同时Mozilla 赢得了微软对 asm.js 的支持。该方法更新了浏览器,因此它可以非常快速地运行底层的 JavaScript 指令子集(另一个项目使 C/C++ 代码可以转换为这些指令)。
在这两个阵营都没有得到广泛采用的情况下,各方在 2015 年同意围绕一种称为 WebAssembly 的新标准,以 asm.js 所采用的基本方法为基础,共同努力。[如 CNET 的 Stephen Shankland 当时所写][3],“在当今的 Web 上,浏览器的 JavaScript 将这些指令转换为机器代码。但是,通过 WebAssembly程序员可以在此过程的早期阶段完成很多工作从而生成介于两种状态之间的程序。这使浏览器摆脱了创建机器代码的繁琐工作但也实现了 Web 的承诺——该软件将在具有浏览器的任何设备上运行,而无需考虑基础硬件的详细信息。”
在 2017 年Mozilla 宣布了它的最小可行的产品,并使其脱离预览版。到该年年底,所有主流的浏览器都采用了它。[2019 年 12 月][4]WebAssembly 工作组发布了三个 W3C 推荐的 WebAssembly 规范。
WebAssembly 为可执行程序、相应的文本汇编语言以及用于促进此类程序与其主机环境之间的交互的接口定义了一种可移植的二进制代码格式。WebAssembly 代码在低级虚拟机中运行该虚拟机模仿可以在其上运行的许多微处理器的功能。通过即时JIT编译或解释WebAssembly 引擎可以以近乎原生平台编译代码的速度执行。
### 为什么现在感兴趣?
当然,最近对 WebAssembly 感兴趣的部分原因是最初希望在浏览器中运行更多计算密集型代码。尤其是笔记本电脑用户,越来越多的时间都花在浏览器上(或者,对于 Chromebook 用户来说,基本上是所有时间)。这种趋势已经迫切需要消除在浏览器中运行各种应用程序的障碍。这些障碍之一通常是性能的某些方面,这正是 WebAssembly 及其前身最初旨在解决的问题。
但是WebAssembly 并不仅仅适用于浏览器。在 2019 年,[Mozilla 宣布了一个名为 WASI][5]WebAssembly 系统接口)的项目,以标准化 WebAssembly 代码如何与浏览器上下文之外的操作系统进行交互。通过将浏览器对 WebAssembly 和 WASI 的支持结合在一起,编译后的二进制文件将能够以接近本机的速度跨不同的设备和操作系统在浏览器内外运行。
WebAssembly 的低开销立即使它可以在浏览器之外使用,但这无疑是赌注;显然,还有其它不会引入性能瓶颈的运行应用程序的方法。为什么要专门使用 WebAssembly
一个重要的原因是它的可移植性。如今,像 C++ 和 Rust 这样的广泛使用的编译语言可能是与 WebAssembly 关联最紧密的语言。但是,[各种各样的其他语言][6] 可以在 WebAssembly 中将其虚拟机编译或拥有它们的虚拟机。此外,尽管 WebAssembly 为其执行环境[假定了某些先决条件][7]但它被设计为在各种操作系统和指令集体系结构上有效执行。因此WebAssembly 代码可以使用多种语言编写,并可以在多种操作系统和处理器类型上运行。
另一个 WebAssembly 优势源于这样一个事实:代码在虚拟机中运行。因此,每个 WebAssembly 模块都在沙盒环境中执行,并使用故障隔离技术将其与主机运行时分开。这意味着,对于其它部分而言,应用程序独立于其主机环境的其余部分执行,如果不调用适当的 API就无法摆脱沙箱。
### WebAssembly 在行动
这一切在实践中意味着什么?
WebAssembly 的一个例子是 [Enarx][8]。
Enarx 是一个提供硬件独立性的项目,可使用<ruby>受信任的执行环境<rt>Trusted Execution Environments(TEE)</rt></ruby>保护应用程序的安全。Enarx 使你可以安全地将编译为 WebAssembly 的应用程序始终交付到云提供程序中,并远程执行它。正如 Red Hat 安全工程师 [Nathaniel McCallum 指出的那样][9]:“我们这样做的方式是,我们将你的应用程序作为输入,并使用远程硬件执行认证过程。我们使用加密技术验证了远程硬件实际上是它声称的硬件。最终的结果不仅是我们对硬件的信任度提高了;它也是一个会话密钥,我们可以使用它将加密的代码和数据传递到我们刚刚要求加密验证的环境中。”
另一个例子是 OPA开放政策代理它在 2019 年 11 月[发布][10],你可以[编译][11]他们的政策定义语言Rego为 WebAssembly。
Rego 允许你编写逻辑来搜索和组合来自不同来源的 JSON/YAML 数据,以询问诸如“是否允许使用此 API”之类的问题。
OPA 已被用于支持策略的软件,包括但不限于 Kubernetes。使用 OPA 之类的工具简化策略[被认为是在各种不同环境中正确保护 Kubernetes 部署的重要步骤][12]。WebAssembly 的可移植性和内置的安全功能非常适合这些工具。
我们的最后一个例子是 [Unity][13]。还记得我们在文章开头提到过 WebAssembly 用于游戏吗?好吧,跨平台游戏引擎 Unity 是 WebAssembly 的较早采用者,它提供了在浏览器中运行的 Wasm 的首个演示,并且自 2018 年 8 月以来,[已使用 WebAssembly][14]作为 Unity WebGL 构建目标的输出目标。
这些只是 WebAssembly 已经开始产生影响的几种方式。你可以在 <https://webassembly.org/> 上查找更多信息并了解 Wasm 的所有最新信息。
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/1/webassembly
作者:[Mike Bursell][a]
选题:[lujun9972][b]
译者:[laingke](https://github.com/laingke)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/mikecamel
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/browser_web_internet_website.png?itok=g5B_Bw62 (Digital creative of a browser on the internet)
[2]: https://opensource.com/article/19/8/webassembly-speed-code-reuse
[3]: https://www.cnet.com/news/the-secret-alliance-that-could-give-the-web-a-massive-speed-boost/
[4]: https://www.w3.org/blog/news/archives/8123
[5]: https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/
[6]: https://github.com/appcypher/awesome-wasm-langs
[7]: https://webassembly.org/docs/portability/
[8]: https://enarx.io
[9]: https://enterprisersproject.com/article/2019/9/application-security-4-facts-confidential-computing-consortium
[10]: https://blog.openpolicyagent.org/tagged/webassembly
[11]: https://github.com/open-policy-agent/opa/tree/master/wasm
[12]: https://enterprisersproject.com/article/2019/11/kubernetes-reality-check-3-takeaways-kubecon
[13]: https://opensource.com/article/20/1/www.unity.com
[14]: https://blogs.unity3d.com/2018/08/15/webassembly-is-here/