diff options
author | Yuri Iozzelli <y.iozzelli@gmail.com> | 2022-02-25 16:36:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-25 15:36:35 +0000 |
commit | 1f59b65d8befc8512ff9045bb371ea5ec379a78c (patch) | |
tree | 349621c048c7716a746d3361c8ec6219d32f0250 /test | |
parent | 08cf71aa180674432750a02581f1e214c310042d (diff) | |
download | wabt-1f59b65d8befc8512ff9045bb371ea5ec379a78c.tar.gz wabt-1f59b65d8befc8512ff9045bb371ea5ec379a78c.tar.bz2 wabt-1f59b65d8befc8512ff9045bb371ea5ec379a78c.zip |
Add initial support for code metadata (#1840)
See https://github.com/WebAssembly/tool-conventions/blob/main/CodeMetadata.md for the specification.
In particular this pr implements the following:
- Parsing code metadata sections in BinaryReader, providing appropriate callbacks that a BinaryReaderDelegate can implement:
- BinaryReaderObjdump: show the sections in a human-readable form
- BinaryReaderIr: add code metadata in the IR as expressions
- Parsing code metadata annotations in text format, adding them in the IR like the BinaryReaderIR does
- Writing the code metadata present in the IR in the proper sections when converting IR to binary
- Support in wasm-decompiler for showing code metadata as comments in the pseudo-code
All the features have corresponding tests.
Support for code metadata is gated through the --enable-code-metadata feature. For reading/writing in the text format, --enable-annotations is also required.
Missing features:
Support for function-level code metadata (offset 0)
Extensive validation in validator.cc (like making sure that all metadata instances are at the same code offset of an instruction)
Diffstat (limited to 'test')
-rw-r--r-- | test/binary/bad-code-metadata-function-count.txt | 65 | ||||
-rw-r--r-- | test/binary/bad-code-metadata-function-duplicate.txt | 67 | ||||
-rw-r--r-- | test/binary/bad-code-metadata-function-index.txt | 62 | ||||
-rw-r--r-- | test/binary/bad-code-metadata-function-out-of-order.txt | 82 | ||||
-rw-r--r-- | test/binary/bad-code-metadata-instance-count.txt | 65 | ||||
-rw-r--r-- | test/binary/bad-code-metadata-instance-duplicate.txt | 68 | ||||
-rw-r--r-- | test/binary/bad-code-metadata-instance-out-of-order.txt | 68 | ||||
-rw-r--r-- | test/binary/code-metadata-section.txt | 61 | ||||
-rw-r--r-- | test/decompile/code-metadata.txt | 17 | ||||
-rw-r--r-- | test/dump/code-metadata.txt | 43 | ||||
-rw-r--r-- | test/help/spectest-interp.txt | 1 | ||||
-rw-r--r-- | test/help/wasm-interp.txt | 1 | ||||
-rw-r--r-- | test/help/wasm-opcodecnt.txt | 1 | ||||
-rw-r--r-- | test/help/wasm-validate.txt | 1 | ||||
-rw-r--r-- | test/help/wasm2wat.txt | 1 | ||||
-rw-r--r-- | test/help/wast2json.txt | 1 | ||||
-rw-r--r-- | test/help/wat-desugar.txt | 1 | ||||
-rw-r--r-- | test/help/wat2wasm.txt | 1 | ||||
-rw-r--r-- | test/roundtrip/code-metadata.txt | 8 | ||||
-rwxr-xr-x | test/run-roundtrip.py | 6 |
20 files changed, 620 insertions, 0 deletions
diff --git a/test/binary/bad-code-metadata-function-count.txt b/test/binary/bad-code-metadata-function-count.txt new file mode 100644 index 00000000..50da843a --- /dev/null +++ b/test/binary/bad-code-metadata-function-count.txt @@ -0,0 +1,65 @@ +;;; TOOL: run-objdump-gen-wasm +;;; ARGS1: -x +magic +version +section(TYPE) { + count[1] + function params[0] results[1] i32 +} +section(FUNCTION) { + count[1] + type[0] +} +section(MEMORY) { + count[1] + has_max[0] + initial[0] +} +section("metadata.code.test") { + function_count[2] + function_index[0] + ann_count[1] + ann_offset[1] + ann_data_size[1] + ann_data[1] +} + +section(CODE) { + count[1] + func { + local_decls[0] + i32.const 1 + return + } +} + +(;; STDERR ;;; +0000033: warning: unable to read u32 leb128: function index +;;; STDERR ;;) +(;; STDOUT ;;; + +bad-code-metadata-function-count.wasm: file format wasm 0x1 + +Section Details: + +Type[1]: + - type[0] () -> i32 +Function[1]: + - func[0] sig=0 +Memory[1]: + - memory[0] pages: initial=0 +Custom: + - name: "metadata.code.test" + - func[0]: + - meta[1]: + - 0000000: 01 . +Code[1]: + - func[0] size=5 + +Code Disassembly: + +000037 func[0]: + 000038: 41 01 | i32.const 1 + 00003a: 0f | return + 00003b: 0b | end +;;; STDOUT ;;) diff --git a/test/binary/bad-code-metadata-function-duplicate.txt b/test/binary/bad-code-metadata-function-duplicate.txt new file mode 100644 index 00000000..4173ece2 --- /dev/null +++ b/test/binary/bad-code-metadata-function-duplicate.txt @@ -0,0 +1,67 @@ +;;; TOOL: run-objdump-gen-wasm +;;; ARGS1: -x +magic +version +section(TYPE) { + count[1] + function params[0] results[1] i32 +} +section(FUNCTION) { + count[1] + type[0] +} +section(MEMORY) { + count[1] + has_max[0] + initial[0] +} +section("metadata.code.test") { + function_count[2] + function_index[0] + ann_count[1] + ann_offset[1] + ann_data_size[1] + ann_data[1] + function_index[0] + ann_count[0] +} + +section(CODE) { + count[1] + func { + local_decls[0] + i32.const 1 + return + } +} + +(;; STDERR ;;; +0000034: warning: duplicate function index: 0 +;;; STDERR ;;) +(;; STDOUT ;;; + +bad-code-metadata-function-duplicate.wasm: file format wasm 0x1 + +Section Details: + +Type[1]: + - type[0] () -> i32 +Function[1]: + - func[0] sig=0 +Memory[1]: + - memory[0] pages: initial=0 +Custom: + - name: "metadata.code.test" + - func[0]: + - meta[1]: + - 0000000: 01 . +Code[1]: + - func[0] size=5 + +Code Disassembly: + +000039 func[0]: + 00003a: 41 01 | i32.const 1 + 00003c: 0f | return + 00003d: 0b | end +;;; STDOUT ;;) diff --git a/test/binary/bad-code-metadata-function-index.txt b/test/binary/bad-code-metadata-function-index.txt new file mode 100644 index 00000000..48bd19c6 --- /dev/null +++ b/test/binary/bad-code-metadata-function-index.txt @@ -0,0 +1,62 @@ +;;; TOOL: run-objdump-gen-wasm +;;; ARGS1: -x +magic +version +section(TYPE) { + count[1] + function params[0] results[1] i32 +} +section(FUNCTION) { + count[1] + type[0] +} +section(MEMORY) { + count[1] + has_max[0] + initial[0] +} +section("metadata.code.test") { + function_count[1] + function_index[2] + ann_count[1] + ann_offset[1] + ann_data_size[1] + ann_data[1] +} + +section(CODE) { + count[1] + func { + local_decls[0] + i32.const 1 + return + } +} + +(;; STDERR ;;; +000002f: warning: invalid function index: 2 +;;; STDERR ;;) +(;; STDOUT ;;; + +bad-code-metadata-function-index.wasm: file format wasm 0x1 + +Section Details: + +Type[1]: + - type[0] () -> i32 +Function[1]: + - func[0] sig=0 +Memory[1]: + - memory[0] pages: initial=0 +Custom: + - name: "metadata.code.test" +Code[1]: + - func[0] size=5 + +Code Disassembly: + +000037 func[0]: + 000038: 41 01 | i32.const 1 + 00003a: 0f | return + 00003b: 0b | end +;;; STDOUT ;;) diff --git a/test/binary/bad-code-metadata-function-out-of-order.txt b/test/binary/bad-code-metadata-function-out-of-order.txt new file mode 100644 index 00000000..b0c05890 --- /dev/null +++ b/test/binary/bad-code-metadata-function-out-of-order.txt @@ -0,0 +1,82 @@ +;;; TOOL: run-objdump-gen-wasm +;;; ARGS1: -x +magic +version +section(TYPE) { + count[1] + function params[0] results[1] i32 +} +section(FUNCTION) { + count[2] + type[0] + type[0] +} +section(MEMORY) { + count[1] + has_max[0] + initial[0] +} +section("metadata.code.test") { + function_count[2] + function_index[1] + ann_count[1] + ann_offset[1] + ann_data_size[1] + ann_data[1] + function_index[0] + ann_count[1] + ann_offset[1] + ann_data_size[1] + ann_data[1] +} + +section(CODE) { + count[2] + func { + local_decls[0] + i32.const 1 + return + } + func { + local_decls[0] + i32.const 1 + return + } +} + +(;; STDERR ;;; +0000035: warning: function index out of order: 0 +;;; STDERR ;;) +(;; STDOUT ;;; + +bad-code-metadata-function-out-of-order.wasm: file format wasm 0x1 + +Section Details: + +Type[1]: + - type[0] () -> i32 +Function[2]: + - func[0] sig=0 + - func[1] sig=0 +Memory[1]: + - memory[0] pages: initial=0 +Custom: + - name: "metadata.code.test" + - func[1]: + - meta[1]: + - 0000000: 01 . +Code[2]: + - func[0] size=5 + - func[1] size=5 + +Code Disassembly: + +00003d func[0]: + 00003e: 41 01 | i32.const 1 + 000040: 0f | return + 000041: 0b | end +000043 func[1]: + 000044: 41 01 | i32.const 1 + 000046: 0f | return + 000047: 0b | end +;;; STDOUT ;;) diff --git a/test/binary/bad-code-metadata-instance-count.txt b/test/binary/bad-code-metadata-instance-count.txt new file mode 100644 index 00000000..c8be5895 --- /dev/null +++ b/test/binary/bad-code-metadata-instance-count.txt @@ -0,0 +1,65 @@ +;;; TOOL: run-objdump-gen-wasm +;;; ARGS1: -x +magic +version +section(TYPE) { + count[1] + function params[0] results[1] i32 +} +section(FUNCTION) { + count[1] + type[0] +} +section(MEMORY) { + count[1] + has_max[0] + initial[0] +} +section("metadata.code.test") { + function_count[1] + function_index[0] + ann_count[2] + ann_offset[1] + ann_data_size[1] + ann_data[1] +} + +section(CODE) { + count[1] + func { + local_decls[0] + i32.const 1 + return + } +} + +(;; STDERR ;;; +0000033: warning: unable to read u32 leb128: code offset +;;; STDERR ;;) +(;; STDOUT ;;; + +bad-code-metadata-instance-count.wasm: file format wasm 0x1 + +Section Details: + +Type[1]: + - type[0] () -> i32 +Function[1]: + - func[0] sig=0 +Memory[1]: + - memory[0] pages: initial=0 +Custom: + - name: "metadata.code.test" + - func[0]: + - meta[1]: + - 0000000: 01 . +Code[1]: + - func[0] size=5 + +Code Disassembly: + +000037 func[0]: + 000038: 41 01 | i32.const 1 + 00003a: 0f | return + 00003b: 0b | end +;;; STDOUT ;;) diff --git a/test/binary/bad-code-metadata-instance-duplicate.txt b/test/binary/bad-code-metadata-instance-duplicate.txt new file mode 100644 index 00000000..95723713 --- /dev/null +++ b/test/binary/bad-code-metadata-instance-duplicate.txt @@ -0,0 +1,68 @@ +;;; TOOL: run-objdump-gen-wasm +;;; ARGS1: -x +magic +version +section(TYPE) { + count[1] + function params[0] results[1] i32 +} +section(FUNCTION) { + count[1] + type[0] +} +section(MEMORY) { + count[1] + has_max[0] + initial[0] +} +section("metadata.code.test") { + function_count[1] + function_index[0] + ann_count[2] + ann_offset[1] + ann_data_size[1] + ann_data[1] + ann_offset[1] + ann_data_size[1] + ann_data[1] +} + +section(CODE) { + count[1] + func { + local_decls[0] + i32.const 1 + return + } +} + +(;; STDERR ;;; +0000034: warning: duplicate code offset: 1 +;;; STDERR ;;) +(;; STDOUT ;;; + +bad-code-metadata-instance-duplicate.wasm: file format wasm 0x1 + +Section Details: + +Type[1]: + - type[0] () -> i32 +Function[1]: + - func[0] sig=0 +Memory[1]: + - memory[0] pages: initial=0 +Custom: + - name: "metadata.code.test" + - func[0]: + - meta[1]: + - 0000000: 01 . +Code[1]: + - func[0] size=5 + +Code Disassembly: + +00003a func[0]: + 00003b: 41 01 | i32.const 1 + 00003d: 0f | return + 00003e: 0b | end +;;; STDOUT ;;) diff --git a/test/binary/bad-code-metadata-instance-out-of-order.txt b/test/binary/bad-code-metadata-instance-out-of-order.txt new file mode 100644 index 00000000..4efb85d8 --- /dev/null +++ b/test/binary/bad-code-metadata-instance-out-of-order.txt @@ -0,0 +1,68 @@ +;;; TOOL: run-objdump-gen-wasm +;;; ARGS1: -x +magic +version +section(TYPE) { + count[1] + function params[0] results[1] i32 +} +section(FUNCTION) { + count[1] + type[0] +} +section(MEMORY) { + count[1] + has_max[0] + initial[0] +} +section("metadata.code.test") { + function_count[1] + function_index[0] + ann_count[2] + ann_offset[3] + ann_data_size[1] + ann_data[1] + ann_offset[1] + ann_data_size[1] + ann_data[1] +} + +section(CODE) { + count[1] + func { + local_decls[0] + i32.const 1 + return + } +} + +(;; STDERR ;;; +0000034: warning: code offset out of order: 1 +;;; STDERR ;;) +(;; STDOUT ;;; + +bad-code-metadata-instance-out-of-order.wasm: file format wasm 0x1 + +Section Details: + +Type[1]: + - type[0] () -> i32 +Function[1]: + - func[0] sig=0 +Memory[1]: + - memory[0] pages: initial=0 +Custom: + - name: "metadata.code.test" + - func[0]: + - meta[3]: + - 0000000: 01 . +Code[1]: + - func[0] size=5 + +Code Disassembly: + +00003a func[0]: + 00003b: 41 01 | i32.const 1 + 00003d: 0f | return + 00003e: 0b | end +;;; STDOUT ;;) diff --git a/test/binary/code-metadata-section.txt b/test/binary/code-metadata-section.txt new file mode 100644 index 00000000..9625f602 --- /dev/null +++ b/test/binary/code-metadata-section.txt @@ -0,0 +1,61 @@ +;;; TOOL: run-objdump-gen-wasm +;;; ARGS: -x +magic +version +section(TYPE) { + count[1] + function params[0] results[1] i32 +} +section(FUNCTION) { + count[1] + type[0] +} +section(MEMORY) { + count[1] + has_max[0] + initial[0] +} +section("metadata.code.test") { + function_count[1] + function_index[0] + ann_count[1] + ann_offset[1] + ann_data_size[1] + ann_data[1] +} + +section(CODE) { + count[1] + func { + local_decls[0] + i32.const 1 + return + } +} +(;; STDOUT ;;; + +code-metadata-section.wasm: file format wasm 0x1 + +Section Details: + +Type[1]: + - type[0] () -> i32 +Function[1]: + - func[0] sig=0 +Memory[1]: + - memory[0] pages: initial=0 +Custom: + - name: "metadata.code.test" + - func[0]: + - meta[1]: + - 0000000: 01 . +Code[1]: + - func[0] size=5 + +Code Disassembly: + +000037 func[0]: + 000038: 41 01 | i32.const 1 + 00003a: 0f | return + 00003b: 0b | end +;;; STDOUT ;;) diff --git a/test/decompile/code-metadata.txt b/test/decompile/code-metadata.txt new file mode 100644 index 00000000..1efd9d65 --- /dev/null +++ b/test/decompile/code-metadata.txt @@ -0,0 +1,17 @@ +;;; TOOL: run-wasm-decompile + +(module + (func $f (param i32) (result i32) + i32.const 1234 + local.get 0 + (@metadata.code.test "aa\01a") i32.add + return)) + +(;; STDOUT ;;; +function f_a(a:int):int { + let t0 = a; + // @metadata.code.test "aa\01a"; + return 1234 + t0; +} + +;;; STDOUT ;;) diff --git a/test/dump/code-metadata.txt b/test/dump/code-metadata.txt new file mode 100644 index 00000000..1b2254d7 --- /dev/null +++ b/test/dump/code-metadata.txt @@ -0,0 +1,43 @@ +;;; TOOL: run-objdump +;;; ARGS0: --enable-annotations --enable-code-metadata +;;; ARGS1: --headers --details +(module + (func $f (param i32) (result i32) + i32.const 1234 + local.get 0 + (@metadata.code.test "aa\01a") i32.add + return)) +(;; STDOUT ;;; + +code-metadata.wasm: file format wasm 0x1 + +Sections: + + Type start=0x0000000a end=0x00000010 (size=0x00000006) count: 1 + Function start=0x00000012 end=0x00000014 (size=0x00000002) count: 1 + Custom start=0x00000016 end=0x00000032 (size=0x0000001c) "metadata.code.test" + Code start=0x00000034 end=0x0000003f (size=0x0000000b) count: 1 + +Section Details: + +Type[1]: + - type[0] (i32) -> i32 +Function[1]: + - func[0] sig=0 +Custom: + - name: "metadata.code.test" + - func[0]: + - meta[6]: + - 0000000: 6161 0161 aa.a +Code[1]: + - func[0] size=9 + +Code Disassembly: + +000036 func[0]: + 000037: 41 d2 09 | i32.const 1234 + 00003a: 20 00 | local.get 0 + 00003c: 6a | i32.add + 00003d: 0f | return + 00003e: 0b | end +;;; STDOUT ;;) diff --git a/test/help/spectest-interp.txt b/test/help/spectest-interp.txt index 225a3938..bbb68061 100644 --- a/test/help/spectest-interp.txt +++ b/test/help/spectest-interp.txt @@ -25,6 +25,7 @@ options: --disable-bulk-memory Disable Bulk-memory operations --disable-reference-types Disable Reference types (externref) --enable-annotations Enable Custom annotation syntax + --enable-code-metadata Enable Code metadata --enable-gc Enable Garbage collection --enable-memory64 Enable 64-bit memory --enable-multi-memory Enable Multi-memory diff --git a/test/help/wasm-interp.txt b/test/help/wasm-interp.txt index efbe0409..f6dc26b7 100644 --- a/test/help/wasm-interp.txt +++ b/test/help/wasm-interp.txt @@ -36,6 +36,7 @@ options: --disable-bulk-memory Disable Bulk-memory operations --disable-reference-types Disable Reference types (externref) --enable-annotations Enable Custom annotation syntax + --enable-code-metadata Enable Code metadata --enable-gc Enable Garbage collection --enable-memory64 Enable 64-bit memory --enable-multi-memory Enable Multi-memory diff --git a/test/help/wasm-opcodecnt.txt b/test/help/wasm-opcodecnt.txt index eff83b99..2a0606b9 100644 --- a/test/help/wasm-opcodecnt.txt +++ b/test/help/wasm-opcodecnt.txt @@ -26,6 +26,7 @@ options: --disable-bulk-memory Disable Bulk-memory operations --disable-reference-types Disable Reference types (externref) --enable-annotations Enable Custom annotation syntax + --enable-code-metadata Enable Code metadata --enable-gc Enable Garbage collection --enable-memory64 Enable 64-bit memory --enable-multi-memory Enable Multi-memory diff --git a/test/help/wasm-validate.txt b/test/help/wasm-validate.txt index 3f76d737..4ceeb329 100644 --- a/test/help/wasm-validate.txt +++ b/test/help/wasm-validate.txt @@ -25,6 +25,7 @@ options: --disable-bulk-memory Disable Bulk-memory operations --disable-reference-types Disable Reference types (externref) --enable-annotations Enable Custom annotation syntax + --enable-code-metadata Enable Code metadata --enable-gc Enable Garbage collection --enable-memory64 Enable 64-bit memory --enable-multi-memory Enable Multi-memory diff --git a/test/help/wasm2wat.txt b/test/help/wasm2wat.txt index 29155532..a2b78d31 100644 --- a/test/help/wasm2wat.txt +++ b/test/help/wasm2wat.txt @@ -31,6 +31,7 @@ options: --disable-bulk-memory Disable Bulk-memory operations --disable-reference-types Disable Reference types (externref) --enable-annotations Enable Custom annotation syntax + --enable-code-metadata Enable Code metadata --enable-gc Enable Garbage collection --enable-memory64 Enable 64-bit memory --enable-multi-memory Enable Multi-memory diff --git a/test/help/wast2json.txt b/test/help/wast2json.txt index 90278883..95fa3a3e 100644 --- a/test/help/wast2json.txt +++ b/test/help/wast2json.txt @@ -28,6 +28,7 @@ options: --disable-bulk-memory Disable Bulk-memory operations --disable-reference-types Disable Reference types (externref) --enable-annotations Enable Custom annotation syntax + --enable-code-metadata Enable Code metadata --enable-gc Enable Garbage collection --enable-memory64 Enable 64-bit memory --enable-multi-memory Enable Multi-memory diff --git a/test/help/wat-desugar.txt b/test/help/wat-desugar.txt index 38f6356c..268fd600 100644 --- a/test/help/wat-desugar.txt +++ b/test/help/wat-desugar.txt @@ -35,6 +35,7 @@ options: --disable-bulk-memory Disable Bulk-memory operations --disable-reference-types Disable Reference types (externref) --enable-annotations Enable Custom annotation syntax + --enable-code-metadata Enable Code metadata --enable-gc Enable Garbage collection --enable-memory64 Enable 64-bit memory --enable-multi-memory Enable Multi-memory diff --git a/test/help/wat2wasm.txt b/test/help/wat2wasm.txt index 71ff482c..2bba6093 100644 --- a/test/help/wat2wasm.txt +++ b/test/help/wat2wasm.txt @@ -35,6 +35,7 @@ options: --disable-bulk-memory Disable Bulk-memory operations --disable-reference-types Disable Reference types (externref) --enable-annotations Enable Custom annotation syntax + --enable-code-metadata Enable Code metadata --enable-gc Enable Garbage collection --enable-memory64 Enable 64-bit memory --enable-multi-memory Enable Multi-memory diff --git a/test/roundtrip/code-metadata.txt b/test/roundtrip/code-metadata.txt new file mode 100644 index 00000000..1526f212 --- /dev/null +++ b/test/roundtrip/code-metadata.txt @@ -0,0 +1,8 @@ +;;; TOOL: run-roundtrip +;;; ARGS: --enable-annotations --enable-code-metadata +(module + (func $f (param i32) (result i32) + i32.const 1234 + local.get 0 + (@metadata.code.test "aa\01a") i32.add + return)) diff --git a/test/run-roundtrip.py b/test/run-roundtrip.py index ee765537..bf7b23cb 100755 --- a/test/run-roundtrip.py +++ b/test/run-roundtrip.py @@ -124,6 +124,8 @@ def main(args): parser.add_argument('--disable-reference-types', action='store_true') parser.add_argument('--enable-memory64', action='store_true') parser.add_argument('--enable-multi-memory', action='store_true') + parser.add_argument('--enable-annotations', action='store_true') + parser.add_argument('--enable-code-metadata', action='store_true') parser.add_argument('--inline-exports', action='store_true') parser.add_argument('--inline-imports', action='store_true') parser.add_argument('--reloc', action='store_true') @@ -146,6 +148,8 @@ def main(args): '--disable-reference-types': options.disable_reference_types, '--enable-memory64': options.enable_memory64, '--enable-multi-memory': options.enable_multi_memory, + '--enable-annotations': options.enable_annotations, + '--enable-code-metadata': options.enable_code_metadata, '--reloc': options.reloc, '--no-check': options.no_check, }) @@ -166,6 +170,8 @@ def main(args): '--enable-threads': options.enable_threads, '--enable-memory64': options.enable_memory64, '--enable-multi-memory': options.enable_multi_memory, + '--enable-annotations': options.enable_annotations, + '--enable-code-metadata': options.enable_code_metadata, '--inline-exports': options.inline_exports, '--inline-imports': options.inline_imports, '--no-debug-names': not options.debug_names, |