Wasmtime: enforce function-body-size implementation limit. #11690
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In #11682 we see a module with an extremely large single function body (function index 193). This causes a panic in Cranelift as we run out of SSA value numbers: the
ValueDataPacked
bit-packing supports only 2^24 (16M) values per function.I started down the path of propagating
CodegenError
s everywhere throughout Cranelift to properly bubble up aCodegenError::CodeTooLarge
, but that turns out to be an extremely pervasive change: it means not only that we have more Result plumbing, but that (i) Cranelift's public API changes so that all function builder methods return Results, which is a huge change for any existing user; and (ii) ISLE can't generate Rust that propagates Results, so we need to awkwardly set an error flag on a context, return a fake Value, and "catch" it on the other side of the invocation, which is error-prone.I then considered a size-check on function bodies as they enter Cranelift, but at that point, realized that Wasm already provides for implementation limits for this purpose. The JS embedding spec at https://webassembly.github.io/spec/js-api/index.html#limits specifies that a function body can be at most 7_654_321 bytes (7.65MB). We don't have to follow the JS embedding's implementation limits, but there is good reason to expect that producers will try to stay within them, and the reasons that led to those limits in Web engines' compilers equally apply to ours. This PR thus instead enforces the limit directly.
Fixes #11682.