VirtualAllocWrapper.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Memory
  7. {
  8. #define PreReservedHeapTrace(...) \
  9. { \
  10. OUTPUT_TRACE(Js::PreReservedHeapAllocPhase, __VA_ARGS__); \
  11. }
  12. class SectionAllocWrapper;
  13. class PreReservedSectionAllocWrapper;
  14. /*
  15. * VirtualAllocWrapper is just a delegator class to call VirtualAlloc and VirtualFree.
  16. */
  17. class VirtualAllocWrapper
  18. {
  19. public:
  20. LPVOID AllocPages(LPVOID lpAddress, DECLSPEC_GUARD_OVERFLOW size_t pageCount, DWORD allocationType, DWORD protectFlags, bool isCustomHeapAllocation);
  21. BOOL Free(LPVOID lpAddress, size_t dwSize, DWORD dwFreeType);
  22. LPVOID AllocLocal(LPVOID lpAddress, DECLSPEC_GUARD_OVERFLOW size_t dwSize) { return lpAddress; }
  23. BOOL FreeLocal(LPVOID lpAddress) { return true; }
  24. bool GetFileInfo(LPVOID address, HANDLE* fileHandle, PVOID* baseAddress) { return true; }
  25. static VirtualAllocWrapper Instance; // single instance
  26. private:
  27. VirtualAllocWrapper() {}
  28. };
  29. #if ENABLE_NATIVE_CODEGEN
  30. /*
  31. * PreReservedVirtualAllocWrapper class takes care of Reserving a large memory region initially
  32. * and then committing mem regions for the size requested.
  33. * Committed pages are being tracked with a bitVector.
  34. * Committing memory outside of the preReserved Memory region is not handled by this allocator
  35. */
  36. class PreReservedVirtualAllocWrapper
  37. {
  38. public:
  39. #if TARGET_32
  40. static const uint PreReservedAllocationSegmentCount = 256; // (256 * 64K) == 16 MB, if 64k is the AllocationGranularity
  41. #else // TARGET_64
  42. static const uint PreReservedAllocationSegmentCount = 4096; //(4096 * 64K) == 256MB, if 64k is the AllocationGranularity
  43. #endif
  44. #if !TARGET_64 && _CONTROL_FLOW_GUARD
  45. static const unsigned MaxPreReserveSegment = 6;
  46. #endif
  47. public:
  48. PreReservedVirtualAllocWrapper();
  49. ~PreReservedVirtualAllocWrapper();
  50. LPVOID AllocPages(LPVOID lpAddress, DECLSPEC_GUARD_OVERFLOW size_t pageCount, DWORD allocationType, DWORD protectFlags, bool isCustomHeapAllocation);
  51. BOOL Free(LPVOID lpAddress, size_t dwSize, DWORD dwFreeType);
  52. LPVOID AllocLocal(LPVOID lpAddress, DECLSPEC_GUARD_OVERFLOW size_t dwSize) { return lpAddress; }
  53. BOOL FreeLocal(LPVOID lpAddress) { return true; }
  54. bool GetFileInfo(LPVOID address, HANDLE* fileHandle, PVOID* baseAddress) { return true; }
  55. bool IsInRange(void * address);
  56. static bool IsInRange(void * regionStart, void * address);
  57. LPVOID EnsurePreReservedRegion();
  58. LPVOID GetPreReservedEndAddress();
  59. static LPVOID GetPreReservedEndAddress(void * regionStart);
  60. #if !TARGET_64 && _CONTROL_FLOW_GUARD
  61. static int NumPreReservedSegment() { return numPreReservedSegment; }
  62. #endif
  63. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  64. bool IsPreReservedEndAddress(LPVOID address)
  65. {
  66. return IsPreReservedRegionPresent() && address == GetPreReservedEndAddress();
  67. }
  68. #endif
  69. private:
  70. LPVOID EnsurePreReservedRegionInternal();
  71. bool IsPreReservedRegionPresent();
  72. LPVOID GetPreReservedStartAddress();
  73. BVStatic<PreReservedAllocationSegmentCount> freeSegments;
  74. LPVOID preReservedStartAddress;
  75. CriticalSection cs;
  76. #if !TARGET_64 && _CONTROL_FLOW_GUARD
  77. static uint numPreReservedSegment;
  78. #endif
  79. };
  80. #endif
  81. #if defined(ENABLE_JIT_CLAMP)
  82. class AutoEnableDynamicCodeGen
  83. {
  84. public:
  85. AutoEnableDynamicCodeGen(bool enable = true);
  86. ~AutoEnableDynamicCodeGen();
  87. private:
  88. bool enabled;
  89. typedef
  90. BOOL
  91. (WINAPI *PSET_THREAD_INFORMATION_PROC)(
  92. _In_ HANDLE hThread,
  93. _In_ THREAD_INFORMATION_CLASS ThreadInformationClass,
  94. _In_reads_bytes_(ThreadInformationSize) PVOID ThreadInformation,
  95. _In_ DWORD ThreadInformationSize
  96. );
  97. typedef
  98. BOOL
  99. (WINAPI *PGET_THREAD_INFORMATION_PROC)(
  100. _In_ HANDLE hThread,
  101. _In_ THREAD_INFORMATION_CLASS ThreadInformationClass,
  102. _Out_writes_bytes_(ThreadInformationSize) PVOID ThreadInformation,
  103. _In_ DWORD ThreadInformationSize
  104. );
  105. static PSET_THREAD_INFORMATION_PROC SetThreadInformationProc;
  106. static PGET_THREAD_INFORMATION_PROC GetThreadInformationProc;
  107. static PROCESS_MITIGATION_DYNAMIC_CODE_POLICY processPolicy;
  108. static CriticalSection processPolicyCS;
  109. static volatile bool processPolicyObtained;
  110. };
  111. #endif
  112. template<typename TVirtualAlloc = VirtualAllocWrapper>
  113. class PageSegmentBase;
  114. template<typename TVirtualAlloc = VirtualAllocWrapper>
  115. class SegmentBase;
  116. template<typename TVirtualAlloc = VirtualAllocWrapper, typename TSegment = SegmentBase<TVirtualAlloc>, typename TPageSegment = PageSegmentBase<TVirtualAlloc>>
  117. class PageAllocatorBase;
  118. typedef PageAllocatorBase<> PageAllocator;
  119. typedef PageSegmentBase<> PageSegment;
  120. typedef SegmentBase<> Segment;
  121. }