Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test traps from start function; check JS Error classes #360

Merged
merged 1 commit into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ml-proto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ assertion:
( assert_malformed <module> <failure> ) ;; assert module cannot be decoded with given failure string
( assert_invalid <module> <failure> ) ;; assert module is invalid with given failure string
( assert_unlinkable <module> <failure> ) ;; assert module fails to link
( assert_trap <module> <failure> ) ;; assert module traps on instantiation

meta:
( script <name>? <script> ) ;; name a subscript
Expand Down
2 changes: 2 additions & 0 deletions ml-proto/host/arrange.ml
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ let assertion mode ass =
Node ("assert_invalid", [definition mode None def; Atom (string re)])
| AssertUnlinkable (def, re) ->
Node ("assert_unlinkable", [definition mode None def; Atom (string re)])
| AssertUninstantiable (def, re) ->
Node ("assert_trap", [definition mode None def; Atom (string re)])
| AssertReturn (act, lits) ->
Node ("assert_return", action act :: List.map literal lits)
| AssertReturnNaN act ->
Expand Down
26 changes: 22 additions & 4 deletions ml-proto/host/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,39 @@ let prefix =
"}\n" ^
"\n" ^
"function assert_malformed(bytes) {\n" ^
" try { module(bytes) } catch (e) { return }\n" ^
" try { module(bytes) } catch (e) {\n" ^
" if (e instanceof WebAssembly.CompileError) return;\n" ^
" }\n" ^
" throw new Error(\"Wasm decoding failure expected\");\n" ^
"}\n" ^
"\n" ^
"function assert_invalid(bytes) {\n" ^
" try { module(bytes) } catch (e) { return }\n" ^
" try { module(bytes) } catch (e) {\n" ^
" if (e instanceof WebAssembly.CompileError) return;\n" ^
" }\n" ^
" throw new Error(\"Wasm validation failure expected\");\n" ^
"}\n" ^
"\n" ^
"function assert_unlinkable(bytes) {\n" ^
" let mod = module(bytes);\n" ^
" try { new WebAssembly.Instance(mod, registry) } catch (e) { return }\n" ^
" try { new WebAssembly.Instance(mod, registry) } catch (e) {\n" ^
" if (e instanceof TypeError) return;\n" ^
" }\n" ^
" throw new Error(\"Wasm linking failure expected\");\n" ^
"}\n" ^
"\n" ^
"function assert_uninstantiable(bytes) {\n" ^
" let mod = module(bytes);\n" ^
" try { new WebAssembly.Instance(mod, registry) } catch (e) {\n" ^
" if (e instanceof WebAssembly.RuntimeError) return;\n" ^
" }\n" ^
" throw new Error(\"Wasm trap expected\");\n" ^
"}\n" ^
"\n" ^
"function assert_trap(action) {\n" ^
" try { action() } catch (e) { return }\n" ^
" try { action() } catch (e) {\n" ^
" if (e instanceof WebAssembly.RuntimeError) return;\n" ^
" }\n" ^
" throw new Error(\"Wasm trap expected\");\n" ^
"}\n" ^
"\n" ^
Expand Down Expand Up @@ -259,6 +275,8 @@ let of_assertion mods ass =
"assert_invalid(" ^ of_definition def ^ ");"
| AssertUnlinkable (def, _) ->
"assert_unlinkable(" ^ of_definition def ^ ");"
| AssertUninstantiable (def, _) ->
"assert_uninstantiable(" ^ of_definition def ^ ");"
| AssertReturn (act, lits) ->
of_return_assertion mods act
(fun act_js ->
Expand Down
2 changes: 2 additions & 0 deletions ml-proto/host/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,8 @@ assertion :
{ AssertInvalid (snd $3, $4) @@ at () }
| LPAR ASSERT_UNLINKABLE module_ TEXT RPAR
{ AssertUnlinkable (snd $3, $4) @@ at () }
| LPAR ASSERT_TRAP module_ TEXT RPAR
{ AssertUninstantiable (snd $3, $4) @@ at () }
| LPAR ASSERT_RETURN action const_list RPAR { AssertReturn ($3, $4) @@ at () }
| LPAR ASSERT_RETURN_NAN action RPAR { AssertReturnNaN $3 @@ at () }
| LPAR ASSERT_TRAP action TEXT RPAR { AssertTrap ($3, $4) @@ at () }
Expand Down
18 changes: 18 additions & 0 deletions ml-proto/host/run.ml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,24 @@ let run_assertion ass =
Assert.error ass.at "expected linking error"
)

| AssertUninstantiable (def, re) ->
trace "Asserting trap...";
let m = run_definition def in
if not !Flags.unchecked then Check.check_module m;
(match
let imports = Import.link m in
ignore (Eval.init m imports)
with
| exception Eval.Trap (_, msg) ->
if not (Str.string_match (Str.regexp re) msg 0) then begin
print_endline ("Result: \"" ^ msg ^ "\"");
print_endline ("Expect: \"" ^ re ^ "\"");
Assert.error ass.at "wrong instantiation trap"
end
| _ ->
Assert.error ass.at "expected instaniation trap"
)

| AssertReturn (act, es) ->
trace ("Asserting return...");
let got_vs = run_action act in
Expand Down
1 change: 1 addition & 0 deletions ml-proto/host/script.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and assertion' =
| AssertMalformed of definition * string
| AssertInvalid of definition * string
| AssertUnlinkable of definition * string
| AssertUninstantiable of definition * string
| AssertReturn of action * Ast.literal list
| AssertReturnNaN of action
| AssertTrap of action * string
Expand Down
5 changes: 5 additions & 0 deletions ml-proto/test/start.wast
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@
(func $main (call $print_i32 (i32.const 2)))
(start $main)
)

(assert_trap
(module (func $main (unreachable)) (start $main))
"unreachable"
)