allocator_config.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "condition_variable.h"
  13. #include "flags.h"
  14. #include "primary32.h"
  15. #include "primary64.h"
  16. #include "secondary.h"
  17. #include "size_class_map.h"
  18. #include "tsd_exclusive.h"
  19. #include "tsd_shared.h"
  20. // To import a custom configuration, define `SCUDO_USE_CUSTOM_CONFIG` and
  21. // aliasing the `Config` like:
  22. //
  23. // namespace scudo {
  24. // // The instance of Scudo will be initiated with `Config`.
  25. // typedef CustomConfig Config;
  26. // // Aliasing as default configuration to run the tests with this config.
  27. // typedef CustomConfig DefaultConfig;
  28. // } // namespace scudo
  29. //
  30. // Put them in the header `custom_scudo_config.h` then you will be using the
  31. // custom configuration and able to run all the tests as well.
  32. #ifdef SCUDO_USE_CUSTOM_CONFIG
  33. #error #include "custom_scudo_config.h"
  34. #endif
  35. namespace scudo {
  36. // The combined allocator uses a structure as a template argument that
  37. // specifies the configuration options for the various subcomponents of the
  38. // allocator.
  39. //
  40. // struct ExampleConfig {
  41. // // Indicates possible support for Memory Tagging.
  42. // static const bool MaySupportMemoryTagging = false;
  43. //
  44. // // Thread-Specific Data Registry used, shared or exclusive.
  45. // template <class A> using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>;
  46. //
  47. // struct Primary {
  48. // // SizeClassMap to use with the Primary.
  49. // using SizeClassMap = DefaultSizeClassMap;
  50. //
  51. // // Log2 of the size of a size class region, as used by the Primary.
  52. // static const uptr RegionSizeLog = 30U;
  53. //
  54. // // Log2 of the size of block group, as used by the Primary. Each group
  55. // // contains a range of memory addresses, blocks in the range will belong
  56. // // to the same group. In general, single region may have 1 or 2MB group
  57. // // size. Multiple regions will have the group size equal to the region
  58. // // size because the region size is usually smaller than 1 MB.
  59. // // Smaller value gives fine-grained control of memory usage but the
  60. // // trade-off is that it may take longer time of deallocation.
  61. // static const uptr GroupSizeLog = 20U;
  62. //
  63. // // Defines the type and scale of a compact pointer. A compact pointer can
  64. // // be understood as the offset of a pointer within the region it belongs
  65. // // to, in increments of a power-of-2 scale.
  66. // // eg: Ptr = Base + (CompactPtr << Scale).
  67. // typedef u32 CompactPtrT;
  68. // static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  69. //
  70. // // Indicates support for offsetting the start of a region by
  71. // // a random number of pages. Only used with primary64.
  72. // static const bool EnableRandomOffset = true;
  73. //
  74. // // Call map for user memory with at least this size. Only used with
  75. // // primary64.
  76. // static const uptr MapSizeIncrement = 1UL << 18;
  77. //
  78. // // Defines the minimal & maximal release interval that can be set.
  79. // static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
  80. // static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
  81. //
  82. // // Use condition variable to shorten the waiting time of refillment of
  83. // // freelist. Note that this depends on the implementation of condition
  84. // // variable on each platform and the performance may vary so that it
  85. // // doesn't guarantee a performance benefit.
  86. // // Note that both variables have to be defined to enable it.
  87. // static const bool UseConditionVariable = true;
  88. // using ConditionVariableT = ConditionVariableLinux;
  89. // };
  90. // // Defines the type of Primary allocator to use.
  91. // template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
  92. //
  93. // // Defines the type of cache used by the Secondary. Some additional
  94. // // configuration entries can be necessary depending on the Cache.
  95. // struct Secondary {
  96. // struct Cache {
  97. // static const u32 EntriesArraySize = 32U;
  98. // static const u32 QuarantineSize = 0U;
  99. // static const u32 DefaultMaxEntriesCount = 32U;
  100. // static const uptr DefaultMaxEntrySize = 1UL << 19;
  101. // static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
  102. // static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
  103. // };
  104. // // Defines the type of Secondary Cache to use.
  105. // template <typename Config> using CacheT = MapAllocatorCache<Config>;
  106. // };
  107. // // Defines the type of Secondary allocator to use.
  108. // template <typename Config> using SecondaryT = MapAllocator<Config>;
  109. // };
  110. #ifndef SCUDO_USE_CUSTOM_CONFIG
  111. // Default configurations for various platforms. Note this is only enabled when
  112. // there's no custom configuration in the build system.
  113. struct DefaultConfig {
  114. static const bool MaySupportMemoryTagging = true;
  115. template <class A> using TSDRegistryT = TSDRegistryExT<A>; // Exclusive
  116. struct Primary {
  117. using SizeClassMap = DefaultSizeClassMap;
  118. #if SCUDO_CAN_USE_PRIMARY64
  119. static const uptr RegionSizeLog = 32U;
  120. static const uptr GroupSizeLog = 21U;
  121. typedef uptr CompactPtrT;
  122. static const uptr CompactPtrScale = 0;
  123. static const bool EnableRandomOffset = true;
  124. static const uptr MapSizeIncrement = 1UL << 18;
  125. #else
  126. static const uptr RegionSizeLog = 19U;
  127. static const uptr GroupSizeLog = 19U;
  128. typedef uptr CompactPtrT;
  129. #endif
  130. static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
  131. static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
  132. };
  133. #if SCUDO_CAN_USE_PRIMARY64
  134. template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
  135. #else
  136. template <typename Config> using PrimaryT = SizeClassAllocator32<Config>;
  137. #endif
  138. struct Secondary {
  139. struct Cache {
  140. static const u32 EntriesArraySize = 32U;
  141. static const u32 QuarantineSize = 0U;
  142. static const u32 DefaultMaxEntriesCount = 32U;
  143. static const uptr DefaultMaxEntrySize = 1UL << 19;
  144. static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
  145. static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
  146. };
  147. template <typename Config> using CacheT = MapAllocatorCache<Config>;
  148. };
  149. template <typename Config> using SecondaryT = MapAllocator<Config>;
  150. };
  151. #endif // SCUDO_USE_CUSTOM_CONFIG
  152. struct AndroidConfig {
  153. static const bool MaySupportMemoryTagging = true;
  154. template <class A>
  155. using TSDRegistryT = TSDRegistrySharedT<A, 8U, 2U>; // Shared, max 8 TSDs.
  156. struct Primary {
  157. using SizeClassMap = AndroidSizeClassMap;
  158. #if SCUDO_CAN_USE_PRIMARY64
  159. static const uptr RegionSizeLog = 28U;
  160. typedef u32 CompactPtrT;
  161. static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  162. static const uptr GroupSizeLog = 20U;
  163. static const bool EnableRandomOffset = true;
  164. static const uptr MapSizeIncrement = 1UL << 18;
  165. #else
  166. static const uptr RegionSizeLog = 18U;
  167. static const uptr GroupSizeLog = 18U;
  168. typedef uptr CompactPtrT;
  169. #endif
  170. static const s32 MinReleaseToOsIntervalMs = 1000;
  171. static const s32 MaxReleaseToOsIntervalMs = 1000;
  172. };
  173. #if SCUDO_CAN_USE_PRIMARY64
  174. template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
  175. #else
  176. template <typename Config> using PrimaryT = SizeClassAllocator32<Config>;
  177. #endif
  178. struct Secondary {
  179. struct Cache {
  180. static const u32 EntriesArraySize = 256U;
  181. static const u32 QuarantineSize = 32U;
  182. static const u32 DefaultMaxEntriesCount = 32U;
  183. static const uptr DefaultMaxEntrySize = 2UL << 20;
  184. static const s32 MinReleaseToOsIntervalMs = 0;
  185. static const s32 MaxReleaseToOsIntervalMs = 1000;
  186. };
  187. template <typename Config> using CacheT = MapAllocatorCache<Config>;
  188. };
  189. template <typename Config> using SecondaryT = MapAllocator<Config>;
  190. };
  191. #if SCUDO_CAN_USE_PRIMARY64
  192. struct FuchsiaConfig {
  193. static const bool MaySupportMemoryTagging = false;
  194. template <class A>
  195. using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>; // Shared, max 8 TSDs.
  196. struct Primary {
  197. using SizeClassMap = FuchsiaSizeClassMap;
  198. #if SCUDO_RISCV64
  199. // Support 39-bit VMA for riscv-64
  200. static const uptr RegionSizeLog = 28U;
  201. static const uptr GroupSizeLog = 19U;
  202. #else
  203. static const uptr RegionSizeLog = 30U;
  204. static const uptr GroupSizeLog = 21U;
  205. #endif
  206. typedef u32 CompactPtrT;
  207. static const bool EnableRandomOffset = true;
  208. static const uptr MapSizeIncrement = 1UL << 18;
  209. static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  210. static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
  211. static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
  212. };
  213. template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
  214. struct Secondary {
  215. template <typename Config> using CacheT = MapAllocatorNoCache<Config>;
  216. };
  217. template <typename Config> using SecondaryT = MapAllocator<Config>;
  218. };
  219. struct TrustyConfig {
  220. static const bool MaySupportMemoryTagging = true;
  221. template <class A>
  222. using TSDRegistryT = TSDRegistrySharedT<A, 1U, 1U>; // Shared, max 1 TSD.
  223. struct Primary {
  224. using SizeClassMap = TrustySizeClassMap;
  225. static const uptr RegionSizeLog = 28U;
  226. static const uptr GroupSizeLog = 20U;
  227. typedef u32 CompactPtrT;
  228. static const bool EnableRandomOffset = false;
  229. static const uptr MapSizeIncrement = 1UL << 12;
  230. static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  231. static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
  232. static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
  233. };
  234. template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
  235. struct Secondary {
  236. template <typename Config> using CacheT = MapAllocatorNoCache<Config>;
  237. };
  238. template <typename Config> using SecondaryT = MapAllocator<Config>;
  239. };
  240. #endif
  241. #ifndef SCUDO_USE_CUSTOM_CONFIG
  242. #if SCUDO_ANDROID
  243. typedef AndroidConfig Config;
  244. #elif SCUDO_FUCHSIA
  245. typedef FuchsiaConfig Config;
  246. #elif SCUDO_TRUSTY
  247. typedef TrustyConfig Config;
  248. #else
  249. typedef DefaultConfig Config;
  250. #endif
  251. #endif // SCUDO_USE_CUSTOM_CONFIG
  252. } // namespace scudo
  253. #endif // SCUDO_ALLOCATOR_CONFIG_H_