From bc6dd69319d1e506282c059f48d87f3e62492cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Wed, 31 Jul 2024 11:46:58 +0200 Subject: [PATCH 1/6] Handle single-segment source mapping in source map header decoder --- src/wasm/wasm-binary.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 0a65fbadbf4..06e12b85ecb 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -2939,13 +2939,20 @@ void WasmBinaryReader::readSourceMapHeader() { // investigation (if it does, it will assert in readBase64VLQ, so it // would not be a silent error at least). uint32_t position = readBase64VLQ(*sourceMap); - uint32_t fileIndex = readBase64VLQ(*sourceMap); - uint32_t lineNumber = - readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number - uint32_t columnNumber = readBase64VLQ(*sourceMap); nextDebugPos = position; - nextDebugLocation = {fileIndex, lineNumber, columnNumber}; - nextDebugLocationHasDebugInfo = true; + + auto peek = sourceMap->peek(); + if (peek == ',' || peek == '\"') { + // This is a 1-length entry, so the next location has no debug info. + nextDebugLocationHasDebugInfo = false; + } else { + uint32_t fileIndex = readBase64VLQ(*sourceMap); + uint32_t lineNumber = + readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number + uint32_t columnNumber = readBase64VLQ(*sourceMap); + nextDebugLocation = {fileIndex, lineNumber, columnNumber}; + nextDebugLocationHasDebugInfo = true; + } } void WasmBinaryReader::readNextDebugLocation() { From 3214f8acbcd591ee852deb7c6809f2a67911a331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Thu, 1 Aug 2024 09:55:36 +0200 Subject: [PATCH 2/6] Add a unit test --- test/gtest/CMakeLists.txt | 1 + test/gtest/binary-reader.cpp | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 test/gtest/binary-reader.cpp diff --git a/test/gtest/CMakeLists.txt b/test/gtest/CMakeLists.txt index 4604eea77b0..9485c97e014 100644 --- a/test/gtest/CMakeLists.txt +++ b/test/gtest/CMakeLists.txt @@ -2,6 +2,7 @@ include_directories(../../third_party/googletest/googletest/include) include_directories(../../src/wasm) set(unittest_SOURCES + binary-reader.cpp cfg.cpp dfa_minimization.cpp json.cpp diff --git a/test/gtest/binary-reader.cpp b/test/gtest/binary-reader.cpp new file mode 100644 index 00000000000..3198dea6595 --- /dev/null +++ b/test/gtest/binary-reader.cpp @@ -0,0 +1,53 @@ +/* + * Copyright 2024 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "wasm-binary.h" +#include "gtest/gtest.h" + +using namespace wasm; + +// Check that debug location parsers can handle single-segment mappings. +TEST(BinaryReaderTest, SourceMappingSingleSegment) { + std::vector moduleBytes = { + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00}; + + // A single-segment mapping starting at offset 0. + std::string sourceMap = R"( + { + "version": 3, + "sources": [], + "names": [], + "mappings": "A" + } + )"; + std::stringstream sourceMapStream(sourceMap); + + // Test `readSourceMapHeader`. + { + Module module; + WasmBinaryReader binaryReader(module, FeatureSet::All, moduleBytes); + binaryReader.setDebugLocations(&sourceMapStream); + binaryReader.readSourceMapHeader(); + } + + // Test `readNextDebugLocation`. + { + Module module; + WasmBinaryReader binaryReader(module, FeatureSet::All, moduleBytes); + binaryReader.setDebugLocations(&sourceMapStream); + binaryReader.readNextDebugLocation(); + } +} From ff0e62a520a10db5a74c21c0231f08878d735c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Thu, 1 Aug 2024 10:03:33 +0200 Subject: [PATCH 3/6] Fix formatting --- src/wasm/wasm-binary.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 06e12b85ecb..89615ac5093 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -2948,7 +2948,7 @@ void WasmBinaryReader::readSourceMapHeader() { } else { uint32_t fileIndex = readBase64VLQ(*sourceMap); uint32_t lineNumber = - readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number + readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number uint32_t columnNumber = readBase64VLQ(*sourceMap); nextDebugLocation = {fileIndex, lineNumber, columnNumber}; nextDebugLocationHasDebugInfo = true; From 3445d0e33686508098cc11e07d4aa75143c5abbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Tue, 6 Aug 2024 13:12:44 +0200 Subject: [PATCH 4/6] Update test --- test/gtest/binary-reader.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/gtest/binary-reader.cpp b/test/gtest/binary-reader.cpp index 3198dea6595..94ad052563f 100644 --- a/test/gtest/binary-reader.cpp +++ b/test/gtest/binary-reader.cpp @@ -14,15 +14,24 @@ * limitations under the License. */ -#include "wasm-binary.h" #include "gtest/gtest.h" +#include "parser/wat-parser.h" +#include "print-test.h" +#include "wasm-binary.h" using namespace wasm; +using BinaryReaderTest = PrintTest; + // Check that debug location parsers can handle single-segment mappings. -TEST(BinaryReaderTest, SourceMappingSingleSegment) { - std::vector moduleBytes = { - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00}; +TEST_F(BinaryReaderTest, SourceMappingSingleSegment) { + auto moduleText = "(module)"; + Module module; + parseWast(module, moduleText); + + BufferWithRandomAccess buffer; + WasmBinaryWriter(&module, buffer, PassOptions()); + auto moduleBytes = buffer.getAsChars(); // A single-segment mapping starting at offset 0. std::string sourceMap = R"( From cc29a5f1a6e105322d0fe56f8e349e1b6911785d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Tue, 6 Aug 2024 19:12:41 +0200 Subject: [PATCH 5/6] Fix lint --- test/gtest/binary-reader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/gtest/binary-reader.cpp b/test/gtest/binary-reader.cpp index 94ad052563f..5ce9d7e19a8 100644 --- a/test/gtest/binary-reader.cpp +++ b/test/gtest/binary-reader.cpp @@ -14,10 +14,10 @@ * limitations under the License. */ -#include "gtest/gtest.h" #include "parser/wat-parser.h" #include "print-test.h" #include "wasm-binary.h" +#include "gtest/gtest.h" using namespace wasm; From 2833da7a96ec74c7375b019704240d3b31384cd5 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 6 Aug 2024 10:34:39 -0700 Subject: [PATCH 6/6] Update test/gtest/binary-reader.cpp --- test/gtest/binary-reader.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/gtest/binary-reader.cpp b/test/gtest/binary-reader.cpp index 5ce9d7e19a8..b73fe55bda3 100644 --- a/test/gtest/binary-reader.cpp +++ b/test/gtest/binary-reader.cpp @@ -44,7 +44,8 @@ TEST_F(BinaryReaderTest, SourceMappingSingleSegment) { )"; std::stringstream sourceMapStream(sourceMap); - // Test `readSourceMapHeader`. + // Test `readSourceMapHeader` (only check for errors, as there is no mapping + // to print). { Module module; WasmBinaryReader binaryReader(module, FeatureSet::All, moduleBytes);