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.

215 lines
6.0 KiB

  1. // Some nice shared memory macros
  2. //
  3. // Changed by DaveHart 25-Feb-96 so that the shared memory
  4. // is kept open and mapped for the duration of the process.
  5. // At the same time added needed synchronization.
  6. //
  7. #define LOCKSHAREWOW() LockWowSharedMemory()
  8. #define UNLOCKSHAREWOW() ReleaseMutex(hSharedWOWMutex)
  9. #define MAX_SHARED_OBJECTS 200
  10. typedef struct _SHAREDTASKMEM {
  11. BOOL fInitialized;
  12. DWORD dwFirstProcess; // Offset into shared memory where 1st shared process struct begins
  13. } SHAREDTASKMEM, *LPSHAREDTASKMEM;
  14. typedef struct _SHAREDPROCESS {
  15. DWORD dwType;
  16. DWORD dwProcessId;
  17. DWORD dwAttributes; // WOW_SYSTEM for shared WOW
  18. DWORD dwNextProcess; // Offset into shared memory where next shared process struct begins
  19. DWORD dwFirstTask; // Offset into shared memory where 1st task for this process begins
  20. LPTHREAD_START_ROUTINE pfnW32HungAppNotifyThread; // For VDMTerminateTaskWOW
  21. } SHAREDPROCESS, *LPSHAREDPROCESS;
  22. typedef struct _SHAREDTASK {
  23. DWORD dwType;
  24. DWORD dwThreadId;
  25. WORD hTask16;
  26. WORD hMod16;
  27. DWORD dwNextTask; // Offset into shared memory where next task for this process begins
  28. CHAR szModName[9]; // null terminated
  29. CHAR szFilePath[128]; // null terminated
  30. } SHAREDTASK, *LPSHAREDTASK;
  31. typedef union _SHAREDMEMOBJECT {
  32. SHAREDPROCESS sp;
  33. SHAREDTASK st;
  34. DWORD dwType;
  35. } SHAREDMEMOBJECT, *LPSHAREDMEMOBJECT;
  36. #define SMO_AVAILABLE 0
  37. #define SMO_PROCESS 1
  38. #define SMO_TASK 2
  39. #ifndef SHAREWOW_MAIN
  40. extern HANDLE hSharedWOWMem;
  41. extern LPSHAREDTASKMEM lpSharedWOWMem;
  42. extern CHAR szWowSharedMemName[];
  43. extern HANDLE hSharedWOWMutex;
  44. LPSHAREDTASKMEM LockWowSharedMemory(VOID);
  45. #else
  46. HANDLE hSharedWOWMem = NULL;
  47. LPSHAREDTASKMEM lpSharedWOWMem = NULL;
  48. CHAR szWowSharedMemName[] = "msvdmdbg.wow";
  49. CHAR szWowSharedMutexName[] = "msvdmdbg.mtx";
  50. HANDLE hSharedWOWMutex = NULL;
  51. #define MALLOC_SHAREWOW(cb) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb)
  52. #define FREE_SHAREWOW(addr) HeapFree(GetProcessHeap(), 0, addr)
  53. BOOL WOWCreateSecurityDescriptor(PSECURITY_DESCRIPTOR* ppsd)
  54. {
  55. SID_IDENTIFIER_AUTHORITY WorldAuthority = SECURITY_WORLD_SID_AUTHORITY;
  56. PSID Anyone = NULL;
  57. ULONG AclSize;
  58. ACL* pAcl;
  59. PSECURITY_DESCRIPTOR psd = NULL;
  60. BOOL fSuccess = FALSE;
  61. // -- create world authority
  62. if (!AllocateAndInitializeSid(&WorldAuthority, 1, SECURITY_WORLD_RID,
  63. 0, 0, 0, 0, 0, 0, 0,
  64. &Anyone)) {
  65. goto Fail;
  66. }
  67. // -- calc the size of the ACL (one ace)
  68. // 1 is one ACE, which includes a single ULONG from SID, since we add the size
  69. // of any Sids in -- we don't need to count the said ULONG twice
  70. AclSize = sizeof(ACL) + (1 * (sizeof(ACCESS_ALLOWED_ACE) - sizeof(ULONG))) +
  71. GetLengthSid(Anyone);
  72. psd = (PSECURITY_DESCRIPTOR)MALLOC_SHAREWOW(SECURITY_DESCRIPTOR_MIN_LENGTH + AclSize);
  73. if (NULL == psd) {
  74. goto Fail;
  75. }
  76. pAcl = (ACL*)((BYTE*)psd + SECURITY_DESCRIPTOR_MIN_LENGTH);
  77. if (!InitializeAcl(pAcl, AclSize, ACL_REVISION)) {
  78. goto Fail;
  79. }
  80. if (!AddAccessAllowedAce(pAcl,
  81. ACL_REVISION,
  82. (SPECIFIC_RIGHTS_ALL|STANDARD_RIGHTS_ALL)
  83. & ~(WRITE_DAC|WRITE_OWNER),
  84. Anyone)) {
  85. goto Fail;
  86. }
  87. InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION);
  88. if (!SetSecurityDescriptorDacl(psd, TRUE, pAcl, FALSE)) {
  89. goto Fail;
  90. }
  91. *ppsd = psd;
  92. fSuccess = TRUE;
  93. Fail:
  94. if (!fSuccess && NULL != psd) {
  95. FREE_SHAREWOW(psd);
  96. }
  97. if (NULL != Anyone) {
  98. FreeSid(Anyone);
  99. }
  100. return(fSuccess);
  101. }
  102. LPSHAREDTASKMEM LockWowSharedMemory(VOID)
  103. {
  104. DWORD dwWaitResult;
  105. LPSHAREDTASKMEM RetVal = NULL;
  106. PSECURITY_DESCRIPTOR psd = NULL;
  107. SECURITY_ATTRIBUTES sa;
  108. if (!WOWCreateSecurityDescriptor(&psd)) {
  109. goto Fail;
  110. }
  111. RtlZeroMemory(&sa, sizeof sa);
  112. sa.nLength = sizeof sa;
  113. sa.lpSecurityDescriptor = psd;
  114. if (! lpSharedWOWMem ) {
  115. if (!hSharedWOWMutex) {
  116. hSharedWOWMutex = OpenMutex(SYNCHRONIZE, FALSE, szWowSharedMutexName);
  117. if (!hSharedWOWMutex) {
  118. hSharedWOWMutex = CreateMutex(&sa, FALSE, szWowSharedMutexName); // will open if exists
  119. }
  120. if (! hSharedWOWMutex) {
  121. goto Fail;
  122. }
  123. }
  124. if (! hSharedWOWMem) {
  125. hSharedWOWMem = OpenFileMapping(
  126. FILE_MAP_WRITE,
  127. FALSE,
  128. szWowSharedMemName);
  129. if (!hSharedWOWMem) {
  130. hSharedWOWMem = CreateFileMapping( // will open if it exists
  131. (HANDLE)-1,
  132. &sa,
  133. PAGE_READWRITE,
  134. 0,
  135. sizeof(SHAREDTASKMEM) +
  136. (MAX_SHARED_OBJECTS *
  137. sizeof(SHAREDMEMOBJECT)),
  138. szWowSharedMemName);
  139. }
  140. if (! hSharedWOWMem) {
  141. goto Fail;
  142. }
  143. }
  144. lpSharedWOWMem = MapViewOfFile(hSharedWOWMem, FILE_MAP_WRITE, 0, 0, 0);
  145. if (! lpSharedWOWMem) {
  146. goto Fail;
  147. }
  148. }
  149. dwWaitResult = WaitForSingleObject(hSharedWOWMutex, INFINITE);
  150. if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_ABANDONED) {
  151. goto Fail;
  152. }
  153. RetVal = lpSharedWOWMem;
  154. goto Succeed;
  155. Fail:
  156. if (! RetVal) {
  157. if (hSharedWOWMutex) {
  158. CloseHandle(hSharedWOWMutex);
  159. hSharedWOWMutex = NULL;
  160. }
  161. if (lpSharedWOWMem) {
  162. UnmapViewOfFile(lpSharedWOWMem);
  163. lpSharedWOWMem = NULL;
  164. }
  165. if (hSharedWOWMem) {
  166. CloseHandle(hSharedWOWMem);
  167. hSharedWOWMem = NULL;
  168. }
  169. }
  170. Succeed:
  171. if (NULL != psd) {
  172. FREE_SHAREWOW(psd);
  173. }
  174. return RetVal;
  175. }
  176. #endif