Skip to content

Commit 56c5ff7

Browse files
authored
More validation tests and fixes for SIMD (WebAssembly#1964)
Moves the feature validation unit test file to a new directory, 'unit', and adds new tests for SIMD and sign-ext. Adds validation for v128 globals and v128.const.
1 parent 5e19a7b commit 56c5ff7

4 files changed

Lines changed: 96 additions & 18 deletions

File tree

src/wasm/wasm-validator.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ struct FunctionValidator : public WalkerPass<PostWalker<FunctionValidator>> {
235235
void visitSwitch(Switch* curr);
236236
void visitCall(Call* curr);
237237
void visitCallIndirect(CallIndirect* curr);
238+
void visitConst(Const* curr);
238239
void visitGetLocal(GetLocal* curr);
239240
void visitSetLocal(SetLocal* curr);
240241
void visitGetGlobal(GetGlobal* curr);
@@ -475,6 +476,11 @@ void FunctionValidator::visitCallIndirect(CallIndirect* curr) {
475476
}
476477
}
477478

479+
void FunctionValidator::visitConst(Const* curr) {
480+
shouldBeTrue(getFeatures(curr->type) <= info.features, curr,
481+
"all used features should be allowed");
482+
}
483+
478484
void FunctionValidator::visitGetLocal(GetLocal* curr) {
479485
shouldBeTrue(curr->index < getFunction()->getNumLocals(), curr, "local.get index must be small enough");
480486
shouldBeTrue(isConcreteType(curr->type), curr, "local.get must have a valid type - check what you provided when you constructed the node");
@@ -1270,6 +1276,8 @@ static void validateExports(Module& module, ValidationInfo& info) {
12701276

12711277
static void validateGlobals(Module& module, ValidationInfo& info) {
12721278
ModuleUtils::iterDefinedGlobals(module, [&](Global* curr) {
1279+
info.shouldBeTrue(getFeatures(curr->type) <= info.features, curr->name,
1280+
"all used types should be allowed");
12731281
info.shouldBeTrue(curr->init != nullptr, curr->name, "global init must be non-null");
12741282
info.shouldBeTrue(curr->init->is<Const>() || curr->init->is<GetGlobal>(), curr->name, "global init must be valid");
12751283
if (!info.shouldBeEqual(curr->type, curr->init->type, curr->init, "global init must have correct type") && !info.quiet) {

test/crash/test_features.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/unit/test_features.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import unittest
2+
from scripts.test.shared import WASM_OPT, run_process
3+
4+
5+
class FeatureValidationTest(unittest.TestCase):
6+
def check_feature(self, module, error, flag):
7+
p = run_process(WASM_OPT + ['--mvp-features', '--print'],
8+
input=module, check=False, capture_output=True)
9+
self.assertIn(error, p.stderr)
10+
self.assertIn("Fatal: error in validating input", p.stderr)
11+
self.assertNotEqual(p.returncode, 0)
12+
p = run_process(WASM_OPT + ['--mvp-features', flag, '--print'],
13+
input=module, check=False, capture_output=True)
14+
self.assertEqual(p.returncode, 0)
15+
16+
def check_simd(self, module, error):
17+
self.check_feature(module, error, '--enable-simd')
18+
19+
def check_sign_ext(self, module, error):
20+
self.check_feature(module, error, '--enable-sign-ext')
21+
22+
def test_v128_signature(self):
23+
module = """
24+
(module
25+
(func $foo (param $0 v128) (result v128)
26+
(local.get $0)
27+
)
28+
)
29+
"""
30+
self.check_simd(module, "all used types should be allowed")
31+
32+
def test_v128_global(self):
33+
module = """
34+
(module
35+
(global $foo (mut v128) (v128.const i32x4 0 0 0 0))
36+
)
37+
"""
38+
self.check_simd(module, "all used types should be allowed")
39+
40+
def test_v128_local(self):
41+
module = """
42+
(module
43+
(func $foo
44+
(local v128)
45+
)
46+
)
47+
"""
48+
self.check_simd(module, "all used types should be allowed")
49+
50+
def test_simd_const(self):
51+
module = """
52+
(module
53+
(func $foo
54+
(drop (v128.const i32x4 0 0 0 0))
55+
)
56+
)
57+
"""
58+
self.check_simd(module, "all used features should be allowed")
59+
60+
def test_simd_load(self):
61+
module = """
62+
(module
63+
(func $foo
64+
(drop (v128.load (i32.const 0)))
65+
)
66+
)
67+
"""
68+
self.check_simd(module, "SIMD operation (SIMD is disabled)")
69+
70+
def test_simd_splat(self):
71+
module = """
72+
(module
73+
(func $foo
74+
(drop (i32x4.splat (i32.const 0)))
75+
)
76+
)
77+
"""
78+
self.check_simd(module, "all used features should be allowed")
79+
80+
def test_sign_ext(self):
81+
module = """
82+
(module
83+
(func $foo
84+
(drop (i32.extend8_s (i32.const 7)))
85+
)
86+
)
87+
"""
88+
self.check_sign_ext(module, "all used features should be allowed")

0 commit comments

Comments
 (0)