Skip to content

Commit

Permalink
Save build ID in a source map (#6799)
Browse files Browse the repository at this point in the history
  • Loading branch information
loganek authored Aug 15, 2024
1 parent cde2a52 commit ad08022
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ extern const char* Dylink;
extern const char* Dylink0;
extern const char* Linking;
extern const char* Producers;
extern const char* BuildId;
extern const char* TargetFeatures;

extern const char* AtomicsFeature;
Expand Down
27 changes: 26 additions & 1 deletion src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <algorithm>
#include <fstream>
#include <iomanip>

#include "ir/eh-utils.h"
#include "ir/module-utils.h"
Expand Down Expand Up @@ -1212,7 +1213,31 @@ void WasmBinaryWriter::initializeDebugInfo() {
}

void WasmBinaryWriter::writeSourceMapProlog() {
*sourceMap << "{\"version\":3,\"sources\":[";
*sourceMap << "{\"version\":3,";

for (const auto& section : wasm->customSections) {
if (section.name == BinaryConsts::CustomSections::BuildId) {
U32LEB ret;
size_t pos = 0;
ret.read([&]() { return section.data[pos++]; });

if (section.data.size() != pos + ret.value) {
std::cerr
<< "warning: build id section with an incorrect size detected!\n";
break;
}

*sourceMap << "\"debugId\":\"";
for (size_t i = pos; i < section.data.size(); i++) {
*sourceMap << std::setfill('0') << std::setw(2) << std::hex
<< static_cast<int>(static_cast<uint8_t>(section.data[i]));
}
*sourceMap << "\",";
break;
}
}

*sourceMap << "\"sources\":[";
for (size_t i = 0; i < wasm->debugInfoFileNames.size(); i++) {
if (i > 0) {
*sourceMap << ",";
Expand Down
1 change: 1 addition & 0 deletions src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const char* Dylink = "dylink";
const char* Dylink0 = "dylink.0";
const char* Linking = "linking";
const char* Producers = "producers";
const char* BuildId = "build_id";
const char* TargetFeatures = "target_features";
const char* AtomicsFeature = "atomics";
const char* BulkMemoryFeature = "bulk-memory";
Expand Down
7 changes: 7 additions & 0 deletions test/lit/binary/custom-section-build-id.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Verify that the build id is included in the source map.

;; RUN: wasm-opt %s.wasm -o %t.wasm -osm %t.map
;; RUN: cat %t.map | filecheck %s

;; CHECK: {"version":3,"debugId":"01ab23cd45ef67ab89","sources":[],"names":[],"mappings":""}

Binary file added test/lit/binary/custom-section-build-id.test.wasm
Binary file not shown.

0 comments on commit ad08022

Please sign in to comment.