dfsan_allocator.cpp 9.2 KB

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