sanitizer_allocator_primary64.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. //===-- sanitizer_allocator_primary64.h -------------------------*- C++ -*-===//
  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. // Part of the Sanitizer Allocator.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_ALLOCATOR_H
  13. #error This file must be included inside sanitizer_allocator.h
  14. #endif
  15. template<class SizeClassAllocator> struct SizeClassAllocator64LocalCache;
  16. // SizeClassAllocator64 -- allocator for 64-bit address space.
  17. // The template parameter Params is a class containing the actual parameters.
  18. //
  19. // Space: a portion of address space of kSpaceSize bytes starting at SpaceBeg.
  20. // If kSpaceBeg is ~0 then SpaceBeg is chosen dynamically by mmap.
  21. // Otherwise SpaceBeg=kSpaceBeg (fixed address).
  22. // kSpaceSize is a power of two.
  23. // At the beginning the entire space is mprotect-ed, then small parts of it
  24. // are mapped on demand.
  25. //
  26. // Region: a part of Space dedicated to a single size class.
  27. // There are kNumClasses Regions of equal size.
  28. //
  29. // UserChunk: a piece of memory returned to user.
  30. // MetaChunk: kMetadataSize bytes of metadata associated with a UserChunk.
  31. // FreeArray is an array free-d chunks (stored as 4-byte offsets)
  32. //
  33. // A Region looks like this:
  34. // UserChunk1 ... UserChunkN <gap> MetaChunkN ... MetaChunk1 FreeArray
  35. struct SizeClassAllocator64FlagMasks { // Bit masks.
  36. enum {
  37. kRandomShuffleChunks = 1,
  38. };
  39. };
  40. template <typename Allocator>
  41. class MemoryMapper {
  42. public:
  43. typedef typename Allocator::CompactPtrT CompactPtrT;
  44. explicit MemoryMapper(const Allocator &allocator) : allocator_(allocator) {}
  45. bool GetAndResetStats(uptr &ranges, uptr &bytes) {
  46. ranges = released_ranges_count_;
  47. released_ranges_count_ = 0;
  48. bytes = released_bytes_;
  49. released_bytes_ = 0;
  50. return ranges != 0;
  51. }
  52. u64 *MapPackedCounterArrayBuffer(uptr count) {
  53. buffer_.clear();
  54. buffer_.resize(count);
  55. return buffer_.data();
  56. }
  57. // Releases [from, to) range of pages back to OS.
  58. void ReleasePageRangeToOS(uptr class_id, CompactPtrT from, CompactPtrT to) {
  59. const uptr region_base = allocator_.GetRegionBeginBySizeClass(class_id);
  60. const uptr from_page = allocator_.CompactPtrToPointer(region_base, from);
  61. const uptr to_page = allocator_.CompactPtrToPointer(region_base, to);
  62. ReleaseMemoryPagesToOS(from_page, to_page);
  63. released_ranges_count_++;
  64. released_bytes_ += to_page - from_page;
  65. }
  66. private:
  67. const Allocator &allocator_;
  68. uptr released_ranges_count_ = 0;
  69. uptr released_bytes_ = 0;
  70. InternalMmapVector<u64> buffer_;
  71. };
  72. template <class Params>
  73. class SizeClassAllocator64 {
  74. public:
  75. using AddressSpaceView = typename Params::AddressSpaceView;
  76. static const uptr kSpaceBeg = Params::kSpaceBeg;
  77. static const uptr kSpaceSize = Params::kSpaceSize;
  78. static const uptr kMetadataSize = Params::kMetadataSize;
  79. typedef typename Params::SizeClassMap SizeClassMap;
  80. typedef typename Params::MapUnmapCallback MapUnmapCallback;
  81. static const bool kRandomShuffleChunks =
  82. Params::kFlags & SizeClassAllocator64FlagMasks::kRandomShuffleChunks;
  83. typedef SizeClassAllocator64<Params> ThisT;
  84. typedef SizeClassAllocator64LocalCache<ThisT> AllocatorCache;
  85. typedef MemoryMapper<ThisT> MemoryMapperT;
  86. // When we know the size class (the region base) we can represent a pointer
  87. // as a 4-byte integer (offset from the region start shifted right by 4).
  88. typedef u32 CompactPtrT;
  89. static const uptr kCompactPtrScale = 4;
  90. CompactPtrT PointerToCompactPtr(uptr base, uptr ptr) const {
  91. return static_cast<CompactPtrT>((ptr - base) >> kCompactPtrScale);
  92. }
  93. uptr CompactPtrToPointer(uptr base, CompactPtrT ptr32) const {
  94. return base + (static_cast<uptr>(ptr32) << kCompactPtrScale);
  95. }
  96. // If heap_start is nonzero, assumes kSpaceSize bytes are already mapped R/W
  97. // at heap_start and places the heap there. This mode requires kSpaceBeg ==
  98. // ~(uptr)0.
  99. void Init(s32 release_to_os_interval_ms, uptr heap_start = 0) {
  100. uptr TotalSpaceSize = kSpaceSize + AdditionalSize();
  101. PremappedHeap = heap_start != 0;
  102. if (PremappedHeap) {
  103. CHECK(!kUsingConstantSpaceBeg);
  104. NonConstSpaceBeg = heap_start;
  105. uptr RegionInfoSize = AdditionalSize();
  106. RegionInfoSpace =
  107. address_range.Init(RegionInfoSize, PrimaryAllocatorName);
  108. CHECK_NE(RegionInfoSpace, ~(uptr)0);
  109. CHECK_EQ(RegionInfoSpace,
  110. address_range.MapOrDie(RegionInfoSpace, RegionInfoSize,
  111. "SizeClassAllocator: region info"));
  112. MapUnmapCallback().OnMap(RegionInfoSpace, RegionInfoSize);
  113. } else {
  114. if (kUsingConstantSpaceBeg) {
  115. CHECK(IsAligned(kSpaceBeg, SizeClassMap::kMaxSize));
  116. CHECK_EQ(kSpaceBeg,
  117. address_range.Init(TotalSpaceSize, PrimaryAllocatorName,
  118. kSpaceBeg));
  119. } else {
  120. // Combined allocator expects that an 2^N allocation is always aligned
  121. // to 2^N. For this to work, the start of the space needs to be aligned
  122. // as high as the largest size class (which also needs to be a power of
  123. // 2).
  124. NonConstSpaceBeg = address_range.InitAligned(
  125. TotalSpaceSize, SizeClassMap::kMaxSize, PrimaryAllocatorName);
  126. CHECK_NE(NonConstSpaceBeg, ~(uptr)0);
  127. }
  128. RegionInfoSpace = SpaceEnd();
  129. MapWithCallbackOrDie(RegionInfoSpace, AdditionalSize(),
  130. "SizeClassAllocator: region info");
  131. }
  132. SetReleaseToOSIntervalMs(release_to_os_interval_ms);
  133. // Check that the RegionInfo array is aligned on the CacheLine size.
  134. DCHECK_EQ(RegionInfoSpace % kCacheLineSize, 0);
  135. }
  136. s32 ReleaseToOSIntervalMs() const {
  137. return atomic_load(&release_to_os_interval_ms_, memory_order_relaxed);
  138. }
  139. void SetReleaseToOSIntervalMs(s32 release_to_os_interval_ms) {
  140. atomic_store(&release_to_os_interval_ms_, release_to_os_interval_ms,
  141. memory_order_relaxed);
  142. }
  143. void ForceReleaseToOS() {
  144. MemoryMapperT memory_mapper(*this);
  145. for (uptr class_id = 1; class_id < kNumClasses; class_id++) {
  146. Lock l(&GetRegionInfo(class_id)->mutex);
  147. MaybeReleaseToOS(&memory_mapper, class_id, true /*force*/);
  148. }
  149. }
  150. static bool CanAllocate(uptr size, uptr alignment) {
  151. return size <= SizeClassMap::kMaxSize &&
  152. alignment <= SizeClassMap::kMaxSize;
  153. }
  154. NOINLINE void ReturnToAllocator(MemoryMapperT *memory_mapper,
  155. AllocatorStats *stat, uptr class_id,
  156. const CompactPtrT *chunks, uptr n_chunks) {
  157. RegionInfo *region = GetRegionInfo(class_id);
  158. uptr region_beg = GetRegionBeginBySizeClass(class_id);
  159. CompactPtrT *free_array = GetFreeArray(region_beg);
  160. Lock l(&region->mutex);
  161. uptr old_num_chunks = region->num_freed_chunks;
  162. uptr new_num_freed_chunks = old_num_chunks + n_chunks;
  163. // Failure to allocate free array space while releasing memory is non
  164. // recoverable.
  165. if (UNLIKELY(!EnsureFreeArraySpace(region, region_beg,
  166. new_num_freed_chunks))) {
  167. Report("FATAL: Internal error: %s's allocator exhausted the free list "
  168. "space for size class %zd (%zd bytes).\n", SanitizerToolName,
  169. class_id, ClassIdToSize(class_id));
  170. Die();
  171. }
  172. for (uptr i = 0; i < n_chunks; i++)
  173. free_array[old_num_chunks + i] = chunks[i];
  174. region->num_freed_chunks = new_num_freed_chunks;
  175. region->stats.n_freed += n_chunks;
  176. MaybeReleaseToOS(memory_mapper, class_id, false /*force*/);
  177. }
  178. NOINLINE bool GetFromAllocator(AllocatorStats *stat, uptr class_id,
  179. CompactPtrT *chunks, uptr n_chunks) {
  180. RegionInfo *region = GetRegionInfo(class_id);
  181. uptr region_beg = GetRegionBeginBySizeClass(class_id);
  182. CompactPtrT *free_array = GetFreeArray(region_beg);
  183. Lock l(&region->mutex);
  184. #if SANITIZER_WINDOWS
  185. /* On Windows unmapping of memory during __sanitizer_purge_allocator is
  186. explicit and immediate, so unmapped regions must be explicitly mapped back
  187. in when they are accessed again. */
  188. if (region->rtoi.last_released_bytes > 0) {
  189. MmapFixedOrDie(region_beg, region->mapped_user,
  190. "SizeClassAllocator: region data");
  191. region->rtoi.n_freed_at_last_release = 0;
  192. region->rtoi.last_released_bytes = 0;
  193. }
  194. #endif
  195. if (UNLIKELY(region->num_freed_chunks < n_chunks)) {
  196. if (UNLIKELY(!PopulateFreeArray(stat, class_id, region,
  197. n_chunks - region->num_freed_chunks)))
  198. return false;
  199. CHECK_GE(region->num_freed_chunks, n_chunks);
  200. }
  201. region->num_freed_chunks -= n_chunks;
  202. uptr base_idx = region->num_freed_chunks;
  203. for (uptr i = 0; i < n_chunks; i++)
  204. chunks[i] = free_array[base_idx + i];
  205. region->stats.n_allocated += n_chunks;
  206. return true;
  207. }
  208. bool PointerIsMine(const void *p) const {
  209. uptr P = reinterpret_cast<uptr>(p);
  210. if (kUsingConstantSpaceBeg && (kSpaceBeg % kSpaceSize) == 0)
  211. return P / kSpaceSize == kSpaceBeg / kSpaceSize;
  212. return P >= SpaceBeg() && P < SpaceEnd();
  213. }
  214. uptr GetRegionBegin(const void *p) {
  215. if (kUsingConstantSpaceBeg)
  216. return reinterpret_cast<uptr>(p) & ~(kRegionSize - 1);
  217. uptr space_beg = SpaceBeg();
  218. return ((reinterpret_cast<uptr>(p) - space_beg) & ~(kRegionSize - 1)) +
  219. space_beg;
  220. }
  221. uptr GetRegionBeginBySizeClass(uptr class_id) const {
  222. return SpaceBeg() + kRegionSize * class_id;
  223. }
  224. uptr GetSizeClass(const void *p) {
  225. if (kUsingConstantSpaceBeg && (kSpaceBeg % kSpaceSize) == 0)
  226. return ((reinterpret_cast<uptr>(p)) / kRegionSize) % kNumClassesRounded;
  227. return ((reinterpret_cast<uptr>(p) - SpaceBeg()) / kRegionSize) %
  228. kNumClassesRounded;
  229. }
  230. void *GetBlockBegin(const void *p) {
  231. uptr class_id = GetSizeClass(p);
  232. if (class_id >= kNumClasses) return nullptr;
  233. uptr size = ClassIdToSize(class_id);
  234. if (!size) return nullptr;
  235. uptr chunk_idx = GetChunkIdx((uptr)p, size);
  236. uptr reg_beg = GetRegionBegin(p);
  237. uptr beg = chunk_idx * size;
  238. uptr next_beg = beg + size;
  239. const RegionInfo *region = AddressSpaceView::Load(GetRegionInfo(class_id));
  240. if (region->mapped_user >= next_beg)
  241. return reinterpret_cast<void*>(reg_beg + beg);
  242. return nullptr;
  243. }
  244. uptr GetActuallyAllocatedSize(void *p) {
  245. CHECK(PointerIsMine(p));
  246. return ClassIdToSize(GetSizeClass(p));
  247. }
  248. static uptr ClassID(uptr size) { return SizeClassMap::ClassID(size); }
  249. void *GetMetaData(const void *p) {
  250. CHECK(kMetadataSize);
  251. uptr class_id = GetSizeClass(p);
  252. uptr size = ClassIdToSize(class_id);
  253. if (!size)
  254. return nullptr;
  255. uptr chunk_idx = GetChunkIdx(reinterpret_cast<uptr>(p), size);
  256. uptr region_beg = GetRegionBeginBySizeClass(class_id);
  257. return reinterpret_cast<void *>(GetMetadataEnd(region_beg) -
  258. (1 + chunk_idx) * kMetadataSize);
  259. }
  260. uptr TotalMemoryUsed() {
  261. uptr res = 0;
  262. for (uptr i = 0; i < kNumClasses; i++)
  263. res += GetRegionInfo(i)->allocated_user;
  264. return res;
  265. }
  266. // Test-only.
  267. void TestOnlyUnmap() {
  268. UnmapWithCallbackOrDie((uptr)address_range.base(), address_range.size());
  269. }
  270. static void FillMemoryProfile(uptr start, uptr rss, bool file, uptr *stats) {
  271. for (uptr class_id = 0; class_id < kNumClasses; class_id++)
  272. if (stats[class_id] == start)
  273. stats[class_id] = rss;
  274. }
  275. void PrintStats(uptr class_id, uptr rss) {
  276. RegionInfo *region = GetRegionInfo(class_id);
  277. if (region->mapped_user == 0) return;
  278. uptr in_use = region->stats.n_allocated - region->stats.n_freed;
  279. uptr avail_chunks = region->allocated_user / ClassIdToSize(class_id);
  280. Printf(
  281. "%s %02zd (%6zd): mapped: %6zdK allocs: %7zd frees: %7zd inuse: %6zd "
  282. "num_freed_chunks %7zd avail: %6zd rss: %6zdK releases: %6zd "
  283. "last released: %6lldK region: 0x%zx\n",
  284. region->exhausted ? "F" : " ", class_id, ClassIdToSize(class_id),
  285. region->mapped_user >> 10, region->stats.n_allocated,
  286. region->stats.n_freed, in_use, region->num_freed_chunks, avail_chunks,
  287. rss >> 10, region->rtoi.num_releases,
  288. region->rtoi.last_released_bytes >> 10,
  289. SpaceBeg() + kRegionSize * class_id);
  290. }
  291. void PrintStats() {
  292. uptr rss_stats[kNumClasses];
  293. for (uptr class_id = 0; class_id < kNumClasses; class_id++)
  294. rss_stats[class_id] = SpaceBeg() + kRegionSize * class_id;
  295. GetMemoryProfile(FillMemoryProfile, rss_stats);
  296. uptr total_mapped = 0;
  297. uptr total_rss = 0;
  298. uptr n_allocated = 0;
  299. uptr n_freed = 0;
  300. for (uptr class_id = 1; class_id < kNumClasses; class_id++) {
  301. RegionInfo *region = GetRegionInfo(class_id);
  302. if (region->mapped_user != 0) {
  303. total_mapped += region->mapped_user;
  304. total_rss += rss_stats[class_id];
  305. }
  306. n_allocated += region->stats.n_allocated;
  307. n_freed += region->stats.n_freed;
  308. }
  309. Printf("Stats: SizeClassAllocator64: %zdM mapped (%zdM rss) in "
  310. "%zd allocations; remains %zd\n", total_mapped >> 20,
  311. total_rss >> 20, n_allocated, n_allocated - n_freed);
  312. for (uptr class_id = 1; class_id < kNumClasses; class_id++)
  313. PrintStats(class_id, rss_stats[class_id]);
  314. }
  315. // ForceLock() and ForceUnlock() are needed to implement Darwin malloc zone
  316. // introspection API.
  317. void ForceLock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
  318. for (uptr i = 0; i < kNumClasses; i++) {
  319. GetRegionInfo(i)->mutex.Lock();
  320. }
  321. }
  322. void ForceUnlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
  323. for (int i = (int)kNumClasses - 1; i >= 0; i--) {
  324. GetRegionInfo(i)->mutex.Unlock();
  325. }
  326. }
  327. // Iterate over all existing chunks.
  328. // The allocator must be locked when calling this function.
  329. void ForEachChunk(ForEachChunkCallback callback, void *arg) {
  330. for (uptr class_id = 1; class_id < kNumClasses; class_id++) {
  331. RegionInfo *region = GetRegionInfo(class_id);
  332. uptr chunk_size = ClassIdToSize(class_id);
  333. uptr region_beg = SpaceBeg() + class_id * kRegionSize;
  334. uptr region_allocated_user_size =
  335. AddressSpaceView::Load(region)->allocated_user;
  336. for (uptr chunk = region_beg;
  337. chunk < region_beg + region_allocated_user_size;
  338. chunk += chunk_size) {
  339. // Too slow: CHECK_EQ((void *)chunk, GetBlockBegin((void *)chunk));
  340. callback(chunk, arg);
  341. }
  342. }
  343. }
  344. static uptr ClassIdToSize(uptr class_id) {
  345. return SizeClassMap::Size(class_id);
  346. }
  347. static uptr AdditionalSize() {
  348. return RoundUpTo(sizeof(RegionInfo) * kNumClassesRounded,
  349. GetPageSizeCached());
  350. }
  351. typedef SizeClassMap SizeClassMapT;
  352. static const uptr kNumClasses = SizeClassMap::kNumClasses;
  353. static const uptr kNumClassesRounded = SizeClassMap::kNumClassesRounded;
  354. // A packed array of counters. Each counter occupies 2^n bits, enough to store
  355. // counter's max_value. Ctor will try to allocate the required buffer via
  356. // mapper->MapPackedCounterArrayBuffer and the caller is expected to check
  357. // whether the initialization was successful by checking IsAllocated() result.
  358. // For the performance sake, none of the accessors check the validity of the
  359. // arguments, it is assumed that index is always in [0, n) range and the value
  360. // is not incremented past max_value.
  361. class PackedCounterArray {
  362. public:
  363. template <typename MemoryMapper>
  364. PackedCounterArray(u64 num_counters, u64 max_value, MemoryMapper *mapper)
  365. : n(num_counters) {
  366. CHECK_GT(num_counters, 0);
  367. CHECK_GT(max_value, 0);
  368. constexpr u64 kMaxCounterBits = sizeof(*buffer) * 8ULL;
  369. // Rounding counter storage size up to the power of two allows for using
  370. // bit shifts calculating particular counter's index and offset.
  371. uptr counter_size_bits =
  372. RoundUpToPowerOfTwo(MostSignificantSetBitIndex(max_value) + 1);
  373. CHECK_LE(counter_size_bits, kMaxCounterBits);
  374. counter_size_bits_log = Log2(counter_size_bits);
  375. counter_mask = ~0ULL >> (kMaxCounterBits - counter_size_bits);
  376. uptr packing_ratio = kMaxCounterBits >> counter_size_bits_log;
  377. CHECK_GT(packing_ratio, 0);
  378. packing_ratio_log = Log2(packing_ratio);
  379. bit_offset_mask = packing_ratio - 1;
  380. buffer = mapper->MapPackedCounterArrayBuffer(
  381. RoundUpTo(n, 1ULL << packing_ratio_log) >> packing_ratio_log);
  382. }
  383. bool IsAllocated() const {
  384. return !!buffer;
  385. }
  386. u64 GetCount() const {
  387. return n;
  388. }
  389. uptr Get(uptr i) const {
  390. DCHECK_LT(i, n);
  391. uptr index = i >> packing_ratio_log;
  392. uptr bit_offset = (i & bit_offset_mask) << counter_size_bits_log;
  393. return (buffer[index] >> bit_offset) & counter_mask;
  394. }
  395. void Inc(uptr i) const {
  396. DCHECK_LT(Get(i), counter_mask);
  397. uptr index = i >> packing_ratio_log;
  398. uptr bit_offset = (i & bit_offset_mask) << counter_size_bits_log;
  399. buffer[index] += 1ULL << bit_offset;
  400. }
  401. void IncRange(uptr from, uptr to) const {
  402. DCHECK_LE(from, to);
  403. for (uptr i = from; i <= to; i++)
  404. Inc(i);
  405. }
  406. private:
  407. const u64 n;
  408. u64 counter_size_bits_log;
  409. u64 counter_mask;
  410. u64 packing_ratio_log;
  411. u64 bit_offset_mask;
  412. u64* buffer;
  413. };
  414. template <class MemoryMapperT>
  415. class FreePagesRangeTracker {
  416. public:
  417. FreePagesRangeTracker(MemoryMapperT *mapper, uptr class_id)
  418. : memory_mapper(mapper),
  419. class_id(class_id),
  420. page_size_scaled_log(Log2(GetPageSizeCached() >> kCompactPtrScale)) {}
  421. void NextPage(bool freed) {
  422. if (freed) {
  423. if (!in_the_range) {
  424. current_range_start_page = current_page;
  425. in_the_range = true;
  426. }
  427. } else {
  428. CloseOpenedRange();
  429. }
  430. current_page++;
  431. }
  432. void Done() {
  433. CloseOpenedRange();
  434. }
  435. private:
  436. void CloseOpenedRange() {
  437. if (in_the_range) {
  438. memory_mapper->ReleasePageRangeToOS(
  439. class_id, current_range_start_page << page_size_scaled_log,
  440. current_page << page_size_scaled_log);
  441. in_the_range = false;
  442. }
  443. }
  444. MemoryMapperT *const memory_mapper = nullptr;
  445. const uptr class_id = 0;
  446. const uptr page_size_scaled_log = 0;
  447. bool in_the_range = false;
  448. uptr current_page = 0;
  449. uptr current_range_start_page = 0;
  450. };
  451. // Iterates over the free_array to identify memory pages containing freed
  452. // chunks only and returns these pages back to OS.
  453. // allocated_pages_count is the total number of pages allocated for the
  454. // current bucket.
  455. template <typename MemoryMapper>
  456. static void ReleaseFreeMemoryToOS(CompactPtrT *free_array,
  457. uptr free_array_count, uptr chunk_size,
  458. uptr allocated_pages_count,
  459. MemoryMapper *memory_mapper,
  460. uptr class_id) {
  461. const uptr page_size = GetPageSizeCached();
  462. // Figure out the number of chunks per page and whether we can take a fast
  463. // path (the number of chunks per page is the same for all pages).
  464. uptr full_pages_chunk_count_max;
  465. bool same_chunk_count_per_page;
  466. if (chunk_size <= page_size && page_size % chunk_size == 0) {
  467. // Same number of chunks per page, no cross overs.
  468. full_pages_chunk_count_max = page_size / chunk_size;
  469. same_chunk_count_per_page = true;
  470. } else if (chunk_size <= page_size && page_size % chunk_size != 0 &&
  471. chunk_size % (page_size % chunk_size) == 0) {
  472. // Some chunks are crossing page boundaries, which means that the page
  473. // contains one or two partial chunks, but all pages contain the same
  474. // number of chunks.
  475. full_pages_chunk_count_max = page_size / chunk_size + 1;
  476. same_chunk_count_per_page = true;
  477. } else if (chunk_size <= page_size) {
  478. // Some chunks are crossing page boundaries, which means that the page
  479. // contains one or two partial chunks.
  480. full_pages_chunk_count_max = page_size / chunk_size + 2;
  481. same_chunk_count_per_page = false;
  482. } else if (chunk_size > page_size && chunk_size % page_size == 0) {
  483. // One chunk covers multiple pages, no cross overs.
  484. full_pages_chunk_count_max = 1;
  485. same_chunk_count_per_page = true;
  486. } else if (chunk_size > page_size) {
  487. // One chunk covers multiple pages, Some chunks are crossing page
  488. // boundaries. Some pages contain one chunk, some contain two.
  489. full_pages_chunk_count_max = 2;
  490. same_chunk_count_per_page = false;
  491. } else {
  492. UNREACHABLE("All chunk_size/page_size ratios must be handled.");
  493. }
  494. PackedCounterArray counters(allocated_pages_count,
  495. full_pages_chunk_count_max, memory_mapper);
  496. if (!counters.IsAllocated())
  497. return;
  498. const uptr chunk_size_scaled = chunk_size >> kCompactPtrScale;
  499. const uptr page_size_scaled = page_size >> kCompactPtrScale;
  500. const uptr page_size_scaled_log = Log2(page_size_scaled);
  501. // Iterate over free chunks and count how many free chunks affect each
  502. // allocated page.
  503. if (chunk_size <= page_size && page_size % chunk_size == 0) {
  504. // Each chunk affects one page only.
  505. for (uptr i = 0; i < free_array_count; i++)
  506. counters.Inc(free_array[i] >> page_size_scaled_log);
  507. } else {
  508. // In all other cases chunks might affect more than one page.
  509. for (uptr i = 0; i < free_array_count; i++) {
  510. counters.IncRange(
  511. free_array[i] >> page_size_scaled_log,
  512. (free_array[i] + chunk_size_scaled - 1) >> page_size_scaled_log);
  513. }
  514. }
  515. // Iterate over pages detecting ranges of pages with chunk counters equal
  516. // to the expected number of chunks for the particular page.
  517. FreePagesRangeTracker<MemoryMapper> range_tracker(memory_mapper, class_id);
  518. if (same_chunk_count_per_page) {
  519. // Fast path, every page has the same number of chunks affecting it.
  520. for (uptr i = 0; i < counters.GetCount(); i++)
  521. range_tracker.NextPage(counters.Get(i) == full_pages_chunk_count_max);
  522. } else {
  523. // Show path, go through the pages keeping count how many chunks affect
  524. // each page.
  525. const uptr pn =
  526. chunk_size < page_size ? page_size_scaled / chunk_size_scaled : 1;
  527. const uptr pnc = pn * chunk_size_scaled;
  528. // The idea is to increment the current page pointer by the first chunk
  529. // size, middle portion size (the portion of the page covered by chunks
  530. // except the first and the last one) and then the last chunk size, adding
  531. // up the number of chunks on the current page and checking on every step
  532. // whether the page boundary was crossed.
  533. uptr prev_page_boundary = 0;
  534. uptr current_boundary = 0;
  535. for (uptr i = 0; i < counters.GetCount(); i++) {
  536. uptr page_boundary = prev_page_boundary + page_size_scaled;
  537. uptr chunks_per_page = pn;
  538. if (current_boundary < page_boundary) {
  539. if (current_boundary > prev_page_boundary)
  540. chunks_per_page++;
  541. current_boundary += pnc;
  542. if (current_boundary < page_boundary) {
  543. chunks_per_page++;
  544. current_boundary += chunk_size_scaled;
  545. }
  546. }
  547. prev_page_boundary = page_boundary;
  548. range_tracker.NextPage(counters.Get(i) == chunks_per_page);
  549. }
  550. }
  551. range_tracker.Done();
  552. }
  553. private:
  554. friend class MemoryMapper<ThisT>;
  555. ReservedAddressRange address_range;
  556. static const uptr kRegionSize = kSpaceSize / kNumClassesRounded;
  557. // FreeArray is the array of free-d chunks (stored as 4-byte offsets).
  558. // In the worst case it may require kRegionSize/SizeClassMap::kMinSize
  559. // elements, but in reality this will not happen. For simplicity we
  560. // dedicate 1/8 of the region's virtual space to FreeArray.
  561. static const uptr kFreeArraySize = kRegionSize / 8;
  562. static const bool kUsingConstantSpaceBeg = kSpaceBeg != ~(uptr)0;
  563. uptr NonConstSpaceBeg;
  564. uptr SpaceBeg() const {
  565. return kUsingConstantSpaceBeg ? kSpaceBeg : NonConstSpaceBeg;
  566. }
  567. uptr SpaceEnd() const { return SpaceBeg() + kSpaceSize; }
  568. // kRegionSize must be >= 2^32.
  569. COMPILER_CHECK((kRegionSize) >= (1ULL << (SANITIZER_WORDSIZE / 2)));
  570. // kRegionSize must be <= 2^36, see CompactPtrT.
  571. COMPILER_CHECK((kRegionSize) <= (1ULL << (SANITIZER_WORDSIZE / 2 + 4)));
  572. // Call mmap for user memory with at least this size.
  573. static const uptr kUserMapSize = 1 << 16;
  574. // Call mmap for metadata memory with at least this size.
  575. static const uptr kMetaMapSize = 1 << 16;
  576. // Call mmap for free array memory with at least this size.
  577. static const uptr kFreeArrayMapSize = 1 << 16;
  578. atomic_sint32_t release_to_os_interval_ms_;
  579. uptr RegionInfoSpace;
  580. // True if the user has already mapped the entire heap R/W.
  581. bool PremappedHeap;
  582. struct Stats {
  583. uptr n_allocated;
  584. uptr n_freed;
  585. };
  586. struct ReleaseToOsInfo {
  587. uptr n_freed_at_last_release;
  588. uptr num_releases;
  589. u64 last_release_at_ns;
  590. u64 last_released_bytes;
  591. };
  592. struct ALIGNED(SANITIZER_CACHE_LINE_SIZE) RegionInfo {
  593. Mutex mutex;
  594. uptr num_freed_chunks; // Number of elements in the freearray.
  595. uptr mapped_free_array; // Bytes mapped for freearray.
  596. uptr allocated_user; // Bytes allocated for user memory.
  597. uptr allocated_meta; // Bytes allocated for metadata.
  598. uptr mapped_user; // Bytes mapped for user memory.
  599. uptr mapped_meta; // Bytes mapped for metadata.
  600. u32 rand_state; // Seed for random shuffle, used if kRandomShuffleChunks.
  601. bool exhausted; // Whether region is out of space for new chunks.
  602. Stats stats;
  603. ReleaseToOsInfo rtoi;
  604. };
  605. COMPILER_CHECK(sizeof(RegionInfo) % kCacheLineSize == 0);
  606. RegionInfo *GetRegionInfo(uptr class_id) const {
  607. DCHECK_LT(class_id, kNumClasses);
  608. RegionInfo *regions = reinterpret_cast<RegionInfo *>(RegionInfoSpace);
  609. return &regions[class_id];
  610. }
  611. uptr GetMetadataEnd(uptr region_beg) const {
  612. return region_beg + kRegionSize - kFreeArraySize;
  613. }
  614. uptr GetChunkIdx(uptr chunk, uptr size) const {
  615. if (!kUsingConstantSpaceBeg)
  616. chunk -= SpaceBeg();
  617. uptr offset = chunk % kRegionSize;
  618. // Here we divide by a non-constant. This is costly.
  619. // size always fits into 32-bits. If the offset fits too, use 32-bit div.
  620. if (offset >> (SANITIZER_WORDSIZE / 2))
  621. return offset / size;
  622. return (u32)offset / (u32)size;
  623. }
  624. CompactPtrT *GetFreeArray(uptr region_beg) const {
  625. return reinterpret_cast<CompactPtrT *>(GetMetadataEnd(region_beg));
  626. }
  627. bool MapWithCallback(uptr beg, uptr size, const char *name) {
  628. if (PremappedHeap)
  629. return beg >= NonConstSpaceBeg &&
  630. beg + size <= NonConstSpaceBeg + kSpaceSize;
  631. uptr mapped = address_range.Map(beg, size, name);
  632. if (UNLIKELY(!mapped))
  633. return false;
  634. CHECK_EQ(beg, mapped);
  635. MapUnmapCallback().OnMap(beg, size);
  636. return true;
  637. }
  638. void MapWithCallbackOrDie(uptr beg, uptr size, const char *name) {
  639. if (PremappedHeap) {
  640. CHECK_GE(beg, NonConstSpaceBeg);
  641. CHECK_LE(beg + size, NonConstSpaceBeg + kSpaceSize);
  642. return;
  643. }
  644. CHECK_EQ(beg, address_range.MapOrDie(beg, size, name));
  645. MapUnmapCallback().OnMap(beg, size);
  646. }
  647. void UnmapWithCallbackOrDie(uptr beg, uptr size) {
  648. if (PremappedHeap)
  649. return;
  650. MapUnmapCallback().OnUnmap(beg, size);
  651. address_range.Unmap(beg, size);
  652. }
  653. bool EnsureFreeArraySpace(RegionInfo *region, uptr region_beg,
  654. uptr num_freed_chunks) {
  655. uptr needed_space = num_freed_chunks * sizeof(CompactPtrT);
  656. if (region->mapped_free_array < needed_space) {
  657. uptr new_mapped_free_array = RoundUpTo(needed_space, kFreeArrayMapSize);
  658. CHECK_LE(new_mapped_free_array, kFreeArraySize);
  659. uptr current_map_end = reinterpret_cast<uptr>(GetFreeArray(region_beg)) +
  660. region->mapped_free_array;
  661. uptr new_map_size = new_mapped_free_array - region->mapped_free_array;
  662. if (UNLIKELY(!MapWithCallback(current_map_end, new_map_size,
  663. "SizeClassAllocator: freearray")))
  664. return false;
  665. region->mapped_free_array = new_mapped_free_array;
  666. }
  667. return true;
  668. }
  669. // Check whether this size class is exhausted.
  670. bool IsRegionExhausted(RegionInfo *region, uptr class_id,
  671. uptr additional_map_size) {
  672. if (LIKELY(region->mapped_user + region->mapped_meta +
  673. additional_map_size <= kRegionSize - kFreeArraySize))
  674. return false;
  675. if (!region->exhausted) {
  676. region->exhausted = true;
  677. Printf("%s: Out of memory. ", SanitizerToolName);
  678. Printf("The process has exhausted %zuMB for size class %zu.\n",
  679. kRegionSize >> 20, ClassIdToSize(class_id));
  680. }
  681. return true;
  682. }
  683. NOINLINE bool PopulateFreeArray(AllocatorStats *stat, uptr class_id,
  684. RegionInfo *region, uptr requested_count) {
  685. // region->mutex is held.
  686. const uptr region_beg = GetRegionBeginBySizeClass(class_id);
  687. const uptr size = ClassIdToSize(class_id);
  688. const uptr total_user_bytes =
  689. region->allocated_user + requested_count * size;
  690. // Map more space for chunks, if necessary.
  691. if (LIKELY(total_user_bytes > region->mapped_user)) {
  692. if (UNLIKELY(region->mapped_user == 0)) {
  693. if (!kUsingConstantSpaceBeg && kRandomShuffleChunks)
  694. // The random state is initialized from ASLR.
  695. region->rand_state = static_cast<u32>(region_beg >> 12);
  696. // Postpone the first release to OS attempt for ReleaseToOSIntervalMs,
  697. // preventing just allocated memory from being released sooner than
  698. // necessary and also preventing extraneous ReleaseMemoryPagesToOS calls
  699. // for short lived processes.
  700. // Do it only when the feature is turned on, to avoid a potentially
  701. // extraneous syscall.
  702. if (ReleaseToOSIntervalMs() >= 0)
  703. region->rtoi.last_release_at_ns = MonotonicNanoTime();
  704. }
  705. // Do the mmap for the user memory.
  706. const uptr user_map_size =
  707. RoundUpTo(total_user_bytes - region->mapped_user, kUserMapSize);
  708. if (UNLIKELY(IsRegionExhausted(region, class_id, user_map_size)))
  709. return false;
  710. if (UNLIKELY(!MapWithCallback(region_beg + region->mapped_user,
  711. user_map_size,
  712. "SizeClassAllocator: region data")))
  713. return false;
  714. stat->Add(AllocatorStatMapped, user_map_size);
  715. region->mapped_user += user_map_size;
  716. }
  717. const uptr new_chunks_count =
  718. (region->mapped_user - region->allocated_user) / size;
  719. if (kMetadataSize) {
  720. // Calculate the required space for metadata.
  721. const uptr total_meta_bytes =
  722. region->allocated_meta + new_chunks_count * kMetadataSize;
  723. const uptr meta_map_size = (total_meta_bytes > region->mapped_meta) ?
  724. RoundUpTo(total_meta_bytes - region->mapped_meta, kMetaMapSize) : 0;
  725. // Map more space for metadata, if necessary.
  726. if (meta_map_size) {
  727. if (UNLIKELY(IsRegionExhausted(region, class_id, meta_map_size)))
  728. return false;
  729. if (UNLIKELY(!MapWithCallback(
  730. GetMetadataEnd(region_beg) - region->mapped_meta - meta_map_size,
  731. meta_map_size, "SizeClassAllocator: region metadata")))
  732. return false;
  733. region->mapped_meta += meta_map_size;
  734. }
  735. }
  736. // If necessary, allocate more space for the free array and populate it with
  737. // newly allocated chunks.
  738. const uptr total_freed_chunks = region->num_freed_chunks + new_chunks_count;
  739. if (UNLIKELY(!EnsureFreeArraySpace(region, region_beg, total_freed_chunks)))
  740. return false;
  741. CompactPtrT *free_array = GetFreeArray(region_beg);
  742. for (uptr i = 0, chunk = region->allocated_user; i < new_chunks_count;
  743. i++, chunk += size)
  744. free_array[total_freed_chunks - 1 - i] = PointerToCompactPtr(0, chunk);
  745. if (kRandomShuffleChunks)
  746. RandomShuffle(&free_array[region->num_freed_chunks], new_chunks_count,
  747. &region->rand_state);
  748. // All necessary memory is mapped and now it is safe to advance all
  749. // 'allocated_*' counters.
  750. region->num_freed_chunks += new_chunks_count;
  751. region->allocated_user += new_chunks_count * size;
  752. CHECK_LE(region->allocated_user, region->mapped_user);
  753. region->allocated_meta += new_chunks_count * kMetadataSize;
  754. CHECK_LE(region->allocated_meta, region->mapped_meta);
  755. region->exhausted = false;
  756. // TODO(alekseyshl): Consider bumping last_release_at_ns here to prevent
  757. // MaybeReleaseToOS from releasing just allocated pages or protect these
  758. // not yet used chunks some other way.
  759. return true;
  760. }
  761. // Attempts to release RAM occupied by freed chunks back to OS. The region is
  762. // expected to be locked.
  763. //
  764. // TODO(morehouse): Support a callback on memory release so HWASan can release
  765. // aliases as well.
  766. void MaybeReleaseToOS(MemoryMapperT *memory_mapper, uptr class_id,
  767. bool force) {
  768. RegionInfo *region = GetRegionInfo(class_id);
  769. const uptr chunk_size = ClassIdToSize(class_id);
  770. const uptr page_size = GetPageSizeCached();
  771. uptr n = region->num_freed_chunks;
  772. if (n * chunk_size < page_size)
  773. return; // No chance to release anything.
  774. if ((region->stats.n_freed -
  775. region->rtoi.n_freed_at_last_release) * chunk_size < page_size) {
  776. return; // Nothing new to release.
  777. }
  778. if (!force) {
  779. s32 interval_ms = ReleaseToOSIntervalMs();
  780. if (interval_ms < 0)
  781. return;
  782. if (region->rtoi.last_release_at_ns + interval_ms * 1000000ULL >
  783. MonotonicNanoTime()) {
  784. return; // Memory was returned recently.
  785. }
  786. }
  787. ReleaseFreeMemoryToOS(
  788. GetFreeArray(GetRegionBeginBySizeClass(class_id)), n, chunk_size,
  789. RoundUpTo(region->allocated_user, page_size) / page_size, memory_mapper,
  790. class_id);
  791. uptr ranges, bytes;
  792. if (memory_mapper->GetAndResetStats(ranges, bytes)) {
  793. region->rtoi.n_freed_at_last_release = region->stats.n_freed;
  794. region->rtoi.num_releases += ranges;
  795. region->rtoi.last_released_bytes = bytes;
  796. }
  797. region->rtoi.last_release_at_ns = MonotonicNanoTime();
  798. }
  799. };