# Wasmtime

**Wasmtime** 是 [WebAssembly](https://www.edgepedia.org/zh/webassembly)（Wasm）、WASI 和组件模型的独立运行时，由字节码联盟（Bytecode Alliance）开发。它在 Web 之外运行 WebAssembly 代码，既可作为命令行工具使用，也可作为库嵌入更大的应用程序中，以 [Rust](https://www.edgepedia.org/zh/rust-programming-language) 编写，提供安全的公共 API。<sup>[1](https://docs.wasmtime.dev/introduction.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | WebAssembly、WASI 和组件模型的独立运行时，可用作命令行工具或嵌入式库<sup>[1](https://docs.wasmtime.dev/introduction.html)</sup> |
| 维护者 | 字节码联盟，由 Mozilla、Fastly、Intel 和 Red Hat 于 2019 年 11 月 12 日创立的行业合作组织<sup>[2](https://bytecodealliance.org/articles/announcing-the-bytecode-alliance)</sup> |
| 编译器 | 默认使用 Cranelift 优化编译器；Winch 基线编译器用于快速编译；也可选用 Pulley 解释器<sup>[1](https://docs.wasmtime.dev/introduction.html)</sup><sup> • </sup><sup>[3](https://docs.wasmtime.dev/examples-fast-compilation.html)</sup> |
| 1.0 版本 | 2022 年 9 月 20 日发布，此前联盟成员已在生产环境中使用约一年<sup>[4](https://bytecodealliance.org/articles/wasmtime-1-0-fast-safe-and-production-ready)</sup> |
| 发布节奏 | 每月 20 日发布新主版本；LTS 版本（每 12 个版本一次）支持 24 个月，普通版本支持 2 个月<sup>[5](https://docs.wasmtime.dev/stability-release.html)</sup> |
| 启动速度 | Wasmtime 可在 5 微秒内创建新的 Wasm 实例，而最快的替代方案——JS 隔离环境——约需 5 毫秒<sup>[4](https://bytecodealliance.org/articles/wasmtime-1-0-fast-safe-and-production-ready)</sup> |
| 中断机制 | 确定性的燃料限制或非确定性的纪元限制，实测性能损失约 10%<sup>[6](https://docs.wasmtime.dev/examples-interrupting-wasm.html)</sup> |
| 语言嵌入 | Python、Go、.NET、Java、Perl、Rust、Zig 和 Ruby<sup>[7](https://bytecodealliance.org/articles/1-year-update)</sup> |

## 目的与背景

WebAssembly 是一种二进制指令格式，设计为编程语言的可移植编译目标；二进制文件通常使用 `.wasm` 扩展名，文本表示使用 `.wat`。<sup>[1](https://docs.wasmtime.dev/introduction.html)</sup> Wasmtime 在浏览器之外执行此类程序。WASI（[WebAssembly 系统接口](https://www.edgepedia.org/zh/webassembly-system-interface)）定义了一组接口，使 Wasm 程序能够安全、可移植地访问类操作系统功能，如文件系统、网络、时钟和随机数。<sup>[1](https://docs.wasmtime.dev/introduction.html)</sup> 组件模型是一种 Wasm 架构，通过接口提供可移植的跨语言组合的二进制格式，WASI 即基于组件模型接口定义。<sup>[1](https://docs.wasmtime.dev/introduction.html)</sup>

该项目提出四个目标：速度——通过优化的 Cranelift 代码生成器实现；安全——开发重点聚焦于正确性；可配置性——对 CPU 和内存消耗进行细粒度控制；标准合规——通过官方 WebAssembly 测试套件，开发者积极参与标准制定过程。<sup>[1](https://docs.wasmtime.dev/introduction.html)</sup>

## 编译

Wasmtime 必须先编译 Wasm 程序才能执行。共有三种执行策略可供选择。Cranelift 默认启用，是在运行时生成原生代码的优化编译器。<sup>[3](https://docs.wasmtime.dev/examples-fast-compilation.html)</sup><sup> • </sup><sup>[8](https://docs.wasmtime.dev/api/wasmtime/)</sup> Winch 是基线编译器：对每个 Wasm 操作码，它在一趟快速处理中发射预置的机器指令序列，编译速度快于 Cranelift，但由于不做任何优化，生成的代码运行较慢。<sup>[3](https://docs.wasmtime.dev/examples-fast-compilation.html)</sup> 配置的执行策略中还包括 Pulley 解释器。<sup>[9](https://docs.wasmtime.dev/examples-pre-compiling-wasm.html)</sup>

编译可以通过多种方式移出关键路径。编译缓存让 Wasmtime 复用之前编译过的程序结果。<sup>[3](https://docs.wasmtime.dev/examples-fast-compilation.html)</sup> 程序可以在一台机器上预编译为原生代码，然后在另一台机器上运行，以惯用的 `.cwasm` 扩展名存储为文件；这可以加快启动速度，通过惰性 `mmap` 代码页降低内存占用，并允许在更小的嵌入式部署中构建不含任何编译器的版本，从而减小攻击面。Wasmtime 不会对预编译模块进行完整的安全验证，因此警告嵌入者只应反序列化自己控制且信任的字节。<sup>[9](https://docs.wasmtime.dev/examples-pre-compiling-wasm.html)</sup>

## 安全模型

Wasmtime 的核心目标是在沙箱内安全地执行不受信任的代码。核心 WebAssembly 规范本身提供了大部分隔离能力：调用栈不可访问，指针编译为线性内存中的偏移量且所有访问都经过边界检查，控制转移只能到达已知且经过类型检查的目标，与外部世界的所有交互都通过导入和导出进行，规范不允许任何未定义行为。<sup>[10](https://docs.wasmtime.dev/security.html)</sup>

由于运行时本身仍可能存在缺陷，Wasmtime 增加了纵深防御缓解措施。线性内存默认前置 2 GB 的保护区域，以防止 Cranelift 中意外的符号扩展缺陷；保护页为原生线程栈提供保护；已完成实例使用的内存在可能的情况下清零，以防止实例间的信息泄漏；选择 Rust 作为实现语言可在编译期捕获错误。公共 API 保证不会发生段错误，因此即使嵌入代码存在缺陷，也能保持 WebAssembly 的安全保证。<sup>[10](https://docs.wasmtime.dev/security.html)</sup>

该项目精确界定了什么算安全缺陷：任何破坏 Wasm 执行沙箱的问题、宿主中的任何内存不安全问题，以及 Wasm 执行期间的服务拒绝，例如在异步燃料机制下永不让出的无限循环。仅仅偏离 Wasm 语义的执行——例如在沙箱内计算出错误的值——不被视为安全漏洞，但警告嵌入者永远不要盲目信任客体程序的值。<sup>[11](https://docs.wasmtime.dev/security-what-is-considered-a-security-vulnerability.html)</sup>

WASI 文件系统访问遵循基于能力的安全模型，应用程序只能访问已被授予访问权限的文件和目录。更广泛地说，WASI 没有环境权限：运行时没有全局命名空间，链接时没有全局函数，所有对外部资源的访问都通过能力提供。<sup>[10](https://docs.wasmtime.dev/security.html)</sup><sup> • </sup><sup>[12](https://github.com/WebAssembly/WASI/blob/main/docs/DesignPrinciples.md)</sup> Wasmtime 还会过滤写入终端连接输出流的 ANSI 转义序列，将其转换为无害的替代内容，因为此类序列否则可能写入文件、执行命令或误导用户。<sup>[10](https://docs.wasmtime.dev/security.html)</sup>

## 控制执行

为防止客体程序的无限循环无限期阻塞宿主，Wasmtime 提供两种中断机制，每种都可以选择抛出陷阱（不可恢复地终止程序）或执行异步让出（暂停程序，由宿主决定是否恢复）。<sup>[6](https://docs.wasmtime.dev/examples-interrupting-wasm.html)</sup>

基于燃料的中断是完全确定性的：同一程序使用相同数量的燃料运行时，总是在同一位置被中断，但比纪元机制带来更多的执行开销。<sup>[6](https://docs.wasmtime.dev/examples-interrupting-wasm.html)</sup> 基于纪元的中断更快，实测性能损失约 10%，但非确定性，因为它基于挂钟时间而非精确的指令计数；同一程序在相同输入下可能在不同位置被中断，也可能成功完成。<sup>[6](https://docs.wasmtime.dev/examples-interrupting-wasm.html)</sup>

## 性能调优

为实现快速实例化，Wasmtime 提供池分配器，预先分配一组内存、表和实例资源，这样创建实例时从池中取用预分配资源，而非按需映射新内存；该功能默认未启用。<sup>[13](https://docs.wasmtime.dev/examples-fast-instantiation.html)</sup><sup> • </sup><sup>[14](https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html)</sup> [写时复制](https://www.edgepedia.org/zh/copy-on-write)堆映像将内存初始化成本从实例化时推迟到首次修改时，当模块只读取其初始数据时完全避免复制。<sup>[13](https://docs.wasmtime.dev/examples-fast-instantiation.html)</sup> `InstancePre` 表示一个导入已解析并经过类型检查的模块，实例化时只需分配资源和执行启动函数。<sup>[13](https://docs.wasmtime.dev/examples-fast-instantiation.html)</sup>

为实现快速编译，文档中记录的选项包括编译缓存、Winch 基线策略和并行编译，后者在平台支持的情况下通常默认启用。<sup>[3](https://docs.wasmtime.dev/examples-fast-compilation.html)</sup>

## 可移植性

Wasmtime 经过重构以尽量减少平台假设。它现在是一个 `no_std` Rust 包，唯一必需的平台要求是全局内存分配器；不再需要 `mmap`、操作系统或虚拟内存，可以配置为依赖显式边界检查而非保护页。<sup>[15](https://bytecodealliance.org/articles/wasmtime-portability)</sup> 该项目承诺遵循开放标准，并表示 Wasmtime 不会也永远不会实现特设的、非标准的 Wasm 扩展。<sup>[15](https://bytecodealliance.org/articles/wasmtime-portability)</sup>

## WASI 与组件模型

Wasmtime 一直是 WASI 的早期实现者。2023 年，该项目为 Preview 2 编写了新的 WASI 实现，通过组件模型以资源类型、底层调度接口（pollables）和流进行分解；Preview 1 在其之上重新实现，自 Wasmtime 15 起默认启用。已实现的 Preview 2 提案包括 `wasi-io`、`wasi-random`、`wasi-clocks`、`wasi-filesystem`、`wasi-sockets`、`wasi-cli` 和 `wasi-http`。<sup>[16](https://bytecodealliance.org/articles/wasmtime-and-cranelift-in-2023)</sup> 命令行工具新增了 `wasmtime serve`，启动本地 HTTP 服务器并针对每个请求运行面向 `wasi:http/proxy` 世界的组件；`wasmtime run` 可以执行使用 `wasi:cli/command` 世界的组件。<sup>[16](https://bytecodealliance.org/articles/wasmtime-and-cranelift-in-2023)</sup>

组件模型提案在 WebAssembly 标准化进程中尚未达到第 4 阶段，但在 Wasmtime 中默认启用。<sup>[17](https://docs.wasmtime.dev/stability-wasm-proposals.html)</sup> `wasmtime-wasi` Rust 包中的 WASI Preview 3 支持是实验性的、不稳定的且不完整的。<sup>[18](https://docs.wasmtime.dev/api/wasmtime_wasi/index.html)</sup>

## 发布流程与采用

Wasmtime 于 2022 年 9 月 20 日达到 1.0 版本。每月 20 日发布新的主版本，版本号在 wasmtime-py 等语言嵌入中保持同步。版本号为 12 的倍数的发布是长期支持版本，支持 24 个月；其他版本支持 2 个月。安全缺陷的修复保证向后移植到所有受支持的版本，Cranelift 误编译的补丁版本即使对 Wasmtime 自身未涉及的情况也会发布。<sup>[4](https://bytecodealliance.org/articles/wasmtime-1-0-fast-safe-and-production-ready)</sup><sup> • </sup><sup>[5](https://docs.wasmtime.dev/stability-release.html)</sup>

生产用户报告了 1.0 里程碑前后的成果：Shopify 于 2021 年 7 月切换后，平均执行性能提升约 50%；Fastly 于 2022 年 3 月切换后，每秒请求数增长 72% 至 163%，此后已处理数万亿次请求；DFINITY 的 Internet Computer 为超过 150,000 个智能合约执行了 10<sup>18</sup> 条指令，未出现生产问题；Fermyon 的 Spin 在单个实例中运行了数万个 Wasm 二进制文件，启动时间低于 1 毫秒。<sup>[4](https://bytecodealliance.org/articles/wasmtime-1-0-fast-safe-and-production-ready)</sup> 2025 年 4 月，Wasmtime 被提升为字节码联盟的首个核心项目，该项目现在发布 LTS 版本并维护安全修复两年。<sup>[19](https://bytecodealliance.org/articles/)</sup>

## References

1. [Introduction - Wasmtime documentation](https://docs.wasmtime.dev/introduction.html)
2. [Announcing the Bytecode Alliance, Bytecode Alliance (November 12, 2019)](https://bytecodealliance.org/articles/announcing-the-bytecode-alliance)
3. [Tuning Wasmtime for Fast Compilation - Wasmtime documentation](https://docs.wasmtime.dev/examples-fast-compilation.html)
4. [Wasmtime Reaches 1.0: Fast, Safe and Production Ready!, Bytecode Alliance (September 20, 2022)](https://bytecodealliance.org/articles/wasmtime-1-0-fast-safe-and-production-ready)
5. [Release Process - Wasmtime documentation](https://docs.wasmtime.dev/stability-release.html)
6. [Interrupting Wasm Execution - Wasmtime documentation](https://docs.wasmtime.dev/examples-interrupting-wasm.html)
7. [bytecode alliance — bytecode alliance:one year update](https://bytecodealliance.org/articles/1-year-update)
8. [wasmtime crate API documentation, docs.rs](https://docs.wasmtime.dev/api/wasmtime/)
9. [Pre-Compiling and Cross-Compiling WebAssembly Programs - Wasmtime documentation](https://docs.wasmtime.dev/examples-pre-compiling-wasm.html)
10. [Security - Wasmtime documentation](https://docs.wasmtime.dev/security.html)
11. [What is considered a security vulnerability? - Wasmtime documentation](https://docs.wasmtime.dev/security-what-is-considered-a-security-vulnerability.html)
12. [WASI Design Principles, WebAssembly/WASI GitHub repository](https://github.com/WebAssembly/WASI/blob/main/docs/DesignPrinciples.md)
13. [Tuning Wasmtime for Fast Instantiation - Wasmtime documentation](https://docs.wasmtime.dev/examples-fast-instantiation.html)
14. [PoolingAllocationConfig in wasmtime, docs.rs](https://docs.wasmtime.dev/api/wasmtime/struct.PoolingAllocationConfig.html)
15. [Making WebAssembly and Wasmtime More Portable, Bytecode Alliance](https://bytecodealliance.org/articles/wasmtime-portability)
16. [Wasmtime and Cranelift in 2023, Bytecode Alliance](https://bytecodealliance.org/articles/wasmtime-and-cranelift-in-2023)
17. [Wasm Proposals - Wasmtime documentation](https://docs.wasmtime.dev/stability-wasm-proposals.html)
18. [wasmtime_wasi crate documentation, docs.rs](https://docs.wasmtime.dev/api/wasmtime_wasi/index.html)
19. [Bytecode Alliance articles index](https://bytecodealliance.org/articles/)

---
*Initially written Oct 15, 2026 · Edited: Oct 25, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Named software products and platforms › Cloud, virtualization, and containers › API management and serverless

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/wasmtime

English original: https://www.edgepedia.org/wasmtime
