allocator_config.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //===-- allocator_config.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. #ifndef SCUDO_ALLOCATOR_CONFIG_H_
  9. #define SCUDO_ALLOCATOR_CONFIG_H_
  10. #include "combined.h"
  11. #include "common.h"
  12. #include "flags.h"
  13. #include "primary32.h"
  14. #include "primary64.h"
  15. #include "secondary.h"
  16. #include "size_class_map.h"
  17. #include "tsd_exclusive.h"
  18. #include "tsd_shared.h"
  19. namespace scudo {
  20. // The combined allocator uses a structure as a template argument that
  21. // specifies the configuration options for the various subcomponents of the
  22. // allocator.
  23. //
  24. // struct ExampleConfig {
  25. // // SizeClasMmap to use with the Primary.
  26. // using SizeClassMap = DefaultSizeClassMap;
  27. // // Indicates possible support for Memory Tagging.
  28. // static const bool MaySupportMemoryTagging = false;
  29. // // Defines the Primary allocator to use.
  30. // typedef SizeClassAllocator64<ExampleConfig> Primary;
  31. // // Log2 of the size of a size class region, as used by the Primary.
  32. // static const uptr PrimaryRegionSizeLog = 30U;
  33. // // Log2 of the size of block group, as used by the Primary. Each group
  34. // // contains a range of memory addresses, blocks in the range will belong to
  35. // // the same group. In general, single region may have 1 or 2MB group size.
  36. // // Multiple regions will have the group size equal to the region size
  37. // // because the region size is usually smaller than 1 MB.
  38. // // Smaller value gives fine-grained control of memory usage but the trade
  39. // // off is that it may take longer time of deallocation.
  40. // static const uptr PrimaryGroupSizeLog = 20U;
  41. // // Defines the type and scale of a compact pointer. A compact pointer can
  42. // // be understood as the offset of a pointer within the region it belongs
  43. // // to, in increments of a power-of-2 scale.
  44. // // eg: Ptr = Base + (CompactPtr << Scale).
  45. // typedef u32 PrimaryCompactPtrT;
  46. // static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  47. // // Indicates support for offsetting the start of a region by
  48. // // a random number of pages. Only used with primary64.
  49. // static const bool PrimaryEnableRandomOffset = true;
  50. // // Call map for user memory with at least this size. Only used with
  51. // // primary64.
  52. // static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  53. // // Defines the minimal & maximal release interval that can be set.
  54. // static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  55. // static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
  56. // // Defines the type of cache used by the Secondary. Some additional
  57. // // configuration entries can be necessary depending on the Cache.
  58. // typedef MapAllocatorNoCache SecondaryCache;
  59. // // Thread-Specific Data Registry used, shared or exclusive.
  60. // template <class A> using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>;
  61. // };
  62. // Default configurations for various platforms.
  63. struct DefaultConfig {
  64. using SizeClassMap = DefaultSizeClassMap;
  65. static const bool MaySupportMemoryTagging = true;
  66. #if SCUDO_CAN_USE_PRIMARY64
  67. typedef SizeClassAllocator64<DefaultConfig> Primary;
  68. static const uptr PrimaryRegionSizeLog = 32U;
  69. static const uptr PrimaryGroupSizeLog = 21U;
  70. typedef uptr PrimaryCompactPtrT;
  71. static const uptr PrimaryCompactPtrScale = 0;
  72. static const bool PrimaryEnableRandomOffset = true;
  73. static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  74. #else
  75. typedef SizeClassAllocator32<DefaultConfig> Primary;
  76. static const uptr PrimaryRegionSizeLog = 19U;
  77. static const uptr PrimaryGroupSizeLog = 19U;
  78. typedef uptr PrimaryCompactPtrT;
  79. #endif
  80. static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  81. static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
  82. typedef MapAllocatorCache<DefaultConfig> SecondaryCache;
  83. static const u32 SecondaryCacheEntriesArraySize = 32U;
  84. static const u32 SecondaryCacheQuarantineSize = 0U;
  85. static const u32 SecondaryCacheDefaultMaxEntriesCount = 32U;
  86. static const uptr SecondaryCacheDefaultMaxEntrySize = 1UL << 19;
  87. static const s32 SecondaryCacheMinReleaseToOsIntervalMs = INT32_MIN;
  88. static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = INT32_MAX;
  89. template <class A> using TSDRegistryT = TSDRegistryExT<A>; // Exclusive
  90. };
  91. struct AndroidConfig {
  92. using SizeClassMap = AndroidSizeClassMap;
  93. static const bool MaySupportMemoryTagging = true;
  94. #if SCUDO_CAN_USE_PRIMARY64
  95. typedef SizeClassAllocator64<AndroidConfig> Primary;
  96. static const uptr PrimaryRegionSizeLog = 28U;
  97. typedef u32 PrimaryCompactPtrT;
  98. static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  99. static const uptr PrimaryGroupSizeLog = 20U;
  100. static const bool PrimaryEnableRandomOffset = true;
  101. static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  102. #else
  103. typedef SizeClassAllocator32<AndroidConfig> Primary;
  104. static const uptr PrimaryRegionSizeLog = 18U;
  105. static const uptr PrimaryGroupSizeLog = 18U;
  106. typedef uptr PrimaryCompactPtrT;
  107. #endif
  108. static const s32 PrimaryMinReleaseToOsIntervalMs = 1000;
  109. static const s32 PrimaryMaxReleaseToOsIntervalMs = 1000;
  110. typedef MapAllocatorCache<AndroidConfig> SecondaryCache;
  111. static const u32 SecondaryCacheEntriesArraySize = 256U;
  112. static const u32 SecondaryCacheQuarantineSize = 32U;
  113. static const u32 SecondaryCacheDefaultMaxEntriesCount = 32U;
  114. static const uptr SecondaryCacheDefaultMaxEntrySize = 2UL << 20;
  115. static const s32 SecondaryCacheMinReleaseToOsIntervalMs = 0;
  116. static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = 1000;
  117. template <class A>
  118. using TSDRegistryT = TSDRegistrySharedT<A, 8U, 2U>; // Shared, max 8 TSDs.
  119. };
  120. struct AndroidSvelteConfig {
  121. using SizeClassMap = SvelteSizeClassMap;
  122. static const bool MaySupportMemoryTagging = false;
  123. #if SCUDO_CAN_USE_PRIMARY64
  124. typedef SizeClassAllocator64<AndroidSvelteConfig> Primary;
  125. static const uptr PrimaryRegionSizeLog = 27U;
  126. typedef u32 PrimaryCompactPtrT;
  127. static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  128. static const uptr PrimaryGroupSizeLog = 18U;
  129. static const bool PrimaryEnableRandomOffset = true;
  130. static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  131. #else
  132. typedef SizeClassAllocator32<AndroidSvelteConfig> Primary;
  133. static const uptr PrimaryRegionSizeLog = 16U;
  134. static const uptr PrimaryGroupSizeLog = 16U;
  135. typedef uptr PrimaryCompactPtrT;
  136. #endif
  137. static const s32 PrimaryMinReleaseToOsIntervalMs = 1000;
  138. static const s32 PrimaryMaxReleaseToOsIntervalMs = 1000;
  139. typedef MapAllocatorCache<AndroidSvelteConfig> SecondaryCache;
  140. static const u32 SecondaryCacheEntriesArraySize = 16U;
  141. static const u32 SecondaryCacheQuarantineSize = 32U;
  142. static const u32 SecondaryCacheDefaultMaxEntriesCount = 4U;
  143. static const uptr SecondaryCacheDefaultMaxEntrySize = 1UL << 18;
  144. static const s32 SecondaryCacheMinReleaseToOsIntervalMs = 0;
  145. static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = 0;
  146. template <class A>
  147. using TSDRegistryT = TSDRegistrySharedT<A, 2U, 1U>; // Shared, max 2 TSDs.
  148. };
  149. #if SCUDO_CAN_USE_PRIMARY64
  150. struct FuchsiaConfig {
  151. using SizeClassMap = FuchsiaSizeClassMap;
  152. static const bool MaySupportMemoryTagging = false;
  153. typedef SizeClassAllocator64<FuchsiaConfig> Primary;
  154. static const uptr PrimaryRegionSizeLog = 30U;
  155. static const uptr PrimaryGroupSizeLog = 21U;
  156. typedef u32 PrimaryCompactPtrT;
  157. static const bool PrimaryEnableRandomOffset = true;
  158. static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  159. static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  160. static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  161. static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
  162. typedef MapAllocatorNoCache SecondaryCache;
  163. template <class A>
  164. using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>; // Shared, max 8 TSDs.
  165. };
  166. struct TrustyConfig {
  167. using SizeClassMap = TrustySizeClassMap;
  168. static const bool MaySupportMemoryTagging = false;
  169. typedef SizeClassAllocator64<TrustyConfig> Primary;
  170. // Some apps have 1 page of heap total so small regions are necessary.
  171. static const uptr PrimaryRegionSizeLog = 10U;
  172. static const uptr PrimaryGroupSizeLog = 10U;
  173. typedef u32 PrimaryCompactPtrT;
  174. static const bool PrimaryEnableRandomOffset = false;
  175. // Trusty is extremely memory-constrained so minimally round up map calls.
  176. static const uptr PrimaryMapSizeIncrement = 1UL << 4;
  177. static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  178. static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  179. static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
  180. typedef MapAllocatorNoCache SecondaryCache;
  181. template <class A>
  182. using TSDRegistryT = TSDRegistrySharedT<A, 1U, 1U>; // Shared, max 1 TSD.
  183. };
  184. #endif
  185. #if SCUDO_ANDROID
  186. typedef AndroidConfig Config;
  187. #elif SCUDO_FUCHSIA
  188. typedef FuchsiaConfig Config;
  189. #elif SCUDO_TRUSTY
  190. typedef TrustyConfig Config;
  191. #else
  192. typedef DefaultConfig Config;
  193. #endif
  194. } // namespace scudo
  195. #endif // SCUDO_ALLOCATOR_CONFIG_H_