Skip to content

Commit

Permalink
[BOLT][Instrumentation] Fix indirect call profile in PIE
Browse files Browse the repository at this point in the history
Because indirect call tables use static addresses for call sites, but pc
values recorded by runtime may be subject to ASLR in PIE, we couldn't
find indirect call descriptions by their runtime address in PIE. It
resulted in [unknown] entries in profile for all indirect calls. We need
to substract base address of .text from runtime addresses to get the
corresponding static addresses. Here we create a getter for base address
of .text and substract it's return value from recorded PC values. It
converts them to static addresses, which then may be used to find the
corresponding indirect call descriptions.

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D154121
  • Loading branch information
treapster committed Aug 23, 2023
1 parent a799298 commit a86dd9a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 3 additions & 2 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1845,8 +1845,9 @@ void RewriteInstance::adjustCommandLineOptions() {
exit(1);
}

if (opts::ReorderFunctions != ReorderFunctions::RT_NONE &&
!opts::HotText.getNumOccurrences()) {
if (opts::Instrument ||
(opts::ReorderFunctions != ReorderFunctions::RT_NONE &&
!opts::HotText.getNumOccurrences())) {
opts::HotText = true;
} else if (opts::HotText && !BC->HasRelocations) {
errs() << "BOLT-WARNING: hot text is disabled in non-relocation mode\n";
Expand Down
14 changes: 14 additions & 0 deletions bolt/runtime/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ int memcmp(const void *s1, const void *s2, size_t n) {
// Anonymous namespace covering everything but our library entry point
namespace {

// Get the difference between runtime addrress of .text section and
// static address in section header table. Can be extracted from arbitrary
// pc value recorded at runtime to get the corresponding static address, which
// in turn can be used to search for indirect call description. Needed because
// indirect call descriptions are read-only non-relocatable data.
uint64_t getTextBaseAddress() {
uint64_t DynAddr;
uint64_t StaticAddr;
__asm__ volatile("leaq __hot_end(%%rip), %0\n\t"
"movabsq $__hot_end, %1\n\t"
: "=r"(DynAddr), "=r"(StaticAddr));
return DynAddr - StaticAddr;
}

constexpr uint32_t BufSize = 10240;

#define _STRINGIFY(x) #x
Expand Down
9 changes: 8 additions & 1 deletion bolt/runtime/instr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ class BumpPtrAllocator {
/// __bolt_instr_setup, our initialization routine.
BumpPtrAllocator *GlobalAlloc;

// Base address which we substract from recorded PC values when searching for
// indirect call description entries. Needed because indCall descriptions are
// mapped read-only and contain static addresses. Initialized in
// __bolt_instr_setup.
uint64_t TextBaseAddress = 0;

// Storage for GlobalAlloc which can be shared if not using
// instrumentation-file-append-pid.
void *GlobalMetadataStorage;
Expand Down Expand Up @@ -1389,7 +1395,7 @@ void visitIndCallCounter(IndirectCallHashTable::MapEntry &Entry,
const IndCallDescription *CallsiteDesc =
&Ctx->IndCallDescriptions[CallsiteID];
const IndCallTargetDescription *TargetDesc =
Ctx->lookupIndCallTarget(Entry.Key);
Ctx->lookupIndCallTarget(Entry.Key - TextBaseAddress);
if (!TargetDesc) {
DEBUG(report("Failed to lookup indirect call target\n"));
char LineBuf[BufSize];
Expand Down Expand Up @@ -1609,6 +1615,7 @@ extern "C" void __bolt_instr_indirect_tailcall();
extern "C" void __attribute((force_align_arg_pointer)) __bolt_instr_setup() {
__bolt_ind_call_counter_func_pointer = __bolt_instr_indirect_call;
__bolt_ind_tailcall_counter_func_pointer = __bolt_instr_indirect_tailcall;
TextBaseAddress = getTextBaseAddress();

const uint64_t CountersStart =
reinterpret_cast<uint64_t>(&__bolt_instr_locations[0]);
Expand Down

0 comments on commit a86dd9a

Please sign in to comment.