lsan_allocator.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 AllocatorThreadFinish();
  30. void InitializeAllocator();
  31. const bool kAlwaysClearMemory = true;
  32. struct ChunkMetadata {
  33. u8 allocated : 8; // Must be first.
  34. ChunkTag tag : 2;
  35. #if SANITIZER_WORDSIZE == 64
  36. uptr requested_size : 54;
  37. #else
  38. uptr requested_size : 32;
  39. uptr padding : 22;
  40. #endif
  41. u32 stack_trace_id;
  42. };
  43. #if defined(__mips64) || defined(__aarch64__) || defined(__i386__) || \
  44. defined(__arm__) || SANITIZER_RISCV64 || defined(__hexagon__)
  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. #elif defined(__x86_64__) || defined(__powerpc64__) || defined(__s390x__)
  60. # if SANITIZER_FUCHSIA
  61. const uptr kAllocatorSpace = ~(uptr)0;
  62. const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
  63. # elif defined(__powerpc64__)
  64. const uptr kAllocatorSpace = 0xa0000000000ULL;
  65. const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
  66. #elif defined(__s390x__)
  67. const uptr kAllocatorSpace = 0x40000000000ULL;
  68. const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
  69. # else
  70. const uptr kAllocatorSpace = 0x600000000000ULL;
  71. const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
  72. # endif
  73. template <typename AddressSpaceViewTy>
  74. struct AP64 { // Allocator64 parameters. Deliberately using a short name.
  75. static const uptr kSpaceBeg = kAllocatorSpace;
  76. static const uptr kSpaceSize = kAllocatorSize;
  77. static const uptr kMetadataSize = sizeof(ChunkMetadata);
  78. typedef DefaultSizeClassMap SizeClassMap;
  79. typedef NoOpMapUnmapCallback MapUnmapCallback;
  80. static const uptr kFlags = 0;
  81. using AddressSpaceView = AddressSpaceViewTy;
  82. };
  83. template <typename AddressSpaceView>
  84. using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
  85. using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
  86. #endif
  87. template <typename AddressSpaceView>
  88. using AllocatorASVT = CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;
  89. using Allocator = AllocatorASVT<LocalAddressSpaceView>;
  90. using AllocatorCache = Allocator::AllocatorCache;
  91. Allocator::AllocatorCache *GetAllocatorCache();
  92. int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,
  93. const StackTrace &stack);
  94. void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack);
  95. void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
  96. void *lsan_malloc(uptr size, const StackTrace &stack);
  97. void lsan_free(void *p);
  98. void *lsan_realloc(void *p, uptr size, const StackTrace &stack);
  99. void *lsan_reallocarray(void *p, uptr nmemb, uptr size,
  100. const StackTrace &stack);
  101. void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack);
  102. void *lsan_valloc(uptr size, const StackTrace &stack);
  103. void *lsan_pvalloc(uptr size, const StackTrace &stack);
  104. uptr lsan_mz_size(const void *p);
  105. } // namespace __lsan
  106. #endif // LSAN_ALLOCATOR_H