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

Wasm source maps #387

Merged
merged 11 commits into from
Oct 1, 2024
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ add_library(libbloaty STATIC
src/range_map.cc
src/range_map.h
src/re.h
src/source_map.cc
src/source_map.h
src/util.cc
src/util.h
src/webassembly.cc
Expand Down
32 changes: 32 additions & 0 deletions src/bloaty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ class Bloaty {

void AddFilename(const std::string& filename, bool base_file);
void AddDebugFilename(const std::string& filename);
void AddSourceMapFilename(const std::string& filename);

size_t GetSourceCount() const { return sources_.size(); }

Expand Down Expand Up @@ -1535,6 +1536,7 @@ class Bloaty {
std::vector<InputFileInfo> input_files_;
std::vector<InputFileInfo> base_files_;
std::map<std::string, std::string> debug_files_;
std::map<std::string, std::string> sourcemap_files_;

// For allocating memory, like to decompress compressed sections.
std::unique_ptr<google::protobuf::Arena> arena_;
Expand Down Expand Up @@ -1592,6 +1594,18 @@ void Bloaty::AddDebugFilename(const std::string& filename) {
debug_files_[build_id] = filename;
}

void Bloaty::AddSourceMapFilename(const std::string& filename) {
std::size_t delimiter = filename.find('=');
if (delimiter == std::string::npos) {
THROWF("Source map filename '$0' must have a build id and file name "
"separated by '='",
filename);
}
std::string sourcemap_build_id = filename.substr(0, delimiter);
std::string sourcemap_filename = filename.substr(delimiter + 1);
sourcemap_files_[sourcemap_build_id] = sourcemap_filename;
}

void Bloaty::DefineCustomDataSource(const CustomDataSource& source) {
if (source.base_data_source() == "symbols") {
THROW(
Expand Down Expand Up @@ -1761,6 +1775,18 @@ void Bloaty::ScanAndRollupFile(const std::string& filename, Rollup* rollup,
file->set_debug_file(debug_file.get());
out_build_ids->push_back(build_id);
}

// Maybe it's a source map file?
auto sourcemap_iter = sourcemap_files_.find(build_id);
if (sourcemap_iter != sourcemap_files_.end()) {
std::unique_ptr<InputFile> sourcemap_file(
file_factory_.OpenFile(sourcemap_iter->second));
// The build id is not present in the source map file itself, so it must
// be passed here.
debug_file = TryOpenSourceMapFile(sourcemap_file, build_id);
file->set_debug_file(debug_file.get());
out_build_ids->push_back(build_id);
}
}

int64_t filesize_before =
Expand Down Expand Up @@ -2166,6 +2192,8 @@ bool DoParseOptions(bool skip_unknown, int* argc, char** argv[],
}
} else if (args.TryParseOption("--source-filter", &option)) {
options->set_source_filter(std::string(option));
} else if (args.TryParseOption("--source-map", &option)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add documentation for this flag to the usage string and into the user guide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - added usage notes to using.md and added the option to the usage string

options->add_source_map(std::string(option));
} else if (args.TryParseFlag("-v")) {
options->set_verbose_level(1);
} else if (args.TryParseFlag("-vv")) {
Expand Down Expand Up @@ -2260,6 +2288,10 @@ void BloatyDoMain(const Options& options, const InputFileFactory& file_factory,
bloaty.AddDebugFilename(debug_filename);
}

for (auto& sourcemap : options.source_map()) {
bloaty.AddSourceMapFilename(sourcemap);
}

for (const auto& custom_data_source : options.custom_data_source()) {
bloaty.DefineCustomDataSource(custom_data_source);
}
Expand Down
2 changes: 2 additions & 0 deletions src/bloaty.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ std::unique_ptr<ObjectFile> TryOpenELFFile(std::unique_ptr<InputFile>& file);
std::unique_ptr<ObjectFile> TryOpenMachOFile(std::unique_ptr<InputFile>& file);
std::unique_ptr<ObjectFile> TryOpenWebAssemblyFile(std::unique_ptr<InputFile>& file);
std::unique_ptr<ObjectFile> TryOpenPEFile(std::unique_ptr<InputFile>& file);
std::unique_ptr<ObjectFile> TryOpenSourceMapFile(
std::unique_ptr<InputFile>& file, std::string build_id);

// Provided by dwarf.cc. To use these, a module should fill in a dwarf::File
// and then call these functions.
Expand Down
4 changes: 4 additions & 0 deletions src/bloaty.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ message Options {
// debug_filename will *not* have their file size counted.
repeated string debug_filename = 10;

// Build id to source map file names, delimited by '='.
repeated string source_map = 15;

// The data sources to scan in each file. At least one data source must be
// specified. If more than one source is specified, the output is
// hierarchical.
Expand Down Expand Up @@ -101,3 +104,4 @@ message Regex {
optional string pattern = 1;
optional string replacement = 2;
}

239 changes: 239 additions & 0 deletions src/source_map.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
// Copyright 2024 Google Inc. All Rights Reserved.
//
// 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 <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "bloaty.h"
#include "source_map.h"
#include "util.h"

#include "absl/strings/string_view.h"

namespace bloaty {
namespace sourcemap {

static bool ReadOpeningBrace(absl::string_view* data) {
return ReadFixed<char>(data) == '{';
}

static absl::string_view ReadQuotedString(absl::string_view* data) {
RequireChar(data, '\"');
// Simply read until the next '\"'. We currently do not handle escaped
// characters. Field names never contain quotes and file names are unlikely to
// contain quotes.
return ReadUntilConsuming(data, '\"');
}

// Finds the field with the given name in the source map. Any fields encountered
// before the field are skipped.
static void FindField(absl::string_view* data, const char* name) {
while (!data->empty()) {
SkipWhitespace(data);
auto field_name = ReadQuotedString(data);
if (field_name == name) {
SkipWhitespace(data);
RequireChar(data, ':');
SkipWhitespace(data);
return;
}

// Skip until the next quote. We don't expect any structures involving
// quotes in the fields that we skip.
ReadUntil(data, '\"');
}
THROWF("field \"$0\" not found in source map", name);
}

static int32_t ReadBase64VLQ(absl::string_view* data) {
uint32_t value = 0;
uint32_t shift = 0;
const char* ptr = data->data();
const char* limit = ptr + data->size();
while (ptr < limit) {
auto ch = *(ptr++);
// Base64 characters A-Z, a-f do not have the continuation bit set and are
// the last digit.
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch < 'g')) {
uint32_t digit = ch < 'a' ? ch - 'A' : ch - 'a' + 26;
value |= digit << shift;
data->remove_prefix(ptr - data->data());
return value & 1
? -static_cast<int32_t>(value >> 1)
: static_cast<int32_t>(value >> 1);
}
if (!(ch >= 'g' && ch <= 'z') && !(ch >= '0' && ch <= '9') && ch != '+' &&
ch != '/') {
THROWF("Invalid Base64VLQ digit $0", ch);
}
// Base64 characters g-z, 0-9, + and / have the continuation bit set and
// must be followed by another digit.
uint32_t digit =
ch > '9' ? ch - 'g' : (ch >= '0' ? ch - '0' + 20 : (ch == '+' ? 30 : 31));
value |= digit << shift;
shift += 5;
}

THROW("Unterminated Base64VLQ");
}

static bool IsBase64Digit(char ch) {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9') || ch == '+' || ch == '/';
}

static int ReadBase64VLQSegment(absl::string_view* data, int32_t (&values)[5]) {
for (int i = 0; i < 5; i++) {
values[i] = ReadBase64VLQ(data);
if (data->empty() || !IsBase64Digit(data->front())) {
if (i != 0 && i != 3 && i != 4) {
THROWF("Invalid source map VLQ segment length $0", i + 1);
}
return i + 1;
}
}
THROW("Unterminated Base64VLQ segment");
}

class VlqSegment {
public:
int32_t col;
int32_t length;

std::string source_file;
int32_t source_line;
int32_t source_col;

VlqSegment(int32_t col, int32_t length,
absl::string_view source_file,
int32_t source_line, int32_t source_col)
: col(col), length(length),
source_file(source_file),
source_line(source_line), source_col(source_col) {}

void addToSink(RangeSink* sink) const {
auto name = sink->data_source() == DataSource::kInlines
? source_file + ":" + std::to_string(source_line)
: source_file;
sink->AddFileRange("sourcemap", name, col, length);
}
};

template <class Func>
void ForEachVLQSegment(absl::string_view* data,
const std::vector<absl::string_view>& sources,
Func&& segment_func) {
if (data->empty() || data->front() == '\"') {
return;
}

// Read the first segment. We don't generate the `VlqSegment` until the next
// one is encountered. This one only points to a particular byte. The next
// segment is required to determine the length.
int32_t values[5];
int values_count = ReadBase64VLQSegment(data, values);
if (values_count < 4) {
THROW("Source file info expected in first VLQ segment");
}
int32_t col = values[0];
int32_t source_file = values[1];
int32_t source_line = values[2];
int32_t source_col = values[3];

while (!data->empty() && data->front() != '\"') {
if (data->front() == ',') {
data->remove_prefix(1);
continue;
}

// We don't support line separators in the source map for now.
if (data->front() == ';') {
THROW("Unsupported line separator in source map");
}

int new_values_count = ReadBase64VLQSegment(data, values);
if (values_count >= 4) {
segment_func(VlqSegment(col, values[0],
sources[source_file], source_line, source_col));
}
values_count = new_values_count;
col += values[0];
if (values_count >= 4) {
source_file += values[1];
source_line += values[2];
source_col += values[3];
}
}
}

static void ProcessToSink(absl::string_view data, RangeSink* sink) {
ReadOpeningBrace(&data);

std::vector<absl::string_view> sources;
FindField(&data, "sources");
RequireChar(&data, '[');
while (!data.empty()) {
SkipWhitespace(&data);
if (data.empty()) {
break;
}
if (data.front() == ']') {
data.remove_prefix(1);
break;
}
if (data.front() == ',') {
data.remove_prefix(1);
}
auto source = ReadQuotedString(&data);
sources.push_back(source);
}
SkipWhitespace(&data);
RequireChar(&data, ',');

FindField(&data, "mappings");
RequireChar(&data, '\"');

ForEachVLQSegment(&data, sources, [&sink](const VlqSegment& segment) {
segment.addToSink(sink);
});

RequireChar(&data, '\"');
}

void SourceMapObjectFile::ProcessFileToSink(RangeSink* sink) const {
if (sink->data_source() != DataSource::kCompileUnits
&& sink->data_source() != DataSource::kInlines) {
THROW("Source map doesn't support this data source");
}

ProcessToSink(file_data().data(), sink);
}

} // namespace sourcemap

std::unique_ptr<ObjectFile> TryOpenSourceMapFile(
std::unique_ptr<InputFile>& file, std::string build_id) {
absl::string_view data = file->data();
if (sourcemap::ReadOpeningBrace(&data)) {
return std::unique_ptr<ObjectFile>(
new sourcemap::SourceMapObjectFile(std::move(file), build_id));
}

return nullptr;
}

} // namespace bloaty

Loading