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.

663 lines
20 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. exdsptch.c
  5. Abstract:
  6. This module implements the dispatching of exception and the unwinding of
  7. procedure call frames.
  8. Author:
  9. David N. Cutler (davec) 13-Aug-1989
  10. Environment:
  11. Any mode.
  12. Revision History:
  13. 10 april 90 bryanwi
  14. Port to the 386.
  15. --*/
  16. #include "ntrtlp.h"
  17. //
  18. // Dispatcher context structure definition.
  19. //
  20. typedef struct _DISPATCHER_CONTEXT {
  21. PEXCEPTION_REGISTRATION_RECORD RegistrationPointer;
  22. } DISPATCHER_CONTEXT;
  23. //
  24. // Execute handler for exception function prototype.
  25. //
  26. EXCEPTION_DISPOSITION
  27. RtlpExecuteHandlerForException (
  28. IN PEXCEPTION_RECORD ExceptionRecord,
  29. IN PVOID EstablisherFrame,
  30. IN OUT PCONTEXT ContextRecord,
  31. IN OUT PVOID DispatcherContext,
  32. IN PEXCEPTION_ROUTINE ExceptionRoutine
  33. );
  34. //
  35. // Execute handler for unwind function prototype.
  36. //
  37. EXCEPTION_DISPOSITION
  38. RtlpExecuteHandlerForUnwind (
  39. IN PEXCEPTION_RECORD ExceptionRecord,
  40. IN PVOID EstablisherFrame,
  41. IN OUT PCONTEXT ContextRecord,
  42. IN OUT PVOID DispatcherContext,
  43. IN PEXCEPTION_ROUTINE ExceptionRoutine
  44. );
  45. BOOLEAN
  46. RtlDispatchException (
  47. IN PEXCEPTION_RECORD ExceptionRecord,
  48. IN PCONTEXT ContextRecord
  49. )
  50. /*++
  51. Routine Description:
  52. This function attempts to dispatch an exception to a call frame based
  53. handler by searching backwards through the stack based call frames. The
  54. search begins with the frame specified in the context record and continues
  55. backward until either a handler is found that handles the exception, the
  56. stack is found to be invalid (i.e., out of limits or unaligned), or the end
  57. of the call hierarchy is reached.
  58. Arguments:
  59. ExceptionRecord - Supplies a pointer to an exception record.
  60. ContextRecord - Supplies a pointer to a context record.
  61. Return Value:
  62. If the exception is handled by one of the frame based handlers, then
  63. a value of TRUE is returned. Otherwise a value of FALSE is returned.
  64. --*/
  65. {
  66. DISPATCHER_CONTEXT DispatcherContext;
  67. EXCEPTION_DISPOSITION Disposition;
  68. PEXCEPTION_REGISTRATION_RECORD RegistrationPointer;
  69. PEXCEPTION_REGISTRATION_RECORD NestedRegistration;
  70. ULONG HighAddress;
  71. ULONG HighLimit;
  72. ULONG LowLimit;
  73. EXCEPTION_RECORD ExceptionRecord1;
  74. #if !defined(WX86_i386)
  75. ULONG Index;
  76. #endif
  77. #ifndef NTOS_KERNEL_RUNTIME
  78. if (RtlCallVectoredExceptionHandlers(ExceptionRecord,ContextRecord)) {
  79. return TRUE;
  80. }
  81. #endif // NTOS_KERNEL_RUNTIME
  82. #if DBG && !defined(NTOS_KERNEL_RUNTIME)
  83. //
  84. // Check if conditions are ripe to catch an access violation before any
  85. // custom try/except (page heap enabled, process under debugger, etc.).
  86. //
  87. {
  88. extern ULONG RtlpDebugPageHeap;
  89. if (RtlpDebugPageHeap) {
  90. if (ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION) {
  91. if (NtCurrentPeb()->BeingDebugged) {
  92. if (ExceptionRecord->NumberParameters > 1) {
  93. if (ExceptionRecord->ExceptionInformation[1] > 0x10000) {
  94. DbgPrint ("Page heap: first chance access violation (.exr %p, .cxr %p)\n",
  95. ExceptionRecord,
  96. ContextRecord);
  97. DbgBreakPoint ();
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. #endif
  105. //
  106. // Get current stack limits.
  107. //
  108. RtlpGetStackLimits(&LowLimit, &HighLimit);
  109. //
  110. // Start with the frame specified by the context record and search
  111. // backwards through the call frame hierarchy attempting to find an
  112. // exception handler that will handler the exception.
  113. //
  114. RegistrationPointer = RtlpGetRegistrationHead();
  115. NestedRegistration = 0;
  116. while (RegistrationPointer != EXCEPTION_CHAIN_END) {
  117. //
  118. // If the call frame is not within the specified stack limits or the
  119. // call frame is unaligned, then set the stack invalid flag in the
  120. // exception record and return FALSE. Else check to determine if the
  121. // frame has an exception handler.
  122. //
  123. HighAddress = (ULONG)RegistrationPointer +
  124. sizeof(EXCEPTION_REGISTRATION_RECORD);
  125. if ( ((ULONG)RegistrationPointer < LowLimit) ||
  126. (HighAddress > HighLimit) ||
  127. (((ULONG)RegistrationPointer & 0x3) != 0)
  128. #if !defined(NTOS_KERNEL_RUNTIME)
  129. ||
  130. (((ULONG)RegistrationPointer->Handler >= LowLimit) && ((ULONG)RegistrationPointer->Handler < HighLimit))
  131. #endif
  132. ) {
  133. #if defined(NTOS_KERNEL_RUNTIME)
  134. //
  135. // Allow for the possibility that the problem occured on the
  136. // DPC stack.
  137. //
  138. ULONG TestAddress = (ULONG)RegistrationPointer;
  139. if (((TestAddress & 0x3) == 0) &&
  140. KeGetCurrentIrql() >= DISPATCH_LEVEL) {
  141. PKPRCB Prcb = KeGetCurrentPrcb();
  142. ULONG DpcStack = (ULONG)Prcb->DpcStack;
  143. if ((Prcb->DpcRoutineActive) &&
  144. (HighAddress <= DpcStack) &&
  145. (TestAddress >= DpcStack - KERNEL_STACK_SIZE)) {
  146. //
  147. // This error occured on the DPC stack, switch
  148. // stack limits to the DPC stack and restart
  149. // the loop.
  150. //
  151. HighLimit = DpcStack;
  152. LowLimit = DpcStack - KERNEL_STACK_SIZE;
  153. continue;
  154. }
  155. }
  156. #endif
  157. ExceptionRecord->ExceptionFlags |= EXCEPTION_STACK_INVALID;
  158. return FALSE;
  159. }
  160. #if !defined(WX86_i386)
  161. //
  162. // The handler must be executed by calling another routine
  163. // that is written in assembler. This is required because
  164. // up level addressing of the handler information is required
  165. // when a nested exception is encountered.
  166. //
  167. if (NtGlobalFlag & FLG_ENABLE_EXCEPTION_LOGGING) {
  168. Index = RtlpLogExceptionHandler(
  169. ExceptionRecord,
  170. ContextRecord,
  171. 0,
  172. (PULONG)RegistrationPointer,
  173. 4 * sizeof(ULONG));
  174. // can't use sizeof(EXCEPTION_REGISTRATION_RECORD
  175. // because we need the 2 dwords above it.
  176. }
  177. #endif
  178. Disposition = RtlpExecuteHandlerForException(
  179. ExceptionRecord,
  180. (PVOID)RegistrationPointer,
  181. ContextRecord,
  182. (PVOID)&DispatcherContext,
  183. (PEXCEPTION_ROUTINE)RegistrationPointer->Handler);
  184. #if !defined(WX86_i386)
  185. if (NtGlobalFlag & FLG_ENABLE_EXCEPTION_LOGGING) {
  186. RtlpLogLastExceptionDisposition(Index, Disposition);
  187. }
  188. #endif
  189. //
  190. // If the current scan is within a nested context and the frame
  191. // just examined is the end of the context region, then clear
  192. // the nested context frame and the nested exception in the
  193. // exception flags.
  194. //
  195. if (NestedRegistration == RegistrationPointer) {
  196. ExceptionRecord->ExceptionFlags &= (~EXCEPTION_NESTED_CALL);
  197. NestedRegistration = 0;
  198. }
  199. //
  200. // Case on the handler disposition.
  201. //
  202. switch (Disposition) {
  203. //
  204. // The disposition is to continue execution. If the
  205. // exception is not continuable, then raise the exception
  206. // STATUS_NONCONTINUABLE_EXCEPTION. Otherwise return
  207. // TRUE.
  208. //
  209. case ExceptionContinueExecution :
  210. if ((ExceptionRecord->ExceptionFlags &
  211. EXCEPTION_NONCONTINUABLE) != 0) {
  212. ExceptionRecord1.ExceptionCode =
  213. STATUS_NONCONTINUABLE_EXCEPTION;
  214. ExceptionRecord1.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
  215. ExceptionRecord1.ExceptionRecord = ExceptionRecord;
  216. ExceptionRecord1.NumberParameters = 0;
  217. RtlRaiseException(&ExceptionRecord1);
  218. } else {
  219. return TRUE;
  220. }
  221. //
  222. // The disposition is to continue the search. Get next
  223. // frame address and continue the search.
  224. //
  225. case ExceptionContinueSearch :
  226. break;
  227. //
  228. // The disposition is nested exception. Set the nested
  229. // context frame to the establisher frame address and set
  230. // nested exception in the exception flags.
  231. //
  232. case ExceptionNestedException :
  233. ExceptionRecord->ExceptionFlags |= EXCEPTION_NESTED_CALL;
  234. if (DispatcherContext.RegistrationPointer > NestedRegistration) {
  235. NestedRegistration = DispatcherContext.RegistrationPointer;
  236. }
  237. break;
  238. //
  239. // All other disposition values are invalid. Raise
  240. // invalid disposition exception.
  241. //
  242. default :
  243. ExceptionRecord1.ExceptionCode = STATUS_INVALID_DISPOSITION;
  244. ExceptionRecord1.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
  245. ExceptionRecord1.ExceptionRecord = ExceptionRecord;
  246. ExceptionRecord1.NumberParameters = 0;
  247. RtlRaiseException(&ExceptionRecord1);
  248. break;
  249. }
  250. //
  251. // If chain goes in wrong direction or loops, report an
  252. // invalid exception stack, otherwise go on to the next one.
  253. //
  254. RegistrationPointer = RegistrationPointer->Next;
  255. }
  256. return FALSE;
  257. }
  258. #ifdef _X86_
  259. #pragma optimize("y", off) // RtlCaptureContext needs EBP to be correct
  260. #endif
  261. VOID
  262. RtlUnwind (
  263. IN PVOID TargetFrame OPTIONAL,
  264. IN PVOID TargetIp OPTIONAL,
  265. IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL,
  266. IN PVOID ReturnValue
  267. )
  268. /*++
  269. Routine Description:
  270. This function initiates an unwind of procedure call frames. The machine
  271. state at the time of the call to unwind is captured in a context record
  272. and the unwinding flag is set in the exception flags of the exception
  273. record. If the TargetFrame parameter is not specified, then the exit unwind
  274. flag is also set in the exception flags of the exception record. A backward
  275. walk through the procedure call frames is then performed to find the target
  276. of the unwind operation.
  277. N.B. The captured context passed to unwinding handlers will not be
  278. a completely accurate context set for the 386. This is because
  279. there isn't a standard stack frame in which registers are stored.
  280. Only the integer registers are affected. The segement and
  281. control registers (ebp, esp) will have correct values for
  282. the flat 32 bit environment.
  283. N.B. If you change the number of arguments, make sure you change the
  284. adjustment of ESP after the call to RtlpCaptureContext (for
  285. STDCALL calling convention)
  286. Arguments:
  287. TargetFrame - Supplies an optional pointer to the call frame that is the
  288. target of the unwind. If this parameter is not specified, then an exit
  289. unwind is performed.
  290. TargetIp - Supplies an optional instruction address that specifies the
  291. continuation address of the unwind. This address is ignored if the
  292. target frame parameter is not specified.
  293. ExceptionRecord - Supplies an optional pointer to an exception record.
  294. ReturnValue - Supplies a value that is to be placed in the integer
  295. function return register just before continuing execution.
  296. Return Value:
  297. None.
  298. --*/
  299. {
  300. PCONTEXT ContextRecord;
  301. CONTEXT ContextRecord1;
  302. DISPATCHER_CONTEXT DispatcherContext;
  303. EXCEPTION_DISPOSITION Disposition;
  304. PEXCEPTION_REGISTRATION_RECORD RegistrationPointer;
  305. PEXCEPTION_REGISTRATION_RECORD PriorPointer;
  306. ULONG HighAddress;
  307. ULONG HighLimit;
  308. ULONG LowLimit;
  309. EXCEPTION_RECORD ExceptionRecord1;
  310. EXCEPTION_RECORD ExceptionRecord2;
  311. //
  312. // Get current stack limits.
  313. //
  314. RtlpGetStackLimits(&LowLimit, &HighLimit);
  315. //
  316. // If an exception record is not specified, then build a local exception
  317. // record for use in calling exception handlers during the unwind operation.
  318. //
  319. if (ARGUMENT_PRESENT(ExceptionRecord) == FALSE) {
  320. ExceptionRecord = &ExceptionRecord1;
  321. ExceptionRecord1.ExceptionCode = STATUS_UNWIND;
  322. ExceptionRecord1.ExceptionFlags = 0;
  323. ExceptionRecord1.ExceptionRecord = NULL;
  324. ExceptionRecord1.ExceptionAddress = _ReturnAddress();
  325. ExceptionRecord1.NumberParameters = 0;
  326. }
  327. //
  328. // If the target frame of the unwind is specified, then set EXCEPTION_UNWINDING
  329. // flag in the exception flags. Otherwise set both EXCEPTION_EXIT_UNWIND and
  330. // EXCEPTION_UNWINDING flags in the exception flags.
  331. //
  332. if (ARGUMENT_PRESENT(TargetFrame) == TRUE) {
  333. ExceptionRecord->ExceptionFlags |= EXCEPTION_UNWINDING;
  334. } else {
  335. ExceptionRecord->ExceptionFlags |= (EXCEPTION_UNWINDING |
  336. EXCEPTION_EXIT_UNWIND);
  337. }
  338. //
  339. // Capture the context.
  340. //
  341. ContextRecord = &ContextRecord1;
  342. ContextRecord1.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL | CONTEXT_SEGMENTS;
  343. RtlpCaptureContext(ContextRecord);
  344. #ifdef STD_CALL
  345. //
  346. // Adjust captured context to pop our arguments off the stack
  347. //
  348. ContextRecord->Esp += sizeof(TargetFrame) +
  349. sizeof(TargetIp) +
  350. sizeof(ExceptionRecord) +
  351. sizeof(ReturnValue);
  352. #endif
  353. ContextRecord->Eax = (ULONG)ReturnValue;
  354. //
  355. // Scan backward through the call frame hierarchy, calling exception
  356. // handlers as they are encountered, until the target frame of the unwind
  357. // is reached.
  358. //
  359. RegistrationPointer = RtlpGetRegistrationHead();
  360. while (RegistrationPointer != EXCEPTION_CHAIN_END) {
  361. //
  362. // If this is the target of the unwind, then continue execution
  363. // by calling the continue system service.
  364. //
  365. if ((ULONG)RegistrationPointer == (ULONG)TargetFrame) {
  366. ZwContinue(ContextRecord, FALSE);
  367. //
  368. // If the target frame is lower in the stack than the current frame,
  369. // then raise STATUS_INVALID_UNWIND exception.
  370. //
  371. } else if ( (ARGUMENT_PRESENT(TargetFrame) == TRUE) &&
  372. ((ULONG)TargetFrame < (ULONG)RegistrationPointer) ) {
  373. ExceptionRecord2.ExceptionCode = STATUS_INVALID_UNWIND_TARGET;
  374. ExceptionRecord2.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
  375. ExceptionRecord2.ExceptionRecord = ExceptionRecord;
  376. ExceptionRecord2.NumberParameters = 0;
  377. RtlRaiseException(&ExceptionRecord2);
  378. }
  379. //
  380. // If the call frame is not within the specified stack limits or the
  381. // call frame is unaligned, then raise the exception STATUS_BAD_STACK.
  382. // Else restore the state from the specified frame to the context
  383. // record.
  384. //
  385. HighAddress = (ULONG)RegistrationPointer +
  386. sizeof(EXCEPTION_REGISTRATION_RECORD);
  387. if ( ((ULONG)RegistrationPointer < LowLimit) ||
  388. (HighAddress > HighLimit) ||
  389. (((ULONG)RegistrationPointer & 0x3) != 0)
  390. #if !defined(NTOS_KERNEL_RUNTIME)
  391. ||
  392. (((ULONG)RegistrationPointer->Handler >= LowLimit) && ((ULONG)RegistrationPointer->Handler < HighLimit))
  393. #endif
  394. ) {
  395. #if defined(NTOS_KERNEL_RUNTIME)
  396. //
  397. // Allow for the possibility that the problem occured on the
  398. // DPC stack.
  399. //
  400. ULONG TestAddress = (ULONG)RegistrationPointer;
  401. if (((TestAddress & 0x3) == 0) &&
  402. KeGetCurrentIrql() >= DISPATCH_LEVEL) {
  403. PKPRCB Prcb = KeGetCurrentPrcb();
  404. ULONG DpcStack = (ULONG)Prcb->DpcStack;
  405. if ((Prcb->DpcRoutineActive) &&
  406. (HighAddress <= DpcStack) &&
  407. (TestAddress >= DpcStack - KERNEL_STACK_SIZE)) {
  408. //
  409. // This error occured on the DPC stack, switch
  410. // stack limits to the DPC stack and restart
  411. // the loop.
  412. //
  413. HighLimit = DpcStack;
  414. LowLimit = DpcStack - KERNEL_STACK_SIZE;
  415. continue;
  416. }
  417. }
  418. #endif
  419. ExceptionRecord2.ExceptionCode = STATUS_BAD_STACK;
  420. ExceptionRecord2.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
  421. ExceptionRecord2.ExceptionRecord = ExceptionRecord;
  422. ExceptionRecord2.NumberParameters = 0;
  423. RtlRaiseException(&ExceptionRecord2);
  424. } else {
  425. //
  426. // The handler must be executed by calling another routine
  427. // that is written in assembler. This is required because
  428. // up level addressing of the handler information is required
  429. // when a collided unwind is encountered.
  430. //
  431. Disposition = RtlpExecuteHandlerForUnwind(
  432. ExceptionRecord,
  433. (PVOID)RegistrationPointer,
  434. ContextRecord,
  435. (PVOID)&DispatcherContext,
  436. RegistrationPointer->Handler);
  437. //
  438. // Case on the handler disposition.
  439. //
  440. switch (Disposition) {
  441. //
  442. // The disposition is to continue the search. Get next
  443. // frame address and continue the search.
  444. //
  445. case ExceptionContinueSearch :
  446. break;
  447. //
  448. // The disposition is colided unwind. Maximize the target
  449. // of the unwind and change the context record pointer.
  450. //
  451. case ExceptionCollidedUnwind :
  452. //
  453. // Pick up the registration pointer that was active at
  454. // the time of the unwind, and simply continue.
  455. //
  456. RegistrationPointer = DispatcherContext.RegistrationPointer;
  457. break;
  458. //
  459. // All other disposition values are invalid. Raise
  460. // invalid disposition exception.
  461. //
  462. default :
  463. ExceptionRecord2.ExceptionCode = STATUS_INVALID_DISPOSITION;
  464. ExceptionRecord2.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
  465. ExceptionRecord2.ExceptionRecord = ExceptionRecord;
  466. ExceptionRecord2.NumberParameters = 0;
  467. RtlRaiseException(&ExceptionRecord2);
  468. break;
  469. }
  470. //
  471. // Step to next registration record
  472. //
  473. PriorPointer = RegistrationPointer;
  474. RegistrationPointer = RegistrationPointer->Next;
  475. //
  476. // Unlink the unwind handler, since it's been called.
  477. //
  478. RtlpUnlinkHandler(PriorPointer);
  479. //
  480. // If chain goes in wrong direction or loops, raise an
  481. // exception.
  482. //
  483. }
  484. }
  485. if (TargetFrame == EXCEPTION_CHAIN_END) {
  486. //
  487. // Caller simply wants to unwind all exception records.
  488. // This differs from an exit_unwind in that no "exit" is desired.
  489. // Do a normal continue, since we've effectively found the
  490. // "target" the caller wanted.
  491. //
  492. ZwContinue(ContextRecord, FALSE);
  493. } else {
  494. //
  495. // Either (1) a real exit unwind was performed, or (2) the
  496. // specified TargetFrame is not present in the exception handler
  497. // list. In either case, give debugger and subsystem a chance
  498. // to see the unwind.
  499. //
  500. ZwRaiseException(ExceptionRecord, ContextRecord, FALSE);
  501. }
  502. return;
  503. }
  504. #ifdef _X86_
  505. #pragma optimize("", on)
  506. #endif