Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
2.9 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. HeapClearAllocation.cpp
  5. ModAbstract:
  6. This shim fills all heap allocations with 0, or a DWORD specified on the command line
  7. Notes:
  8. This is a general purpose shim.
  9. History:
  10. 05/16/2000 dmunsil Created (based on HeapPadAllocation, by linstev)
  11. 10/10/2000 rparsons Added additional hooks for GlobalAlloc & LocalAlloc
  12. --*/
  13. #include "precomp.h"
  14. IMPLEMENT_SHIM_BEGIN(HeapClearAllocation)
  15. #include "ShimHookMacro.h"
  16. APIHOOK_ENUM_BEGIN
  17. APIHOOK_ENUM_ENTRY(RtlAllocateHeap)
  18. APIHOOK_ENUM_ENTRY(LocalAlloc)
  19. APIHOOK_ENUM_ENTRY(GlobalAlloc)
  20. APIHOOK_ENUM_END
  21. #define DEFAULT_CLEAR_DWORD 0
  22. DWORD g_dwClearValue = DEFAULT_CLEAR_DWORD;
  23. /*++
  24. Clear the allocation with the requested DWORD.
  25. --*/
  26. PVOID
  27. APIHOOK(RtlAllocateHeap)(
  28. PVOID HeapHandle,
  29. ULONG Flags,
  30. SIZE_T Size
  31. )
  32. {
  33. PVOID pRet;
  34. pRet = ORIGINAL_API(RtlAllocateHeap)(HeapHandle, Flags, Size);
  35. if (pRet) {
  36. DWORD *pdwBegin = (DWORD*)pRet;
  37. DWORD *pdwEnd = pdwBegin + (Size / sizeof(DWORD));
  38. while (pdwBegin != pdwEnd) {
  39. *pdwBegin++ = g_dwClearValue;
  40. }
  41. }
  42. return pRet;
  43. }
  44. /*++
  45. Clear the allocation with the requested DWORD.
  46. --*/
  47. HLOCAL
  48. APIHOOK(LocalAlloc)(
  49. UINT uFlags,
  50. SIZE_T uBytes
  51. )
  52. {
  53. HLOCAL hLocal;
  54. hLocal = ORIGINAL_API(LocalAlloc)(uFlags, uBytes);
  55. if (hLocal) {
  56. DWORD *pdwBegin = (DWORD*)hLocal;
  57. DWORD *pdwEnd = pdwBegin + (uBytes / sizeof(DWORD));
  58. while (pdwBegin != pdwEnd) {
  59. *pdwBegin++ = g_dwClearValue;
  60. }
  61. }
  62. return hLocal;
  63. }
  64. /*++
  65. Clear the allocation with the requested DWORD.
  66. --*/
  67. HGLOBAL
  68. APIHOOK(GlobalAlloc)(
  69. UINT uFlags,
  70. DWORD dwBytes
  71. )
  72. {
  73. HGLOBAL hGlobal;
  74. hGlobal = ORIGINAL_API(GlobalAlloc)(uFlags, dwBytes);
  75. if (hGlobal) {
  76. DWORD *pdwBegin = (DWORD*)hGlobal;
  77. DWORD *pdwEnd = pdwBegin + (dwBytes / sizeof(DWORD));
  78. while (pdwBegin != pdwEnd) {
  79. *pdwBegin++ = g_dwClearValue;
  80. }
  81. }
  82. return hGlobal;
  83. }
  84. /*++
  85. Get the fill value from the command line.
  86. --*/
  87. BOOL
  88. NOTIFY_FUNCTION(
  89. DWORD fdwReason
  90. )
  91. {
  92. if (fdwReason == DLL_PROCESS_ATTACH)
  93. {
  94. CSTRING_TRY
  95. {
  96. CString csCl(COMMAND_LINE);
  97. if (! csCl.IsEmpty())
  98. {
  99. WCHAR * unused;
  100. g_dwClearValue = wcstol(csCl, &unused, 10);
  101. }
  102. DPFN( eDbgLevelInfo, "Filling all heap allocations with 0x%8.8X\n", g_dwClearValue);
  103. }
  104. CSTRING_CATCH
  105. {
  106. return FALSE;
  107. }
  108. }
  109. return TRUE;
  110. }
  111. /*++
  112. Register hooked functions
  113. --*/
  114. HOOK_BEGIN
  115. APIHOOK_ENTRY(NTDLL.DLL, RtlAllocateHeap)
  116. APIHOOK_ENTRY(KERNEL32.DLL, LocalAlloc)
  117. APIHOOK_ENTRY(KERNEL32.DLL, GlobalAlloc)
  118. CALL_NOTIFY_FUNCTION
  119. HOOK_END
  120. IMPLEMENT_SHIM_END