dfsan_allocator.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //===-- dfsan_allocator.cpp -------------------------- --------------------===//
  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 DataflowSanitizer.
  10. //
  11. // DataflowSanitizer allocator.
  12. //===----------------------------------------------------------------------===//
  13. #include "dfsan_allocator.h"
  14. #include "dfsan.h"
  15. #include "dfsan_flags.h"
  16. #include "dfsan_thread.h"
  17. #include "sanitizer_common/sanitizer_allocator.h"
  18. #include "sanitizer_common/sanitizer_allocator_checks.h"
  19. #include "sanitizer_common/sanitizer_allocator_interface.h"
  20. #include "sanitizer_common/sanitizer_allocator_report.h"
  21. #include "sanitizer_common/sanitizer_errno.h"
  22. namespace __dfsan {
  23. struct Metadata {
  24. uptr requested_size;
  25. };
  26. struct DFsanMapUnmapCallback {
  27. void OnMap(uptr p, uptr size) const { dfsan_set_label(0, (void *)p, size); }
  28. void OnUnmap(uptr p, uptr size) const { dfsan_set_label(0, (void *)p, size); }
  29. };
  30. static const uptr kAllocatorSpace = 0x700000000000ULL;
  31. static const uptr kMaxAllowedMallocSize = 8UL << 30;
  32. struct AP64 { // Allocator64 parameters. Deliberately using a short name.
  33. static const uptr kSpaceBeg = kAllocatorSpace;
  34. static const uptr kSpaceSize = 0x40000000000; // 4T.
  35. static const uptr kMetadataSize = sizeof(Metadata);
  36. typedef DefaultSizeClassMap SizeClassMap;
  37. typedef DFsanMapUnmapCallback MapUnmapCallback;
  38. static const uptr kFlags = 0;
  39. using AddressSpaceView = LocalAddressSpaceView;
  40. };
  41. typedef SizeClassAllocator64<AP64> PrimaryAllocator;
  42. typedef CombinedAllocator<PrimaryAllocator> Allocator;
  43. typedef Allocator::AllocatorCache AllocatorCache;
  44. static Allocator allocator;
  45. static AllocatorCache fallback_allocator_cache;
  46. static StaticSpinMutex fallback_mutex;
  47. static uptr max_malloc_size;
  48. void dfsan_allocator_init() {
  49. SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
  50. allocator.Init(common_flags()->allocator_release_to_os_interval_ms);
  51. if (common_flags()->max_allocation_size_mb)
  52. max_malloc_size = Min(common_flags()->max_allocation_size_mb << 20,
  53. kMaxAllowedMallocSize);
  54. else
  55. max_malloc_size = kMaxAllowedMallocSize;
  56. }
  57. AllocatorCache *GetAllocatorCache(DFsanThreadLocalMallocStorage *ms) {
  58. CHECK(ms);
  59. CHECK_LE(sizeof(AllocatorCache), sizeof(ms->allocator_cache));
  60. return reinterpret_cast<AllocatorCache *>(ms->allocator_cache);
  61. }
  62. void DFsanThreadLocalMallocStorage::CommitBack() {
  63. allocator.SwallowCache(GetAllocatorCache(this));
  64. }
  65. static void *DFsanAllocate(uptr size, uptr alignment, bool zeroise) {
  66. if (size > max_malloc_size) {
  67. if (AllocatorMayReturnNull()) {
  68. Report("WARNING: DataflowSanitizer failed to allocate 0x%zx bytes\n",
  69. size);
  70. return nullptr;
  71. }
  72. BufferedStackTrace stack;
  73. ReportAllocationSizeTooBig(size, max_malloc_size, &stack);
  74. }
  75. if (UNLIKELY(IsRssLimitExceeded())) {
  76. if (AllocatorMayReturnNull())
  77. return nullptr;
  78. BufferedStackTrace stack;
  79. ReportRssLimitExceeded(&stack);
  80. }
  81. DFsanThread *t = GetCurrentThread();
  82. void *allocated;
  83. if (t) {
  84. AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
  85. allocated = allocator.Allocate(cache, size, alignment);
  86. } else {
  87. SpinMutexLock l(&fallback_mutex);
  88. AllocatorCache *cache = &fallback_allocator_cache;
  89. allocated = allocator.Allocate(cache, size, alignment);
  90. }
  91. if (UNLIKELY(!allocated)) {
  92. SetAllocatorOutOfMemory();
  93. if (AllocatorMayReturnNull())
  94. return nullptr;
  95. BufferedStackTrace stack;
  96. ReportOutOfMemory(size, &stack);
  97. }
  98. Metadata *meta =
  99. reinterpret_cast<Metadata *>(allocator.GetMetaData(allocated));
  100. meta->requested_size = size;
  101. if (zeroise) {
  102. internal_memset(allocated, 0, size);
  103. dfsan_set_label(0, allocated, size);
  104. } else if (flags().zero_in_malloc) {
  105. dfsan_set_label(0, allocated, size);
  106. }
  107. return allocated;
  108. }
  109. void dfsan_deallocate(void *p) {
  110. CHECK(p);
  111. Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(p));
  112. uptr size = meta->requested_size;
  113. meta->requested_size = 0;
  114. if (flags().zero_in_free)
  115. dfsan_set_label(0, p, size);
  116. DFsanThread *t = GetCurrentThread();
  117. if (t) {
  118. AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
  119. allocator.Deallocate(cache, p);
  120. } else {
  121. SpinMutexLock l(&fallback_mutex);
  122. AllocatorCache *cache = &fallback_allocator_cache;
  123. allocator.Deallocate(cache, p);
  124. }
  125. }
  126. void *DFsanReallocate(void *old_p, uptr new_size, uptr alignment) {
  127. Metadata *meta = reinterpret_cast<Metadata *>(allocator.GetMetaData(old_p));
  128. uptr old_size = meta->requested_size;
  129. uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(old_p);
  130. if (new_size <= actually_allocated_size) {
  131. // We are not reallocating here.
  132. meta->requested_size = new_size;
  133. if (new_size > old_size && flags().zero_in_malloc)
  134. dfsan_set_label(0, (char *)old_p + old_size, new_size - old_size);
  135. return old_p;
  136. }
  137. uptr memcpy_size = Min(new_size, old_size);
  138. void *new_p = DFsanAllocate(new_size, alignment, false /*zeroise*/);
  139. if (new_p) {
  140. dfsan_copy_memory(new_p, old_p, memcpy_size);
  141. dfsan_deallocate(old_p);
  142. }
  143. return new_p;
  144. }
  145. void *DFsanCalloc(uptr nmemb, uptr size) {
  146. if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
  147. if (AllocatorMayReturnNull())
  148. return nullptr;
  149. BufferedStackTrace stack;
  150. ReportCallocOverflow(nmemb, size, &stack);
  151. }
  152. return DFsanAllocate(nmemb * size, sizeof(u64), true /*zeroise*/);
  153. }
  154. static uptr AllocationSize(const void *p) {
  155. if (!p)
  156. return 0;
  157. const void *beg = allocator.GetBlockBegin(p);
  158. if (beg != p)
  159. return 0;
  160. Metadata *b = (Metadata *)allocator.GetMetaData(p);
  161. return b->requested_size;
  162. }
  163. void *dfsan_malloc(uptr size) {
  164. return SetErrnoOnNull(DFsanAllocate(size, sizeof(u64), false /*zeroise*/));
  165. }
  166. void *dfsan_calloc(uptr nmemb, uptr size) {
  167. return SetErrnoOnNull(DFsanCalloc(nmemb, size));
  168. }
  169. void *dfsan_realloc(void *ptr, uptr size) {
  170. if (!ptr)
  171. return SetErrnoOnNull(DFsanAllocate(size, sizeof(u64), false /*zeroise*/));
  172. if (size == 0) {
  173. dfsan_deallocate(ptr);
  174. return nullptr;
  175. }
  176. return SetErrnoOnNull(DFsanReallocate(ptr, size, sizeof(u64)));
  177. }
  178. void *dfsan_reallocarray(void *ptr, uptr nmemb, uptr size) {
  179. if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
  180. errno = errno_ENOMEM;
  181. if (AllocatorMayReturnNull())
  182. return nullptr;
  183. BufferedStackTrace stack;
  184. ReportReallocArrayOverflow(nmemb, size, &stack);
  185. }
  186. return dfsan_realloc(ptr, nmemb * size);
  187. }
  188. void *dfsan_valloc(uptr size) {
  189. return SetErrnoOnNull(
  190. DFsanAllocate(size, GetPageSizeCached(), false /*zeroise*/));
  191. }
  192. void *dfsan_pvalloc(uptr size) {
  193. uptr PageSize = GetPageSizeCached();
  194. if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {
  195. errno = errno_ENOMEM;
  196. if (AllocatorMayReturnNull())
  197. return nullptr;
  198. BufferedStackTrace stack;
  199. ReportPvallocOverflow(size, &stack);
  200. }
  201. // pvalloc(0) should allocate one page.
  202. size = size ? RoundUpTo(size, PageSize) : PageSize;
  203. return SetErrnoOnNull(DFsanAllocate(size, PageSize, false /*zeroise*/));
  204. }
  205. void *dfsan_aligned_alloc(uptr alignment, uptr size) {
  206. if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
  207. errno = errno_EINVAL;
  208. if (AllocatorMayReturnNull())
  209. return nullptr;
  210. BufferedStackTrace stack;
  211. ReportInvalidAlignedAllocAlignment(size, alignment, &stack);
  212. }
  213. return SetErrnoOnNull(DFsanAllocate(size, alignment, false /*zeroise*/));
  214. }
  215. void *dfsan_memalign(uptr alignment, uptr size) {
  216. if (UNLIKELY(!IsPowerOfTwo(alignment))) {
  217. errno = errno_EINVAL;
  218. if (AllocatorMayReturnNull())
  219. return nullptr;
  220. BufferedStackTrace stack;
  221. ReportInvalidAllocationAlignment(alignment, &stack);
  222. }
  223. return SetErrnoOnNull(DFsanAllocate(size, alignment, false /*zeroise*/));
  224. }
  225. int dfsan_posix_memalign(void **memptr, uptr alignment, uptr size) {
  226. if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {
  227. if (AllocatorMayReturnNull())
  228. return errno_EINVAL;
  229. BufferedStackTrace stack;
  230. ReportInvalidPosixMemalignAlignment(alignment, &stack);
  231. }
  232. void *ptr = DFsanAllocate(size, alignment, false /*zeroise*/);
  233. if (UNLIKELY(!ptr))
  234. // OOM error is already taken care of by DFsanAllocate.
  235. return errno_ENOMEM;
  236. CHECK(IsAligned((uptr)ptr, alignment));
  237. *memptr = ptr;
  238. return 0;
  239. }
  240. } // namespace __dfsan
  241. using namespace __dfsan;
  242. uptr __sanitizer_get_current_allocated_bytes() {
  243. uptr stats[AllocatorStatCount];
  244. allocator.GetStats(stats);
  245. return stats[AllocatorStatAllocated];
  246. }
  247. uptr __sanitizer_get_heap_size() {
  248. uptr stats[AllocatorStatCount];
  249. allocator.GetStats(stats);
  250. return stats[AllocatorStatMapped];
  251. }
  252. uptr __sanitizer_get_free_bytes() { return 1; }
  253. uptr __sanitizer_get_unmapped_bytes() { return 1; }
  254. uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
  255. int __sanitizer_get_ownership(const void *p) { return AllocationSize(p) != 0; }
  256. uptr __sanitizer_get_allocated_size(const void *p) { return AllocationSize(p); }