Skip to content

Commit

Permalink
new validation checks for upcoming spec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Sep 3, 2016
1 parent 816ec66 commit 3de52df
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 56 deletions.
3 changes: 2 additions & 1 deletion src/wasm-s-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1402,9 +1402,10 @@ class SExpressionWasmBuilder {
}

void parseData(Element& s) {
if (!hasMemory) throw ParseException("data but no memory");
Index i = 1;
Expression* offset;
if (s[i]->isList()) {
if (i < s.size() && s[i]->isList()) {
// there is an init expression
offset = parseExpression(s[i++]);
} else {
Expand Down
36 changes: 32 additions & 4 deletions src/wasm-validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ struct WasmValidator : public PostWalker<WasmValidator, Visitor<WasmValidator>>
}
}
void visitLoad(Load *curr) {
validateAlignment(curr->align);
validateAlignment(curr->align, curr->type, curr->bytes);
shouldBeEqualOrFirstIsUnreachable(curr->ptr->type, i32, curr, "load pointer type must be i32");
}
void visitStore(Store *curr) {
validateAlignment(curr->align);
validateAlignment(curr->align, curr->type, curr->bytes);
shouldBeEqualOrFirstIsUnreachable(curr->ptr->type, i32, curr, "store pointer type must be i32");
shouldBeUnequal(curr->value->type, none, curr, "store value type must not be none");
shouldBeEqualOrFirstIsUnreachable(curr->value->type, curr->valueType, curr, "store value type must match");
Expand Down Expand Up @@ -351,12 +351,26 @@ struct WasmValidator : public PostWalker<WasmValidator, Visitor<WasmValidator>>
}

bool isConstant(Expression* curr) {
return curr->is<Const>();
return curr->is<Const>() || curr->is<GetGlobal>();
}

void visitMemory(Memory *curr) {
shouldBeFalse(curr->initial > curr->max, "memory", "memory max >= initial");
shouldBeTrue(curr->max <= Memory::kMaxSize, "memory", "max memory must be <= 4GB");
Index mustBeGreaterOrEqual = 0;
for (auto& segment : curr->segments) {
if (!shouldBeEqual(segment.offset->type, i32, segment.offset, "segment offset should be i32")) continue;
shouldBeTrue(isConstant(segment.offset), segment.offset, "segment offset should be constant");
Index size = segment.data.size();
shouldBeTrue(size <= curr->initial * Memory::kPageSize, segment.data.size(), "segment size should fit in memory");
if (segment.offset->is<Const>()) {
Index start = segment.offset->cast<Const>()->value.geti32();
Index end = start + size;
shouldBeTrue(end <= curr->initial * Memory::kPageSize, segment.data.size(), "segment size should fit in memory");
shouldBeTrue(start >= mustBeGreaterOrEqual, segment.data.size(), "segment size should fit in memory");
mustBeGreaterOrEqual = end;
}
}
}
void visitTable(Table* curr) {
for (auto& segment : curr->segments) {
Expand Down Expand Up @@ -476,7 +490,7 @@ struct WasmValidator : public PostWalker<WasmValidator, Visitor<WasmValidator>>
return true;
}

void validateAlignment(size_t align) {
void validateAlignment(size_t align, WasmType type, Index bytes) {
switch (align) {
case 1:
case 2:
Expand All @@ -488,6 +502,20 @@ struct WasmValidator : public PostWalker<WasmValidator, Visitor<WasmValidator>>
break;
}
}
shouldBeTrue(align <= bytes, align, "alignment must not exceed natural");
switch (type) {
case i32:
case f32: {
shouldBeTrue(align <= 4, align, "alignment must not exceed natural");
break;
}
case i64:
case f64: {
shouldBeTrue(align <= 8, align, "alignment must not exceed natural");
break;
}
default: {}
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/example/c-api-kitchen-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void test_core() {
BinaryenSetLocal(module, 0, makeInt32(module, 101)),
BinaryenDrop(module, BinaryenTeeLocal(module, 0, makeInt32(module, 102))),
BinaryenLoad(module, 4, 0, 0, 0, BinaryenInt32(), makeInt32(module, 1)),
BinaryenLoad(module, 1, 1, 2, 4, BinaryenInt64(), makeInt32(module, 8)),
BinaryenLoad(module, 2, 1, 2, 1, BinaryenInt64(), makeInt32(module, 8)),
BinaryenLoad(module, 4, 0, 0, 0, BinaryenFloat32(), makeInt32(module, 2)),
BinaryenLoad(module, 8, 0, 2, 8, BinaryenFloat64(), makeInt32(module, 9)),
BinaryenStore(module, 4, 0, 0, temp13, temp14, BinaryenInt32()),
Expand Down
6 changes: 3 additions & 3 deletions test/example/c-api-kitchen-sink.txt
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ BinaryenFloat64: 4
)
)
(drop
(i64.load8_s offset=2 align=4
(i64.load16_s offset=2 align=1
(i32.const 8)
)
)
Expand Down Expand Up @@ -1528,7 +1528,7 @@ int main() {
expressions[228] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
expressions[229] = BinaryenLoad(the_module, 4, 0, 0, 0, 1, expressions[228]);
expressions[230] = BinaryenConst(the_module, BinaryenLiteralInt32(8));
expressions[231] = BinaryenLoad(the_module, 1, 1, 2, 4, 2, expressions[230]);
expressions[231] = BinaryenLoad(the_module, 2, 1, 2, 1, 2, expressions[230]);
expressions[232] = BinaryenConst(the_module, BinaryenLiteralInt32(2));
expressions[233] = BinaryenLoad(the_module, 4, 0, 0, 0, 3, expressions[232]);
expressions[234] = BinaryenConst(the_module, BinaryenLiteralInt32(9));
Expand Down Expand Up @@ -2077,7 +2077,7 @@ int main() {
)
)
(drop
(i64.load8_s offset=2 align=4
(i64.load16_s offset=2 align=1
(i32.const 8)
)
)
Expand Down
2 changes: 1 addition & 1 deletion test/example/c-api-kitchen-sink.txt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@
)
)
(drop
(i64.load8_s offset=2 align=4
(i64.load16_s offset=2 align=1
(i32.const 8)
)
)
Expand Down
44 changes: 22 additions & 22 deletions test/passes/duplicate-function-elimination.txt
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@
)
)
(drop
(i32.load8_s offset=3 align=2
(i32.load16_s offset=3
(i32.const 0)
)
)
Expand All @@ -518,14 +518,14 @@
(type $0 (func))
(func $keep2 (type $0)
(drop
(i32.load16_s offset=3
(i32.load offset=3
(i32.const 0)
)
)
)
(func $other (type $0)
(drop
(i32.load8_s offset=3 align=2
(i32.load16_s offset=3
(i32.const 0)
)
)
Expand All @@ -536,14 +536,14 @@
(type $0 (func))
(func $keep2 (type $0)
(drop
(i32.load8_s offset=3
(i32.load16_s offset=3
(i32.const 0)
)
)
)
(func $other (type $0)
(drop
(i32.load8_s offset=3 align=2
(i32.load16_s offset=3 align=1
(i32.const 0)
)
)
Expand All @@ -554,14 +554,14 @@
(type $0 (func))
(func $keep2 (type $0)
(drop
(i32.load8_s align=2
(i32.load16_s
(i32.const 0)
)
)
)
(func $other (type $0)
(drop
(i32.load8_s offset=3 align=2
(i32.load16_s offset=3
(i32.const 0)
)
)
Expand All @@ -572,14 +572,14 @@
(type $0 (func))
(func $keep2 (type $0)
(drop
(i32.load8_s offset=3 align=2
(i32.load16_s offset=3
(i32.const 0)
)
)
)
(func $other (type $0)
(drop
(i32.load8_s offset=3 align=2
(i32.load16_s offset=3
(i32.const 1)
)
)
Expand All @@ -590,14 +590,14 @@
(type $0 (func))
(func $keep2 (type $0)
(drop
(i32.load8_u offset=3 align=2
(i32.load16_u offset=3
(i32.const 0)
)
)
)
(func $other (type $0)
(drop
(i32.load8_s offset=3 align=2
(i32.load16_s offset=3
(i32.const 0)
)
)
Expand All @@ -611,7 +611,7 @@
(i32.const 0)
(i32.const 100)
)
(i32.store8 offset=3 align=2
(i32.store16 offset=3
(i32.const 0)
(i32.const 100)
)
Expand All @@ -621,13 +621,13 @@
(memory 10)
(type $0 (func))
(func $keep2 (type $0)
(i32.store16 offset=3
(i32.store offset=3
(i32.const 0)
(i32.const 100)
)
)
(func $other (type $0)
(i32.store8 offset=3 align=2
(i32.store16 offset=3
(i32.const 0)
(i32.const 100)
)
Expand All @@ -637,13 +637,13 @@
(memory 10)
(type $0 (func))
(func $keep2 (type $0)
(i32.store8 offset=3
(i32.store16 offset=3
(i32.const 0)
(i32.const 100)
)
)
(func $other (type $0)
(i32.store8 offset=3 align=2
(i32.store16 offset=3 align=1
(i32.const 0)
(i32.const 100)
)
Expand All @@ -653,13 +653,13 @@
(memory 10)
(type $0 (func))
(func $keep2 (type $0)
(i32.store8 align=2
(i32.store16
(i32.const 0)
(i32.const 100)
)
)
(func $other (type $0)
(i32.store8 offset=3 align=2
(i32.store16 offset=3
(i32.const 0)
(i32.const 100)
)
Expand All @@ -669,13 +669,13 @@
(memory 10)
(type $0 (func))
(func $keep2 (type $0)
(i32.store8 offset=3 align=2
(i32.store16 offset=3
(i32.const 0)
(i32.const 100)
)
)
(func $other (type $0)
(i32.store8 offset=3 align=2
(i32.store16 offset=3
(i32.const 1)
(i32.const 100)
)
Expand All @@ -685,13 +685,13 @@
(memory 10)
(type $0 (func))
(func $keep2 (type $0)
(i32.store8 offset=3 align=2
(i32.store16 offset=3
(i32.const 0)
(i32.const 100)
)
)
(func $other (type $0)
(i32.store8 offset=3 align=2
(i32.store16 offset=3
(i32.const 0)
(i32.const 101)
)
Expand Down
Loading

0 comments on commit 3de52df

Please sign in to comment.