[compiler-rt] r354625 - [hwasan,asan] Intercept vfork.
Evgeniy Stepanov via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 21 13:32:25 PST 2019
Author: eugenis
Date: Thu Feb 21 13:32:24 2019
New Revision: 354625
URL: http://llvm.org/viewvc/llvm-project?rev=354625&view=rev
Log:
[hwasan,asan] Intercept vfork.
Summary: AArch64 only for now.
Reviewers: vitalybuka, pcc
Subscribers: srhines, kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, jdoerfert, #sanitizers, llvm-commits, kcc
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58313
Added:
compiler-rt/trunk/lib/asan/asan_interceptors_vfork.S
compiler-rt/trunk/lib/hwasan/hwasan_interceptors_vfork.S
compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_vfork_aarch64.inc.S
compiler-rt/trunk/test/asan/TestCases/Linux/vfork.cc
compiler-rt/trunk/test/hwasan/TestCases/Linux/vfork.c
Modified:
compiler-rt/trunk/include/sanitizer/hwasan_interface.h
compiler-rt/trunk/lib/asan/CMakeLists.txt
compiler-rt/trunk/lib/asan/asan_interceptors.cc
compiler-rt/trunk/lib/asan/asan_interface.inc
compiler-rt/trunk/lib/asan/asan_interface_internal.h
compiler-rt/trunk/lib/asan/asan_rtl.cc
compiler-rt/trunk/lib/asan/asan_thread.h
compiler-rt/trunk/lib/hwasan/CMakeLists.txt
compiler-rt/trunk/lib/hwasan/hwasan.cc
compiler-rt/trunk/lib/hwasan/hwasan_interceptors.cc
compiler-rt/trunk/lib/hwasan/hwasan_interface_internal.h
compiler-rt/trunk/lib/hwasan/hwasan_thread.h
compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt
Modified: compiler-rt/trunk/include/sanitizer/hwasan_interface.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/include/sanitizer/hwasan_interface.h?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/include/sanitizer/hwasan_interface.h (original)
+++ compiler-rt/trunk/include/sanitizer/hwasan_interface.h Thu Feb 21 13:32:24 2019
@@ -50,6 +50,10 @@ extern "C" {
// does would cause false reports.
void __hwasan_handle_longjmp(const void *sp_dst);
+ // Set memory tag for the part of the current thread stack below sp_dst to
+ // zero. Call this in vfork() before returning in the parent process.
+ void __hwasan_handle_vfork(const void *sp_dst);
+
// Libc hook for thread creation. Should be called in the child thread before
// any instrumented code.
void __hwasan_thread_enter();
@@ -65,6 +69,10 @@ extern "C" {
// Print one-line report about the memory usage of the current process.
void __hwasan_print_memory_usage();
+ /* Returns the offset of the first byte in the memory range that can not be
+ * accessed through the pointer in x, or -1 if the whole range is good. */
+ intptr_t __hwasan_test_shadow(const volatile void *x, size_t size);
+
int __sanitizer_posix_memalign(void **memptr, size_t alignment, size_t size);
void * __sanitizer_memalign(size_t alignment, size_t size);
void * __sanitizer_aligned_alloc(size_t alignment, size_t size);
Modified: compiler-rt/trunk/lib/asan/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/CMakeLists.txt?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/asan/CMakeLists.txt Thu Feb 21 13:32:24 2019
@@ -13,6 +13,7 @@ set(ASAN_SOURCES
asan_globals_win.cc
asan_interceptors.cc
asan_interceptors_memintrinsics.cc
+ asan_interceptors_vfork.S
asan_linux.cc
asan_mac.cc
asan_malloc_linux.cc
Modified: compiler-rt/trunk/lib/asan/asan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interceptors.cc?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_interceptors.cc Thu Feb 21 13:32:24 2019
@@ -579,6 +579,8 @@ INTERCEPTOR(int, __cxa_atexit, void (*fu
}
#endif // ASAN_INTERCEPT___CXA_ATEXIT
+DEFINE_REAL(void, vfork);
+
// ---------------------- InitializeAsanInterceptors ---------------- {{{1
namespace __asan {
void InitializeAsanInterceptors() {
@@ -656,6 +658,8 @@ void InitializeAsanInterceptors() {
ASAN_INTERCEPT_FUNC(__cxa_atexit);
#endif
+ __interception::GetRealFunctionAddress("vfork", (uptr *)&REAL(vfork), 0, 0);
+
InitializePlatformInterceptors();
VReport(1, "AddressSanitizer: libc interceptors initialized\n");
Added: compiler-rt/trunk/lib/asan/asan_interceptors_vfork.S
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interceptors_vfork.S?rev=354625&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interceptors_vfork.S (added)
+++ compiler-rt/trunk/lib/asan/asan_interceptors_vfork.S Thu Feb 21 13:32:24 2019
@@ -0,0 +1,7 @@
+#define COMMON_INTERCEPTOR_SPILL_AREA __asan_extra_spill_area
+#define COMMON_INTERCEPTOR_HANDLE_VFORK __asan_handle_vfork
+#include "sanitizer_common/sanitizer_common_interceptors_vfork_aarch64.inc.S"
+
+#if defined(__linux__)
+.section .note.GNU-stack,"", at progbits
+#endif
Modified: compiler-rt/trunk/lib/asan/asan_interface.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interface.inc?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interface.inc (original)
+++ compiler-rt/trunk/lib/asan/asan_interface.inc Thu Feb 21 13:32:24 2019
@@ -26,6 +26,7 @@ INTERFACE_FUNCTION(__asan_exp_store4)
INTERFACE_FUNCTION(__asan_exp_store8)
INTERFACE_FUNCTION(__asan_exp_store16)
INTERFACE_FUNCTION(__asan_exp_storeN)
+INTERFACE_FUNCTION(__asan_extra_spill_area)
INTERFACE_FUNCTION(__asan_get_alloc_stack)
INTERFACE_FUNCTION(__asan_get_current_fake_stack)
INTERFACE_FUNCTION(__asan_get_free_stack)
@@ -38,6 +39,7 @@ INTERFACE_FUNCTION(__asan_get_report_pc)
INTERFACE_FUNCTION(__asan_get_report_sp)
INTERFACE_FUNCTION(__asan_get_shadow_mapping)
INTERFACE_FUNCTION(__asan_handle_no_return)
+INTERFACE_FUNCTION(__asan_handle_vfork)
INTERFACE_FUNCTION(__asan_init)
INTERFACE_FUNCTION(__asan_load_cxx_array_cookie)
INTERFACE_FUNCTION(__asan_load1)
Modified: compiler-rt/trunk/lib/asan/asan_interface_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interface_internal.h?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interface_internal.h (original)
+++ compiler-rt/trunk/lib/asan/asan_interface_internal.h Thu Feb 21 13:32:24 2019
@@ -249,6 +249,9 @@ extern "C" {
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
const char* __asan_default_suppressions();
+
+ SANITIZER_INTERFACE_ATTRIBUTE void *__asan_extra_spill_area();
+ SANITIZER_INTERFACE_ATTRIBUTE void __asan_handle_vfork(void *sp);
} // extern "C"
#endif // ASAN_INTERFACE_INTERNAL_H
Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_rtl.cc Thu Feb 21 13:32:24 2019
@@ -597,6 +597,21 @@ void NOINLINE __asan_handle_no_return()
curr_thread->fake_stack()->HandleNoReturn();
}
+void *__asan_extra_spill_area() {
+ AsanThread *t = GetCurrentThread();
+ CHECK(t);
+ return t->extra_spill_area();
+}
+
+void __asan_handle_vfork(void *sp) {
+ AsanThread *t = GetCurrentThread();
+ CHECK(t);
+ uptr PageSize = GetPageSizeCached();
+ uptr top = t->stack_top();
+ uptr bottom = ((uptr)sp - PageSize) & ~(PageSize - 1);
+ PoisonShadow(bottom, top - bottom, 0);
+}
+
void NOINLINE __asan_set_death_callback(void (*callback)(void)) {
SetUserDieCallback(callback);
}
Modified: compiler-rt/trunk/lib/asan/asan_thread.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.h?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.h (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.h Thu Feb 21 13:32:24 2019
@@ -130,6 +130,8 @@ class AsanThread {
AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
AsanStats &stats() { return stats_; }
+ void *extra_spill_area() { return &extra_spill_area_; }
+
private:
// NOTE: There is no AsanThread constructor. It is allocated
// via mmap() and *must* be valid in zero-initialized state.
@@ -165,6 +167,7 @@ class AsanThread {
AsanThreadLocalMallocStorage malloc_storage_;
AsanStats stats_;
bool unwinding_;
+ uptr extra_spill_area_;
};
// ScopedUnwinding is a scope for stacktracing member of a context
Modified: compiler-rt/trunk/lib/hwasan/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/CMakeLists.txt?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/hwasan/CMakeLists.txt Thu Feb 21 13:32:24 2019
@@ -6,6 +6,7 @@ set(HWASAN_RTL_SOURCES
hwasan_allocator.cc
hwasan_dynamic_shadow.cc
hwasan_interceptors.cc
+ hwasan_interceptors_vfork.S
hwasan_linux.cc
hwasan_memintrinsics.cc
hwasan_poisoning.cc
Modified: compiler-rt/trunk/lib/hwasan/hwasan.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan.cc?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan.cc (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan.cc Thu Feb 21 13:32:24 2019
@@ -477,6 +477,30 @@ void __hwasan_handle_longjmp(const void
TagMemory(sp, dst - sp, 0);
}
+void __hwasan_handle_vfork(const void *sp_dst) {
+ uptr sp = (uptr)sp_dst;
+ Thread *t = GetCurrentThread();
+ CHECK(t);
+ uptr top = t->stack_top();
+ uptr bottom = t->stack_bottom();
+ static const uptr kMaxExpectedCleanupSize = 64 << 20; // 64M
+ if (top == 0 || bottom == 0 || sp < bottom || sp >= top ||
+ sp - bottom > kMaxExpectedCleanupSize) {
+ Report(
+ "WARNING: HWASan is ignoring requested __hwasan_handle_vfork: "
+ "stack top: %zx; current %zx; bottom: %zx \n"
+ "False positive error reports may follow\n",
+ top, sp, bottom);
+ return;
+ }
+ TagMemory(bottom, sp - bottom, 0);
+}
+
+void *__hwasan_extra_spill_area() {
+ Thread *t = GetCurrentThread();
+ return &t->vfork_spill();
+}
+
void __hwasan_print_memory_usage() {
InternalScopedString s(kMemoryUsageBufferSize);
HwasanFormatMemoryUsage(s);
Modified: compiler-rt/trunk/lib/hwasan/hwasan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_interceptors.cc?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan_interceptors.cc Thu Feb 21 13:32:24 2019
@@ -227,6 +227,10 @@ INTERCEPTOR(int, pthread_create, void *t
}
#endif
+#if HWASAN_WITH_INTERCEPTORS
+DEFINE_REAL(void, vfork);
+#endif
+
static void BeforeFork() {
StackDepotLockAll();
}
@@ -266,6 +270,7 @@ void InitializeInterceptors() {
INTERCEPT_FUNCTION(fork);
#if HWASAN_WITH_INTERCEPTORS
+ __interception::GetRealFunctionAddress("vfork", (uptr *)&REAL(vfork), 0, 0);
#if !defined(__aarch64__)
INTERCEPT_FUNCTION(pthread_create);
#endif
Added: compiler-rt/trunk/lib/hwasan/hwasan_interceptors_vfork.S
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_interceptors_vfork.S?rev=354625&view=auto
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_interceptors_vfork.S (added)
+++ compiler-rt/trunk/lib/hwasan/hwasan_interceptors_vfork.S Thu Feb 21 13:32:24 2019
@@ -0,0 +1,7 @@
+#define COMMON_INTERCEPTOR_SPILL_AREA __hwasan_extra_spill_area
+#define COMMON_INTERCEPTOR_HANDLE_VFORK __hwasan_handle_vfork
+#include "sanitizer_common/sanitizer_common_interceptors_vfork_aarch64.inc.S"
+
+#if defined(__linux__)
+.section .note.GNU-stack,"", at progbits
+#endif
Modified: compiler-rt/trunk/lib/hwasan/hwasan_interface_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_interface_internal.h?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_interface_internal.h (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan_interface_internal.h Thu Feb 21 13:32:24 2019
@@ -117,6 +117,9 @@ SANITIZER_INTERFACE_ATTRIBUTE
void __hwasan_handle_longjmp(const void *sp_dst);
SANITIZER_INTERFACE_ATTRIBUTE
+void __hwasan_handle_vfork(const void *sp_dst);
+
+SANITIZER_INTERFACE_ATTRIBUTE
u16 __sanitizer_unaligned_load16(const uu16 *p);
SANITIZER_INTERFACE_ATTRIBUTE
@@ -200,6 +203,9 @@ SANITIZER_INTERFACE_ATTRIBUTE
void *__hwasan_memset(void *s, int c, uptr n);
SANITIZER_INTERFACE_ATTRIBUTE
void *__hwasan_memmove(void *dest, const void *src, uptr n);
+
+SANITIZER_INTERFACE_ATTRIBUTE
+void *__hwasan_extra_spill_area();
} // extern "C"
#endif // HWASAN_INTERFACE_INTERNAL_H
Modified: compiler-rt/trunk/lib/hwasan/hwasan_thread.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/hwasan/hwasan_thread.h?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/hwasan/hwasan_thread.h (original)
+++ compiler-rt/trunk/lib/hwasan/hwasan_thread.h Thu Feb 21 13:32:24 2019
@@ -67,11 +67,14 @@ class Thread {
Print("Thread: ");
}
+ uptr &vfork_spill() { return vfork_spill_; }
+
private:
// NOTE: There is no Thread constructor. It is allocated
// via mmap() and *must* be valid in zero-initialized state.
void ClearShadowForThreadStackAndTLS();
void Print(const char *prefix);
+ uptr vfork_spill_;
uptr stack_top_;
uptr stack_bottom_;
uptr tls_begin_;
Modified: compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt?rev=354625&r1=354624&r2=354625&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt Thu Feb 21 13:32:24 2019
@@ -126,6 +126,7 @@ set(SANITIZER_IMPL_HEADERS
sanitizer_common_interceptors.inc
sanitizer_common_interceptors_format.inc
sanitizer_common_interceptors_ioctl.inc
+ sanitizer_common_interceptors_vfork_aarch64.inc.S
sanitizer_common_interface.inc
sanitizer_common_interface_posix.inc
sanitizer_common_syscalls.inc
Added: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_vfork_aarch64.inc.S
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_vfork_aarch64.inc.S?rev=354625&view=auto
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_vfork_aarch64.inc.S (added)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors_vfork_aarch64.inc.S Thu Feb 21 13:32:24 2019
@@ -0,0 +1,43 @@
+#if defined(__aarch64__) && defined(__linux__)
+
+#include "sanitizer_common/sanitizer_asm.h"
+
+ASM_HIDDEN(COMMON_INTERCEPTOR_SPILL_AREA)
+
+.comm _ZN14__interception10real_vforkE,8,8
+.globl vfork
+ASM_TYPE_FUNCTION(vfork)
+vfork:
+ // Save x30 in the off-stack spill area.
+ stp xzr, x30, [sp, #-16]!
+ bl COMMON_INTERCEPTOR_SPILL_AREA
+ ldp xzr, x30, [sp], 16
+ str x30, [x0]
+
+ // Call real vfork. This may return twice. User code that runs between the first and the second return
+ // may clobber the stack frame of the interceptor; that's why it does not have a frame.
+ adrp x0, _ZN14__interception10real_vforkE
+ ldr x0, [x0, :lo12:_ZN14__interception10real_vforkE]
+ blr x0
+
+ stp x0, xzr, [sp, #-16]!
+ cmp x0, #0
+ b.eq .L_exit
+
+ // x0 != 0 => parent process. Clear stack shadow.
+ add x0, sp, #16
+ bl COMMON_INTERCEPTOR_HANDLE_VFORK
+
+.L_exit:
+ // Restore x30.
+ bl COMMON_INTERCEPTOR_SPILL_AREA
+ ldr x30, [x0]
+ ldp x0, xzr, [sp], 16
+
+ ret
+ASM_SIZE(vfork)
+
+.globl __interceptor_vfork
+.set __interceptor_vfork, vfork
+
+#endif
Added: compiler-rt/trunk/test/asan/TestCases/Linux/vfork.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Linux/vfork.cc?rev=354625&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Linux/vfork.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Linux/vfork.cc Thu Feb 21 13:32:24 2019
@@ -0,0 +1,31 @@
+// https://github.com/google/sanitizers/issues/925
+// RUN: %clang_asan -O0 %s -o %t && %run %t 2>&1
+
+// REQUIRES: aarch64-android-target-arch
+
+#include <assert.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sanitizer/asan_interface.h>
+
+__attribute__((noinline, no_sanitize("address"))) void child() {
+ char x[10000];
+ __asan_poison_memory_region(x, sizeof(x));
+ _exit(0);
+}
+
+__attribute__((noinline, no_sanitize("address"))) void parent() {
+ char x[10000];
+ assert(__asan_address_is_poisoned(x + 5000) == 0);
+}
+
+int main(int argc, char **argv) {
+ if (vfork())
+ parent();
+ else
+ child();
+
+ return 0;
+}
Added: compiler-rt/trunk/test/hwasan/TestCases/Linux/vfork.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/hwasan/TestCases/Linux/vfork.c?rev=354625&view=auto
==============================================================================
--- compiler-rt/trunk/test/hwasan/TestCases/Linux/vfork.c (added)
+++ compiler-rt/trunk/test/hwasan/TestCases/Linux/vfork.c Thu Feb 21 13:32:24 2019
@@ -0,0 +1,32 @@
+// https://github.com/google/sanitizers/issues/925
+// RUN: %clang_hwasan -O0 %s -o %t && %run %t 2>&1
+
+// REQUIRES: aarch64-target-arch
+
+#include <assert.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sanitizer/hwasan_interface.h>
+
+__attribute__((noinline, no_sanitize("hwaddress"))) void child() {
+ char x[10000];
+ __hwasan_tag_memory(x, 0xAA, sizeof(x));
+ _exit(0);
+}
+
+__attribute__((noinline, no_sanitize("hwaddress"))) void parent() {
+ char x[10000];
+ __hwasan_print_shadow(&x, sizeof(x));
+ assert(__hwasan_test_shadow(x, sizeof(x)) == -1);
+}
+
+int main(int argc, char **argv) {
+ if (vfork())
+ parent();
+ else
+ child();
+
+ return 0;
+}
More information about the llvm-commits
mailing list