Skip to content

Commit

Permalink
[BOLT] Fix stack alignment for runtime lib
Browse files Browse the repository at this point in the history
Summary:
Right now, the SAVE_ALL sequence executed upon entry of both
of our runtime libs (hugify and instrumentation) will cause the stack to
not be aligned at a 16B boundary because it saves 15 8-byte regs. Change
the code sequence to adjust for that. The compiler may generate code
that assumes the stack is aligned by using movaps instructions, which
will crash.

(cherry picked from FBD22744307)
  • Loading branch information
rafaelauler authored and maksfb committed Jul 27, 2020
1 parent ed02946 commit c6799a6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
6 changes: 5 additions & 1 deletion bolt/runtime/common.h
Expand Up @@ -6,6 +6,7 @@
#include <elf.h>
#endif

// Save all registers while keeping 16B stack alignment
#define SAVE_ALL \
"push %%rax\n" \
"push %%rbx\n" \
Expand All @@ -21,9 +22,12 @@
"push %%r12\n" \
"push %%r13\n" \
"push %%r14\n" \
"push %%r15\n"
"push %%r15\n" \
"sub $8, %%rsp\n"

// Mirrors SAVE_ALL
#define RESTORE_ALL \
"add $8, %%rsp\n" \
"pop %%r15\n" \
"pop %%r14\n" \
"pop %%r13\n" \
Expand Down
8 changes: 5 additions & 3 deletions bolt/runtime/hugify.cpp
Expand Up @@ -166,7 +166,9 @@ extern "C" void __bolt_hugify_self_impl() {

/// This is hooking ELF's entry, it needs to save all machine state.
extern "C" __attribute((naked)) void __bolt_hugify_self() {
__asm__ __volatile__(SAVE_ALL "call __bolt_hugify_self_impl\n" RESTORE_ALL
"jmp *__bolt_hugify_init_ptr(%%rip)\n" ::
:);
__asm__ __volatile__(SAVE_ALL
"call __bolt_hugify_self_impl\n"
RESTORE_ALL
"jmp *__bolt_hugify_init_ptr(%%rip)\n"
:::);
}
8 changes: 4 additions & 4 deletions bolt/runtime/instr.cpp
Expand Up @@ -1419,8 +1419,8 @@ extern "C" void instrumentIndirectCall(uint64_t Target, uint64_t IndCallID) {
extern "C" __attribute((naked)) void __bolt_instr_indirect_call()
{
__asm__ __volatile__(SAVE_ALL
"mov 0x88(%%rsp), %%rdi\n"
"mov 0x80(%%rsp), %%rsi\n"
"mov 0x90(%%rsp), %%rdi\n"
"mov 0x88(%%rsp), %%rsi\n"
"call instrumentIndirectCall\n"
RESTORE_ALL
"pop %%rdi\n"
Expand All @@ -1433,8 +1433,8 @@ extern "C" __attribute((naked)) void __bolt_instr_indirect_call()
extern "C" __attribute((naked)) void __bolt_instr_indirect_tailcall()
{
__asm__ __volatile__(SAVE_ALL
"mov 0x80(%%rsp), %%rdi\n"
"mov 0x78(%%rsp), %%rsi\n"
"mov 0x88(%%rsp), %%rdi\n"
"mov 0x80(%%rsp), %%rsi\n"
"call instrumentIndirectCall\n"
RESTORE_ALL
"add $16, %%rsp\n"
Expand Down
2 changes: 1 addition & 1 deletion bolt/test/X86/user-func-reorder.c
Expand Up @@ -30,7 +30,7 @@ REQUIRES: system-linux
RUN: %host_cc %s -o %t.exe -Wl,-q
RUN: llvm-bolt %t.exe -relocs=1 -lite -reorder-functions=user \
RUN: -function-order=%p/Inputs/user_func_order.txt -o %t
RUN: -hugify -function-order=%p/Inputs/user_func_order.txt -o %t
RUN: nm -ns %t | FileCheck %s -check-prefix=CHECK-NM
RUN: %t 1 2 3 | FileCheck %s -check-prefix=CHECK-OUTPUT
Expand Down

0 comments on commit c6799a6

Please sign in to comment.