Skip to content

Commit e873e2a

Browse files
authored
Use BinaryLocation instead of hardcoding uint32_t (#2598)
This will make it easier to switch to something else for offsets in wasm binaries if we get >4GB files.
1 parent 0ec999d commit e873e2a

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

src/wasm.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,12 +1160,17 @@ struct Importable {
11601160

11611161
class Function;
11621162

1163+
// Represents an offset into a wasm binary file. This is used for debug info.
1164+
// For now, assume this is 32 bits as that's the size limit of wasm files
1165+
// anyhow.
1166+
using BinaryLocation = uint32_t;
1167+
11631168
// Represents a mapping of wasm module elements to their location in the
11641169
// binary representation. This is used for general debugging info support.
11651170
// Offsets are relative to the beginning of the code section, as in DWARF.
11661171
struct BinaryLocations {
11671172
struct Span {
1168-
uint32_t start, end;
1173+
BinaryLocation start, end;
11691174
};
11701175
std::unordered_map<Expression*, Span> expressions;
11711176
std::unordered_map<Function*, Span> functions;
@@ -1204,7 +1209,7 @@ class Function : public Importable {
12041209

12051210
// Source maps debugging info: map expression nodes to their file, line, col.
12061211
struct DebugLocation {
1207-
uint32_t fileIndex, lineNumber, columnNumber;
1212+
BinaryLocation fileIndex, lineNumber, columnNumber;
12081213
bool operator==(const DebugLocation& other) const {
12091214
return fileIndex == other.fileIndex && lineNumber == other.lineNumber &&
12101215
columnNumber == other.columnNumber;

src/wasm/wasm-binary.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,9 @@ void WasmBinaryWriter::writeFunctions() {
346346
}
347347
}
348348
if (!binaryLocationTrackedExpressionsForFunc.empty()) {
349-
binaryLocations.functions[func] = BinaryLocations::Span{
350-
uint32_t(start - adjustmentForLEBShrinking), uint32_t(o.size())};
349+
binaryLocations.functions[func] =
350+
BinaryLocations::Span{BinaryLocation(start - adjustmentForLEBShrinking),
351+
BinaryLocation(o.size())};
351352
}
352353
tableOfContents.functionBodies.emplace_back(
353354
func->name, sizePos + sizeFieldSize, size);
@@ -712,7 +713,7 @@ void WasmBinaryWriter::writeDebugLocation(Expression* curr, Function* func) {
712713
// binary locations tracked, then track it in the output as well.
713714
if (func && !func->expressionLocations.empty()) {
714715
binaryLocations.expressions[curr] =
715-
BinaryLocations::Span{uint32_t(o.size()), 0};
716+
BinaryLocations::Span{BinaryLocation(o.size()), 0};
716717
binaryLocationTrackedExpressionsForFunc.push_back(curr);
717718
}
718719
}
@@ -1383,8 +1384,8 @@ void WasmBinaryBuilder::readFunctions() {
13831384

13841385
if (DWARF) {
13851386
func->funcLocation =
1386-
BinaryLocations::Span{uint32_t(pos - codeSectionLocation),
1387-
uint32_t(pos - codeSectionLocation + size)};
1387+
BinaryLocations::Span{BinaryLocation(pos - codeSectionLocation),
1388+
BinaryLocation(pos - codeSectionLocation + size)};
13881389
}
13891390

13901391
readNextDebugLocation();
@@ -2306,8 +2307,8 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) {
23062307
}
23072308
if (DWARF && currFunction) {
23082309
currFunction->expressionLocations[curr] =
2309-
BinaryLocations::Span{uint32_t(startPos - codeSectionLocation),
2310-
uint32_t(pos - codeSectionLocation)};
2310+
BinaryLocations::Span{BinaryLocation(startPos - codeSectionLocation),
2311+
BinaryLocation(pos - codeSectionLocation)};
23112312
}
23122313
}
23132314
BYN_TRACE("zz recurse from " << depth-- << " at " << pos << std::endl);

src/wasm/wasm-debug.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ struct LineState {
334334
// to let us use contextual information (we may know we are looking up the end
335335
// of an instruction).
336336
struct AddrExprMap {
337-
std::unordered_map<uint32_t, Expression*> startMap;
338-
std::unordered_map<uint32_t, Expression*> endMap;
337+
std::unordered_map<BinaryLocation, Expression*> startMap;
338+
std::unordered_map<BinaryLocation, Expression*> endMap;
339339

340340
// Construct the map from the binaryLocations loaded from the wasm.
341341
AddrExprMap(const Module& wasm) {
@@ -353,15 +353,15 @@ struct AddrExprMap {
353353
}
354354
}
355355

356-
Expression* getStart(uint32_t addr) const {
356+
Expression* getStart(BinaryLocation addr) const {
357357
auto iter = startMap.find(addr);
358358
if (iter != startMap.end()) {
359359
return iter->second;
360360
}
361361
return nullptr;
362362
}
363363

364-
Expression* getEnd(uint32_t addr) const {
364+
Expression* getEnd(BinaryLocation addr) const {
365365
auto iter = endMap.find(addr);
366366
if (iter != endMap.end()) {
367367
return iter->second;
@@ -382,7 +382,7 @@ struct AddrExprMap {
382382
// map for the start and end addresses, since there is no chance of a function's
383383
// start overlapping with another's end (there is the size LEB in the middle).
384384
struct FuncAddrMap {
385-
std::unordered_map<uint32_t, Function*> map;
385+
std::unordered_map<BinaryLocation, Function*> map;
386386

387387
// Construct the map from the binaryLocations loaded from the wasm.
388388
FuncAddrMap(const Module& wasm) {
@@ -392,7 +392,7 @@ struct FuncAddrMap {
392392
}
393393
}
394394

395-
Function* get(uint32_t addr) const {
395+
Function* get(BinaryLocation addr) const {
396396
auto iter = map.find(addr);
397397
if (iter != map.end()) {
398398
return iter->second;
@@ -436,29 +436,29 @@ struct LocationUpdater {
436436
// Updates an expression's address. If there was never an instruction at that
437437
// address, or if there was but if that instruction no longer exists, return
438438
// 0. Otherwise, return the new updated location.
439-
uint32_t getNewExprAddr(uint32_t oldAddr) const {
439+
BinaryLocation getNewExprAddr(BinaryLocation oldAddr) const {
440440
if (auto* expr = oldExprAddrMap.getStart(oldAddr)) {
441441
auto iter = newLocations.expressions.find(expr);
442442
if (iter != newLocations.expressions.end()) {
443-
uint32_t newAddr = iter->second.start;
443+
BinaryLocation newAddr = iter->second.start;
444444
return newAddr;
445445
}
446446
}
447447
return 0;
448448
}
449449

450-
uint32_t getNewExprEndAddr(uint32_t oldAddr) const {
450+
BinaryLocation getNewExprEndAddr(BinaryLocation oldAddr) const {
451451
if (auto* expr = oldExprAddrMap.getEnd(oldAddr)) {
452452
auto iter = newLocations.expressions.find(expr);
453453
if (iter != newLocations.expressions.end()) {
454-
uint32_t newAddr = iter->second.end;
454+
BinaryLocation newAddr = iter->second.end;
455455
return newAddr;
456456
}
457457
}
458458
return 0;
459459
}
460460

461-
uint32_t getNewFuncAddr(uint32_t oldAddr) const {
461+
BinaryLocation getNewFuncAddr(BinaryLocation oldAddr) const {
462462
if (auto* func = oldFuncAddrMap.get(oldAddr)) {
463463
// The function might have been optimized away, check.
464464
auto iter = newLocations.functions.find(func);
@@ -482,8 +482,8 @@ static void updateDebugLines(llvm::DWARFYAML::Data& data,
482482
// Parse the original opcodes and emit new ones.
483483
LineState state(table);
484484
// All the addresses we need to write out.
485-
std::vector<uint32_t> newAddrs;
486-
std::unordered_map<uint32_t, LineState> newAddrInfo;
485+
std::vector<BinaryLocation> newAddrs;
486+
std::unordered_map<BinaryLocation, LineState> newAddrInfo;
487487
// If the address was zeroed out, we must omit the entire range (we could
488488
// also leave it unchanged, so that the debugger ignores it based on the
489489
// initial zero; but it's easier and better to just not emit it at all).
@@ -523,7 +523,7 @@ static void updateDebugLines(llvm::DWARFYAML::Data& data,
523523
{
524524
std::vector<llvm::DWARFYAML::LineTableOpcode> newOpcodes;
525525
LineState state(table);
526-
for (uint32_t addr : newAddrs) {
526+
for (BinaryLocation addr : newAddrs) {
527527
LineState oldState(state);
528528
state = newAddrInfo.at(addr);
529529
if (state.needToEmit()) {
@@ -557,7 +557,7 @@ static void updateDIE(const llvm::DWARFDebugInfoEntry& DIE,
557557
auto tag = DIE.getTag();
558558
// Pairs of low/high_pc require some special handling, as the high
559559
// may be an offset relative to the low. First, process the low_pcs.
560-
uint32_t oldLowPC = 0, newLowPC = 0;
560+
BinaryLocation oldLowPC = 0, newLowPC = 0;
561561
iterContextAndYAML(
562562
abbrevDecl->attributes(),
563563
yamlEntry.Values,
@@ -567,7 +567,7 @@ static void updateDIE(const llvm::DWARFDebugInfoEntry& DIE,
567567
if (attr != llvm::dwarf::DW_AT_low_pc) {
568568
return;
569569
}
570-
uint32_t oldValue = yamlValue.Value, newValue = 0;
570+
BinaryLocation oldValue = yamlValue.Value, newValue = 0;
571571
if (tag == llvm::dwarf::DW_TAG_GNU_call_site ||
572572
tag == llvm::dwarf::DW_TAG_inlined_subroutine ||
573573
tag == llvm::dwarf::DW_TAG_lexical_block ||
@@ -596,7 +596,7 @@ static void updateDIE(const llvm::DWARFDebugInfoEntry& DIE,
596596
if (attr != llvm::dwarf::DW_AT_high_pc) {
597597
return;
598598
}
599-
uint32_t oldValue = yamlValue.Value, newValue = 0;
599+
BinaryLocation oldValue = yamlValue.Value, newValue = 0;
600600
bool isRelative = attrSpec.Form == llvm::dwarf::DW_FORM_data4;
601601
if (isRelative) {
602602
oldValue += oldLowPC;

0 commit comments

Comments
 (0)