This is an archive of the discontinued LLVM Phabricator instance.

[BOLT][Instrumentation] Fix indirect call profile in PIE
ClosedPublic

Authored by treapster on Jun 29 2023, 10:55 AM.

Details

Summary

Proof-of-concept on how we may fix PIE indCall profile.
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. The getter works by loading static address of __hot_end via leaq, then loading dynamic address via rip-relative leaq and returning the difference. I picked the end of text because it is the closest point of .text to the runtime library, which means it will almost certainly be within the reach of load instructions (important for future AArch64 support), and because __hot_end is easy to reuse.

Diff Detail

Event Timeline

treapster created this revision.Jun 29 2023, 10:55 AM
Herald added a reviewer: Amir. · View Herald Transcript
Herald added a reviewer: maksfb. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
treapster requested review of this revision.Jun 29 2023, 10:55 AM
treapster edited the summary of this revision. (Show Details)
treapster edited the summary of this revision. (Show Details)
treapster edited the summary of this revision. (Show Details)
treapster edited the summary of this revision. (Show Details)Jun 29 2023, 10:58 AM

While this works on our server, i get the following error on my x86_64 laptop:

FAILED: CMakeFiles/bolt_rt_instr_osx.dir/instr.cpp.o 
/usr/bin/clang++  -I/home/denis/randomshit/build/tools/bolt/bolt_rt-bins -O3 -DNDEBUG -std=c++17 -target x86_64-apple-darwin19.6.0 -ffreestanding -fno-exceptions -fno-rtti -fno-stack-protector -mno-sse -fPIC -MD -MT CMakeFiles/bolt_rt_instr_osx.dir/instr.cpp.o -MF CMakeFiles/bolt_rt_instr_osx.dir/instr.cpp.o.d -o CMakeFiles/bolt_rt_instr_osx.dir/instr.cpp.o -c /home/denis/randomshit/llvm-project/bolt/runtime/instr.cpp
In file included from /home/denis/randomshit/llvm-project/bolt/runtime/instr.cpp:44:
/home/denis/randomshit/llvm-project/bolt/runtime/common.h:146:48: error: 32-bit absolute addressing is not supported in 64-bit mode
  __asm__ volatile("leaq __hot_end(%%rip), %0\n\t"
                                               ^
<inline asm>:2:2: note: instantiated into assembly here
        leaq __hot_end, %rcx
        ^

I don't get why it says 32-bit addressing when leaq is 64-bit instruction? Changing it to movq makes no difference, but it definetely has something to do with OSX which i know nothing about.

treapster edited the summary of this revision. (Show Details)Jun 29 2023, 11:11 AM
treapster edited the summary of this revision. (Show Details)
treapster edited the summary of this revision. (Show Details)Jun 29 2023, 11:25 AM
treapster edited the summary of this revision. (Show Details)

Turns out, the cause of the issue was getBaseAddress() being in the extern "C" block(it wasn't on the server). Although i still don't get why it had such effect. Also, sorry for all the edits - i just tried to make underscores work in the diff description, but apparently there is no way without backticks.

treapster updated this revision to Diff 537647.Jul 6 2023, 3:04 AM

Wait for profile in test - depends on D154436

Currently i cannot reproduce a failure which happens in CI

treapster updated this revision to Diff 538153.Jul 7 2023, 8:27 AM

getBaseAddress -> getTextBaseAddress
Use movabsq to allow big addresses
Add a comment

treapster updated this revision to Diff 538593.Jul 10 2023, 5:34 AM

Quote variables in bash

treapster updated this revision to Diff 538595.Jul 10 2023, 5:46 AM

Add -fpie compiler flag
Hope it helps with failures

treapster updated this revision to Diff 538640.Jul 10 2023, 7:35 AM

Change test to no-pie to check if PIE is the issue. If the failure persists, it must be bash script.
Unfortunately i can't reproduce it locally so i have to poke it in random places.

treapster updated this revision to Diff 538645.Jul 10 2023, 7:52 AM

Changing test to no-pie made no difference. Now i replace wait_file calls with sleeps and if it passes, the script is definitely the issue. Even though i have no idea why.

treapster updated this revision to Diff 538668.Jul 10 2023, 8:45 AM

Okay, this is stupid: it seems debian in CI just lacks fuser command, or it's not in path. I couldn't see the error because of stderr redirection to /dev/null. Here i add more return code checks and we should get an error that fuser is not found. What should we do next?

treapster updated this revision to Diff 538680.Jul 10 2023, 9:07 AM

For now i just added fuser requirement to the test.

hzq added a subscriber: hzq.Jul 13 2023, 8:15 PM
treapster updated this revision to Diff 544669.Jul 27 2023, 2:48 AM
  • Use fuser -s instead of /dev/null redirection
  • Fix wait_file.sh invoked with one extra argument
treapster updated this revision to Diff 544695.Jul 27 2023, 4:06 AM

Pushed wrong patch last time

treapster updated this revision to Diff 544704.Jul 27 2023, 4:30 AM

(once again) use -s flag with fuser

Thanks! I can start looking into this next week (and the week after it) during my shift.

rafauler accepted this revision.Aug 23 2023, 12:18 PM

Thanks for working on this. LGTM

This revision is now accepted and ready to land.Aug 23 2023, 12:18 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 23 2023, 1:51 PM