Skip to content

Commit a985e16

Browse files
authored
[Source maps] Handle single-segment entries in source map header decoder (#6794)
Single-segment mappings were already handled in readNextDebugLocation, but not in readSourceMapHeader.
1 parent 1c3578c commit a985e16

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

src/wasm/wasm-binary.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2941,13 +2941,20 @@ void WasmBinaryReader::readSourceMapHeader() {
29412941
// investigation (if it does, it will assert in readBase64VLQ, so it
29422942
// would not be a silent error at least).
29432943
uint32_t position = readBase64VLQ(*sourceMap);
2944-
uint32_t fileIndex = readBase64VLQ(*sourceMap);
2945-
uint32_t lineNumber =
2946-
readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number
2947-
uint32_t columnNumber = readBase64VLQ(*sourceMap);
29482944
nextDebugPos = position;
2949-
nextDebugLocation = {fileIndex, lineNumber, columnNumber};
2950-
nextDebugLocationHasDebugInfo = true;
2945+
2946+
auto peek = sourceMap->peek();
2947+
if (peek == ',' || peek == '\"') {
2948+
// This is a 1-length entry, so the next location has no debug info.
2949+
nextDebugLocationHasDebugInfo = false;
2950+
} else {
2951+
uint32_t fileIndex = readBase64VLQ(*sourceMap);
2952+
uint32_t lineNumber =
2953+
readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number
2954+
uint32_t columnNumber = readBase64VLQ(*sourceMap);
2955+
nextDebugLocation = {fileIndex, lineNumber, columnNumber};
2956+
nextDebugLocationHasDebugInfo = true;
2957+
}
29512958
}
29522959

29532960
void WasmBinaryReader::readNextDebugLocation() {

test/gtest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ include_directories(../../third_party/googletest/googletest/include)
22
include_directories(../../src/wasm)
33

44
set(unittest_SOURCES
5+
binary-reader.cpp
56
cfg.cpp
67
dfa_minimization.cpp
78
disjoint_sets.cpp

test/gtest/binary-reader.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2024 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "parser/wat-parser.h"
18+
#include "print-test.h"
19+
#include "wasm-binary.h"
20+
#include "gtest/gtest.h"
21+
22+
using namespace wasm;
23+
24+
using BinaryReaderTest = PrintTest;
25+
26+
// Check that debug location parsers can handle single-segment mappings.
27+
TEST_F(BinaryReaderTest, SourceMappingSingleSegment) {
28+
auto moduleText = "(module)";
29+
Module module;
30+
parseWast(module, moduleText);
31+
32+
BufferWithRandomAccess buffer;
33+
WasmBinaryWriter(&module, buffer, PassOptions());
34+
auto moduleBytes = buffer.getAsChars();
35+
36+
// A single-segment mapping starting at offset 0.
37+
std::string sourceMap = R"(
38+
{
39+
"version": 3,
40+
"sources": [],
41+
"names": [],
42+
"mappings": "A"
43+
}
44+
)";
45+
std::stringstream sourceMapStream(sourceMap);
46+
47+
// Test `readSourceMapHeader` (only check for errors, as there is no mapping
48+
// to print).
49+
{
50+
Module module;
51+
WasmBinaryReader binaryReader(module, FeatureSet::All, moduleBytes);
52+
binaryReader.setDebugLocations(&sourceMapStream);
53+
binaryReader.readSourceMapHeader();
54+
}
55+
56+
// Test `readNextDebugLocation`.
57+
{
58+
Module module;
59+
WasmBinaryReader binaryReader(module, FeatureSet::All, moduleBytes);
60+
binaryReader.setDebugLocations(&sourceMapStream);
61+
binaryReader.readNextDebugLocation();
62+
}
63+
}

0 commit comments

Comments
 (0)