Leaked source code of windows server 2003
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.

272 lines
4.5 KiB

  1. /*******************************************************************
  2. *
  3. * File : util.cxx
  4. * Author : Eyal Schwartz
  5. * Copyrights : Microsoft Corp (C) 1996
  6. * Date : 7/21/1998
  7. * Description :
  8. *
  9. * Revisions : <date> <name> <description>
  10. *******************************************************************/
  11. #ifndef UTIL_CXX
  12. #define UTIL_CXX
  13. // include //
  14. #include "common.h"
  15. #include "util.hxx"
  16. // defines //
  17. #define DNSEXT_CUSTOMER_CODE 0xD0000000
  18. #define STACK_OVERFLOW STATUS_STACK_OVERFLOW | DNSEXT_CUSTOMER_CODE
  19. // types //
  20. // global variables //
  21. PVOID gAllocationStack[MAXLIST];
  22. // local variables //
  23. static INT iStackSize=0;
  24. // prototypes //
  25. PVOID
  26. ReadMemory(
  27. IN PVOID pvAddr,
  28. IN DWORD dwSize);
  29. VOID
  30. FreeMemory(
  31. IN PVOID pv);
  32. // functions //
  33. PVOID PushMemory(
  34. IN PVOID pvAddr,
  35. IN DWORD dwSize)
  36. /*++
  37. Routine Description:
  38. Shells on read memory to remember the pointer in a local stack
  39. Arguments:
  40. pvAddr - Address of memory block to read in the address space of the
  41. process being debugged.
  42. dwSize - Count of bytes to read/allocate/copy.
  43. Return Value:
  44. Pointer to debugger local memory.
  45. --*/
  46. {
  47. if(iStackSize == MAXLIST-1){
  48. Printf("Exception: No more allocation stack memory\n");
  49. RaiseException(STACK_OVERFLOW, 0, 0, NULL);
  50. }
  51. PVOID pv = ReadMemory(pvAddr, dwSize);
  52. if(!pv){
  53. Printf("Exception: No memory\n");
  54. RaiseException(STACK_OVERFLOW, 0, 0, NULL);
  55. }
  56. return gAllocationStack[iStackSize++] = pv;
  57. }
  58. VOID
  59. PopMemory(
  60. IN PVOID pv)
  61. /*++
  62. Routine Description:
  63. Frees memory returned by PushMemory.
  64. Arguments:
  65. pv - Address of debugger local memory to free.
  66. Return Value:
  67. None.
  68. --*/
  69. {
  70. FreeMemory(pv);
  71. if(0 == iStackSize){
  72. Printf("Exception: Invalid stack operation (iStackSize == 0).\n");
  73. RaiseException(STACK_OVERFLOW, 0, 0, NULL);
  74. }
  75. gAllocationStack[iStackSize--] = NULL;
  76. }
  77. VOID
  78. CleanMemory( VOID)
  79. /*++
  80. Routine Description:
  81. Frees all stack memory returned by PushMemory.
  82. Arguments:
  83. Return Value:
  84. None.
  85. --*/
  86. {
  87. for (INT i=0; i<MAXLIST; i++) {
  88. if(gAllocationStack[i]){
  89. FreeMemory(gAllocationStack[i]);
  90. }
  91. }
  92. }
  93. //
  94. // NOTE: The following mem utils were copied & modified from dsexts.dll code base
  95. //
  96. PVOID
  97. ReadMemory(
  98. IN PVOID pvAddr,
  99. IN DWORD dwSize)
  100. /*++
  101. Routine Description:
  102. This function reads memory from the address space of the process
  103. being debugged and copies its contents to newly allocated memory
  104. in the debuggers address space. NULL is returned on error. Returned
  105. memory should be deallocated via FreeMemory().
  106. Arguments:
  107. pvAddr - Address of memory block to read in the address space of the
  108. process being debugged.
  109. dwSize - Count of bytes to read/allocate/copy.
  110. Return Value:
  111. Pointer to debugger local memory.
  112. --*/
  113. {
  114. SIZE_T cRead;
  115. PVOID pv;
  116. DEBUG1("HeapAlloc(0x%x)\n", dwSize);
  117. if ( NULL == (pv = HeapAlloc(GetProcessHeap(), 0, dwSize)) )
  118. {
  119. Printf("Memory allocation error for %x bytes\n", dwSize);
  120. return(NULL);
  121. }
  122. DEBUG2("ReadProcessMemory(0x%x @ %p)\n", dwSize, pvAddr);
  123. if ( !ReadProcessMemory(ghCurrentProcess, pvAddr, pv, dwSize, &cRead) )
  124. {
  125. FreeMemory(pv);
  126. Printf("ReadProcessMemory error %x (%x@%p)\n",
  127. GetLastError(),
  128. dwSize,
  129. pvAddr);
  130. return(NULL);
  131. }
  132. if ( dwSize != ( DWORD ) cRead )
  133. {
  134. FreeMemory(pv);
  135. Printf("ReadProcessMemory size error - off by %x bytes\n",
  136. ( dwSize > ( DWORD ) cRead ) ?
  137. dwSize - ( DWORD ) cRead :
  138. ( DWORD ) cRead - dwSize );
  139. return(NULL);
  140. }
  141. return(pv);
  142. }
  143. VOID
  144. FreeMemory(
  145. IN PVOID pv)
  146. /*++
  147. Routine Description:
  148. Frees memory returned by ReadMemory.
  149. Arguments:
  150. pv - Address of debugger local memory to free.
  151. Return Value:
  152. None.
  153. --*/
  154. {
  155. DEBUG1("HeapFree(%p)\n", pv);
  156. if ( NULL != pv )
  157. {
  158. if ( !HeapFree(GetProcessHeap(), 0, pv) )
  159. {
  160. Printf("Error %x freeing memory at %p\n", GetLastError(), pv);
  161. }
  162. }
  163. }
  164. #endif
  165. /******************* EOF *********************/
  166.