From 0f21545a3c9e57b7df4c68aaff92cb83ae7ce5d2 Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Sat, 27 Apr 2019 06:30:52 +0000 Subject: [PATCH] [sanitizer] Calculate SizeClassAllocator32::ByteMap type from Params::kSpaceSize and Params::kRegionSizeLog Reviewers: eugenis Subscribers: kubamracek, cryptoad, #sanitizers, llvm-commits Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D61206 llvm-svn: 359374 --- compiler-rt/lib/asan/asan_allocator.h | 13 +------- compiler-rt/lib/lsan/lsan_allocator.h | 15 +-------- compiler-rt/lib/msan/msan_allocator.cc | 12 ++----- .../sanitizer_allocator_internal.h | 11 +----- .../sanitizer_allocator_primary32.h | 39 ++++------------------ .../tests/sanitizer_allocator_test.cc | 11 ++---- compiler-rt/lib/scudo/scudo_allocator.h | 7 ---- compiler-rt/lib/tsan/rtl/tsan_rtl.h | 11 ++---- 8 files changed, 17 insertions(+), 102 deletions(-) diff --git a/compiler-rt/lib/asan/asan_allocator.h b/compiler-rt/lib/asan/asan_allocator.h index f00776c..7b9e730 100644 --- a/compiler-rt/lib/asan/asan_allocator.h +++ b/compiler-rt/lib/asan/asan_allocator.h @@ -166,16 +166,6 @@ template using PrimaryAllocatorASVT = SizeClassAllocator64>; using PrimaryAllocator = PrimaryAllocatorASVT; #else // Fallback to SizeClassAllocator32. -static const uptr kRegionSizeLog = 20; -static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog; -# if SANITIZER_WORDSIZE == 32 -template -using ByteMapASVT = FlatByteMap; -# elif SANITIZER_WORDSIZE == 64 -template -using ByteMapASVT = - TwoLevelByteMap<(kNumRegions >> 12), 1 << 12, AddressSpaceView>; -# endif typedef CompactSizeClassMap SizeClassMap; template struct AP32 { @@ -183,9 +173,8 @@ struct AP32 { static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; static const uptr kMetadataSize = 16; typedef __asan::SizeClassMap SizeClassMap; - static const uptr kRegionSizeLog = __asan::kRegionSizeLog; + static const uptr kRegionSizeLog = 20; using AddressSpaceView = AddressSpaceViewTy; - using ByteMap = __asan::ByteMapASVT; typedef AsanMapUnmapCallback MapUnmapCallback; static const uptr kFlags = 0; }; diff --git a/compiler-rt/lib/lsan/lsan_allocator.h b/compiler-rt/lib/lsan/lsan_allocator.h index e3bdfe8..f3888b9 100644 --- a/compiler-rt/lib/lsan/lsan_allocator.h +++ b/compiler-rt/lib/lsan/lsan_allocator.h @@ -51,27 +51,14 @@ struct ChunkMetadata { #if defined(__mips64) || defined(__aarch64__) || defined(__i386__) || \ defined(__arm__) -static const uptr kRegionSizeLog = 20; -static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog; - -#if SANITIZER_WORDSIZE == 32 -template -using ByteMapASVT = FlatByteMap; -#elif SANITIZER_WORDSIZE == 64 -template -using ByteMapASVT = - TwoLevelByteMap<(kNumRegions >> 12), 1 << 12, AddressSpaceView>; -#endif - template struct AP32 { static const uptr kSpaceBeg = 0; static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; static const uptr kMetadataSize = sizeof(ChunkMetadata); typedef __sanitizer::CompactSizeClassMap SizeClassMap; - static const uptr kRegionSizeLog = __lsan::kRegionSizeLog; + static const uptr kRegionSizeLog = 20; using AddressSpaceView = AddressSpaceViewTy; - using ByteMap = __lsan::ByteMapASVT; typedef NoOpMapUnmapCallback MapUnmapCallback; static const uptr kFlags = 0; }; diff --git a/compiler-rt/lib/msan/msan_allocator.cc b/compiler-rt/lib/msan/msan_allocator.cc index 51e8704..48430b6 100644 --- a/compiler-rt/lib/msan/msan_allocator.cc +++ b/compiler-rt/lib/msan/msan_allocator.cc @@ -46,18 +46,14 @@ struct MsanMapUnmapCallback { #if defined(__mips64) static const uptr kMaxAllowedMallocSize = 2UL << 30; -static const uptr kRegionSizeLog = 20; -static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog; -typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap; struct AP32 { static const uptr kSpaceBeg = 0; static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; static const uptr kMetadataSize = sizeof(Metadata); typedef __sanitizer::CompactSizeClassMap SizeClassMap; - static const uptr kRegionSizeLog = __msan::kRegionSizeLog; + static const uptr kRegionSizeLog = 20; using AddressSpaceView = LocalAddressSpaceView; - using ByteMap = __msan::ByteMap; typedef MsanMapUnmapCallback MapUnmapCallback; static const uptr kFlags = 0; }; @@ -99,18 +95,14 @@ struct AP64 { // Allocator64 parameters. Deliberately using a short name. typedef SizeClassAllocator64 PrimaryAllocator; #elif defined(__aarch64__) static const uptr kMaxAllowedMallocSize = 2UL << 30; // 2G -static const uptr kRegionSizeLog = 20; -static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog; -typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap; struct AP32 { static const uptr kSpaceBeg = 0; static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; static const uptr kMetadataSize = sizeof(Metadata); typedef __sanitizer::CompactSizeClassMap SizeClassMap; - static const uptr kRegionSizeLog = __msan::kRegionSizeLog; + static const uptr kRegionSizeLog = 20; using AddressSpaceView = LocalAddressSpaceView; - using ByteMap = __msan::ByteMap; typedef MsanMapUnmapCallback MapUnmapCallback; static const uptr kFlags = 0; }; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_internal.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_internal.h index 7066e35..1b122f1 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_internal.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_internal.h @@ -22,22 +22,13 @@ namespace __sanitizer { // purposes. typedef CompactSizeClassMap InternalSizeClassMap; -static const uptr kInternalAllocatorRegionSizeLog = 20; -static const uptr kInternalAllocatorNumRegions = - SANITIZER_MMAP_RANGE_SIZE >> kInternalAllocatorRegionSizeLog; -#if SANITIZER_WORDSIZE == 32 -typedef FlatByteMap ByteMap; -#elif SANITIZER_WORDSIZE == 64 -typedef TwoLevelByteMap<(kInternalAllocatorNumRegions >> 12), 1 << 12> ByteMap; -#endif struct AP32 { static const uptr kSpaceBeg = 0; static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; static const uptr kMetadataSize = 0; typedef InternalSizeClassMap SizeClassMap; - static const uptr kRegionSizeLog = kInternalAllocatorRegionSizeLog; + static const uptr kRegionSizeLog = 20; using AddressSpaceView = LocalAddressSpaceView; - using ByteMap = __sanitizer::ByteMap; typedef NoOpMapUnmapCallback MapUnmapCallback; static const uptr kFlags = 0; }; diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h index cb0cdbe..47dc45a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_allocator_primary32.h @@ -41,23 +41,13 @@ struct SizeClassAllocator32FlagMasks { // Bit masks. enum { kRandomShuffleChunks = 1, kUseSeparateSizeClassForBatch = 2, - kForTest = 4, }; }; -// This template is not necessary but t helps to see values if static_assert -// fails. -// FIXME: Replace check with automatic type detection. D61206 -template -struct CheckTwoLevelByteMapSize { - static_assert((ActualSize >= MinSize) == Expected, - "Unexpected ByteMap type for the size"); -}; - template class SizeClassAllocator32 { private: - static const u64 TwoLevelByteMapSize1 = + static const u64 kTwoLevelByteMapSize1 = (Params::kSpaceSize >> Params::kRegionSizeLog) >> 12; static const u64 kMinFirstMapSizeTwoLevelByteMap = 4; @@ -68,29 +58,12 @@ class SizeClassAllocator32 { static const uptr kMetadataSize = Params::kMetadataSize; typedef typename Params::SizeClassMap SizeClassMap; static const uptr kRegionSizeLog = Params::kRegionSizeLog; - typedef typename Params::ByteMap ByteMap; typedef typename Params::MapUnmapCallback MapUnmapCallback; - -#if SANITIZER_WORDSIZE == 32 - CheckTwoLevelByteMapSize - Check; - using BM = FlatByteMap<(Params::kSpaceSize >> Params::kRegionSizeLog), - AddressSpaceView>; -#elif SANITIZER_WORDSIZE == 64 - CheckTwoLevelByteMapSize - Check; - using BM = TwoLevelByteMap; -#endif - static_assert((Params::kFlags & SizeClassAllocator32FlagMasks::kForTest) || - is_same::value, - "Unexpected ByteMap type"); - - static_assert( - - is_same::value, - "AddressSpaceView type mismatch"); + using ByteMap = typename conditional< + (kTwoLevelByteMapSize1 < kMinFirstMapSizeTwoLevelByteMap), + FlatByteMap<(Params::kSpaceSize >> Params::kRegionSizeLog), + AddressSpaceView>, + TwoLevelByteMap>::type; COMPILER_CHECK(!SANITIZER_SIGN_EXTENDED_ADDRESSES || (kSpaceSize & (kSpaceSize - 1)) == 0); diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator_test.cc b/compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator_test.cc index dcbe46f..d19b376 100644 --- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator_test.cc +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_allocator_test.cc @@ -143,7 +143,6 @@ static const u64 kAddressSpaceSize = 1ULL << 32; #endif static const uptr kRegionSizeLog = FIRST_32_SECOND_64(20, 24); -static const uptr kFlatByteMapSize = kAddressSpaceSize >> kRegionSizeLog; template struct AP32Compact { @@ -153,9 +152,8 @@ struct AP32Compact { typedef CompactSizeClassMap SizeClassMap; static const uptr kRegionSizeLog = ::kRegionSizeLog; using AddressSpaceView = AddressSpaceViewTy; - using ByteMap = FlatByteMap; typedef NoOpMapUnmapCallback MapUnmapCallback; - static const uptr kFlags = SizeClassAllocator32FlagMasks::kForTest; + static const uptr kFlags = 0; }; template using Allocator32CompactASVT = @@ -299,11 +297,9 @@ struct AP32SeparateBatches { typedef DefaultSizeClassMap SizeClassMap; static const uptr kRegionSizeLog = ::kRegionSizeLog; using AddressSpaceView = AddressSpaceViewTy; - using ByteMap = FlatByteMap; typedef NoOpMapUnmapCallback MapUnmapCallback; static const uptr kFlags = - SizeClassAllocator32FlagMasks::kUseSeparateSizeClassForBatch | - SizeClassAllocator32FlagMasks::kForTest; + SizeClassAllocator32FlagMasks::kUseSeparateSizeClassForBatch; }; template using Allocator32SeparateBatchesASVT = @@ -475,9 +471,8 @@ struct AP32WithCallback { typedef CompactSizeClassMap SizeClassMap; static const uptr kRegionSizeLog = ::kRegionSizeLog; using AddressSpaceView = AddressSpaceViewTy; - using ByteMap = FlatByteMap; typedef TestMapUnmapCallback MapUnmapCallback; - static const uptr kFlags = SizeClassAllocator32FlagMasks::kForTest; + static const uptr kFlags = 0; }; TEST(SanitizerCommon, SizeClassAllocator32MapUnmapCallback) { diff --git a/compiler-rt/lib/scudo/scudo_allocator.h b/compiler-rt/lib/scudo/scudo_allocator.h index 0edcd12..59225b6 100644 --- a/compiler-rt/lib/scudo/scudo_allocator.h +++ b/compiler-rt/lib/scudo/scudo_allocator.h @@ -84,12 +84,6 @@ struct AP64 { }; typedef SizeClassAllocator64 PrimaryT; #else -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 struct AP32 { static const uptr kSpaceBeg = 0; static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; @@ -97,7 +91,6 @@ struct AP32 { typedef __scudo::SizeClassMap SizeClassMap; static const uptr kRegionSizeLog = RegionSizeLog; using AddressSpaceView = LocalAddressSpaceView; - using ByteMap = __scudo::ByteMap; typedef NoOpMapUnmapCallback MapUnmapCallback; static const uptr kFlags = SizeClassAllocator32FlagMasks::kRandomShuffleChunks | diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl.h b/compiler-rt/lib/tsan/rtl/tsan_rtl.h index 033eb11..2c8ae29f 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_rtl.h +++ b/compiler-rt/lib/tsan/rtl/tsan_rtl.h @@ -55,21 +55,16 @@ namespace __tsan { #if !SANITIZER_GO struct MapUnmapCallback; #if defined(__mips64) || defined(__aarch64__) || defined(__powerpc__) -static const uptr kAllocatorRegionSizeLog = 20; -static const uptr kAllocatorNumRegions = - SANITIZER_MMAP_RANGE_SIZE >> kAllocatorRegionSizeLog; -using ByteMap = TwoLevelByteMap<(kAllocatorNumRegions >> 12), 1 << 12, - LocalAddressSpaceView, MapUnmapCallback>; + struct AP32 { static const uptr kSpaceBeg = 0; static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE; static const uptr kMetadataSize = 0; typedef __sanitizer::CompactSizeClassMap SizeClassMap; - static const uptr kRegionSizeLog = kAllocatorRegionSizeLog; + static const uptr kRegionSizeLog = 20; using AddressSpaceView = LocalAddressSpaceView; - using ByteMap = __tsan::ByteMap; typedef __tsan::MapUnmapCallback MapUnmapCallback; - static const uptr kFlags = SizeClassAllocator32FlagMasks::kForTest; + static const uptr kFlags = 0; }; typedef SizeClassAllocator32 PrimaryAllocator; #else -- 2.7.4