Commit 3363adbe authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

cppgc: TSAN support for stack scanning

We cannot rely on the clang compiler to generate the trampoline entry
and the right mangling because `__attribute__((naked))` does not
prevent clang from generating TSAN function entry stub
(`__tsan_func_entry`). Even with `__attribute__((no_sanitize_thread)`
annotation clang generates the entry stub.

Upstream bug: https://bugs.llvm.org/show_bug.cgi?id=45400

Bug: chromium:1056170
Change-Id: I7a2063d7d205ee071e6a41ce4d9cb2d8d6423987
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2132797Reviewed-by: 's avatarOmer Katz <omerkatz@chromium.org>
Reviewed-by: 's avatarAnton Bikineev <bikineev@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66976}
parent 1e132398
......@@ -5,12 +5,13 @@
// Push all callee-saved registers to get them on the stack for conservative
// stack scanning.
//
// Do not add any C code to the function. The function is naked to avoid
// emitting a prologue/epilogue that could violate alginment computations.
extern "C" __attribute__((naked, noinline)) void
PushAllRegistersAndIterateStack(void* /* {Stack*} */,
void* /* {StackVisitor*} */,
void* /* {IterateStackCallback} */) {
// We cannot rely on clang generating the function and right symbol mangling
// as `__attribite__((naked))` does not prevent clang from generating TSAN
// function entry stubs (`__tsan_func_entry`). Even with
// `__attribute__((no_sanitize_thread)` annotation clang generates the entry
// stub.
// See https://bugs.llvm.org/show_bug.cgi?id=45400.
// Do not depend on V8_TARGET_OS_* defines as some embedders may override the
// GN toolchain (e.g. ChromeOS) and not provide them.
// _WIN64 Defined as 1 when the compilation target is 64-bit ARM or x64.
......@@ -21,32 +22,33 @@ PushAllRegistersAndIterateStack(void* /* {Stack*} */,
// on the stack and we push 72 bytes which maintains 16-byte stack alignment
// at the call.
// Source: https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention
asm volatile(
// rbp is callee-saved. Maintain proper frame pointer for debugging.
" push %rbp \n"
" mov %rsp, %rbp \n"
// Dummy for alignment.
" push $0xCDCDCD \n"
" push %rsi \n"
" push %rdi \n"
" push %rbx \n"
" push %r12 \n"
" push %r13 \n"
" push %r14 \n"
" push %r15 \n"
// Pass 1st parameter (rcx) unchanged (Stack*).
// Pass 2nd parameter (rdx) unchanged (StackVisitor*).
// Save 3rd parameter (r8; IterateStackCallback)
" mov %r8, %r9 \n"
// Pass 3rd parameter as rsp (stack pointer).
" mov %rsp, %r8 \n"
// Call the callback.
" call *%r9 \n"
// Pop the callee-saved registers.
" add $64, %rsp \n"
// Restore rbp as it was used as frame pointer.
" pop %rbp \n"
" ret \n");
asm(".globl PushAllRegistersAndIterateStack \n"
"PushAllRegistersAndIterateStack: \n"
// rbp is callee-saved. Maintain proper frame pointer for debugging.
" push %rbp \n"
" mov %rsp, %rbp \n"
// Dummy for alignment.
" push $0xCDCDCD \n"
" push %rsi \n"
" push %rdi \n"
" push %rbx \n"
" push %r12 \n"
" push %r13 \n"
" push %r14 \n"
" push %r15 \n"
// Pass 1st parameter (rcx) unchanged (Stack*).
// Pass 2nd parameter (rdx) unchanged (StackVisitor*).
// Save 3rd parameter (r8; IterateStackCallback)
" mov %r8, %r9 \n"
// Pass 3rd parameter as rsp (stack pointer).
" mov %rsp, %r8 \n"
// Call the callback.
" call *%r9 \n"
// Pop the callee-saved registers.
" add $64, %rsp \n"
// Restore rbp as it was used as frame pointer.
" pop %rbp \n"
" ret \n");
#else // !_WIN64
......@@ -54,30 +56,38 @@ PushAllRegistersAndIterateStack(void* /* {Stack*} */,
// on the stack and we push 56 bytes which maintains 16-byte stack alignment
// at the call.
// Source: https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-1.0.pdf
asm volatile(
// rbp is callee-saved. Maintain proper frame pointer for debugging.
" push %rbp \n"
" mov %rsp, %rbp \n"
// Dummy for alignment.
" push $0xCDCDCD \n"
" push %rbx \n"
" push %r12 \n"
" push %r13 \n"
" push %r14 \n"
" push %r15 \n"
// Pass 1st parameter (rdi) unchanged (Stack*).
// Pass 2nd parameter (rsi) unchanged (StackVisitor*).
// Save 3rd parameter (rdx; IterateStackCallback)
" mov %rdx, %r8 \n"
// Pass 3rd parameter as rsp (stack pointer).
" mov %rsp, %rdx \n"
// Call the callback.
" call *%r8 \n"
// Pop the callee-saved registers.
" add $48, %rsp \n"
// Restore rbp as it was used as frame pointer.
" pop %rbp \n"
" ret \n");
asm(
#ifdef __APPLE__
".globl _PushAllRegistersAndIterateStack \n"
".private_extern _PushAllRegistersAndIterateStack \n"
"_PushAllRegistersAndIterateStack: \n"
#else // !__APPLE__
".globl PushAllRegistersAndIterateStack \n"
".hidden PushAllRegistersAndIterateStack \n"
"PushAllRegistersAndIterateStack: \n"
#endif // !__APPLE__
// rbp is callee-saved. Maintain proper frame pointer for debugging.
" push %rbp \n"
" mov %rsp, %rbp \n"
// Dummy for alignment.
" push $0xCDCDCD \n"
" push %rbx \n"
" push %r12 \n"
" push %r13 \n"
" push %r14 \n"
" push %r15 \n"
// Pass 1st parameter (rdi) unchanged (Stack*).
// Pass 2nd parameter (rsi) unchanged (StackVisitor*).
// Save 3rd parameter (rdx; IterateStackCallback)
" mov %rdx, %r8 \n"
// Pass 3rd parameter as rsp (stack pointer).
" mov %rsp, %rdx \n"
// Call the callback.
" call *%r8 \n"
// Pop the callee-saved registers.
" add $48, %rsp \n"
// Restore rbp as it was used as frame pointer.
" pop %rbp \n"
" ret \n");
#endif // !_WIN64
}
......@@ -47,7 +47,6 @@ TEST_F(GCStackTest, IsOnStackForHeapValue) {
}
#ifdef CPPGC_SUPPORTS_CONSERVATIVE_STACK_SCAN
#ifndef THREAD_SANITIZER
namespace {
......@@ -357,7 +356,6 @@ TEST_F(GCStackTest, StackAlignment) {
#endif // V8_OS_LINUX && (V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64)
#endif // CPPGC_SUPPORTS_CONSERVATIVE_STACK_SCAN
#endif // !THREAD_SANITIZER
} // namespace internal
} // namespace cppgc
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment