[BOLT] Fix stack alignment for runtime lib

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)
This commit is contained in:
Rafael Auler 2020-07-27 16:52:51 -07:00 committed by Maksim Panchenko
parent ed02946281
commit c6799a689d
4 changed files with 15 additions and 9 deletions

View File

@ -6,6 +6,7 @@
#include <elf.h> #include <elf.h>
#endif #endif
// Save all registers while keeping 16B stack alignment
#define SAVE_ALL \ #define SAVE_ALL \
"push %%rax\n" \ "push %%rax\n" \
"push %%rbx\n" \ "push %%rbx\n" \
@ -21,9 +22,12 @@
"push %%r12\n" \ "push %%r12\n" \
"push %%r13\n" \ "push %%r13\n" \
"push %%r14\n" \ "push %%r14\n" \
"push %%r15\n" "push %%r15\n" \
"sub $8, %%rsp\n"
// Mirrors SAVE_ALL
#define RESTORE_ALL \ #define RESTORE_ALL \
"add $8, %%rsp\n" \
"pop %%r15\n" \ "pop %%r15\n" \
"pop %%r14\n" \ "pop %%r14\n" \
"pop %%r13\n" \ "pop %%r13\n" \

View File

@ -166,7 +166,9 @@ extern "C" void __bolt_hugify_self_impl() {
/// This is hooking ELF's entry, it needs to save all machine state. /// This is hooking ELF's entry, it needs to save all machine state.
extern "C" __attribute((naked)) void __bolt_hugify_self() { extern "C" __attribute((naked)) void __bolt_hugify_self() {
__asm__ __volatile__(SAVE_ALL "call __bolt_hugify_self_impl\n" RESTORE_ALL __asm__ __volatile__(SAVE_ALL
"jmp *__bolt_hugify_init_ptr(%%rip)\n" :: "call __bolt_hugify_self_impl\n"
:); RESTORE_ALL
"jmp *__bolt_hugify_init_ptr(%%rip)\n"
:::);
} }

View File

@ -1419,8 +1419,8 @@ extern "C" void instrumentIndirectCall(uint64_t Target, uint64_t IndCallID) {
extern "C" __attribute((naked)) void __bolt_instr_indirect_call() extern "C" __attribute((naked)) void __bolt_instr_indirect_call()
{ {
__asm__ __volatile__(SAVE_ALL __asm__ __volatile__(SAVE_ALL
"mov 0x88(%%rsp), %%rdi\n" "mov 0x90(%%rsp), %%rdi\n"
"mov 0x80(%%rsp), %%rsi\n" "mov 0x88(%%rsp), %%rsi\n"
"call instrumentIndirectCall\n" "call instrumentIndirectCall\n"
RESTORE_ALL RESTORE_ALL
"pop %%rdi\n" "pop %%rdi\n"
@ -1433,8 +1433,8 @@ extern "C" __attribute((naked)) void __bolt_instr_indirect_call()
extern "C" __attribute((naked)) void __bolt_instr_indirect_tailcall() extern "C" __attribute((naked)) void __bolt_instr_indirect_tailcall()
{ {
__asm__ __volatile__(SAVE_ALL __asm__ __volatile__(SAVE_ALL
"mov 0x80(%%rsp), %%rdi\n" "mov 0x88(%%rsp), %%rdi\n"
"mov 0x78(%%rsp), %%rsi\n" "mov 0x80(%%rsp), %%rsi\n"
"call instrumentIndirectCall\n" "call instrumentIndirectCall\n"
RESTORE_ALL RESTORE_ALL
"add $16, %%rsp\n" "add $16, %%rsp\n"

View File

@ -30,7 +30,7 @@ REQUIRES: system-linux
RUN: %host_cc %s -o %t.exe -Wl,-q RUN: %host_cc %s -o %t.exe -Wl,-q
RUN: llvm-bolt %t.exe -relocs=1 -lite -reorder-functions=user \ 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: nm -ns %t | FileCheck %s -check-prefix=CHECK-NM
RUN: %t 1 2 3 | FileCheck %s -check-prefix=CHECK-OUTPUT RUN: %t 1 2 3 | FileCheck %s -check-prefix=CHECK-OUTPUT