Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary parser: lift the limit on the number of locals #6973

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ void WasmBinaryWriter::writeFunctions() {
std::cerr << "Some VMs may not accept this binary because it has a large "
<< "number of parameters in function " << func->name << ".\n";
}
if (func->getNumLocals() > WebLimitations::MaxFunctionLocals) {
std::cerr << "Some VMs may not accept this binary because it has a large "
<< "number of locals in function " << func->name << ".\n";
}
});
finishSection(sectionStart);
}
Expand Down Expand Up @@ -2722,16 +2726,20 @@ void WasmBinaryReader::readFunctions() {
void WasmBinaryReader::readVars() {
uint32_t totalVars = 0;
size_t numLocalTypes = getU32LEB();
// Use a SmallVector as in the common (MVP) case there are only 4 possible
// types.
SmallVector<std::pair<uint32_t, Type>, 4> decodedVars;
decodedVars.reserve(numLocalTypes);
for (size_t t = 0; t < numLocalTypes; t++) {
auto num = getU32LEB();
// The core spec allows up to 2^32 locals, but to avoid allocation failures,
// we additionally impose a much smaller limit, matching the JS embedding.
if (std::ckd_add(&totalVars, totalVars, num) ||
totalVars > WebLimitations::MaxFunctionLocals) {
throwError("too many locals");
if (std::ckd_add(&totalVars, totalVars, num)) {
throwError("unaddressable number of locals");
}
auto type = getConcreteType();

decodedVars.emplace_back(num, type);
}
currFunction->vars.reserve(totalVars);
for (auto [num, type] : decodedVars) {
while (num > 0) {
currFunction->vars.push_back(type);
num--;
Expand Down
16 changes: 16 additions & 0 deletions test/unit/test_web_limitations.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@ def test_many_params(self):
input=module, capture_output=True)
self.assertIn('Some VMs may not accept this binary because it has a large number of parameters in function foo.',
p.stderr)

def test_many_locals(self):
"""Test that we warn on large numbers of locals, which Web VMs
disallow."""

params = '(local i32) ' * 50_001
module = '''
(module
(func $foo %s
)
)
''' % params
p = shared.run_process(shared.WASM_OPT + ['-o', os.devnull],
input=module, capture_output=True)
self.assertIn('Some VMs may not accept this binary because it has a large number of locals in function foo.',
p.stderr)
Loading