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