From b59abb2590898d1325f1b67b6ea2f75a1bfbd78c Mon Sep 17 00:00:00 2001 From: Kostya Kortchinsky Date: Tue, 26 Sep 2017 17:20:02 +0000 Subject: [PATCH] [scudo] Scudo thread specific data refactor, part 3 Summary: Previous parts: D38139, D38183. In this part of the refactor, we abstract the Linux vs Android TSD dissociation in favor of a Exclusive vs Shared one, allowing for easier platform introduction and configuration. Most of this change consist of shuffling the files around to reflect the new organization. We introduce `scudo_platform.h` where platform specific definition lie. This involves the TSD model and the platform specific allocator parameters. In an upcoming CL, those will be configurable via defines, but we currently stick with conservative defaults. Reviewers: alekseyshl, dvyukov Reviewed By: alekseyshl, dvyukov Subscribers: srhines, llvm-commits, mgorny Differential Revision: https://reviews.llvm.org/D38244 llvm-svn: 314224 --- compiler-rt/lib/scudo/CMakeLists.txt | 4 +- compiler-rt/lib/scudo/scudo_allocator.cpp | 2 +- compiler-rt/lib/scudo/scudo_allocator.h | 16 +----- compiler-rt/lib/scudo/scudo_platform.h | 58 ++++++++++++++++++++++ compiler-rt/lib/scudo/{scudo_tls.h => scudo_tsd.h} | 19 +++---- ...scudo_tls_linux.cpp => scudo_tsd_exclusive.cpp} | 13 ++--- ...scudo_tls_linux.inc => scudo_tsd_exclusive.inc} | 15 +++--- ...{scudo_tls_android.cpp => scudo_tsd_shared.cpp} | 12 ++--- ...{scudo_tls_android.inc => scudo_tsd_shared.inc} | 14 +++--- 9 files changed, 94 insertions(+), 59 deletions(-) create mode 100644 compiler-rt/lib/scudo/scudo_platform.h rename compiler-rt/lib/scudo/{scudo_tls.h => scudo_tsd.h} (78%) rename compiler-rt/lib/scudo/{scudo_tls_linux.cpp => scudo_tsd_exclusive.cpp} (86%) rename compiler-rt/lib/scudo/{scudo_tls_linux.inc => scudo_tsd_exclusive.inc} (73%) rename compiler-rt/lib/scudo/{scudo_tls_android.cpp => scudo_tsd_shared.cpp} (90%) rename compiler-rt/lib/scudo/{scudo_tls_android.inc => scudo_tsd_shared.inc} (72%) diff --git a/compiler-rt/lib/scudo/CMakeLists.txt b/compiler-rt/lib/scudo/CMakeLists.txt index 14c199f..2539241 100644 --- a/compiler-rt/lib/scudo/CMakeLists.txt +++ b/compiler-rt/lib/scudo/CMakeLists.txt @@ -14,8 +14,8 @@ set(SCUDO_SOURCES scudo_interceptors.cpp scudo_new_delete.cpp scudo_termination.cpp - scudo_tls_android.cpp - scudo_tls_linux.cpp + scudo_tsd_exclusive.cpp + scudo_tsd_shared.cpp scudo_utils.cpp) # Enable the SSE 4.2 instruction set for scudo_crc32.cpp, if available. diff --git a/compiler-rt/lib/scudo/scudo_allocator.cpp b/compiler-rt/lib/scudo/scudo_allocator.cpp index 606439e..077d577 100644 --- a/compiler-rt/lib/scudo/scudo_allocator.cpp +++ b/compiler-rt/lib/scudo/scudo_allocator.cpp @@ -17,7 +17,7 @@ #include "scudo_allocator.h" #include "scudo_crc32.h" #include "scudo_flags.h" -#include "scudo_tls.h" +#include "scudo_tsd.h" #include "scudo_utils.h" #include "sanitizer_common/sanitizer_allocator_checks.h" diff --git a/compiler-rt/lib/scudo/scudo_allocator.h b/compiler-rt/lib/scudo/scudo_allocator.h index a5f0ab0..2f317d2 100644 --- a/compiler-rt/lib/scudo/scudo_allocator.h +++ b/compiler-rt/lib/scudo/scudo_allocator.h @@ -14,11 +14,7 @@ #ifndef SCUDO_ALLOCATOR_H_ #define SCUDO_ALLOCATOR_H_ -#include "sanitizer_common/sanitizer_allocator.h" - -#if !SANITIZER_LINUX -# error "The Scudo hardened allocator is currently only supported on Linux." -#endif +#include "scudo_platform.h" namespace __scudo { @@ -70,14 +66,6 @@ const uptr AlignedChunkHeaderSize = #if SANITIZER_CAN_USE_ALLOCATOR64 const uptr AllocatorSpace = ~0ULL; -# if defined(__aarch64__) && SANITIZER_ANDROID -const uptr AllocatorSize = 0x4000000000ULL; // 256G. -# elif defined(__aarch64__) -const uptr AllocatorSize = 0x10000000000ULL; // 1T. -# else -const uptr AllocatorSize = 0x40000000000ULL; // 4T. -# endif -typedef DefaultSizeClassMap SizeClassMap; struct AP64 { static const uptr kSpaceBeg = AllocatorSpace; static const uptr kSpaceSize = AllocatorSize; @@ -92,14 +80,12 @@ typedef SizeClassAllocator64 PrimaryAllocator; // Currently, the 32-bit Sanitizer allocator has not yet benefited from all the // security improvements brought to the 64-bit one. This makes the 32-bit // version of Scudo slightly less toughened. -static const uptr RegionSizeLog = 20; static const uptr NumRegions = SANITIZER_MMAP_RANGE_SIZE >> RegionSizeLog; # if SANITIZER_WORDSIZE == 32 typedef FlatByteMap ByteMap; # elif SANITIZER_WORDSIZE == 64 typedef TwoLevelByteMap<(NumRegions >> 12), 1 << 12> ByteMap; # endif // SANITIZER_WORDSIZE -typedef DefaultSizeClassMap SizeClassMap; struct AP32 { static const uptr kSpaceBeg = 0; static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; diff --git a/compiler-rt/lib/scudo/scudo_platform.h b/compiler-rt/lib/scudo/scudo_platform.h new file mode 100644 index 0000000..86ddc1a --- /dev/null +++ b/compiler-rt/lib/scudo/scudo_platform.h @@ -0,0 +1,58 @@ +//===-- scudo_platform.h ----------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// Scudo platform specific definitions. +/// +//===----------------------------------------------------------------------===// + +#ifndef SCUDO_PLATFORM_H_ +#define SCUDO_PLATFORM_H_ + +#include "sanitizer_common/sanitizer_allocator.h" + +#if !SANITIZER_LINUX && !SANITIZER_FUCHSIA +# error "The Scudo hardened allocator is not supported on this platform." +#endif + +#if SANITIZER_ANDROID || SANITIZER_FUCHSIA +// Android and Fuchsia use a pool of TSDs shared between threads. +# define SCUDO_TSD_EXCLUSIVE 0 +#elif SANITIZER_LINUX && !SANITIZER_ANDROID +// Non-Android Linux use an exclusive TSD per thread. +# define SCUDO_TSD_EXCLUSIVE 1 +#else +# error "No default TSD model defined for this platform." +#endif // SANITIZER_ANDROID || SANITIZER_FUCHSIA + +namespace __scudo { + +#if SANITIZER_CAN_USE_ALLOCATOR64 +# if defined(__aarch64__) && SANITIZER_ANDROID +const uptr AllocatorSize = 0x2000000000ULL; // 128G. +typedef VeryCompactSizeClassMap SizeClassMap; +# elif defined(__aarch64__) +const uptr AllocatorSize = 0x10000000000ULL; // 1T. +typedef CompactSizeClassMap SizeClassMap; +# else +const uptr AllocatorSize = 0x40000000000ULL; // 4T. +typedef CompactSizeClassMap SizeClassMap; +# endif +#else +# if SANITIZER_ANDROID +static const uptr RegionSizeLog = 19; +typedef VeryCompactSizeClassMap SizeClassMap; +# else +static const uptr RegionSizeLog = 20; +typedef CompactSizeClassMap SizeClassMap; +# endif +#endif // SANITIZER_CAN_USE_ALLOCATOR64 + +} // namespace __scudo + +#endif // SCUDO_PLATFORM_H_ diff --git a/compiler-rt/lib/scudo/scudo_tls.h b/compiler-rt/lib/scudo/scudo_tsd.h similarity index 78% rename from compiler-rt/lib/scudo/scudo_tls.h rename to compiler-rt/lib/scudo/scudo_tsd.h index 57480c2..9ee89d3 100644 --- a/compiler-rt/lib/scudo/scudo_tls.h +++ b/compiler-rt/lib/scudo/scudo_tsd.h @@ -1,4 +1,4 @@ -//===-- scudo_tls.h ---------------------------------------------*- C++ -*-===// +//===-- scudo_tsd.h ---------------------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -7,21 +7,18 @@ // //===----------------------------------------------------------------------===// /// -/// Scudo thread local structure definition. +/// Scudo thread specific data definition. /// Implementation will differ based on the thread local storage primitives /// offered by the underlying platform. /// //===----------------------------------------------------------------------===// -#ifndef SCUDO_TLS_H_ -#define SCUDO_TLS_H_ +#ifndef SCUDO_TSD_H_ +#define SCUDO_TSD_H_ #include "scudo_allocator.h" #include "scudo_utils.h" -#include "sanitizer_common/sanitizer_linux.h" -#include "sanitizer_common/sanitizer_platform.h" - namespace __scudo { struct ALIGNED(64) ScudoTSD { @@ -65,10 +62,10 @@ struct ALIGNED(64) ScudoTSD { void initThread(bool MinimalInit); -// Platform specific fastpath functions definitions. -#include "scudo_tls_android.inc" -#include "scudo_tls_linux.inc" +// TSD model specific fastpath functions definitions. +#include "scudo_tsd_exclusive.inc" +#include "scudo_tsd_shared.inc" } // namespace __scudo -#endif // SCUDO_TLS_H_ +#endif // SCUDO_TSD_H_ diff --git a/compiler-rt/lib/scudo/scudo_tls_linux.cpp b/compiler-rt/lib/scudo/scudo_tsd_exclusive.cpp similarity index 86% rename from compiler-rt/lib/scudo/scudo_tls_linux.cpp rename to compiler-rt/lib/scudo/scudo_tsd_exclusive.cpp index 1f51ccc..eb47197 100644 --- a/compiler-rt/lib/scudo/scudo_tls_linux.cpp +++ b/compiler-rt/lib/scudo/scudo_tsd_exclusive.cpp @@ -1,4 +1,4 @@ -//===-- scudo_tls_linux.cpp -------------------------------------*- C++ -*-===// +//===-- scudo_tsd_exclusive.cpp ---------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -7,16 +7,13 @@ // //===----------------------------------------------------------------------===// /// -/// Scudo thread local structure implementation for platforms supporting -/// thread_local. +/// Scudo exclusive TSD implementation. /// //===----------------------------------------------------------------------===// -#include "sanitizer_common/sanitizer_platform.h" +#include "scudo_tsd.h" -#if SANITIZER_LINUX && !SANITIZER_ANDROID - -#include "scudo_tls.h" +#if SCUDO_TSD_EXCLUSIVE #include @@ -70,4 +67,4 @@ void initThread(bool MinimalInit) { } // namespace __scudo -#endif // SANITIZER_LINUX && !SANITIZER_ANDROID +#endif // SCUDO_TSD_EXCLUSIVE diff --git a/compiler-rt/lib/scudo/scudo_tls_linux.inc b/compiler-rt/lib/scudo/scudo_tsd_exclusive.inc similarity index 73% rename from compiler-rt/lib/scudo/scudo_tls_linux.inc rename to compiler-rt/lib/scudo/scudo_tsd_exclusive.inc index e0c6deb..567b6a1 100644 --- a/compiler-rt/lib/scudo/scudo_tls_linux.inc +++ b/compiler-rt/lib/scudo/scudo_tsd_exclusive.inc @@ -1,4 +1,4 @@ -//===-- scudo_tls_linux.inc -------------------------------------*- C++ -*-===// +//===-- scudo_tsd_exclusive.inc ---------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -7,16 +7,15 @@ // //===----------------------------------------------------------------------===// /// -/// Scudo thread local structure fastpath functions implementation for platforms -/// supporting thread_local. +/// Scudo exclusive TSD fastpath functions implementation. /// //===----------------------------------------------------------------------===// -#ifndef SCUDO_TLS_H_ -# error "This file must be included inside scudo_tls.h." -#endif // SCUDO_TLS_H_ +#ifndef SCUDO_TSD_H_ +# error "This file must be included inside scudo_tsd.h." +#endif // SCUDO_TSD_H_ -#if SANITIZER_LINUX && !SANITIZER_ANDROID +#if SCUDO_TSD_EXCLUSIVE enum ThreadState : u8 { ThreadNotInitialized = 0, @@ -44,4 +43,4 @@ ALWAYS_INLINE ScudoTSD *getTSDAndLock() { return &TSD; } -#endif // SANITIZER_LINUX && !SANITIZER_ANDROID +#endif // SCUDO_TSD_EXCLUSIVE diff --git a/compiler-rt/lib/scudo/scudo_tls_android.cpp b/compiler-rt/lib/scudo/scudo_tsd_shared.cpp similarity index 90% rename from compiler-rt/lib/scudo/scudo_tls_android.cpp rename to compiler-rt/lib/scudo/scudo_tsd_shared.cpp index 695a610..481635e 100644 --- a/compiler-rt/lib/scudo/scudo_tls_android.cpp +++ b/compiler-rt/lib/scudo/scudo_tsd_shared.cpp @@ -1,4 +1,4 @@ -//===-- scudo_tls_android.cpp -----------------------------------*- C++ -*-===// +//===-- scudo_tsd_shared.cpp ------------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -7,15 +7,13 @@ // //===----------------------------------------------------------------------===// /// -/// Scudo thread local structure implementation for Android. +/// Scudo shared TSD implementation. /// //===----------------------------------------------------------------------===// -#include "sanitizer_common/sanitizer_platform.h" +#include "scudo_tsd.h" -#if SANITIZER_LINUX && SANITIZER_ANDROID - -#include "scudo_tls.h" +#if !SCUDO_TSD_EXCLUSIVE #include @@ -95,4 +93,4 @@ ScudoTSD *getTSDAndLockSlow() { } // namespace __scudo -#endif // SANITIZER_LINUX && SANITIZER_ANDROID +#endif // !SCUDO_TSD_EXCLUSIVE diff --git a/compiler-rt/lib/scudo/scudo_tls_android.inc b/compiler-rt/lib/scudo/scudo_tsd_shared.inc similarity index 72% rename from compiler-rt/lib/scudo/scudo_tls_android.inc rename to compiler-rt/lib/scudo/scudo_tsd_shared.inc index 62855f7..874942b 100644 --- a/compiler-rt/lib/scudo/scudo_tls_android.inc +++ b/compiler-rt/lib/scudo/scudo_tsd_shared.inc @@ -1,4 +1,4 @@ -//===-- scudo_tls_android.inc -----------------------------------*- C++ -*-===// +//===-- scudo_tsd_shared.inc ------------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -7,15 +7,15 @@ // //===----------------------------------------------------------------------===// /// -/// Scudo thread local structure fastpath functions implementation for Android. +/// Scudo shared TSD fastpath functions implementation. /// //===----------------------------------------------------------------------===// -#ifndef SCUDO_TLS_H_ -# error "This file must be included inside scudo_tls.h." -#endif // SCUDO_TLS_H_ +#ifndef SCUDO_TSD_H_ +# error "This file must be included inside scudo_tsd.h." +#endif // SCUDO_TSD_H_ -#if SANITIZER_LINUX && SANITIZER_ANDROID +#if !SCUDO_TSD_EXCLUSIVE ALWAYS_INLINE void initThreadMaybe(bool MinimalInit = false) { if (LIKELY(*get_android_tls_ptr())) @@ -35,4 +35,4 @@ ALWAYS_INLINE ScudoTSD *getTSDAndLock() { return getTSDAndLockSlow(); } -#endif // SANITIZER_LINUX && SANITIZER_ANDROID +#endif // !SCUDO_TSD_EXCLUSIVE -- 2.7.4