Skip to content

Commit

Permalink
Binary parser: lift the limit on the number of locals
Browse files Browse the repository at this point in the history
This raises the number of locals accepted by the binary parser to the
absolute limit in the spec. A warning is now printed when writing a
binary file if the Web limit of 50,000 locals is exceeded. This fixes #6968.
  • Loading branch information
vouillon committed Sep 26, 2024
1 parent ccef354 commit 7fd5f7f
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 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,18 @@ void WasmBinaryReader::readFunctions() {
void WasmBinaryReader::readVars() {
uint32_t totalVars = 0;
size_t numLocalTypes = getU32LEB();
std::vector<std::pair<uint32_t, Type>> 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

0 comments on commit 7fd5f7f

Please sign in to comment.