Skip to content

Commit b068098

Browse files
committed
Rename getAddress to computeAddress, add a comment
1 parent 2a25f3a commit b068098

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

bolt/include/bolt/Core/MCInstUtils.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,17 @@ class MCInstReference {
9090
return nullptr;
9191
}
9292

93-
// MCCodeEmitter is not thread safe.
94-
uint64_t getAddress(const MCCodeEmitter *Emitter = nullptr) const;
93+
/// Computes the address of the instruction (or offset from base for PIC).
94+
///
95+
/// This function is intended for the use cases like debug printing, as it
96+
/// is only as precise as BinaryContext::computeCodeSize() is and requires
97+
/// iterating over the prefix of the basic block (when CFG is available) or
98+
/// of the function (when CFG is unavailable).
99+
///
100+
/// MCCodeEmitter is not thread safe and the default instance from
101+
/// BinaryContext is used by default, thus pass an instance explicitly if
102+
/// this function may be called from multithreaded code.
103+
uint64_t computeAddress(const MCCodeEmitter *Emitter = nullptr) const;
95104

96105
raw_ostream &print(raw_ostream &OS) const;
97106

bolt/lib/Core/MCInstUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ MCInstReference MCInstReference::get(const MCInst *Inst,
5353
return {};
5454
}
5555

56-
uint64_t MCInstReference::getAddress(const MCCodeEmitter *Emitter) const {
56+
uint64_t MCInstReference::computeAddress(const MCCodeEmitter *Emitter) const {
5757
assert(!empty() && "Taking instruction address by empty reference");
5858

5959
const BinaryContext &BC = getFunction()->getBinaryContext();

bolt/lib/Passes/PAuthGadgetScanner.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ static void printBB(const BinaryContext &BC, const BinaryBasicBlock *BB,
16801680
MCInstReference Inst(BB, I);
16811681
if (BC.MIB->isCFI(Inst))
16821682
continue;
1683-
BC.printInstruction(outs(), Inst, Inst.getAddress(), BF);
1683+
BC.printInstruction(outs(), Inst, Inst.computeAddress(), BF);
16841684
}
16851685
}
16861686

@@ -1700,14 +1700,15 @@ void Diagnostic::printBasicInfo(raw_ostream &OS, const BinaryContext &BC,
17001700
StringRef IssueKind) const {
17011701
const BinaryBasicBlock *BB = Location.getBasicBlock();
17021702
const BinaryFunction *BF = Location.getFunction();
1703+
const uint64_t Address = Location.computeAddress();
17031704

17041705
OS << "\nGS-PAUTH: " << IssueKind;
17051706
OS << " in function " << BF->getPrintName();
17061707
if (BB)
17071708
OS << ", basic block " << BB->getName();
1708-
OS << ", at address " << llvm::format("%x", Location.getAddress()) << "\n";
1709+
OS << ", at address " << llvm::format("%x", Address) << "\n";
17091710
OS << " The instruction is ";
1710-
BC.printInstruction(OS, Location, Location.getAddress(), BF);
1711+
BC.printInstruction(OS, Location, Address, BF);
17111712
}
17121713

17131714
void GadgetDiagnostic::generateReport(raw_ostream &OS,
@@ -1721,15 +1722,17 @@ static void printRelatedInstrs(raw_ostream &OS, const MCInstReference Location,
17211722
const BinaryContext &BC = BF.getBinaryContext();
17221723

17231724
// Sort by address to ensure output is deterministic.
1724-
SmallVector<MCInstReference> RI(RelatedInstrs);
1725-
llvm::sort(RI, [](const MCInstReference &A, const MCInstReference &B) {
1726-
return A.getAddress() < B.getAddress();
1727-
});
1725+
SmallVector<std::pair<uint64_t, MCInstReference>> RI;
1726+
for (auto &InstRef : RelatedInstrs)
1727+
RI.push_back(std::make_pair(InstRef.computeAddress(), InstRef));
1728+
llvm::sort(RI, [](auto A, auto B) { return A.first < B.first; });
1729+
17281730
for (unsigned I = 0; I < RI.size(); ++I) {
1729-
MCInstReference InstRef = RI[I];
1731+
auto [Address, InstRef] = RI[I];
17301732
OS << " " << (I + 1) << ". ";
1731-
BC.printInstruction(OS, InstRef, InstRef.getAddress(), &BF);
1733+
BC.printInstruction(OS, InstRef, InstRef.computeAddress(), &BF);
17321734
};
1735+
17331736
if (RelatedInstrs.size() == 1) {
17341737
const MCInstReference RelatedInst = RelatedInstrs[0];
17351738
// Printing the details for the MCInstReference::FunctionParent case

0 commit comments

Comments
 (0)