diff options
-rw-r--r-- | src/binary-reader.cc | 21 | ||||
-rw-r--r-- | src/tools/wasm-link.cc | 14 | ||||
-rw-r--r-- | test/binary/bad-names-duplicate-locals.txt | 34 | ||||
-rw-r--r-- | test/binary/bad-names-duplicates.txt | 26 | ||||
-rw-r--r-- | test/binary/bad-names-function-locals-out-of-order.txt | 37 | ||||
-rw-r--r-- | test/binary/bad-names-locals-out-of-order.txt | 34 | ||||
-rw-r--r-- | test/binary/bad-names-out-of-order.txt | 27 | ||||
-rw-r--r-- | test/link/function_calls.txt | 46 |
8 files changed, 215 insertions, 24 deletions
diff --git a/src/binary-reader.cc b/src/binary-reader.cc index 48e0fbed..7c9e4d4b 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -1021,11 +1021,19 @@ Result BinaryReader::ReadNamesSection(Offset section_size) { Index num_names; CHECK_RESULT(ReadIndex(&num_names, "name count")); CALLBACK(OnFunctionNamesCount, num_names); + Index last_function_index = kInvalidIndex; + for (Index j = 0; j < num_names; ++j) { Index function_index; StringSlice function_name; CHECK_RESULT(ReadIndex(&function_index, "function index")); + ERROR_UNLESS(function_index != last_function_index, + "duplicate function name: %u", function_index); + ERROR_UNLESS(last_function_index == kInvalidIndex || + function_index > last_function_index, + "function index out of order: %u", function_index); + last_function_index = function_index; ERROR_UNLESS(function_index < NumTotalFuncs(), "invalid function index: %" PRIindex, function_index); CHECK_RESULT(ReadStr(&function_name, "function name")); @@ -1039,19 +1047,32 @@ Result BinaryReader::ReadNamesSection(Offset section_size) { Index num_funcs; CHECK_RESULT(ReadIndex(&num_funcs, "function count")); CALLBACK(OnLocalNameFunctionCount, num_funcs); + Index last_function_index = kInvalidIndex; for (Index j = 0; j < num_funcs; ++j) { Index function_index; CHECK_RESULT(ReadIndex(&function_index, "function index")); ERROR_UNLESS(function_index < NumTotalFuncs(), "invalid function index: %u", function_index); + ERROR_UNLESS(last_function_index == kInvalidIndex || + function_index > last_function_index, + "locals function index out of order: %u", + function_index); + last_function_index = function_index; Index num_locals; CHECK_RESULT(ReadIndex(&num_locals, "local count")); CALLBACK(OnLocalNameLocalCount, function_index, num_locals); + Index last_local_index = kInvalidIndex; for (Index k = 0; k < num_locals; ++k) { Index local_index; StringSlice local_name; CHECK_RESULT(ReadIndex(&local_index, "named index")); + ERROR_UNLESS(local_index != last_local_index, + "duplicate local index: %u", local_index); + ERROR_UNLESS(last_local_index == kInvalidIndex || + local_index > last_local_index, + "local index out of order: %u", local_index); + last_local_index = local_index; CHECK_RESULT(ReadStr(&local_name, "name")); CALLBACK(OnLocalName, function_index, local_index, local_name); } diff --git a/src/tools/wasm-link.cc b/src/tools/wasm-link.cc index b69f2b5a..240813c1 100644 --- a/src/tools/wasm-link.cc +++ b/src/tools/wasm-link.cc @@ -523,9 +523,11 @@ static void write_names_section(Context* ctx) { stream->WriteU8Enum(NameSectionSubsection::Function, "subsection code"); WRITE_UNKNOWN_SIZE(stream); write_u32_leb128(stream, total_count, "element count"); + + // Write import names for (const std::unique_ptr<LinkerInputBinary>& binary: ctx->inputs) { for (size_t i = 0; i < binary->debug_names.size(); i++) { - if (binary->debug_names[i].empty()) + if (binary->debug_names[i].empty() || !binary->IsFunctionImport(i)) continue; if (binary->IsInactiveFunctionImport(i)) continue; @@ -534,6 +536,16 @@ static void write_names_section(Context* ctx) { } } + // Write non-import names + for (const std::unique_ptr<LinkerInputBinary>& binary: ctx->inputs) { + for (size_t i = 0; i < binary->debug_names.size(); i++) { + if (binary->debug_names[i].empty() || binary->IsFunctionImport(i)) + continue; + write_u32_leb128(stream, binary->RelocateFuncIndex(i), "function index"); + write_string(stream, binary->debug_names[i], "function name"); + } + } + FIXUP_SIZE(stream); FIXUP_SIZE(stream); diff --git a/test/binary/bad-names-duplicate-locals.txt b/test/binary/bad-names-duplicate-locals.txt new file mode 100644 index 00000000..90d23f26 --- /dev/null +++ b/test/binary/bad-names-duplicate-locals.txt @@ -0,0 +1,34 @@ +;;; ERROR: 1 +;;; TOOL: run-gen-wasm +magic +version +section(TYPE) { count[1] function params[0] results[1] i32 } +section(FUNCTION) { count[1] type[0] } +section(CODE) { + count[1] + func { + locals[decl_count[1] i32_count[2] i32] + get_local 0 + } +} +section("name") { + subsection[1] + length[5] + func_count[1] + index[0] + str("F0") + subsection[2] + length[11] + func_count[1] + index[0] + local_count[2] + index[0] + str("L0") + index[0] + str("L1") +} +(;; STDERR ;;; +Error running "wasm2wast": +error: @0x00000035: duplicate local index: 0 + +;;; STDERR ;;) diff --git a/test/binary/bad-names-duplicates.txt b/test/binary/bad-names-duplicates.txt new file mode 100644 index 00000000..87c3d6f4 --- /dev/null +++ b/test/binary/bad-names-duplicates.txt @@ -0,0 +1,26 @@ +;;; ERROR: 1 +;;; TOOL: run-gen-wasm +;; This test file contains a name section that names that same function twice +magic +version +section(TYPE) { count[1] function params[0] results[1] i32 } +section(FUNCTION) { count[2] type[0] type[0] } +section(CODE) { + count[2] + func { locals[decl_count[0]] } + func { locals[decl_count[0]] } +} +section("name") { + subsection[1] + length[9] + func_count[2] + index[0] + str("F0") + index[0] + str("F1") +} +(;; STDERR ;;; +Error running "wasm2wast": +error: @0x0000002c: duplicate function name: 0 + +;;; STDERR ;;) diff --git a/test/binary/bad-names-function-locals-out-of-order.txt b/test/binary/bad-names-function-locals-out-of-order.txt new file mode 100644 index 00000000..5ecc80a8 --- /dev/null +++ b/test/binary/bad-names-function-locals-out-of-order.txt @@ -0,0 +1,37 @@ +;;; ERROR: 1 +;;; TOOL: run-gen-wasm +magic +version +section(TYPE) { count[1] function params[0] results[1] i32 } +section(FUNCTION) { count[2] type[0] type[0] } +section(CODE) { + count[2] + func { locals[decl_count[1] i32_count[2] i32] } + func { locals[decl_count[1] i32_count[2] i32] } +} +section("name") { + subsection[1] + length[9] + func_count[2] + index[0] + str("F0") + index[1] + str("F2") + + subsection[2] + length[13] + func_count[2] + index[1] + local_count[1] + index[0] + str("L0") + index[0] + local_count[1] + index[0] + str("L0") +} +(;; STDERR ;;; +Error running "wasm2wast": +error: @0x0000003d: locals function index out of order: 0 + +;;; STDERR ;;) diff --git a/test/binary/bad-names-locals-out-of-order.txt b/test/binary/bad-names-locals-out-of-order.txt new file mode 100644 index 00000000..746f15b3 --- /dev/null +++ b/test/binary/bad-names-locals-out-of-order.txt @@ -0,0 +1,34 @@ +;;; ERROR: 1 +;;; TOOL: run-gen-wasm +magic +version +section(TYPE) { count[1] function params[0] results[1] i32 } +section(FUNCTION) { count[1] type[0] } +section(CODE) { + count[1] + func { + locals[decl_count[1] i32_count[2] i32] + get_local 0 + } +} +section("name") { + subsection[1] + length[5] + func_count[1] + index[0] + str("F0") + subsection[2] + length[11] + func_count[1] + index[0] + local_count[2] + index[1] + str("L1") + index[0] + str("L0") +} +(;; STDERR ;;; +Error running "wasm2wast": +error: @0x00000035: local index out of order: 0 + +;;; STDERR ;;) diff --git a/test/binary/bad-names-out-of-order.txt b/test/binary/bad-names-out-of-order.txt new file mode 100644 index 00000000..d3f28447 --- /dev/null +++ b/test/binary/bad-names-out-of-order.txt @@ -0,0 +1,27 @@ +;;; ERROR: 1 +;;; TOOL: run-gen-wasm +;; This test file contains two functions, but their names are listed in the +;; names section out of order (1 first, then 0) which is an error. +magic +version +section(TYPE) { count[1] function params[0] results[1] i32 } +section(FUNCTION) { count[2] type[0] type[0] } +section(CODE) { + count[2] + func { locals[decl_count[0]] } + func { locals[decl_count[0]] } +} +section("name") { + subsection[1] + length[9] + func_count[2] + index[1] + str("F1") + index[0] + str("F0") +} +(;; STDERR ;;; +Error running "wasm2wast": +error: @0x0000002c: function index out of order: 0 + +;;; STDERR ;;) diff --git a/test/link/function_calls.txt b/test/link/function_calls.txt index 0d094eb0..e6993c27 100644 --- a/test/link/function_calls.txt +++ b/test/link/function_calls.txt @@ -10,13 +10,13 @@ call $import1) ) (module - (import "__extern" "baz" (func $import0 (param f64))) - (export "foo" (func $local_func)) - (func $local_func (param i64) + (import "__extern" "baz" (func $m2_import0 (param f64))) + (export "foo" (func $m2_local_func)) + (func $m2_local_func (param i64) f64.const 1 - call $import0 + call $m2_import0 i64.const 10 - call $local_func) + call $m2_local_func) ) (;; STDOUT ;;; @@ -29,8 +29,8 @@ Sections: Function start=0x00000047 end=0x0000004a (size=0x00000003) count: 2 Export start=0x00000050 end=0x00000057 (size=0x00000007) count: 1 Code start=0x00000059 end=0x0000008b (size=0x00000032) count: 2 - Custom start=0x00000091 end=0x000000c7 (size=0x00000036) "name" - Custom start=0x000000cd end=0x000000e9 (size=0x0000001c) "reloc.Code" + Custom start=0x00000091 end=0x000000cd (size=0x0000003c) "name" + Custom start=0x000000d3 end=0x000000ef (size=0x0000001c) "reloc.Code" Section Details: @@ -41,18 +41,18 @@ Type: - [3] (i64) -> nil Import: - func[0] sig=0 <import1> <- __extern.bar - - func[1] sig=2 <import0> <- __extern.baz + - func[1] sig=2 <m2_import0> <- __extern.baz Function: - - func[2] sig=1 - - func[3] sig=3 <local_func> + - func[2] sig=1 <local_func> + - func[3] sig=3 <m2_local_func> Export: - - func[3] <local_func> -> "foo" + - func[3] <m2_local_func> -> "foo" Custom: - name: "name" - func[0] import1 + - func[1] m2_import0 - func[2] local_func - - func[1] import0 - - func[3] local_func + - func[3] m2_local_func Custom: - name: "reloc.Code" - section: Code @@ -64,21 +64,21 @@ Custom: Code Disassembly: -00005a func[2]: +00005a <local_func>: 00005c: 20 00 | get_local 0 - 00005e: 10 82 80 80 80 00 | call 2 - 00005f: R_FUNC_INDEX_LEB 2 - 000064: 10 83 80 80 80 00 | call 3 <local_func> - 000065: R_FUNC_INDEX_LEB 3 <local_func> + 00005e: 10 82 80 80 80 00 | call 2 <local_func> + 00005f: R_FUNC_INDEX_LEB 2 <local_func> + 000064: 10 83 80 80 80 00 | call 3 <m2_local_func> + 000065: R_FUNC_INDEX_LEB 3 <m2_local_func> 00006a: 10 80 80 80 80 00 | call 0 <import1> 00006b: R_FUNC_INDEX_LEB 0 <import1> 000070: 0b | end -000071 <local_func>: +000071 <m2_local_func>: 000073: 44 00 00 00 00 00 00 f0 3f | f64.const 0x1p+0 - 00007c: 10 81 80 80 80 00 | call 1 <import0> - 00007d: R_FUNC_INDEX_LEB 1 <import0> + 00007c: 10 81 80 80 80 00 | call 1 <m2_import0> + 00007d: R_FUNC_INDEX_LEB 1 <m2_import0> 000082: 42 0a | i64.const 10 - 000084: 10 83 80 80 80 00 | call 3 <local_func> - 000085: R_FUNC_INDEX_LEB 3 <local_func> + 000084: 10 83 80 80 80 00 | call 3 <m2_local_func> + 000085: R_FUNC_INDEX_LEB 3 <m2_local_func> 00008a: 0b | end ;;; STDOUT ;;) |