From 996342157be83168e9a95a7e3c4a8fe72cf516c2 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 9 Jan 2026 10:46:16 -0800 Subject: [PATCH 1/3] fix --- src/wasm/wasm-binary.cpp | 14 +++++++++-- test/lit/debug/source-map-url.wast | 38 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 test/lit/debug/source-map-url.wast diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 556fc535e32..52a6b11fb5e 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1372,9 +1372,19 @@ void WasmBinaryWriter::writeSourceMapEpilog() { void WasmBinaryWriter::writeLateCustomSections() { for (auto& section : wasm->customSections) { - if (section.name != BinaryConsts::CustomSections::Dylink) { - writeCustomSection(section); + if (section.name == BinaryConsts::CustomSections::Dylink) { + // This is an early custom section. + continue; } + + if (section.name == BinaryConsts::CustomSections::SourceMapUrl && sourceMap + && !sourceMapUrl.empty()) { + // We are writing a SourceMapURL manually, following the user's request. + // Do not emit the existing custom section as a second one. + continue; + } + + writeCustomSection(section); } } diff --git a/test/lit/debug/source-map-url.wast b/test/lit/debug/source-map-url.wast new file mode 100644 index 00000000000..8d5df7d6b27 --- /dev/null +++ b/test/lit/debug/source-map-url.wast @@ -0,0 +1,38 @@ + +(module + (func $test (param i32) (result i32) + ;;@ waka:100:1 + (i32.const 42) + ) +) + +;; 1. Generate a binary with a source map and url. +;; +;; RUN: wasm-opt %s -g -o %t.wasm -osm %t.wasm.map -osu=one +;; RUN: wasm-dis %t.wasm | filecheck %s --check-prefix=ONE + +;; ONE: ;; custom section "sourceMappingURL", size 4 + + +;; 2. Round-trip that binary, again using -osu. This should not end up with two +;; sourceMappingURL sections (one could arrive from the flag, and one from the +;; existing custom section, if we copy it). Note the length increases, from +;; "one\0" (4) to "swap\0" (5), as we swap the URL. +;; +;; RUN: wasm-opt %t.wasm -o %t.wasm2 -ism %t.wasm.map -osm %t.wasm.map2 -osu=swap +;; RUN: wasm-dis %t.wasm2 | filecheck %s --check-prefix=SWAP + +;; Look for a wrong section both before and after the correct one. + +;; SWAP-NOT: ;; custom section "sourceMappingURL", size 4 +;; SWAP: ;; custom section "sourceMappingURL", size 5 +;; SWAP-NOT: ;; custom section "sourceMappingURL" + + +;; 3. Round-trip without -osu. Now we just copy the old URL. +;; +;; RUN: wasm-opt %t.wasm2 -o %t.wasm3 -ism %t.wasm.map2 -osm %t.wasm.map3 +;; RUN: wasm-dis %t.wasm3 | filecheck %s --check-prefix=ROUND + +;; ROUND: ;; custom section "sourceMappingURL", size 5 + From 82caacf2d1efa34811758d3b54d7fb1dfb07e429 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 9 Jan 2026 10:46:40 -0800 Subject: [PATCH 2/3] format --- src/wasm/wasm-binary.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 9cbc82e254d..9cc55af0522 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1378,8 +1378,8 @@ void WasmBinaryWriter::writeLateCustomSections() { continue; } - if (section.name == BinaryConsts::CustomSections::SourceMapUrl && sourceMap - && !sourceMapUrl.empty()) { + if (section.name == BinaryConsts::CustomSections::SourceMapUrl && + sourceMap && !sourceMapUrl.empty()) { // We are writing a SourceMapURL manually, following the user's request. // Do not emit the existing custom section as a second one. continue; From 0ecf8d7432ceb3c3539caa10f915f4e7d9418b94 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 12 Jan 2026 16:22:32 -0800 Subject: [PATCH 3/3] Update test/lit/debug/source-map-url.wast Co-authored-by: Heejin Ahn --- test/lit/debug/source-map-url.wast | 1 - 1 file changed, 1 deletion(-) diff --git a/test/lit/debug/source-map-url.wast b/test/lit/debug/source-map-url.wast index 8d5df7d6b27..906ae8efb61 100644 --- a/test/lit/debug/source-map-url.wast +++ b/test/lit/debug/source-map-url.wast @@ -1,4 +1,3 @@ - (module (func $test (param i32) (result i32) ;;@ waka:100:1