allocator_config.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // // Defines the type and scale of a compact pointer. A compact pointer can
  34. // // be understood as the offset of a pointer within the region it belongs
  35. // // to, in increments of a power-of-2 scale.
  36. // // eg: Ptr = Base + (CompactPtr << Scale).
  37. // typedef u32 PrimaryCompactPtrT;
  38. // static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  39. // // Indicates support for offsetting the start of a region by
  40. // // a random number of pages. Only used with primary64.
  41. // static const bool PrimaryEnableRandomOffset = true;
  42. // // Call map for user memory with at least this size. Only used with
  43. // // primary64.
  44. // static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  45. // // Defines the minimal & maximal release interval that can be set.
  46. // static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  47. // static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
  48. // // Defines the type of cache used by the Secondary. Some additional
  49. // // configuration entries can be necessary depending on the Cache.
  50. // typedef MapAllocatorNoCache SecondaryCache;
  51. // // Thread-Specific Data Registry used, shared or exclusive.
  52. // template <class A> using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>;
  53. // };
  54. // Default configurations for various platforms.
  55. struct DefaultConfig {
  56. using SizeClassMap = DefaultSizeClassMap;
  57. static const bool MaySupportMemoryTagging = true;
  58. #if SCUDO_CAN_USE_PRIMARY64
  59. typedef SizeClassAllocator64<DefaultConfig> Primary;
  60. static const uptr PrimaryRegionSizeLog = 32U;
  61. typedef uptr PrimaryCompactPtrT;
  62. static const uptr PrimaryCompactPtrScale = 0;
  63. static const bool PrimaryEnableRandomOffset = true;
  64. static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  65. #else
  66. typedef SizeClassAllocator32<DefaultConfig> Primary;
  67. static const uptr PrimaryRegionSizeLog = 19U;
  68. typedef uptr PrimaryCompactPtrT;
  69. #endif
  70. static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  71. static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
  72. typedef MapAllocatorCache<DefaultConfig> SecondaryCache;
  73. static const u32 SecondaryCacheEntriesArraySize = 32U;
  74. static const u32 SecondaryCacheQuarantineSize = 0U;
  75. static const u32 SecondaryCacheDefaultMaxEntriesCount = 32U;
  76. static const uptr SecondaryCacheDefaultMaxEntrySize = 1UL << 19;
  77. static const s32 SecondaryCacheMinReleaseToOsIntervalMs = INT32_MIN;
  78. static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = INT32_MAX;
  79. template <class A> using TSDRegistryT = TSDRegistryExT<A>; // Exclusive
  80. };
  81. struct AndroidConfig {
  82. using SizeClassMap = AndroidSizeClassMap;
  83. static const bool MaySupportMemoryTagging = true;
  84. #if SCUDO_CAN_USE_PRIMARY64
  85. typedef SizeClassAllocator64<AndroidConfig> Primary;
  86. static const uptr PrimaryRegionSizeLog = 28U;
  87. typedef u32 PrimaryCompactPtrT;
  88. static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  89. static const bool PrimaryEnableRandomOffset = true;
  90. static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  91. #else
  92. typedef SizeClassAllocator32<AndroidConfig> Primary;
  93. static const uptr PrimaryRegionSizeLog = 18U;
  94. typedef uptr PrimaryCompactPtrT;
  95. #endif
  96. static const s32 PrimaryMinReleaseToOsIntervalMs = 1000;
  97. static const s32 PrimaryMaxReleaseToOsIntervalMs = 1000;
  98. typedef MapAllocatorCache<AndroidConfig> SecondaryCache;
  99. static const u32 SecondaryCacheEntriesArraySize = 256U;
  100. static const u32 SecondaryCacheQuarantineSize = 32U;
  101. static const u32 SecondaryCacheDefaultMaxEntriesCount = 32U;
  102. static const uptr SecondaryCacheDefaultMaxEntrySize = 2UL << 20;
  103. static const s32 SecondaryCacheMinReleaseToOsIntervalMs = 0;
  104. static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = 1000;
  105. template <class A>
  106. using TSDRegistryT = TSDRegistrySharedT<A, 8U, 2U>; // Shared, max 8 TSDs.
  107. };
  108. struct AndroidSvelteConfig {
  109. using SizeClassMap = SvelteSizeClassMap;
  110. static const bool MaySupportMemoryTagging = false;
  111. #if SCUDO_CAN_USE_PRIMARY64
  112. typedef SizeClassAllocator64<AndroidSvelteConfig> Primary;
  113. static const uptr PrimaryRegionSizeLog = 27U;
  114. typedef u32 PrimaryCompactPtrT;
  115. static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  116. static const bool PrimaryEnableRandomOffset = true;
  117. static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  118. #else
  119. typedef SizeClassAllocator32<AndroidSvelteConfig> Primary;
  120. static const uptr PrimaryRegionSizeLog = 16U;
  121. typedef uptr PrimaryCompactPtrT;
  122. #endif
  123. static const s32 PrimaryMinReleaseToOsIntervalMs = 1000;
  124. static const s32 PrimaryMaxReleaseToOsIntervalMs = 1000;
  125. typedef MapAllocatorCache<AndroidSvelteConfig> SecondaryCache;
  126. static const u32 SecondaryCacheEntriesArraySize = 16U;
  127. static const u32 SecondaryCacheQuarantineSize = 32U;
  128. static const u32 SecondaryCacheDefaultMaxEntriesCount = 4U;
  129. static const uptr SecondaryCacheDefaultMaxEntrySize = 1UL << 18;
  130. static const s32 SecondaryCacheMinReleaseToOsIntervalMs = 0;
  131. static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = 0;
  132. template <class A>
  133. using TSDRegistryT = TSDRegistrySharedT<A, 2U, 1U>; // Shared, max 2 TSDs.
  134. };
  135. #if SCUDO_CAN_USE_PRIMARY64
  136. struct FuchsiaConfig {
  137. using SizeClassMap = FuchsiaSizeClassMap;
  138. static const bool MaySupportMemoryTagging = false;
  139. typedef SizeClassAllocator64<FuchsiaConfig> Primary;
  140. static const uptr PrimaryRegionSizeLog = 30U;
  141. typedef u32 PrimaryCompactPtrT;
  142. static const bool PrimaryEnableRandomOffset = true;
  143. static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  144. static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  145. static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  146. static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
  147. typedef MapAllocatorNoCache SecondaryCache;
  148. template <class A>
  149. using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>; // Shared, max 8 TSDs.
  150. };
  151. struct TrustyConfig {
  152. using SizeClassMap = TrustySizeClassMap;
  153. static const bool MaySupportMemoryTagging = false;
  154. typedef SizeClassAllocator64<TrustyConfig> Primary;
  155. // Some apps have 1 page of heap total so small regions are necessary.
  156. static const uptr PrimaryRegionSizeLog = 10U;
  157. typedef u32 PrimaryCompactPtrT;
  158. static const bool PrimaryEnableRandomOffset = false;
  159. // Trusty is extremely memory-constrained so minimally round up map calls.
  160. static const uptr PrimaryMapSizeIncrement = 1UL << 4;
  161. static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  162. static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  163. static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
  164. typedef MapAllocatorNoCache SecondaryCache;
  165. template <class A>
  166. using TSDRegistryT = TSDRegistrySharedT<A, 1U, 1U>; // Shared, max 1 TSD.
  167. };
  168. #endif
  169. #if SCUDO_ANDROID
  170. typedef AndroidConfig Config;
  171. #elif SCUDO_FUCHSIA
  172. typedef FuchsiaConfig Config;
  173. #elif SCUDO_TRUSTY
  174. typedef TrustyConfig Config;
  175. #else
  176. typedef DefaultConfig Config;
  177. #endif
  178. } // namespace scudo
  179. #endif // SCUDO_ALLOCATOR_CONFIG_H_