diff --git a/test/core/compact-import-section/binary-compact-imports.wast b/test/core/compact-import-section/binary-compact-imports.wast new file mode 100644 index 000000000..bf5d9e021 --- /dev/null +++ b/test/core/compact-import-section/binary-compact-imports.wast @@ -0,0 +1,174 @@ +;; Auxiliary modules to import + +(module + (func (export "b") (result i32) (i32.const 0x0f)) + (func (export "c") (result i32) (i32.const 0xf0)) +) +(register "a") +(module + (func (export "") (result i32) (i32.const 0xab)) +) +(register "") + + +;; Valid compact encodings + +(module binary + "\00asm" "\01\00\00\00" + "\01\05\01\60\00\01\7f" ;; Type section: (type (func (result i32))) + "\02\0e" ;; Import section + "\01" ;; 1 group + "\01a" ;; "a" + "\00" "\7f" ;; "" + 0x7f (compact encoding) + "\02" ;; 2 items + "\01b" "\00\00" ;; "b" (func (type 0)) + "\01c" "\00\00" ;; "c" (func (type 0)) + "\03\02" "\01" ;; Function section, 1 func + "\00" ;; func 2: type 0 + "\07\08" "\01" ;; Export section, 1 export + "\04test" "\00\02" ;; "test" func 2 + "\0a\09" "\01" ;; Code section, 1 func + "\07" "\00" ;; len, 0 locals + "\10\00" ;; call 0 + "\10\01" ;; call 1 + "\6a" ;; i32.add + "\0b" ;; end +) +(assert_return (invoke "test") (i32.const 0xff)) + +(module binary + "\00asm" "\01\00\00\00" + "\01\05\01\60\00\01\7f" ;; Type section: (type (func (result i32))) + "\02\0c" ;; Import section + "\01" ;; 1 group + "\01a" ;; "a" + "\00" "\7e" ;; "" + 0x7e (compact encoding) + "\00\00" ;; (func (type 0)) + "\02" ;; 2 items + "\01b" ;; "b" + "\01c" ;; "c" + "\03\02" "\01" ;; Function section, 1 func + "\00" ;; func 2: type 0 + "\07\08" "\01" ;; Export section, 1 export + "\04test" "\00\02" ;; "test" func 2 + "\0a\09" "\01" ;; Code section, 1 func + "\07" "\00" ;; len, 0 locals + "\10\00" ;; call 0 + "\10\01" ;; call 1 + "\6a" ;; i32.add + "\0b" ;; end +) +(assert_return (invoke "test") (i32.const 0xff)) + + +;; Overly-long empty name encodings are valid + +(module binary + "\00asm" "\01\00\00\00" + "\01\05\01\60\00\01\7f" ;; Type section: (type (func (result i32))) + "\02\11" ;; Import section + "\01" ;; 1 group + "\01a" ;; "a" + "\80\80\80\00" "\7f" ;; "" (long encoding) + 0x7f + "\02" ;; 2 items + "\01b" "\00\00" ;; "b" (func (type 0)) + "\01c" "\00\00" ;; "c" (func (type 0)) +) +(module binary + "\00asm" "\01\00\00\00" + "\01\05\01\60\00\01\7f" ;; Type section: (type (func (result i32))) + "\02\0f" ;; Import section + "\01" ;; 1 group + "\01a" ;; "a" + "\80\80\80\00" "\7e" ;; "" (long encoding) + 0x7e + "\00\00" ;; (func (type 0)) + "\02" ;; 2 items + "\01b" ;; "b" + "\01c" ;; "c" +) + + +;; Discriminator is not valid except after empty names + +(assert_malformed + (module binary + "\00asm" "\01\00\00\00" + "\01\05\01\60\00\01\7f" ;; Type section: (type (func (result i32))) + "\02\12" ;; Import section + "\01" ;; 1 group + "\01a" ;; "a" + "\01b" "\7f" ;; "b" + 0x7f + "\02" ;; 2 items + "\01b" "\00\00" ;; "b" (func (type 0)) + "\01c" "\00\00" ;; "c" (func (type 0)) + ) + "malformed import kind" +) +(assert_malformed + (module binary + "\00asm" "\01\00\00\00" + "\01\05\01\60\00\01\7f" ;; Type section: (type (func (result i32))) + "\02\10" ;; Import section + "\01" ;; 1 group + "\01a" ;; "a" + "\01b" "\7e" ;; "" + 0x7e (long encoding) + "\00\00" ;; (func (type 0)) + "\02" ;; 2 items + "\01b" ;; "b" + "\01c" ;; "c" + ) + "malformed import kind" +) + + +;; Discriminator is not to be interpreted as LEB128 + +(assert_malformed + (module binary + "\00asm" "\01\00\00\00" + "\01\05\01\60\00\01\7f" ;; Type section: (type (func (result i32))) + "\02\11" ;; Import section + "\01" ;; 1 group + "\01a" ;; "a" + "\00\ff\80\80\00" ;; "" + 0x7f (long encoding) + "\02" ;; 2 items + "\01b" "\00\00" ;; "b" (func (type 0)) + "\01c" "\00\00" ;; "c" (func (type 0)) + ) + "malformed import kind" +) +(assert_malformed + (module binary + "\00asm" "\01\00\00\00" + "\01\05\01\60\00\01\7f" ;; Type section: (type (func (result i32))) + "\02\0f" ;; Import section + "\01" ;; 1 group + "\01a" ;; "a" + "\00\fe\80\80\00" ;; "" + 0x7e (long encoding) + "\00\00" ;; (func (type 0)) + "\02" ;; 2 items + "\01b" ;; "b" + "\01c" ;; "c" + ) + "malformed import kind" +) + + +;; Empty names are still valid if not followed by a discriminator + +(module binary + "\00asm" "\01\00\00\00" + "\01\05\01\60\00\01\7f" ;; Type section: (type (func (result i32))) + "\02\05" ;; Import section + "\01" ;; 1 group + "\00\00\00\00" ;; "" "" (func (type 0)) + "\03\02" "\01" ;; Function section, 1 func + "\00" ;; func 1: type 0 + "\07\08" "\01" ;; Export section, 1 export + "\04test" "\00\01" ;; "test" func 1 + "\0a\06" "\01" ;; Code section, 1 func + "\04" "\00" ;; len, 0 locals + "\10\00" ;; call 0 + "\0b" ;; end +) +(assert_return (invoke "test") (i32.const 0xab)) diff --git a/test/core/compact-import-section/compact_imports.wast b/test/core/compact-import-section/compact_imports.wast deleted file mode 100644 index 133942a2b..000000000 --- a/test/core/compact-import-section/compact_imports.wast +++ /dev/null @@ -1,24 +0,0 @@ -(module - (func (export "func->11") (result i32) (i32.const 11)) - (func (export "func->22") (result f32) (f32.const 22)) - (global (export "global->1") i32 (i32.const 55)) - (global (export "global->20") i32 (i32.const 44)) -) -(register "test") - -(module - (import "test" - (item "func->11" (func $f11 (result i32))) - (item "func->22" (func $f22 (result f32))) - ) - - (func (export "sum") (result i32) - (local i32) - - call $f11 - (i32.trunc_f32_s (call $f22)) - i32.add - ) -) - -(assert_return (invoke "sum") (i32.const 33)) diff --git a/test/core/compact-import-section/imports-compact.wast b/test/core/compact-import-section/imports-compact.wast new file mode 100644 index 000000000..43422356b --- /dev/null +++ b/test/core/compact-import-section/imports-compact.wast @@ -0,0 +1,124 @@ +;; Auxiliary module to import from + +(module + (func (export "func->11i") (result i32) (i32.const 11)) + (func (export "func->22f") (result f32) (f32.const 22)) + (global (export "global->1") i32 (i32.const 1)) + (global (export "global->20") i32 (i32.const 20)) + (global (export "global->300") i32 (i32.const 300)) + (global (export "global->4000") i32 (i32.const 4000)) +) +(register "test") + + +;; Basic behavior + +(module + (import "test" + (item "func->11i" (func (result i32))) + (item "func->22f" (func (result f32))) + ) + (import "test" + (item "global->1") + (item "global->20") + (item "global->300") + (item "global->4000") + (global i32) + ) + + (global i32 (i32.const 50000)) + + (func (export "sum1") (result i32) + (local i32) + + call 0 + (i32.trunc_f32_s (call 1)) + i32.add + ) + (func (export "sum2") (result i32) + (local i32) + + global.get 0 + global.get 1 + global.get 2 + global.get 3 + i32.add + i32.add + i32.add + ) + + ;; Tests that indices were tracked correctly + (func (export "sum3") (result i32) + call 2 ;; sum1 + call 3 ;; sum2 + i32.add + + global.get 4 + i32.add + ) +) + +(assert_return (invoke "sum1") (i32.const 33)) +(assert_return (invoke "sum2") (i32.const 4321)) +(assert_return (invoke "sum3") (i32.const 54354)) + +(module (import "test" (item "func->11i" (func (result i32))))) +(assert_unlinkable + (module (import "test" (item "unknown" (func (result i32))))) + "unknown import" +) +(assert_unlinkable + (module (import "test" (item "func->11i" (func (result i32))) (item "unknown" (func (result i32))))) + "unknown import" +) + +(module (import "test" (item "func->11i") (func (result i32)))) +(assert_unlinkable + (module (import "test" (item "unknown") (func (result i32)))) + "unknown import" +) +(assert_unlinkable + (module (import "test" (item "func->11i") (item "unknown") (func (result i32)))) + "unknown import" +) + +(assert_unlinkable + (module (import "test" (item "func->11i" (func)))) + "incompatible import type" +) +(assert_unlinkable + (module (import "test" (item "func->11i" (func (result i32))) (item "func->22f" (func)))) + "incompatible import type" +) + +(assert_unlinkable + (module (import "test" (item "func->11i") (item "func->22f") (func (result i32)))) + "incompatible import type" +) + + +;; Identifiers + +(module + (import "test" "func->11i" (func $f11i (result i32))) + (import "test" + (item "global->1" (global $g1 i32)) + (item "global->20" (global $g20 i32)) + ) + ;; Shared-type form does not allow identifiers + + (func (export "sum") (result i32) + call $f11i + global.get $g1 + global.get $g20 + i32.add + i32.add + ) +) + +(assert_return (invoke "sum") (i32.const 32)) + +(assert_malformed + (module quote "(import \"test\" (item \"foo\") (func $foo))") + "identifier not allowed" +)