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.

113 lines
2.0 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. HeapPadAllocation.cpp
  5. ModAbstract:
  6. This shim pads heap allocations by n bytes - where n is 256 by default but
  7. can be specified by command line.
  8. Notes:
  9. This is a general purpose shim.
  10. History:
  11. 09/28/1999 linstev Created
  12. 04/25/2000 linstev Added command line
  13. --*/
  14. #include "precomp.h"
  15. IMPLEMENT_SHIM_BEGIN(HeapPadAllocation)
  16. #include "ShimHookMacro.h"
  17. APIHOOK_ENUM_BEGIN
  18. APIHOOK_ENUM_ENTRY(RtlAllocateHeap)
  19. APIHOOK_ENUM_END
  20. #define DEFAULT_PAD_SIZE 256
  21. DWORD g_dwPadSize = DEFAULT_PAD_SIZE;
  22. /*++
  23. Increase the heap allocation size.
  24. --*/
  25. PVOID
  26. APIHOOK(RtlAllocateHeap)(
  27. PVOID HeapHandle,
  28. ULONG Flags,
  29. SIZE_T Size
  30. )
  31. {
  32. return ORIGINAL_API(RtlAllocateHeap)(HeapHandle, Flags, Size + g_dwPadSize);
  33. }
  34. /*++
  35. Handle DLL_PROCESS_ATTACH and DLL_PROCESS_DETACH in your notify function
  36. to do initialization and uninitialization.
  37. IMPORTANT: Make sure you ONLY call NTDLL and KERNEL32 APIs during
  38. DLL_PROCESS_ATTACH notification. No other DLLs are initialized at that
  39. point.
  40. If your shim cannot initialize properly, return FALSE and none of the
  41. APIs specified will be hooked.
  42. --*/
  43. BOOL
  44. NOTIFY_FUNCTION(
  45. DWORD fdwReason)
  46. {
  47. if (fdwReason == DLL_PROCESS_ATTACH)
  48. {
  49. CSTRING_TRY
  50. {
  51. CString csCl(COMMAND_LINE);
  52. if (! csCl.IsEmpty())
  53. {
  54. WCHAR * unused;
  55. g_dwPadSize = wcstol(csCl, &unused, 10);
  56. }
  57. if (g_dwPadSize == 0)
  58. {
  59. g_dwPadSize = DEFAULT_PAD_SIZE;
  60. }
  61. DPFN( eDbgLevelInfo, "Padding all heap allocations by %d bytes\n", g_dwPadSize);
  62. }
  63. CSTRING_CATCH
  64. {
  65. return FALSE;
  66. }
  67. }
  68. return TRUE;
  69. }
  70. /*++
  71. Register hooked functions
  72. --*/
  73. HOOK_BEGIN
  74. APIHOOK_ENTRY(NTDLL.DLL, RtlAllocateHeap)
  75. CALL_NOTIFY_FUNCTION
  76. HOOK_END
  77. IMPLEMENT_SHIM_END