# WebAssembly 系统接口

WASI 是 [WebAssembly](https://www.edgepedia.org/zh/webassembly) 系统接口（WebAssembly System Interface）的缩写，由一组处于标准轨道的 API 规范组成，面向为万维网联盟（W3C）的 WebAssembly（Wasm）标准构建的程序，为这些程序提供在任何环境中安全、统一运行的方式——无论是浏览器、云基础设施还是嵌入式硬件。<sup>[1](https://wasi.dev/)</sup> 由于由任何语言编译而成的 Wasm 模块都无法知道它将在哪个操作系统上运行，它不能使用任何单一操作系统的系统接口；WASI 转而为一种概念性操作系统定义接口，由每个宿主运行时为自己的平台实现。<sup>[2](https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/)</sup>

| 关键事实 | 详情 |
|---|---|
| 它是什么 | 面向 Wasm 软件的标准轨道 API 规范，由 W3C WebAssembly 社区组的 WASI 子组制定<sup>[1](https://wasi.dev/)</sup> |
| 安全模型 | 基于能力：模块或组件初始不具备任何环境权限，只能执行宿主明确授予的操作<sup>[3](https://wasi.dev/security)</sup> |
| 版本 | 迄今有三个里程碑版本：0.1、0.2 和 0.3（也称 Preview 1/2/3）<sup>[1](https://wasi.dev/)</sup> |
| WASI 0.2 | 2024 年 1 月 25 日由 WASI 子组投票确定为稳定版；将 WASI 重新建立在组件模型和 WIT 接口描述语言之上<sup>[4](https://bytecodealliance.org/articles/WASI-0.2)</sup> |
| WASI 0.3 | 2026 年 6 月 11 日发布；通过 `stream<T>` 和 `future<T>` 为组件模型加入原生异步，并移除 `wasi:io` 包<sup>[5](https://github.com/bytecodealliance/wasi.dev/blob/main/docs/releases/wasi-p3.md)</sup> |
| 运行时 | Wasmtime、WAMR、WasmEdge、wazero、Wasmer、wasmi、wasm3、jco、pywasm 和 zwasm 等<sup>[1](https://wasi.dev/)</sup> |
| 工具链 | wasi-sdk 提供支持 WASI 的 C/C++ 工具链，其默认目标是 `wasm32-wasip1`<sup>[6](https://github.com/webassembly/wasi-sdk)</sup> |

## 目的与设计

WebAssembly 是一种面向概念性机器而非真实机器的汇编语言，因此它需要的是一套面向概念性操作系统的系统接口，而不是面向 Linux、Windows 或 macOS 的接口。最初的提案要求建立一组模块化的标准接口，从最基础的模块 wasi-core 开始，覆盖与 POSIX 大致相同的领域：文件、网络连接、时钟和随机数，并采用 POSIX 面向文件的方法，使用 `open`、`close`、`read` 和 `write` 等调用。<sup>[2](https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/)</sup> wasi-core 有意不覆盖 POSIX 的所有内容；进程概念无法清晰地映射到 WebAssembly 上，而且并非每个引擎都需要 `fork` 之类的操作，不过模块化方法为单独标准化这类内容留出了空间。<sup>[2](https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/)</sup>

运行时以导入形式将 WASI 函数传给模块，每个宿主再为自己的平台实现这些函数，这正是可移植性的来源：同一个 Wasm 二进制文件可以运行在不同的运行时和操作系统上。<sup>[2](https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/)</sup> 各语言通过其标准库使用 WASI；例如，[Rust](https://www.edgepedia.org/zh/rust-programming-language) 的 `open` 在编译为 WebAssembly 时会调用 `__wasi_path_open`，而 C 和 C++ 使用 wasi-sysroot，以 WASI 函数实现 libc。<sup>[2](https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/)</sup>

## 基于能力的安全

WASI 建立在[基于能力的安全](https://www.edgepedia.org/zh/capability-based-security)之上。WASI 代码在没有环境权限的情况下运行，这意味着 WebAssembly 模块或组件一开始对外部世界没有任何访问权，只能执行宿主明确允许的操作。这与 Linux 或 Windows 等系统的环境权限模式不同：在后一种模式中，进程会获得其用户范围广泛的权利，并且除非额外叠加沙箱，否则可以访问该用户能够访问的任何内容。WASI 默认拒绝，更准确地说，默认不授予任何能力句柄。<sup>[3](https://wasi.dev/security)</sup>

每个版本授予能力的方式不同。WASI 0.1 模块会引入 `wasi_snapshot_preview1` 函数，但只能对运行时在启动它们时提供的资源进行操作，主要是预打开文件描述符——`fd_read` 只对交给模块的描述符有效，能力是在运行时调用模块时分配的，而不是在二进制文件中分配的。WASI 0.2 组件在 WIT 中显式声明能力：每一种能力（文件系统访问、网络、环境、时钟、随机数）都是单独的导入项，宿主只使用其批准的那些导入项来实例化组件，因此组件二进制文件的能力面可以静态检查。WASI 0.3 更进一步：0.2 要求客体程序在每次调用时显式传入某些能力——例如提供给 `bind`、`connect` 和 DNS 解析的 `wasi:sockets/network` 句柄——而 0.3 在世界层面隐式处理这些能力，同时宿主继续在世界边界授予网络访问权。<sup>[3](https://wasi.dev/security)</sup>

在实践中，没有文件系统导入项的组件无法读取文件，没有网络导入项的组件无法打开套接字，没有预打开文件描述符的模块根本无法访问文件系统。无论宿主进程本身被允许做什么，这些保证都成立。<sup>[3](https://wasi.dev/security)</sup>

## 发布历史

**WASI 0.1。** 第一个里程碑为 WebAssembly 模块提供了面向外部世界的类 POSIX 接口。<sup>[7](https://www.systemshardening.com/articles/wasm/wasi-preview2-capability-isolation/)</sup> WASI 0.1 的许多内容直接源自 CloudABI，这是一个将基于能力的安全与一小组精心设计的 Unix 风格原语结合起来的系统。<sup>[4](https://bytecodealliance.org/articles/WASI-0.2)</sup> WASI 0.1 现在被归类为遗留的、受 POSIX 启发的模块 API，拥有广泛的运行时支持。<sup>[8](https://wasi.dev/releases?trk=article-ssr-frontend-pulse_little-text-block)</sup>

**WASI 0.2。** WASI 子组于 2024 年 1 月 25 日投票启动 WASI 0.2，宣布其 API 稳定。<sup>[4](https://bytecodealliance.org/articles/WASI-0.2)</sup> 在此版本中，WASI 完全重建在组件模型和用于描述接口的 WebAssembly 接口类型（WIT）语言之上，取代了 WASI 0.1 中类似 C 的 WITX IDL。<sup>[9](https://wasi.dev/releases/wasi-p2)</sup> API 在 WIT 中定义，并作为 WebAssembly 组件使用，从而实现模块化、可虚拟化（用一种实现补足另一种实现的功能）和跨语言互操作：由 Rust 编译的组件可以与由 Go、[JavaScript](https://www.edgepedia.org/zh/javascript)、Python 或其他语言借助组件工具编译的组件通信或组合。<sup>[9](https://wasi.dev/releases/wasi-p2)</sup> WASI 0.2 定义了两个标准世界：用于命令行程序的 `wasi:cli/command` 和用于 HTTP 代理的 `wasi:http/proxy`，并通过 `wasi:io` 包以 `pollable` 资源、输入流和输出流以及 `poll` 函数来建模异步 I/O。<sup>[9](https://wasi.dev/releases/wasi-p2)</sup> [Wasmtime](https://www.edgepedia.org/zh/wasmtime) 和 jco 在认证时满足 WASI 0.2 的可移植性标准，WAMR 和 WasmEdge 则提供不同程度的支持。<sup>[9](https://wasi.dev/releases/wasi-p2)</sup>

**WASI 0.3。** WASI 0.3.0 于 2026 年 6 月 11 日发布。<sup>[5](https://github.com/bytecodealliance/wasi.dev/blob/main/docs/releases/wasi-p3.md)</sup> WASI 0.3 将异步引入为 WebAssembly 组件的原生特性：过去由 `wasi:io` 包处理的能力，通过 `stream<T>`、`future<T>` 和 `async func` 原语转移到组件模型本身；整个 `wasi:io` 包随后被移除，不再出现在任何 WASI 0.3 版本中。<sup>[5](https://github.com/bytecodealliance/wasi.dev/blob/main/docs/releases/wasi-p3.md)</sup> 其动机是可组合性：在 WASI 0.2 中，像 A → B → 宿主 这样的调用链里的组件 B 无法将来自宿主 `pollable` 的唤醒信号转发给组件 A，因为 `pollable` 的作用域限于单个组件实例，所以 B 不得不主动轮询，仅仅为了转达就绪信号。<sup>[5](https://github.com/bytecodealliance/wasi.dev/blob/main/docs/releases/wasi-p3.md)</sup> WASI 0.3 还围绕统一的 `request` 和 `response` 资源重构了 HTTP，并定义了新的世界，包括带有 `async func run()` 入口点的 `wasi:cli/command`，以及取代 `wasi:http/proxy` 的 `wasi:http/service`。<sup>[5](https://github.com/bytecodealliance/wasi.dev/blob/main/docs/releases/wasi-p3.md)</sup>

## 标准化与治理

WASI 是一项开放标准，由 W3C WebAssembly 社区组中的 WASI 子组积极开发，讨论通过 GitHub 议题、拉取请求和每两周一次的会议进行。<sup>[1](https://wasi.dev/)</sup> API 以提案形式开发，经历从预提案到标准化的五个开发阶段；提案在达到第 5 阶段之前一直处于 0.x 的 semver 范围内，达到第 5 阶段时应当发布 1.0 版本。<sup>[10](https://github.com/WebAssembly/WASI/blob/main/docs/Proposals.md)</sup> 要纳入 WASI 0.3，提案必须达到第 3 阶段、满足自身的可移植性标准，并由 WASI 子组投票同意纳入。<sup>[10](https://github.com/WebAssembly/WASI/blob/main/docs/Proposals.md)</sup> 活跃的第 3 阶段提案包括时钟、随机数、文件系统、套接字、命令行接口（CLI）和 HTTP，第 2 阶段提案则涵盖键值存储、机器学习（wasi-nn）、WebGPU 和消息传递等领域。<sup>[10](https://github.com/WebAssembly/WASI/blob/main/docs/Proposals.md)</sup> WASI 尚未达到 1.0 版本的稳定状态。<sup>[11](https://github.com/WebAssembly/wasi-testsuite)</sup>

字节码联盟（Bytecode Alliance）是一个行业合作组织，由创始成员 Mozilla、Fastly、英特尔（Intel）和红帽（Red Hat）于 2019 年 11 月 12 日宣布成立，致力于合作实现标准并为 WebAssembly 在浏览器之外的未来提出新标准；其项目包括 Wasmtime 运行时和 wasi-common——一个可供运行时使用的 WASI 独立实现。<sup>[12](https://bytecodealliance.org/articles/announcing-the-bytecode-alliance)</sup>

## 运行时与工具

许多运行时支持 WASI，但侧重点不同：WAMR 面向物联网（IoT）、嵌入式设备和边缘；Wasmtime 面向服务器端以及使用组件的非 Web 嵌入场景；jco 在 JavaScript 环境和浏览器中运行组件。<sup>[1](https://wasi.dev/)</sup> WASI 的实现既以核心 Wasm 模块的形式存在，也以遵循组件模型的应用形式存在；组件模型是一项面向 Wasm 应用的规范，旨在实现互操作和可组合。<sup>[1](https://wasi.dev/)</sup>

对于 C 和 C++，wasi-sdk 提供支持 WASI 的工具链，目标包括 `wasm32-wasip1`（默认）、`wasm32-wasip2`、`wasm32-wasip3` 和 `wasm32-wasip1-threads`，最后一个通过 wasi-threads 提案提供实验性线程创建。<sup>[6](https://github.com/webassembly/wasi-sdk)</sup> 值得注意的限制包括默认禁用 C++ 异常，以及大多数目标不支持线程创建。<sup>[6](https://github.com/webassembly/wasi-sdk)</sup> 一个 WASI 测试套件带有测试执行器，可针对选定的运行时运行测试，覆盖 Preview 1 和即将推出的 Preview 3。<sup>[11](https://github.com/WebAssembly/wasi-testsuite)</sup>

## 应用

WASI 的可移植性和沙箱能力使其成为在浏览器之外运行 Wasm 的常见基础。编译为 `wasm32-wasi` 的无服务器边缘函数受益于沙箱化执行和语言无关性，一个基于基准测试的成本模型报告称，在相同硬件上，与传统容器化无服务器相比，每次调用成本降低了 68%。<sup>[13](https://votioncloud.org/blog/benchmarking-wasm-serverless-edge-functions-2467)</sup> 研究项目将 WASI 扩展到其他领域，包括通过 Wasm-bpf 在异构云中部署 [eBPF](https://www.edgepedia.org/zh/ebpf) 程序，以及为信息物理系统提供硬件接口。<sup>[15](https://arxiv.org/abs/2408.04856)</sup><sup> • </sup><sup>[16](https://arxiv.org/abs/2410.22919)</sup>

## References

1. [Introduction · WASI.dev](https://wasi.dev/)
2. [Standardizing WASI: A system interface to run WebAssembly outside the web, Mozilla Hacks (2019)](https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/)
3. [Security · WASI.dev](https://wasi.dev/security)
4. [WASI 0.2 Launched, Bytecode Alliance (January 25, 2024)](https://bytecodealliance.org/articles/WASI-0.2)
5. [WASI 0.3 release notes, wasi.dev on GitHub](https://github.com/bytecodealliance/wasi.dev/blob/main/docs/releases/wasi-p3.md)
6. [WebAssembly/wasi-sdk: WASI-enabled WebAssembly C/C++ toolchain, GitHub](https://github.com/webassembly/wasi-sdk)
7. [WASI Preview 2 and the Component Model: What Capability-Based Isolation Actually Prevents, Systemshardening](https://www.systemshardening.com/articles/wasm/wasi-preview2-capability-isolation/)
8. [Releases · WASI.dev](https://wasi.dev/releases?trk=article-ssr-frontend-pulse_little-text-block)
9. [WASI 0.2 · WASI.dev](https://wasi.dev/releases/wasi-p2)
10. [WASI/docs/Proposals.md, WebAssembly/WASI on GitHub](https://github.com/WebAssembly/WASI/blob/main/docs/Proposals.md)
11. [WebAssembly/wasi-testsuite, GitHub](https://github.com/WebAssembly/wasi-testsuite)
12. [Announcing the Bytecode Alliance, Bytecode Alliance (November 12, 2019)](https://bytecodealliance.org/articles/announcing-the-bytecode-alliance)
13. [Benchmarking WASM Serverless Edge Functions, Votion Cloud](https://votioncloud.org/blog/benchmarking-wasm-serverless-edge-functions-2467)
14. [Wasm, Envoy documentation](https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/wasm)
15. [Wasm-bpf: Streamlining eBPF Deployment in Cloud Environments with WebAssembly, arXiv](https://arxiv.org/abs/2408.04856)
16. [Cyber-physical WebAssembly: Secure Hardware Interfaces and Pluggable Drivers, arXiv](https://arxiv.org/abs/2410.22919)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/webassembly-system-interface

English original: https://www.edgepedia.org/webassembly-system-interface
