Skip to content

Commit b76818e

Browse files
committed
Merge pull request WebAssembly#567 from WebAssembly/spec-test-update
Misc fixes for new spec tests
2 parents 1826f41 + 74bc353 commit b76818e

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

src/wasm-interpreter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ class ModuleInstance {
359359
LiteralList arguments;
360360
Flow flow = generateArguments(curr->operands, arguments);
361361
if (flow.breaking()) return flow;
362+
if (func->params.size() != arguments.size()) trap("callIndirect: bad # of arguments");
363+
for (size_t i = 0; i < func->getNumLocals(); i++) {
364+
if (func->params[i] != arguments[i].type) trap("callIndirect: bad argument type");
365+
}
362366
return instance.callFunctionInternal(name, arguments);
363367
}
364368

src/wasm-s-parser.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,15 @@ class SExpressionWasmBuilder {
822822
}
823823

824824
Index getLocalIndex(Element& s) {
825-
if (s.dollared()) return currFunction->getLocalIndex(s.str());
825+
if (s.dollared()) {
826+
auto ret = s.str();
827+
if (currFunction->localIndices.count(ret) == 0) throw ParseException("bad local name", s.line, s.col);
828+
return currFunction->getLocalIndex(ret);
829+
}
826830
// this is a numeric index
827-
return atoi(s.c_str());
831+
Index ret = atoi(s.c_str());
832+
if (ret >= currFunction->getNumLocals()) throw ParseException("bad local index", s.line, s.col);
833+
return ret;
828834
}
829835

830836
Expression* makeGetLocal(Element& s) {
@@ -1088,7 +1094,8 @@ class SExpressionWasmBuilder {
10881094
Expression* makeCallIndirect(Element& s) {
10891095
auto ret = allocator.alloc<CallIndirect>();
10901096
IString type = s[1]->str();
1091-
ret->fullType = wasm.getFunctionType(type);
1097+
ret->fullType = wasm.checkFunctionType(type);
1098+
if (!ret->fullType) throw ParseException("invalid call_indirect type", s.line, s.col);
10921099
assert(ret->fullType);
10931100
ret->type = ret->fullType->result;
10941101
ret->target = parseExpression(s[2]);
@@ -1109,7 +1116,7 @@ class SExpressionWasmBuilder {
11091116
} else {
11101117
// offset, break to nth outside label
11111118
uint64_t offset = std::stoll(s.c_str(), nullptr, 0);
1112-
if (offset >= labelStack.size()) throw ParseException("total memory must be <= 4GB", s.line, s.col);
1119+
if (offset >= labelStack.size()) throw ParseException("invalid label", s.line, s.col);
11131120
return labelStack[labelStack.size() - 1 - offset];
11141121
}
11151122
}

src/wasm-validator.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,26 @@ struct WasmValidator : public PostWalker<WasmValidator, Visitor<WasmValidator>>
9393
shouldBeTrue(curr->condition->type == unreachable || curr->condition->type == i32, curr, "br_table condition must be i32");
9494
}
9595
void visitCall(Call *curr) {
96-
auto* target = getModule()->getFunction(curr->target);
97-
shouldBeTrue(curr->operands.size() == target->params.size(), curr, "call param number must match");
96+
auto* target = getModule()->checkFunction(curr->target);
97+
if (!shouldBeTrue(!!target, curr, "call target must exist")) return;
98+
if (!shouldBeTrue(curr->operands.size() == target->params.size(), curr, "call param number must match")) return;
9899
for (size_t i = 0; i < curr->operands.size(); i++) {
99100
shouldBeEqualOrFirstIsUnreachable(curr->operands[i]->type, target->params[i], curr, "call param types must match");
100101
}
101102
}
102103
void visitCallImport(CallImport *curr) {
103-
auto* target = getModule()->getImport(curr->target)->type;
104-
shouldBeTrue(curr->operands.size() == target->params.size(), curr, "call param number must match");
104+
auto* import = getModule()->checkImport(curr->target);
105+
if (!shouldBeTrue(!!import, curr, "call_import target must exist")) return;
106+
auto* type = import->type;
107+
if (!shouldBeTrue(curr->operands.size() == type->params.size(), curr, "call param number must match")) return;
105108
for (size_t i = 0; i < curr->operands.size(); i++) {
106-
shouldBeEqualOrFirstIsUnreachable(curr->operands[i]->type, target->params[i], curr, "call param types must match");
109+
shouldBeEqualOrFirstIsUnreachable(curr->operands[i]->type, type->params[i], curr, "call param types must match");
107110
}
108111
}
109112
void visitCallIndirect(CallIndirect *curr) {
110113
auto* type = curr->fullType;
111114
shouldBeEqualOrFirstIsUnreachable(curr->target->type, i32, curr, "indirect call target must be an i32");
112-
shouldBeTrue(curr->operands.size() == type->params.size(), curr, "call param number must match");
115+
if (!shouldBeTrue(curr->operands.size() == type->params.size(), curr, "call param number must match")) return;
113116
for (size_t i = 0; i < curr->operands.size(); i++) {
114117
shouldBeEqualOrFirstIsUnreachable(curr->operands[i]->type, type->params[i], curr, "call param types must match");
115118
}
@@ -245,7 +248,7 @@ struct WasmValidator : public PostWalker<WasmValidator, Visitor<WasmValidator>>
245248
}
246249
void visitMemory(Memory *curr) {
247250
shouldBeFalse(curr->initial > curr->max, "memory", "memory max >= initial");
248-
shouldBeTrue(curr->max <= Memory::kMaxSize, "memory", "total memory must be <= 4GB");
251+
shouldBeTrue(curr->max <= Memory::kMaxSize, "memory", "max memory must be <= 4GB");
249252
size_t top = 0;
250253
for (auto& segment : curr->segments) {
251254
shouldBeFalse(segment.offset < top, "memory", "segment offset is small enough");

0 commit comments

Comments
 (0)