From 0efea84ef80c5f22eadfa5b3492a56451dca18e9 Mon Sep 17 00:00:00 2001 From: rossberg-chromium Date: Mon, 5 Sep 2016 17:29:48 +0200 Subject: [PATCH] Check table elements before mutating --- ml-proto/spec/eval.ml | 12 +++++------- ml-proto/test/imports.wast | 11 ----------- ml-proto/test/linking.wast | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/ml-proto/spec/eval.ml b/ml-proto/spec/eval.ml index 3b03ac12ff..bc349ec588 100644 --- a/ml-proto/spec/eval.ml +++ b/ml-proto/spec/eval.ml @@ -328,16 +328,15 @@ let init_func c f = | AstFunc (inst, _) -> inst := c.instance | _ -> assert false -let non_host_func c x = - let f = func c x in - ignore (func_type_of f x.at); - Some (Func f) +let check_elem c seg = + let {init; _} = seg.it in + List.iter (fun x -> ignore (func_type_of (func c x) x.at)) init let init_table c seg = let {index; offset = e; init} = seg.it in let tab = table c index in let offset = int32 (eval_expr c e) e.at in - Table.blit tab offset (List.map (non_host_func c) init) + Table.blit tab offset (List.map (fun x -> Some (Func (func c x))) init) let init_memory c seg = let {index; offset = e; init} = seg.it in @@ -369,14 +368,12 @@ let add_import (ext : extern) (imp : import) (inst : instance) : instance = ); {inst with funcs = f :: inst.funcs} | ExternalTable tab, TableImport (TableType (lim, _)) -> - (* TODO: no checking of element type? *) check_limits (Table.limits tab) lim imp.it.ikind.at; {inst with tables = tab :: inst.tables} | ExternalMemory mem, MemoryImport (MemoryType lim) -> check_limits (Memory.limits mem) lim imp.it.ikind.at; {inst with memories = mem :: inst.memories} | ExternalGlobal glob, GlobalImport _ -> - (* TODO: no checking of value type? *) {inst with globals = ref glob :: inst.globals} | _ -> Link.error imp.it.ikind.at "type mismatch" @@ -410,6 +407,7 @@ let init m externals = in let c = empty_config inst in List.iter (init_func c) fs; + List.iter (check_elem c) elems; List.iter (init_table c) elems; List.iter (init_memory c) data; List.iter2 (init_global c) gs globals; diff --git a/ml-proto/test/imports.wast b/ml-proto/test/imports.wast index 3c7e7de03d..4b032b1209 100644 --- a/ml-proto/test/imports.wast +++ b/ml-proto/test/imports.wast @@ -45,17 +45,6 @@ "type mismatch" ) -(; TODO: check linking against other Wasm module -(assert_unlinkable - (module (import "spectest" "func" (func (param i32)))) - "type mismatch" -) -(assert_unlinkable - (module (import "spectest" "func" (func (result i32)))) - "type mismatch" -) -;) - (assert_unlinkable (module (import "spectest" "print" (func)) (table anyfunc (elem 0))) "invalid use of host function" diff --git a/ml-proto/test/linking.wast b/ml-proto/test/linking.wast index 8dc627e4f4..0f35578962 100644 --- a/ml-proto/test/linking.wast +++ b/ml-proto/test/linking.wast @@ -142,6 +142,18 @@ (assert_trap (invoke $O "call" (i32.const 20)) "undefined") +(assert_unlinkable + (module $Q + (func $host (import "spectest" "print")) + (table (import "M" "tab") 10 anyfunc) + (elem (i32.const 7) $own) + (elem (i32.const 9) $host) + (func $own (result i32) (i32.const 666)) + ) + "invalid use of host function" +) +(assert_trap (invoke $M "call" (i32.const 7)) "uninitialized") + ;; Memories @@ -201,3 +213,16 @@ (assert_return (invoke $P "grow" (i32.const 0)) (i32.const 5)) (assert_return (invoke $P "grow" (i32.const 1)) (i32.const -1)) (assert_return (invoke $P "grow" (i32.const 0)) (i32.const 5)) + +(assert_unlinkable + (module $Q + (func $host (import "spectest" "print")) + (memory (import "M" "mem") 1) + (table 10 anyfunc) + (data (i32.const 0) "abc") + (elem (i32.const 9) $host) + (func $own (result i32) (i32.const 666)) + ) + "invalid use of host function" +) +(assert_return (invoke $M "load" (i32.const 0)) (i32.const 0))