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.

205 lines
6.7 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. chandler.c
  5. Abstract:
  6. This module implements the C specific exception handler that provides
  7. structured condition handling for the C language.
  8. Author:
  9. David N. Cutler (davec) 28-Oct-2000
  10. Environment:
  11. Any mode.
  12. --*/
  13. #include "nt.h"
  14. EXCEPTION_DISPOSITION
  15. __C_specific_handler (
  16. IN PEXCEPTION_RECORD ExceptionRecord,
  17. IN PVOID EstablisherFrame,
  18. IN OUT PCONTEXT ContextRecord,
  19. IN OUT PDISPATCHER_CONTEXT DispatcherContext
  20. )
  21. /*++
  22. Routine Description:
  23. This function scans the scope tables associated with the specified
  24. procedure and calls exception and termination handlers as necessary.
  25. Arguments:
  26. ExceptionRecord - Supplies a pointer to an exception record.
  27. EstablisherFrame - Supplies a pointer to frame of the establisher function.
  28. ContextRecord - Supplies a pointer to a context record.
  29. DispatcherContext - Supplies a pointer to the exception dispatcher or
  30. unwind dispatcher context.
  31. Return Value:
  32. If an exception is being dispatched and the exception is handled by one
  33. of the exception filter routines, then there is no return from this
  34. routine and RtlUnwind is called. Otherwise, an exception disposition
  35. value of continue execution or continue search is returned.
  36. If an unwind is being dispatched, then each termination handler is called
  37. and a value of continue search is returned.
  38. --*/
  39. {
  40. ULONG64 ControlPc;
  41. PEXCEPTION_FILTER ExceptionFilter;
  42. EXCEPTION_POINTERS ExceptionPointers;
  43. ULONG64 ImageBase;
  44. ULONG Index;
  45. PSCOPE_TABLE ScopeTable;
  46. ULONG64 TargetPc;
  47. PTERMINATION_HANDLER TerminationHandler;
  48. LONG Value;
  49. //
  50. // Get the image base address. compute the relative address of where
  51. // control left the establisher, and get the address of the scope table.
  52. //
  53. ImageBase = DispatcherContext->ImageBase;
  54. ControlPc = DispatcherContext->ControlPc - ImageBase;
  55. ScopeTable = (PSCOPE_TABLE)(DispatcherContext->HandlerData);
  56. //
  57. // If an unwind is not in progress, then scan the scope table and call
  58. // the appropriate exception filter routines. Otherwise, scan the scope
  59. // table and call the appropriate termination handlers using the target
  60. // PC obtained from the dispatcher context.
  61. // are called.
  62. //
  63. if (IS_DISPATCHING(ExceptionRecord->ExceptionFlags)) {
  64. //
  65. // Scan the scope table and call the appropriate exception filter
  66. // routines.
  67. //
  68. ExceptionPointers.ExceptionRecord = ExceptionRecord;
  69. ExceptionPointers.ContextRecord = ContextRecord;
  70. for (Index = 0; Index < ScopeTable->Count; Index += 1) {
  71. if ((ControlPc >= ScopeTable->ScopeRecord[Index].BeginAddress) &&
  72. (ControlPc < ScopeTable->ScopeRecord[Index].EndAddress) &&
  73. (ScopeTable->ScopeRecord[Index].JumpTarget != 0)) {
  74. //
  75. // If the Call the exception filter routine.
  76. //
  77. if (ScopeTable->ScopeRecord[Index].HandlerAddress == 1) {
  78. Value = EXCEPTION_EXECUTE_HANDLER;
  79. } else {
  80. ExceptionFilter =
  81. (PEXCEPTION_FILTER)(ScopeTable->ScopeRecord[Index].HandlerAddress + ImageBase);
  82. Value = (ExceptionFilter)(&ExceptionPointers, EstablisherFrame);
  83. }
  84. //
  85. // If the return value is less than zero, then dismiss the
  86. // exception. Otherwise, if the value is greater than zero,
  87. // then unwind to the target exception handler. Otherwise,
  88. // continue the search for an exception filter.
  89. //
  90. if (Value < 0) {
  91. return ExceptionContinueExecution;
  92. } else if (Value > 0) {
  93. RtlUnwindEx(EstablisherFrame,
  94. (PVOID)(ScopeTable->ScopeRecord[Index].JumpTarget + ImageBase),
  95. ExceptionRecord,
  96. (PVOID)((ULONG64)ExceptionRecord->ExceptionCode),
  97. DispatcherContext->ContextRecord,
  98. DispatcherContext->HistoryTable);
  99. }
  100. }
  101. }
  102. } else {
  103. //
  104. // Scan the scope table and call the appropriate termination handler
  105. // routines.
  106. //
  107. TargetPc = DispatcherContext->TargetIp - ImageBase;
  108. for (Index = 0; Index < ScopeTable->Count; Index += 1) {
  109. if ((ControlPc >= ScopeTable->ScopeRecord[Index].BeginAddress) &&
  110. (ControlPc < ScopeTable->ScopeRecord[Index].EndAddress)) {
  111. //
  112. // If the target PC is within the same scope as the control PC,
  113. // then this is an uplevel goto out of an inner try scope or a
  114. // long jump back into a try scope. Terminate the scan for a
  115. // termination handler.
  116. //
  117. // N.B. The target PC can be just beyond the end of the scope,
  118. // in which case it is a leave from the scope.
  119. //
  120. if ((TargetPc >= ScopeTable->ScopeRecord[Index].BeginAddress) &&
  121. (TargetPc <= ScopeTable->ScopeRecord[Index].EndAddress)) {
  122. break;
  123. } else {
  124. //
  125. // If the scope table entry describes an exception filter
  126. // and the associated exception handler is the target of
  127. // the unwind, then terminate the scan for termination
  128. // handlers. Otherwise, if the scope table entry describes
  129. // a termination handler, then record the address of the
  130. // end of the scope as the new control PC address and call
  131. // the termination handler.
  132. //
  133. if (ScopeTable->ScopeRecord[Index].JumpTarget != 0) {
  134. if (TargetPc == ScopeTable->ScopeRecord[Index].JumpTarget) {
  135. break;
  136. }
  137. } else {
  138. DispatcherContext->ControlPc =
  139. ImageBase + ScopeTable->ScopeRecord[Index].EndAddress;
  140. TerminationHandler =
  141. (PTERMINATION_HANDLER)(ScopeTable->ScopeRecord[Index].HandlerAddress + ImageBase);
  142. (TerminationHandler)(TRUE, EstablisherFrame);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. //
  149. // Continue search for exception or termination handlers.
  150. //
  151. return ExceptionContinueSearch;
  152. }