lsan_allocator.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //=-- lsan_allocator.h ----------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of LeakSanitizer.
  10. // Allocator for standalone LSan.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LSAN_ALLOCATOR_H
  14. #define LSAN_ALLOCATOR_H
  15. #include "sanitizer_common/sanitizer_allocator.h"
  16. #include "sanitizer_common/sanitizer_common.h"
  17. #include "sanitizer_common/sanitizer_internal_defs.h"
  18. #include "lsan_common.h"
  19. namespace __lsan {
  20. void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
  21. bool cleared);
  22. void Deallocate(void *p);
  23. void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
  24. uptr alignment);
  25. uptr GetMallocUsableSize(const void *p);
  26. template<typename Callable>
  27. void ForEachChunk(const Callable &callback);
  28. void GetAllocatorCacheRange(uptr *begin, uptr *end);
  29. void AllocatorThreadStart();
  30. void AllocatorThreadFinish();
  31. void InitializeAllocator();
  32. const bool kAlwaysClearMemory = true;
  33. struct ChunkMetadata {
  34. u8 allocated : 8; // Must be first.
  35. ChunkTag tag : 2;
  36. #if SANITIZER_WORDSIZE == 64
  37. uptr requested_size : 54;
  38. #else
  39. uptr requested_size : 32;
  40. uptr padding : 22;
  41. #endif
  42. u32 stack_trace_id;
  43. };
  44. #if !SANITIZER_CAN_USE_ALLOCATOR64
  45. template <typename AddressSpaceViewTy>
  46. struct AP32 {
  47. static const uptr kSpaceBeg = 0;
  48. static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
  49. static const uptr kMetadataSize = sizeof(ChunkMetadata);
  50. typedef __sanitizer::CompactSizeClassMap SizeClassMap;
  51. static const uptr kRegionSizeLog = 20;
  52. using AddressSpaceView = AddressSpaceViewTy;
  53. typedef NoOpMapUnmapCallback MapUnmapCallback;
  54. static const uptr kFlags = 0;
  55. };
  56. template <typename AddressSpaceView>
  57. using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>;
  58. using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
  59. #else
  60. # if SANITIZER_FUCHSIA || defined(__powerpc64__)
  61. const uptr kAllocatorSpace = ~(uptr)0;
  62. # if SANITIZER_RISCV64
  63. // See the comments in compiler-rt/lib/asan/asan_allocator.h for why these
  64. // values were chosen.
  65. const uptr kAllocatorSize = UINT64_C(1) << 33; // 8GB
  66. using LSanSizeClassMap = SizeClassMap</*kNumBits=*/2,
  67. /*kMinSizeLog=*/5,
  68. /*kMidSizeLog=*/8,
  69. /*kMaxSizeLog=*/18,
  70. /*kNumCachedHintT=*/8,
  71. /*kMaxBytesCachedLog=*/10>;
  72. static_assert(LSanSizeClassMap::kNumClassesRounded <= 32,
  73. "32 size classes is the optimal number to ensure tests run "
  74. "effieciently on Fuchsia.");
  75. # else
  76. const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
  77. using LSanSizeClassMap = DefaultSizeClassMap;
  78. # endif
  79. # elif SANITIZER_RISCV64
  80. const uptr kAllocatorSpace = ~(uptr)0;
  81. const uptr kAllocatorSize = 0x2000000000ULL; // 128G.
  82. using LSanSizeClassMap = DefaultSizeClassMap;
  83. # elif SANITIZER_APPLE
  84. const uptr kAllocatorSpace = 0x600000000000ULL;
  85. const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
  86. using LSanSizeClassMap = DefaultSizeClassMap;
  87. # else
  88. const uptr kAllocatorSpace = 0x500000000000ULL;
  89. const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
  90. using LSanSizeClassMap = DefaultSizeClassMap;
  91. # endif
  92. template <typename AddressSpaceViewTy>
  93. struct AP64 { // Allocator64 parameters. Deliberately using a short name.
  94. static const uptr kSpaceBeg = kAllocatorSpace;
  95. static const uptr kSpaceSize = kAllocatorSize;
  96. static const uptr kMetadataSize = sizeof(ChunkMetadata);
  97. using SizeClassMap = LSanSizeClassMap;
  98. typedef NoOpMapUnmapCallback MapUnmapCallback;
  99. static const uptr kFlags = 0;
  100. using AddressSpaceView = AddressSpaceViewTy;
  101. };
  102. template <typename AddressSpaceView>
  103. using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
  104. using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
  105. #endif
  106. template <typename AddressSpaceView>
  107. using AllocatorASVT = CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;
  108. using Allocator = AllocatorASVT<LocalAddressSpaceView>;
  109. using AllocatorCache = Allocator::AllocatorCache;
  110. Allocator::AllocatorCache *GetAllocatorCache();
  111. int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,
  112. const StackTrace &stack);
  113. void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack);
  114. void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
  115. void *lsan_malloc(uptr size, const StackTrace &stack);
  116. void lsan_free(void *p);
  117. void *lsan_realloc(void *p, uptr size, const StackTrace &stack);
  118. void *lsan_reallocarray(void *p, uptr nmemb, uptr size,
  119. const StackTrace &stack);
  120. void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack);
  121. void *lsan_valloc(uptr size, const StackTrace &stack);
  122. void *lsan_pvalloc(uptr size, const StackTrace &stack);
  123. uptr lsan_mz_size(const void *p);
  124. } // namespace __lsan
  125. #endif // LSAN_ALLOCATOR_H