Skip to content

Commit 755cb78

Browse files
yuri91rossberg
andauthored
[interpreter] add new assertions for custom sections (#19)
* [interpreter] add new assertions for custom sections assert_malformed and assert_invalid now have a _custom variant, for asserting custom section/annotation errors. The js implementation is a no-op, since no JS engine currently support any kind of diagnostic for custom sections. * Update interpreter/script/run.ml Co-authored-by: Andreas Rossberg <[email protected]> * Update interpreter/script/run.ml Co-authored-by: Andreas Rossberg <[email protected]> * revert check_module change * add custom assertions in the test harness --------- Co-authored-by: Andreas Rossberg <[email protected]>
1 parent eeb243b commit 755cb78

File tree

10 files changed

+81
-19
lines changed

10 files changed

+81
-19
lines changed

interpreter/script/js.ml

+12
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,21 @@ function assert_malformed(bytes) {
107107
throw new Error("Wasm decoding failure expected");
108108
}
109109

110+
function assert_malformed_custom(bytes) {
111+
return;
112+
}
113+
110114
function assert_invalid(bytes) {
111115
try { module(bytes, false) } catch (e) {
112116
if (e instanceof WebAssembly.CompileError) return;
113117
}
114118
throw new Error("Wasm validation failure expected");
115119
}
116120

121+
function assert_invalid_custom(bytes) {
122+
return;
123+
}
124+
117125
function assert_unlinkable(bytes) {
118126
let mod = module(bytes);
119127
try { new WebAssembly.Instance(mod, registry) } catch (e) {
@@ -571,8 +579,12 @@ let of_assertion mods ass =
571579
match ass.it with
572580
| AssertMalformed (def, _) ->
573581
"assert_malformed(" ^ of_definition def ^ ");"
582+
| AssertMalformedCustom (def, _) ->
583+
"assert_malformed_custom(" ^ of_definition def ^ ");"
574584
| AssertInvalid (def, _) ->
575585
"assert_invalid(" ^ of_definition def ^ ");"
586+
| AssertInvalidCustom (def, _) ->
587+
"assert_invalid_custom(" ^ of_definition def ^ ");"
576588
| AssertUnlinkable (def, _) ->
577589
"assert_unlinkable(" ^ of_definition def ^ ");"
578590
| AssertUninstantiable (def, _) ->

interpreter/script/run.ml

+17-2
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,15 @@ let run_assertion ass =
450450
(match ignore (run_definition def) with
451451
| exception Decode.Code (_, msg) -> assert_message ass.at "decoding" msg re
452452
| exception Parse.Syntax (_, msg) -> assert_message ass.at "parsing" msg re
453+
| _ -> Assert.error ass.at "expected decoding/parsing error"
454+
)
455+
456+
| AssertMalformedCustom (def, re) ->
457+
trace "Asserting malformed custom...";
458+
(match ignore (run_definition def) with
453459
| exception Custom.Syntax (_, msg) ->
454460
assert_message ass.at "annotation parsing" msg re
455-
| _ -> Assert.error ass.at "expected decoding/parsing error"
461+
| _ -> Assert.error ass.at "expected custom decoding/parsing error"
456462
)
457463

458464
| AssertInvalid (def, re) ->
@@ -463,9 +469,18 @@ let run_assertion ass =
463469
with
464470
| exception Valid.Invalid (_, msg) ->
465471
assert_message ass.at "validation" msg re
472+
| _ -> Assert.error ass.at "expected validation error"
473+
)
474+
475+
| AssertInvalidCustom (def, re) ->
476+
trace "Asserting invalid custom...";
477+
(match
478+
let m, cs = run_definition def in
479+
Valid.check_module_with_custom (m, cs)
480+
with
466481
| exception Custom.Invalid (_, msg) ->
467482
assert_message ass.at "custom validation" msg re
468-
| _ -> Assert.error ass.at "expected validation error"
483+
| _ -> Assert.error ass.at "expected custom validation error"
469484
)
470485

471486
| AssertUnlinkable (def, re) ->

interpreter/script/script.ml

+2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ and result' =
4040
type assertion = assertion' Source.phrase
4141
and assertion' =
4242
| AssertMalformed of definition * string
43+
| AssertMalformedCustom of definition * string
4344
| AssertInvalid of definition * string
45+
| AssertInvalidCustom of definition * string
4446
| AssertUnlinkable of definition * string
4547
| AssertUninstantiable of definition * string
4648
| AssertReturn of action * result list

interpreter/text/arrange.ml

+8
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,16 @@ let assertion mode ass =
758758
| _ ->
759759
[Node ("assert_malformed", [definition `Original None def; Atom (string re)])]
760760
)
761+
| AssertMalformedCustom (def, re) ->
762+
(match mode, def.it with
763+
| `Binary, Quoted _ -> []
764+
| _ ->
765+
[Node ("assert_malformed_custom", [definition `Original None def; Atom (string re)])]
766+
)
761767
| AssertInvalid (def, re) ->
762768
[Node ("assert_invalid", [definition mode None def; Atom (string re)])]
769+
| AssertInvalidCustom (def, re) ->
770+
[Node ("assert_invalid_custom", [definition mode None def; Atom (string re)])]
763771
| AssertUnlinkable (def, re) ->
764772
[Node ("assert_unlinkable", [definition mode None def; Atom (string re)])]
765773
| AssertUninstantiable (def, re) ->

interpreter/text/lexer.mll

+2
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,8 @@ rule token = parse
680680
| "get" -> GET
681681
| "assert_malformed" -> ASSERT_MALFORMED
682682
| "assert_invalid" -> ASSERT_INVALID
683+
| "assert_malformed_custom" -> ASSERT_MALFORMED_CUSTOM
684+
| "assert_invalid_custom" -> ASSERT_INVALID_CUSTOM
683685
| "assert_unlinkable" -> ASSERT_UNLINKABLE
684686
| "assert_return" -> ASSERT_RETURN
685687
| "assert_trap" -> ASSERT_TRAP

interpreter/text/parser.mly

+5
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ let parse_annots (m : module_) : Custom.section list =
258258
%token MODULE BIN QUOTE
259259
%token SCRIPT REGISTER INVOKE GET
260260
%token ASSERT_MALFORMED ASSERT_INVALID ASSERT_UNLINKABLE
261+
%token ASSERT_MALFORMED_CUSTOM ASSERT_INVALID_CUSTOM
261262
%token ASSERT_RETURN ASSERT_TRAP ASSERT_EXHAUSTION
262263
%token<Script.nan> NAN
263264
%token INPUT OUTPUT
@@ -1062,6 +1063,10 @@ assertion :
10621063
{ AssertMalformed (snd $3, $4) @@ at () }
10631064
| LPAR ASSERT_INVALID script_module STRING RPAR
10641065
{ AssertInvalid (snd $3, $4) @@ at () }
1066+
| LPAR ASSERT_MALFORMED_CUSTOM script_module STRING RPAR
1067+
{ AssertMalformedCustom (snd $3, $4) @@ at () }
1068+
| LPAR ASSERT_INVALID_CUSTOM script_module STRING RPAR
1069+
{ AssertInvalidCustom (snd $3, $4) @@ at () }
10651070
| LPAR ASSERT_UNLINKABLE script_module STRING RPAR
10661071
{ AssertUnlinkable (snd $3, $4) @@ at () }
10671072
| LPAR ASSERT_TRAP script_module STRING RPAR

test/custom/custom/custom_annot.wast

+14-14
Original file line numberDiff line numberDiff line change
@@ -22,78 +22,78 @@
2222

2323
;; Malformed name
2424

25-
(assert_malformed
25+
(assert_malformed_custom
2626
(module quote "(@custom)")
2727
"@custom annotation: missing section name"
2828
)
2929

30-
(assert_malformed
30+
(assert_malformed_custom
3131
(module quote "(@custom 4)")
3232
"@custom annotation: missing section name"
3333
)
3434

35-
(assert_malformed
35+
(assert_malformed_custom
3636
(module quote "(@custom bla)")
3737
"@custom annotation: missing section name"
3838
)
3939

40-
(assert_malformed
40+
(assert_malformed_custom
4141
(module quote "(@custom \"\\df\")")
4242
"@custom annotation: malformed UTF-8 encoding"
4343
)
4444

4545

4646
;; Malformed placement
4747

48-
(assert_malformed
48+
(assert_malformed_custom
4949
(module quote "(@custom \"bla\" here)")
5050
"@custom annotation: unexpected token"
5151
)
5252

53-
(assert_malformed
53+
(assert_malformed_custom
5454
(module quote "(@custom \"bla\" after)")
5555
"@custom annotation: unexpected token"
5656
)
5757

58-
(assert_malformed
58+
(assert_malformed_custom
5959
(module quote "(@custom \"bla\" (after))")
6060
"@custom annotation: malformed section kind"
6161
)
6262

63-
(assert_malformed
63+
(assert_malformed_custom
6464
(module quote "(@custom \"bla\" (type))")
6565
"@custom annotation: malformed placement"
6666
)
6767

68-
(assert_malformed
68+
(assert_malformed_custom
6969
(module quote "(@custom \"bla\" (aft type))")
7070
"@custom annotation: malformed placement"
7171
)
7272

73-
(assert_malformed
73+
(assert_malformed_custom
7474
(module quote "(@custom \"bla\" (before types))")
7575
"@custom annotation: malformed section kind"
7676
)
7777

7878

7979
;; Misplaced
8080

81-
(assert_malformed
81+
(assert_malformed_custom
8282
(module quote "(type (@custom \"bla\") $t (func))")
8383
"misplaced @custom annotation"
8484
)
8585

86-
(assert_malformed
86+
(assert_malformed_custom
8787
(module quote "(func (@custom \"bla\"))")
8888
"misplaced @custom annotation"
8989
)
9090

91-
(assert_malformed
91+
(assert_malformed_custom
9292
(module quote "(func (block (@custom \"bla\")))")
9393
"misplaced @custom annotation"
9494
)
9595

96-
(assert_malformed
96+
(assert_malformed_custom
9797
(module quote "(func (nop (@custom \"bla\")))")
9898
"misplaced @custom annotation"
9999
)

test/custom/name/name_annot.wast

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
(module $moduel (@name "Modül"))
66

7-
(assert_malformed
7+
(assert_malformed_custom
88
(module quote "(module (@name \"M1\") (@name \"M2\"))")
99
"@name annotation: multiple module"
1010
)
1111

12-
(assert_malformed
12+
(assert_malformed_custom
1313
(module quote "(module (func) (@name \"M\"))")
1414
"misplaced @name annotation"
1515
)
1616

17-
(assert_malformed
17+
(assert_malformed_custom
1818
(module quote "(module (start $f (@name \"M\")) (func $f))")
1919
"misplaced @name annotation"
2020
)

test/harness/async_index.js

+6
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ function assert_invalid(bytes) {
176176

177177
const assert_malformed = assert_invalid;
178178

179+
function assert_invalid_custom(bytes) {
180+
module(bytes);
181+
}
182+
183+
const assert_malformed_custom = assert_invalid_custom;
184+
179185
function instance(bytes, imports, valid = true) {
180186
const test = valid
181187
? "Test that WebAssembly instantiation succeeds"

test/harness/sync_index.js

+12
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ function assert_invalid(bytes) {
191191

192192
const assert_malformed = assert_invalid;
193193

194+
function assert_invalid_custom(bytes) {
195+
uniqueTest(() => {
196+
try {
197+
module(bytes, /* valid */ true);
198+
} catch(e) {
199+
throw new Error('failed on custom section error');
200+
}
201+
}, "A wast module that should have an invalid or malformed custom section.");
202+
}
203+
204+
const assert_malformed_custom = assert_invalid_custom;
205+
194206
function instance(bytes, imports = registry, valid = true) {
195207
if (imports instanceof Result) {
196208
if (imports.isError())

0 commit comments

Comments
 (0)