Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle single-segment source mapping in source map header decoder #6794

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions test/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions test/gtest/binary-reader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 "parser/wat-parser.h"
#include "print-test.h"
#include "wasm-binary.h"
#include "gtest/gtest.h"

using namespace wasm;

using BinaryReaderTest = PrintTest;

// Check that debug location parsers can handle single-segment mappings.
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"(
{
"version": 3,
"sources": [],
"names": [],
"mappings": "A"
}
)";
std::stringstream sourceMapStream(sourceMap);

// Test `readSourceMapHeader` (only check for errors, as there is no mapping
// to print).
{
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();
}
}
Loading