lsan_allocator.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 !SANITIZER_CAN_USE_ALLOCATOR64
  44. template <typename AddressSpaceViewTy>
  45. struct AP32 {
  46. static const uptr kSpaceBeg = 0;
  47. static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
  48. static const uptr kMetadataSize = sizeof(ChunkMetadata);
  49. typedef __sanitizer::CompactSizeClassMap SizeClassMap;
  50. static const uptr kRegionSizeLog = 20;
  51. using AddressSpaceView = AddressSpaceViewTy;
  52. typedef NoOpMapUnmapCallback MapUnmapCallback;
  53. static const uptr kFlags = 0;
  54. };
  55. template <typename AddressSpaceView>
  56. using PrimaryAllocatorASVT = SizeClassAllocator32<AP32<AddressSpaceView>>;
  57. using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
  58. #else
  59. # if SANITIZER_FUCHSIA || defined(__powerpc64__)
  60. const uptr kAllocatorSpace = ~(uptr)0;
  61. const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
  62. #elif defined(__s390x__)
  63. const uptr kAllocatorSpace = 0x40000000000ULL;
  64. const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
  65. # else
  66. const uptr kAllocatorSpace = 0x600000000000ULL;
  67. const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
  68. # endif
  69. template <typename AddressSpaceViewTy>
  70. struct AP64 { // Allocator64 parameters. Deliberately using a short name.
  71. static const uptr kSpaceBeg = kAllocatorSpace;
  72. static const uptr kSpaceSize = kAllocatorSize;
  73. static const uptr kMetadataSize = sizeof(ChunkMetadata);
  74. typedef DefaultSizeClassMap SizeClassMap;
  75. typedef NoOpMapUnmapCallback MapUnmapCallback;
  76. static const uptr kFlags = 0;
  77. using AddressSpaceView = AddressSpaceViewTy;
  78. };
  79. template <typename AddressSpaceView>
  80. using PrimaryAllocatorASVT = SizeClassAllocator64<AP64<AddressSpaceView>>;
  81. using PrimaryAllocator = PrimaryAllocatorASVT<LocalAddressSpaceView>;
  82. #endif
  83. template <typename AddressSpaceView>
  84. using AllocatorASVT = CombinedAllocator<PrimaryAllocatorASVT<AddressSpaceView>>;
  85. using Allocator = AllocatorASVT<LocalAddressSpaceView>;
  86. using AllocatorCache = Allocator::AllocatorCache;
  87. Allocator::AllocatorCache *GetAllocatorCache();
  88. int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,
  89. const StackTrace &stack);
  90. void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack);
  91. void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
  92. void *lsan_malloc(uptr size, const StackTrace &stack);
  93. void lsan_free(void *p);
  94. void *lsan_realloc(void *p, uptr size, const StackTrace &stack);
  95. void *lsan_reallocarray(void *p, uptr nmemb, uptr size,
  96. const StackTrace &stack);
  97. void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack);
  98. void *lsan_valloc(uptr size, const StackTrace &stack);
  99. void *lsan_pvalloc(uptr size, const StackTrace &stack);
  100. uptr lsan_mz_size(const void *p);
  101. } // namespace __lsan
  102. #endif // LSAN_ALLOCATOR_H