pub struct WasmLimits {
pub memory_bytes: usize,
pub fuel_per_call: u64,
pub max_input_bytes: usize,
pub max_output_bytes: usize,
pub table_elements: usize,
pub max_element_bytes: usize,
pub max_module_bytes: usize,
}Expand description
Resource budget applied to every Wasm call. 每次 Wasm 调用使用的资源预算。
Fields§
§memory_bytes: usizeLinear-memory ceiling per instance, in bytes; growth beyond it traps. 每个实例的线性内存上限(字节);超出即触发 trap。
fuel_per_call: u64Fuel granted to each call; an exhausted call traps. 每次调用授予的燃料;燃料耗尽时调用触发 trap。
The unit belongs to the engine, not to this host: it is a bound on how much work a call may do, not a promise that a given number buys a given amount of work. A major engine upgrade can re-scale it, so a host that tuned this value against an older engine should re-measure rather than assume the same number still fits. 该单位属于引擎而不属于本宿主:它是“一次调用最多做多少工作“的上限,而不是“某个数值 能买到多少工作“的承诺。引擎大版本升级可能重新标定它,因此针对旧引擎调过这个值的宿主 应当重新实测,而不是假设同一个数字仍然够用。
max_input_bytes: usizeLargest request payload accepted, in bytes — but the host writes the input at
offset 0 of the plugin’s initial memory before calling the handler, so the real
ceiling is the smaller of this and that memory. A one-page plugin therefore
refuses every input above 64 KiB however large this number is; a plugin that needs
more declares it in its initial memory (or grows it from start).
接受的最大请求负载字节数——但宿主在调用处理函数之前把输入写在插件初始内存的偏移 0
处,因此真正的上限是这个值与那块内存中较小的一个。于一页内存的插件无论这里多大都会拒绝
超过 64 KiB 的输入;需要更多输入的插件应在初始内存里声明(或在 start 里增长)。
max_output_bytes: usizeLargest response payload accepted, in bytes. 接受的最大响应负载字节数。
table_elements: usizeLargest number of table elements a module may instantiate. 模块可实例化的表元素上限。
The memory ceiling does not bound a table: a table is a separate array of
function references, instantiated eagerly, so a module that declares
(table 100000000 funcref) costs hundreds of megabytes of host memory
without touching a single memory page. This is the ceiling for that array,
and a module over it fails to instantiate rather than being honoured.
内存上限并不约束表:表是一块独立的函数引用数组,会即时实例化,因此声明
(table 100000000 funcref) 的模块会花掉宿主数百兆内存,而一页线性内存都没碰。
这里是那块数组的上限,超过它的模块实例化失败,而不是被照办。
Measured rather than estimated: a function reference costs the host 8 bytes
(plugin-host/tests/wasm_table_cost.rs), so the default ceiling of 4096 is
32 KiB and the hundred-million-entry module above would be 762 MiB. The
limiter denies the allocation before the table exists, so refusing it was
measured at 5 KiB of peak allocation, not 762 MiB. This field is the only
bound on a single table’s size: wasmi’s EnforcedLimits::strict(), which
WasmBackend::load also applies, caps how many tables a module may declare
(max_tables) and how many element segments it may carry
(max_element_segments), but not how large one table may grow.
实测而非估计:一个函数引用在宿主一侧占 8 字节(plugin-host/tests/wasm_table_cost.rs),
因此默认上限 4096 是 32 KiB,而上面那个一亿条目的模块本来会是 762 MiB。限制器在表存在
之前就拒绝这次分配,因此拒绝它的实测峰值是 5 KiB,而不是 762 MiB。本字段是单张表大小的
唯一约束:wasmi 的 EnforcedLimits::strict()(WasmBackend::load 也会施加)限制的是一个
模块可以声明多少张表(max_tables)与多少个元素段(max_element_segments),而不是
单张表能长到多大。
max_element_bytes: usizeLargest total element-section payload a module may carry, in bytes. 模块可携带的元素段负载总量上限,以字节计。
A passive element segment is invisible to the two ceilings that look like
they cover it: table_elements bounds a table’s growth and a passive
segment never grows one, while wasmi’s EnforcedLimits::strict() caps how
many element segments a module may declare, not how many entries they
carry. wasmi materializes every entry at instantiation, measured at about
32 bytes per entry against about one byte per entry in the compact
encoding — a 2 000 103-byte module carrying two million entries cost
64 070 402 bytes of host memory, with (table 1 funcref) and default
limits. This budget is therefore also an allocation budget of roughly
thirty-two times it, which is why the default caps that shape at about
8 MiB. The payload is measured from the binary’s section headers before
compilation, the same hand-checked seam max_module_bytes uses, because no
engine limit applies before the engine runs.
被动元素段对两道看起来覆盖它的上限都不可见:table_elements 约束的是表的增长,
而被动段从不增长表;wasmi 的 EnforcedLimits::strict() 限制的是一个模块可以声明多少个
元素段,而不是它们携带多少条目。wasmi 在实例化时为每个条目物化约 32 字节,而紧凑编码
下每条约一字节——一个 2 000 103 字节、带两百万条目的模块,在 (table 1 funcref) 与默认
上限下花掉 64 070 402 字节宿主内存。因此本预算同时也是约三十二倍的分配预算,这正是默认值
把那种形状压在约 8 MiB 的原因。负载在编译前从二进制的段头量出,与 max_module_bytes
用的是同一处手工检查接缝,因为在引擎运行之前没有任何引擎限制生效。
max_module_bytes: usizeLargest artifact the backend will compile, in bytes. 后端愿意编译的最大工件字节数。
Compilation happens before any limit below can apply, so this is the one bound that has to be checked by hand; it is what keeps a huge artifact from spending the host’s memory and time before the sandbox is even entered. 编译发生在下面任何限制生效之前,因此这是唯一必须手工检查的上限;正是它阻止一个巨大 工件在沙箱都没进入之前就花掉宿主的内存与时间。