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.

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