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.

218 lines
5.3 KiB

  1. #if !defined(_FUSION_INC_SXSEXCEPTIONHANDLING_H_INCLUDED_)
  2. #define _FUSION_INC_SXSEXCEPTIONHANDLING_H_INCLUDED_
  3. /*++
  4. Copyright (c) Microsoft Corporation
  5. Module Name:
  6. sxsexceptionhandling.h
  7. Abstract:
  8. Author:
  9. Jay Krell (a-JayK, JayKrell) October 2000
  10. Revision History:
  11. --*/
  12. #pragma once
  13. #include "nt.h"
  14. #include "ntrtl.h"
  15. #include "nturtl.h"
  16. #include "windows.h"
  17. #include "fusionlastwin32error.h"
  18. #include "fusionntdll.h"
  19. #include "fusiontrace.h"
  20. #include "csxspreservelasterror.h" // Most destructors should use this.
  21. #include "fusionheap.h"
  22. /*-----------------------------------------------------------------------------
  23. Instead of:
  24. __except(EXECEPTION_EXECUTE_HANDLER)
  25. say:
  26. __except(SXSP_EXCEPTION_FILTER())
  27. This way all exceptions will be logged with DbgPrint,
  28. and probably hit a breakpoint if under a debugger, and any other behavior we
  29. want.
  30. If your exception filter is other than (EXECEPTION_EXECUTE_HANDLER), then
  31. you are on your own.
  32. -----------------------------------------------------------------------------*/
  33. LONG
  34. SxspExceptionFilter(
  35. PEXCEPTION_POINTERS ExceptionPointers,
  36. PCSTR Function
  37. );
  38. LONG
  39. FusionpReadMappedMemoryExceptionFilter(
  40. PEXCEPTION_POINTERS ExceptionPointers,
  41. IN PNTSTATUS ExceptionCode
  42. );
  43. /*
  44. Use instead of InitializeCriticalSection.
  45. */
  46. BOOL
  47. FusionpInitializeCriticalSection(
  48. LPCRITICAL_SECTION CriticalSection
  49. );
  50. /*
  51. Use instead of InitializeCriticalSectionAndSpinCount
  52. */
  53. BOOL
  54. FusionpInitializeCriticalSectionAndSpinCount(
  55. LPCRITICAL_SECTION CriticalSection,
  56. DWORD SpinCount
  57. );
  58. #define SXSP_EXCEPTION_FILTER() (::SxspExceptionFilter(GetExceptionInformation(), __FUNCTION__))
  59. #define SXS_REPORT_SEH_EXCEPTION(string, fBreakin) \
  60. do { \
  61. ::FusionpReportCondition(fBreakin, "SXS.DLL: " __FUNCTION__ " - Unhandled exception caught: 0x%08lx", GetExceptionCode()); \
  62. } while (0)
  63. class CCriticalSectionNoConstructor : public CRITICAL_SECTION
  64. {
  65. void operator=(const CCriticalSectionNoConstructor&); // deliberately not implemented
  66. public:
  67. BOOL Initialize(PCSTR Function = "");
  68. BOOL Destruct();
  69. };
  70. inline BOOL
  71. CCriticalSectionNoConstructor::Destruct()
  72. {
  73. ::DeleteCriticalSection(this);
  74. return TRUE;
  75. }
  76. inline BOOL
  77. CCriticalSectionNoConstructor::Initialize(
  78. PCSTR /* Function */)
  79. {
  80. return ::FusionpInitializeCriticalSection(this);
  81. }
  82. class CSxsLockCriticalSection
  83. {
  84. public:
  85. CSxsLockCriticalSection(CRITICAL_SECTION &rcs) : m_rcs(rcs), m_fIsLocked(false) { }
  86. BOOL Lock();
  87. BOOL TryLock();
  88. BOOL LockWithSEH();
  89. ~CSxsLockCriticalSection() { if (m_fIsLocked) { CSxsPreserveLastError ple; ::LeaveCriticalSection(&m_rcs); ple.Restore(); } }
  90. BOOL Unlock();
  91. protected:
  92. CRITICAL_SECTION &m_rcs;
  93. bool m_fIsLocked;
  94. private:
  95. void operator=(const CSxsLockCriticalSection&);
  96. CSxsLockCriticalSection(const CSxsLockCriticalSection&);
  97. };
  98. inline
  99. BOOL
  100. CSxsLockCriticalSection::Lock()
  101. {
  102. BOOL fSuccess = FALSE;
  103. FN_TRACE_WIN32(fSuccess);
  104. INTERNAL_ERROR_CHECK(!m_fIsLocked);
  105. ::EnterCriticalSection(&m_rcs);
  106. m_fIsLocked = true;
  107. fSuccess = TRUE;
  108. Exit:
  109. return fSuccess;
  110. }
  111. inline
  112. BOOL
  113. CSxsLockCriticalSection::LockWithSEH()
  114. {
  115. //
  116. // EnterCriticalSection on XP and above does not throw exceptions
  117. // (other than continuable "possible deadlock" exception).
  118. //
  119. // EnterCriticalSection on NT4 and Win2000 may throw exceptions.
  120. // On Win2000 you can avoid this by preallocating the event
  121. // but it does use up memory. Catching the exception does no
  122. // good, the critical section is left corrupt.
  123. //
  124. // EnterCriticalSection on Win9x does not throw exceptions.
  125. //
  126. #if defined(FUSION_WIN)
  127. return this->Lock();
  128. #else
  129. BOOL fSuccess = FALSE;
  130. // We can't use the spiffy macros in the same frame as a __try block.
  131. ASSERT_NTC(!m_fIsLocked);
  132. if (m_fIsLocked)
  133. {
  134. ::FusionpSetLastWin32Error(ERROR_INTERNAL_ERROR);
  135. goto Exit;
  136. }
  137. if (!this->Lock())
  138. goto Exit;
  139. m_fIsLocked = true;
  140. fSuccess = TRUE;
  141. Exit:
  142. return fSuccess;
  143. #endif // FUSION_WIN
  144. }
  145. inline
  146. BOOL
  147. CSxsLockCriticalSection::TryLock()
  148. {
  149. //
  150. // NTRAID#NTBUG9-591667-2002/03/31-JayKrell
  151. // It is not an error for TryEnterCriticalSection to return false.
  152. //
  153. BOOL fSuccess = FALSE;
  154. FN_TRACE_WIN32(fSuccess);
  155. INTERNAL_ERROR_CHECK(!m_fIsLocked);
  156. IFW32FALSE_ORIGINATE_AND_EXIT(::TryEnterCriticalSection(&m_rcs));
  157. m_fIsLocked = true;
  158. fSuccess = TRUE;
  159. Exit:
  160. return fSuccess;
  161. }
  162. inline
  163. BOOL
  164. CSxsLockCriticalSection::Unlock()
  165. {
  166. //
  167. // LeaveCriticalSection on XP and above does not throw exceptions.
  168. //
  169. // LeaveCriticalSection on NT4 and Win2000 may throw exceptions.
  170. // On Win2000 you can avoid this by preallocating the event
  171. // but it does use up memory. Catching the exception does no
  172. // good, the critical section is left corrupt.
  173. //
  174. // LeaveCriticalSection on Win9x does not throw exceptions.
  175. //
  176. BOOL fSuccess = FALSE;
  177. FN_TRACE_WIN32(fSuccess);
  178. INTERNAL_ERROR_CHECK(m_fIsLocked);
  179. ::LeaveCriticalSection(&m_rcs);
  180. m_fIsLocked = false;
  181. fSuccess = TRUE;
  182. Exit:
  183. return fSuccess;
  184. }
  185. #endif // !defined(_FUSION_INC_SXSEXCEPTIONHANDLING_H_INCLUDED_)