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.

2206 lines
73 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. exdsptch.c
  5. Abstract:
  6. This module implements the dispatching of exceptions and the unwinding of
  7. procedure call frames.
  8. Author:
  9. William K. Cheung (wcheung) 23-Dec-1995
  10. based on the version by David N. Cutler (davec) 11-Sep-1990
  11. Environment:
  12. Any mode.
  13. Revision History:
  14. ATM Shafiqul Khalid [askhalid] 8-23-99
  15. Added RtlAddFunctionTable and RtlDeleteFunctionTable
  16. --*/
  17. #include "ntrtlp.h"
  18. #if defined(NTOS_KERNEL_RUNTIME)
  19. //
  20. // Define function address table for kernel mode.
  21. //
  22. // This table is used to initialize the global history table.
  23. //
  24. VOID
  25. KiDispatchException (
  26. IN PEXCEPTION_RECORD ExceptionRecord,
  27. IN PKEXCEPTION_FRAME ExceptionFrame,
  28. IN PKTRAP_FRAME TrapFrame,
  29. IN KPROCESSOR_MODE PreviousMode,
  30. IN BOOLEAN FirstChance
  31. );
  32. VOID
  33. KiExceptionDispatch (
  34. VOID
  35. );
  36. PVOID RtlpFunctionAddressTable[] = {
  37. &KiExceptionDispatch,
  38. &KiDispatchException,
  39. &RtlDispatchException,
  40. &RtlpExecuteEmHandlerForException,
  41. &__C_specific_handler,
  42. &RtlUnwindEx,
  43. NULL
  44. };
  45. #else
  46. VOID
  47. KiUserExceptionDispatch (
  48. VOID
  49. );
  50. PVOID RtlpFunctionAddressTable[] = {
  51. &KiUserExceptionDispatch,
  52. &RtlDispatchException,
  53. &RtlpExecuteEmHandlerForException,
  54. &__C_specific_handler,
  55. &RtlUnwindEx,
  56. NULL
  57. };
  58. #endif
  59. PRUNTIME_FUNCTION
  60. RtlLookupStaticFunctionEntry(
  61. IN ULONG_PTR ControlPc,
  62. OUT PBOOLEAN InImage
  63. );
  64. PRUNTIME_FUNCTION
  65. RtlLookupDynamicFunctionEntry(
  66. IN ULONG_PTR ControlPc,
  67. OUT PULONGLONG ImageBase,
  68. OUT PULONGLONG TargetGp
  69. );
  70. //
  71. // Define local macros.
  72. //
  73. // Raise noncontinuable exception with associated exception record.
  74. //
  75. #define IS_HANDLER_DEFINED(f, base) \
  76. (f->UnwindInfoAddress && \
  77. (((PUNWIND_INFO)(base+f->UnwindInfoAddress))->Flags & 0x3))
  78. #define HANDLER(f, base, target) \
  79. (((PUNWIND_INFO)(base + f->UnwindInfoAddress))->Version <= 2) ? \
  80. ((PEXCEPTION_ROUTINE) \
  81. (*(PULONGLONG) ((LONGLONG)target + \
  82. (*(PULONGLONG) (base + f->UnwindInfoAddress + sizeof(UNWIND_INFO) + \
  83. (((PUNWIND_INFO) (base + f->UnwindInfoAddress))->DataLength * sizeof(ULONGLONG))))))) : \
  84. ((PEXCEPTION_ROUTINE) \
  85. (base + \
  86. (*(PULONG) (base + f->UnwindInfoAddress + sizeof(UNWIND_INFO) + \
  87. (((PUNWIND_INFO) (base + f->UnwindInfoAddress))->DataLength * sizeof(ULONGLONG))))))
  88. #define RAISE_EXCEPTION(Status, ExceptionRecordt) { \
  89. EXCEPTION_RECORD ExceptionRecordn; \
  90. \
  91. ExceptionRecordn.ExceptionCode = Status; \
  92. ExceptionRecordn.ExceptionFlags = EXCEPTION_NONCONTINUABLE; \
  93. ExceptionRecordn.ExceptionRecord = ExceptionRecordt; \
  94. ExceptionRecordn.NumberParameters = 0; \
  95. RtlRaiseException(&ExceptionRecordn); \
  96. }
  97. #define IS_SAME_FRAME(Frame1, Frame2) \
  98. ( (Frame1.MemoryStackFp == Frame2.MemoryStackFp) && \
  99. (Frame1.BackingStoreFp == Frame2.BackingStoreFp) )
  100. #define INITIALIZE_FRAME(Frame) \
  101. Frame.MemoryStackFp = Frame.BackingStoreFp = 0
  102. #define CHECK_MSTACK_FRAME(Establisher, Target) \
  103. ((Establisher.MemoryStackFp < LowStackLimit) || \
  104. (Establisher.MemoryStackFp > HighStackLimit) || \
  105. ((Target.MemoryStackFp != 0) && \
  106. (Target.MemoryStackFp < Establisher.MemoryStackFp)) || \
  107. ((Establisher.MemoryStackFp & 0x3) != 0))
  108. #define CHECK_BSTORE_FRAME(Establisher, Target) \
  109. ((Establisher.BackingStoreFp < LowBStoreLimit) || \
  110. (Establisher.BackingStoreFp > HighBStoreLimit) || \
  111. ((Target.BackingStoreFp != 0) && \
  112. (Target.BackingStoreFp > Establisher.BackingStoreFp)) || \
  113. ((Establisher.BackingStoreFp & 0x7) != 0))
  114. VOID
  115. RtlpCopyContext (
  116. OUT PCONTEXT Destination,
  117. IN PCONTEXT Source
  118. );
  119. ULONGLONG
  120. RtlpVirtualUnwind (
  121. IN ULONGLONG ImageBase,
  122. IN ULONGLONG ControlPc,
  123. IN PRUNTIME_FUNCTION FunctionEntry,
  124. IN PCONTEXT ContextRecord,
  125. OUT PBOOLEAN InFunction,
  126. OUT PFRAME_POINTERS EstablisherFrame,
  127. IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
  128. );
  129. PRUNTIME_FUNCTION
  130. RtlpLookupFunctionEntry (
  131. IN ULONGLONG ControlPc,
  132. OUT PULONGLONG ImageBase,
  133. OUT PULONGLONG TargetGp,
  134. IN OUT PUNWIND_HISTORY_TABLE HistoryTable OPTIONAL
  135. );
  136. PRUNTIME_FUNCTION
  137. RtlLookupFunctionEntry (
  138. IN ULONGLONG ControlPc,
  139. OUT PULONGLONG ImageBase,
  140. OUT PULONGLONG TargetGp
  141. )
  142. /*++
  143. Routine Description:
  144. This function searches the currently active function tables for an
  145. entry that corresponds to the specified PC value.
  146. Arguments:
  147. ControlPc - Supplies the virtual address of an instruction bundle
  148. within the specified function.
  149. ImageBase - Returns the base address of the module to which the
  150. function belongs.
  151. TargetGp - Returns the global pointer value of the module.
  152. Return Value:
  153. If there is no entry in the function table for the specified PC, then
  154. NULL is returned. Otherwise, the address of the function table entry
  155. that corresponds to the specified PC is returned.
  156. --*/
  157. {
  158. return RtlpLookupFunctionEntry ( ControlPc,
  159. ImageBase,
  160. TargetGp,
  161. NULL
  162. );
  163. }
  164. PRUNTIME_FUNCTION
  165. RtlpLookupFunctionEntry (
  166. IN ULONGLONG ControlPc,
  167. OUT PULONGLONG ImageBase,
  168. OUT PULONGLONG TargetGp,
  169. IN OUT PUNWIND_HISTORY_TABLE HistoryTable OPTIONAL
  170. )
  171. /*++
  172. Routine Description:
  173. This function searches the currently active function tables for an
  174. entry that corresponds to the specified PC value.
  175. Arguments:
  176. ControlPc - Supplies the virtual address of an instruction bundle
  177. within the specified function.
  178. ImageBase - Returns the base address of the module to which the
  179. function belongs.
  180. TargetGp - Returns the global pointer value of the module.
  181. HistoryTable - Supplies an optional pointer to an unwind history table.
  182. Return Value:
  183. If there is no entry in the function table for the specified PC, then
  184. NULL is returned. Otherwise, the address of the function table entry
  185. that corresponds to the specified PC is returned.
  186. --*/
  187. {
  188. ULONG64 BaseAddress;
  189. ULONG64 BeginAddress;
  190. ULONG64 EndAddress;
  191. PRUNTIME_FUNCTION FunctionEntry;
  192. PRUNTIME_FUNCTION FunctionTable;
  193. LONG High;
  194. ULONG Index;
  195. LONG Middle;
  196. LONG Low;
  197. ULONG RelativePc;
  198. ULONG Size;
  199. ULONG SizeOfExceptionTable;
  200. //
  201. // Attempt to find an image that contains the specified control PC. If
  202. // an image is found, then search its function table for a function table
  203. // entry that contains the specified control PC. If an image is not found
  204. // then search the dynamic function table for an image that contains the
  205. // specified control PC.
  206. //
  207. // If a history table is supplied and search is specfied, then the current
  208. // operation that is being performed is the unwind phase of an exception
  209. // dispatch followed by a unwind.
  210. //
  211. if ((ARGUMENT_PRESENT(HistoryTable)) &&
  212. (HistoryTable->Search != UNWIND_HISTORY_TABLE_NONE)) {
  213. //
  214. // Search the global unwind history table if there is a chance of a
  215. // match.
  216. //
  217. if (HistoryTable->Search == UNWIND_HISTORY_TABLE_GLOBAL) {
  218. if ((ControlPc >= RtlpUnwindHistoryTable.LowAddress) &&
  219. (ControlPc < RtlpUnwindHistoryTable.HighAddress)) {
  220. for (Index = 0; Index < RtlpUnwindHistoryTable.Count; Index += 1) {
  221. BaseAddress = RtlpUnwindHistoryTable.Entry[Index].ImageBase;
  222. FunctionEntry = RtlpUnwindHistoryTable.Entry[Index].FunctionEntry;
  223. BeginAddress = FunctionEntry->BeginAddress + BaseAddress;
  224. EndAddress = FunctionEntry->EndAddress + BaseAddress;
  225. if ((ControlPc >= BeginAddress) && (ControlPc < EndAddress)) {
  226. *ImageBase = BaseAddress;
  227. *TargetGp = RtlpUnwindHistoryTable.Entry[Index].Gp;
  228. return FunctionEntry;
  229. }
  230. }
  231. }
  232. HistoryTable->Search = UNWIND_HISTORY_TABLE_LOCAL;
  233. }
  234. //
  235. // Search the dynamic unwind history table if there is a chance of a
  236. // match.
  237. //
  238. if ((ControlPc >= HistoryTable->LowAddress) &&
  239. (ControlPc < HistoryTable->HighAddress)) {
  240. for (Index = 0; Index < HistoryTable->Count; Index += 1) {
  241. BaseAddress = HistoryTable->Entry[Index].ImageBase;
  242. FunctionEntry = HistoryTable->Entry[Index].FunctionEntry;
  243. BeginAddress = FunctionEntry->BeginAddress + BaseAddress;
  244. EndAddress = FunctionEntry->EndAddress + BaseAddress;
  245. if ((ControlPc >= BeginAddress) && (ControlPc < EndAddress)) {
  246. *ImageBase = BaseAddress;
  247. *TargetGp = HistoryTable->Entry[Index].Gp;
  248. return FunctionEntry;
  249. }
  250. }
  251. }
  252. }
  253. //
  254. // There was not a match in either of the unwind history tables so attempt
  255. // to find a matching entry in the loaded module list.
  256. //
  257. FunctionTable = RtlLookupFunctionTable(
  258. (PVOID)ControlPc,
  259. (PVOID *)ImageBase,
  260. TargetGp,
  261. &SizeOfExceptionTable
  262. );
  263. //
  264. // If a function table is located, then search the table for a
  265. // function table entry for the specified PC.
  266. //
  267. __try {
  268. if (FunctionTable != NULL) {
  269. //
  270. // Initialize search indices.
  271. //
  272. Low = 0;
  273. High = (SizeOfExceptionTable / sizeof(RUNTIME_FUNCTION)) - 1;
  274. RelativePc = (ULONG)(ControlPc - *ImageBase);
  275. //
  276. // Perform binary search on the function table for a function table
  277. // entry that subsumes the specified PC.
  278. //
  279. while (High >= Low) {
  280. //
  281. // Compute next probe index and test entry. If the specified PC
  282. // is greater than of equal to the beginning address and less
  283. // than the ending address of the function table entry, then
  284. // return the address of the function table entry. Otherwise,
  285. // continue the search.
  286. //
  287. Middle = (Low + High) >> 1;
  288. FunctionEntry = &FunctionTable[Middle];
  289. if (RelativePc < FunctionEntry->BeginAddress) {
  290. High = Middle - 1;
  291. } else if (RelativePc >= FunctionEntry->EndAddress) {
  292. Low = Middle + 1;
  293. } else {
  294. break;
  295. }
  296. }
  297. if (High < Low) {
  298. FunctionEntry = NULL;
  299. }
  300. } else {
  301. //
  302. // There was not a match in the loaded module list so attempt to find
  303. // a matching entry in the dynamic function table list.
  304. //
  305. #if !defined(NTOS_KERNEL_RUNTIME)
  306. FunctionEntry = RtlLookupDynamicFunctionEntry(ControlPc,
  307. ImageBase,
  308. TargetGp);
  309. #else
  310. FunctionEntry = NULL;
  311. #endif // NTOS_KERNEL_RUNTIME
  312. }
  313. } except(EXCEPTION_EXECUTE_HANDLER) {
  314. DbgPrint("NTDLL:RtlpLookupFunctionEntry exception occured during function lookup. Status = %lx\n", GetExceptionCode());
  315. FunctionEntry = NULL;
  316. }
  317. //
  318. // If a function table entry was located, search is not specified, and
  319. // the specfied history table is not full, then attempt to make an entry
  320. // in the history table.
  321. //
  322. if (FunctionEntry != NULL) {
  323. if (ARGUMENT_PRESENT(HistoryTable) &&
  324. (HistoryTable->Search == UNWIND_HISTORY_TABLE_NONE) &&
  325. (HistoryTable->Count < UNWIND_HISTORY_TABLE_SIZE)) {
  326. Index = HistoryTable->Count;
  327. HistoryTable->Count += 1;
  328. HistoryTable->Entry[Index].ImageBase = *ImageBase;
  329. HistoryTable->Entry[Index].Gp = *TargetGp;
  330. HistoryTable->Entry[Index].FunctionEntry = FunctionEntry;
  331. BeginAddress = FunctionEntry->BeginAddress + *ImageBase;
  332. EndAddress = FunctionEntry->EndAddress + *ImageBase;
  333. if (BeginAddress < HistoryTable->LowAddress) {
  334. HistoryTable->LowAddress = BeginAddress;
  335. }
  336. if (EndAddress > HistoryTable->HighAddress) {
  337. HistoryTable->HighAddress = EndAddress;
  338. }
  339. }
  340. }
  341. return FunctionEntry;
  342. }
  343. VOID
  344. RtlpRaiseException (
  345. IN PEXCEPTION_RECORD ExceptionRecord
  346. )
  347. /*++
  348. Routine Description:
  349. This function raises a software exception by building a context record
  350. and calling the raise exception system service.
  351. Arguments:
  352. ExceptionRecord - Supplies a pointer to an exception record.
  353. Return Value:
  354. None.
  355. --*/
  356. {
  357. ULONGLONG ImageBase;
  358. ULONGLONG TargetGp;
  359. ULONGLONG ControlPc;
  360. CONTEXT ContextRecord;
  361. FRAME_POINTERS EstablisherFrame;
  362. PRUNTIME_FUNCTION FunctionEntry;
  363. BOOLEAN InFunction;
  364. ULONGLONG NextPc;
  365. NTSTATUS Status;
  366. //
  367. // Capture the current context, virtually unwind to the caller of this
  368. // routine, set the fault instruction address to that of the caller, and
  369. // call the raise exception system service.
  370. //
  371. RtlCaptureNonVolatileContext(&ContextRecord);
  372. ControlPc = RtlIa64InsertIPSlotNumber((ContextRecord.BrRp-16), 2);
  373. FunctionEntry = RtlpLookupFunctionEntry(ControlPc,
  374. &ImageBase,
  375. &TargetGp,
  376. NULL);
  377. NextPc = RtlVirtualUnwind(ImageBase,
  378. ControlPc,
  379. FunctionEntry,
  380. &ContextRecord,
  381. &InFunction,
  382. &EstablisherFrame,
  383. NULL);
  384. ContextRecord.StIIP = NextPc + 8;
  385. ContextRecord.StIPSR &= ~((ULONGLONG) 3 << PSR_RI);
  386. ExceptionRecord->ExceptionAddress = (PVOID)ContextRecord.StIIP;
  387. #if defined(NTOS_KERNEL_RUNTIME)
  388. if (RtlDispatchException(ExceptionRecord, &ContextRecord) != FALSE) {
  389. return;
  390. }
  391. Status = ZwRaiseException(ExceptionRecord, &ContextRecord, FALSE);
  392. #else
  393. if (ZwQueryPortInformationProcess() == FALSE) {
  394. if (RtlDispatchException(ExceptionRecord, &ContextRecord) != FALSE) {
  395. return;
  396. }
  397. } else {
  398. Status = ZwRaiseException(ExceptionRecord, &ContextRecord, TRUE);
  399. }
  400. #endif
  401. //
  402. // There should never be a return from either exception dispatch or the
  403. // system service unless there is a problem with the argument list itself.
  404. // Raise another exception specifying the status value returned.
  405. //
  406. RtlRaiseStatus(Status);
  407. return;
  408. }
  409. VOID
  410. RtlRaiseException (
  411. IN PEXCEPTION_RECORD ExceptionRecord
  412. )
  413. /*++
  414. Routine Description:
  415. This function raises a software exception by building a context record
  416. and calling the raise exception system service.
  417. N.B. This routine is a shell routine that simply calls another routine
  418. to do the real work. The reason this is done is to avoid a problem
  419. in try/finally scopes where the last statement in the scope is a
  420. call to raise an exception.
  421. Arguments:
  422. ExceptionRecord - Supplies a pointer to an exception record.
  423. Return Value:
  424. None.
  425. --*/
  426. {
  427. RtlpRaiseException(ExceptionRecord);
  428. return;
  429. }
  430. #pragma warning(disable:4717) // recursive function
  431. VOID
  432. RtlpRaiseStatus (
  433. IN NTSTATUS Status
  434. )
  435. /*++
  436. Routine Description:
  437. This function raises an exception with the specified status value. The
  438. exception is marked as noncontinuable with no parameters.
  439. Arguments:
  440. Status - Supplies the status value to be used as the exception code
  441. for the exception that is to be raised.
  442. Return Value:
  443. None.
  444. --*/
  445. {
  446. ULONGLONG ImageBase;
  447. ULONGLONG TargetGp;
  448. ULONGLONG ControlPc;
  449. ULONGLONG NextPc;
  450. CONTEXT ContextRecord;
  451. FRAME_POINTERS EstablisherFrame;
  452. EXCEPTION_RECORD ExceptionRecord;
  453. PRUNTIME_FUNCTION FunctionEntry;
  454. BOOLEAN InFunction;
  455. //
  456. // Construct an exception record.
  457. //
  458. ExceptionRecord.ExceptionCode = Status;
  459. ExceptionRecord.ExceptionRecord = (PEXCEPTION_RECORD)NULL;
  460. ExceptionRecord.NumberParameters = 0;
  461. ExceptionRecord.ExceptionFlags = EXCEPTION_NONCONTINUABLE;
  462. //
  463. // Capture the current context, virtually unwind to the caller of this
  464. // routine, set the fault instruction address to that of the caller, and
  465. // call the raise exception system service.
  466. //
  467. RtlCaptureNonVolatileContext(&ContextRecord);
  468. ControlPc = RtlIa64InsertIPSlotNumber((ContextRecord.BrRp-16), 2);
  469. FunctionEntry = RtlpLookupFunctionEntry(ControlPc,
  470. &ImageBase,
  471. &TargetGp,
  472. NULL);
  473. NextPc = RtlVirtualUnwind(ImageBase,
  474. ControlPc,
  475. FunctionEntry,
  476. &ContextRecord,
  477. &InFunction,
  478. &EstablisherFrame,
  479. NULL);
  480. ContextRecord.StIIP = NextPc + 8;
  481. ContextRecord.StIPSR &= ~((ULONGLONG) 3 << PSR_RI);
  482. ExceptionRecord.ExceptionAddress = (PVOID)ContextRecord.StIIP;
  483. #if defined(NTOS_KERNEL_RUNTIME)
  484. RtlDispatchException(&ExceptionRecord, &ContextRecord);
  485. Status = ZwRaiseException(&ExceptionRecord, &ContextRecord, FALSE);
  486. #else
  487. //
  488. // Attempt to dispatch the exception.
  489. //
  490. // N.B. This exception is non-continuable.
  491. //
  492. if (ZwQueryPortInformationProcess() == FALSE) {
  493. RtlDispatchException(&ExceptionRecord, &ContextRecord);
  494. Status = ZwRaiseException(&ExceptionRecord, &ContextRecord, FALSE);
  495. } else {
  496. Status = ZwRaiseException(&ExceptionRecord, &ContextRecord, TRUE);
  497. }
  498. #endif
  499. //
  500. // There should never be a return from either exception dispatch or the
  501. // system service unless there is a problem with the argument list itself.
  502. // Raise another exception specifying the status value returned.
  503. //
  504. RtlRaiseStatus(Status);
  505. return;
  506. }
  507. VOID
  508. RtlRaiseStatus (
  509. IN NTSTATUS Status
  510. )
  511. /*++
  512. Routine Description:
  513. This function raises an exception with the specified status value. The
  514. exception is marked as noncontinuable with no parameters.
  515. N.B. This routine is a shell routine that simply calls another routine
  516. to do the real work. The reason this is done is to avoid a problem
  517. in try/finally scopes where the last statement in the scope is a
  518. call to raise an exception.
  519. Arguments:
  520. Status - Supplies the status value to be used as the exception code
  521. for the exception that is to be raised.
  522. Return Value:
  523. None.
  524. --*/
  525. {
  526. RtlpRaiseStatus(Status);
  527. return;
  528. }
  529. VOID
  530. RtlUnwind (
  531. IN PVOID TargetFrame OPTIONAL,
  532. IN PVOID TargetIp OPTIONAL,
  533. IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL,
  534. IN PVOID ReturnValue
  535. )
  536. /*++
  537. Obsolete API in IA64.
  538. --*/
  539. {
  540. return;
  541. }
  542. VOID
  543. RtlUnwind2 (
  544. IN FRAME_POINTERS TargetFrame OPTIONAL,
  545. IN PVOID TargetIp OPTIONAL,
  546. IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL,
  547. IN PVOID ReturnValue,
  548. IN PCONTEXT OriginalContext
  549. )
  550. /*++
  551. Routine Description:
  552. This function initiates an unwind of procedure call frames. The machine
  553. state at the time of the call to unwind is captured in a context record
  554. and the unwinding flag is set in the exception flags of the exception
  555. record. If the TargetFrame parameter is not specified, then the exit unwind
  556. flag is also set in the exception flags of the exception record. A backward
  557. scan through the procedure call frames is then performed to find the target
  558. of the unwind operation.
  559. As each frame is encounter, the PC where control left the corresponding
  560. function is determined and used to lookup exception handler information
  561. in the runtime function table built by the linker. If the respective
  562. routine has an exception handler, then the handler is called.
  563. Arguments:
  564. TargetFrame - Supplies an optional pointer to the call frame that is the
  565. target of the unwind. If this parameter is not specified, then an exit
  566. unwind is performed.
  567. TargetIp - Supplies an optional instruction address that specifies the
  568. continuation address of the unwind. This address is ignored if the
  569. target frame parameter is not specified.
  570. ExceptionRecord - Supplies an optional pointer to an exception record.
  571. ReturnValue - Supplies a value that is to be placed in the integer
  572. function return register just before continuing execution.
  573. OriginalContext - Supplies a pointer to a context record that can be used
  574. to store context druing the unwind operation.
  575. Return Value:
  576. None.
  577. --*/
  578. {
  579. //
  580. // Call real unwind routine specifying a history table address as an extra
  581. // argument.
  582. //
  583. RtlUnwindEx(TargetFrame,
  584. TargetIp,
  585. ExceptionRecord,
  586. ReturnValue,
  587. OriginalContext,
  588. NULL);
  589. return;
  590. }
  591. VOID
  592. RtlUnwindEx (
  593. IN FRAME_POINTERS TargetFrame OPTIONAL,
  594. IN PVOID TargetIp OPTIONAL,
  595. IN PEXCEPTION_RECORD ExceptionRecord OPTIONAL,
  596. IN PVOID ReturnValue,
  597. IN PCONTEXT OriginalContext,
  598. IN PUNWIND_HISTORY_TABLE HistoryTable OPTIONAL
  599. )
  600. /*++
  601. Routine Description:
  602. This function initiates an unwind of procedure call frames. The machine
  603. state at the time of the call to unwind is captured in a context record
  604. and the unwinding flag is set in the exception flags of the exception
  605. record. If the TargetFrame parameter is not specified, then the exit unwind
  606. flag is also set in the exception flags of the exception record. A backward
  607. scan through the procedure call frames is then performed to find the target
  608. of the unwind operation.
  609. As each frame is encounter, the PC where control left the corresponding
  610. function is determined and used to lookup exception handler information
  611. in the runtime function table built by the linker. If the respective
  612. routine has an exception handler, then the handler is called.
  613. Arguments:
  614. TargetFrame - Supplies an optional pointer to the call frame that is the
  615. target of the unwind. If this parameter is not specified, then an exit
  616. unwind is performed.
  617. TargetIp - Supplies an optional instruction address that specifies the
  618. continuation address of the unwind. This address is ignored if the
  619. target frame parameter is not specified.
  620. ExceptionRecord - Supplies an optional pointer to an exception record.
  621. ReturnValue - Supplies a value that is to be placed in the integer
  622. function return register just before continuing execution.
  623. OriginalContext - Supplies a pointer to a context record that can be used
  624. to store context druing the unwind operation.
  625. HistoryTable - Supplies an optional pointer to an unwind history table.
  626. Return Value:
  627. None.
  628. --*/
  629. {
  630. PCONTEXT CurrentContext;
  631. ULONGLONG TargetGp;
  632. ULONGLONG ImageBase;
  633. ULONGLONG ControlPc;
  634. ULONGLONG NextPc;
  635. ULONG ExceptionFlags;
  636. DISPATCHER_CONTEXT DispatcherContext;
  637. EXCEPTION_DISPOSITION Disposition;
  638. FRAME_POINTERS EstablisherFrame;
  639. EXCEPTION_RECORD ExceptionRecord1;
  640. PRUNTIME_FUNCTION FunctionEntry;
  641. ULONGLONG HighStackLimit;
  642. ULONGLONG LowStackLimit;
  643. ULONGLONG HighBStoreLimit;
  644. ULONGLONG LowBStoreLimit;
  645. ULONG Size;
  646. BOOLEAN InFunction;
  647. PCONTEXT PreviousContext;
  648. PCONTEXT TempContext;
  649. CONTEXT LocalContext;
  650. //
  651. // Get current memory stack and backing store limits, capture the
  652. // current context, virtually unwind to the caller of this routine,
  653. // get the initial PC value, and set the unwind target address.
  654. //
  655. // N.B. The target gp value is found in the input context record.
  656. // The unwinder guarantees that it will not be destroyed
  657. // as it is just a scratch register.
  658. //
  659. CurrentContext = OriginalContext;
  660. PreviousContext = &LocalContext;
  661. RtlCaptureNonVolatileContext(CurrentContext);
  662. //
  663. // If a history table is specified, then set to search history table.
  664. //
  665. if (ARGUMENT_PRESENT(HistoryTable)) {
  666. HistoryTable->Search = UNWIND_HISTORY_TABLE_GLOBAL;
  667. }
  668. //
  669. // Before getting the limits from the TEB, must flush the RSE to have
  670. // the OS to grow the backing store and update the BStoreLimit.
  671. //
  672. Rtlp64GetStackLimits(&LowStackLimit, &HighStackLimit);
  673. Rtlp64GetBStoreLimits(&LowBStoreLimit, &HighBStoreLimit);
  674. ControlPc = RtlIa64InsertIPSlotNumber((CurrentContext->BrRp-16), 2);
  675. FunctionEntry = RtlpLookupFunctionEntry(ControlPc,
  676. &ImageBase,
  677. &TargetGp,
  678. HistoryTable);
  679. NextPc = RtlVirtualUnwind(ImageBase,
  680. ControlPc,
  681. FunctionEntry,
  682. CurrentContext,
  683. &InFunction,
  684. &EstablisherFrame,
  685. NULL);
  686. ControlPc = NextPc;
  687. CurrentContext->StIIP = (ULONGLONG)TargetIp;
  688. //
  689. // If an exception record is not specified, then build a local exception
  690. // record for use in calling exception handlers during the unwind operation.
  691. //
  692. if (ARGUMENT_PRESENT(ExceptionRecord) == FALSE) {
  693. ExceptionRecord = &ExceptionRecord1;
  694. ExceptionRecord1.ExceptionCode = STATUS_UNWIND;
  695. ExceptionRecord1.ExceptionRecord = NULL;
  696. ExceptionRecord1.ExceptionAddress = (PVOID)ControlPc;
  697. ExceptionRecord1.NumberParameters = 0;
  698. }
  699. //
  700. // If the target frame of the unwind is specified, then a normal unwind
  701. // is being performed. Otherwise, an exit unwind is being performed.
  702. //
  703. ExceptionFlags = EXCEPTION_UNWINDING;
  704. if (TargetFrame.BackingStoreFp == 0 && TargetFrame.MemoryStackFp == 0) {
  705. ExceptionRecord->ExceptionFlags |= EXCEPTION_EXIT_UNWIND;
  706. }
  707. //
  708. // Scan backward through the call frame hierarchy and call exception
  709. // handlers until the target frame of the unwind is reached.
  710. //
  711. do {
  712. //
  713. // Lookup the function table entry using the point at which control
  714. // left the procedure.
  715. //
  716. FunctionEntry = RtlpLookupFunctionEntry(ControlPc,
  717. &ImageBase,
  718. &TargetGp,
  719. HistoryTable);
  720. //
  721. // If there is a function table entry for the routine, then
  722. // virtually unwind to the caller of the routine to obtain the
  723. // virtual frame pointer of the establisher, but don't update
  724. // the context record.
  725. //
  726. if (FunctionEntry != NULL) {
  727. RtlpCopyContext(PreviousContext, CurrentContext);
  728. NextPc = RtlVirtualUnwind(ImageBase,
  729. ControlPc,
  730. FunctionEntry,
  731. PreviousContext,
  732. &InFunction,
  733. &EstablisherFrame,
  734. NULL);
  735. //
  736. // If virtual frame is not within the specified limits, unaligned,
  737. // or the target frame is below the virtual frame and an exit
  738. // unwind is not being performed, then raise the exception
  739. // STATUS_BAD_STACK or STATUS_BAD_BSTORE. Otherwise,
  740. // check to determine if the current routine has an exception
  741. // handler.
  742. //
  743. if (CHECK_MSTACK_FRAME(EstablisherFrame, TargetFrame)) {
  744. RAISE_EXCEPTION(STATUS_BAD_STACK, ExceptionRecord);
  745. } else if (CHECK_BSTORE_FRAME(EstablisherFrame, TargetFrame)) {
  746. RAISE_EXCEPTION(STATUS_BAD_STACK, ExceptionRecord);
  747. } else if (InFunction && IS_HANDLER_DEFINED(FunctionEntry, ImageBase)) {
  748. //
  749. // The frame has an exception handler.
  750. //
  751. // The handler (i.e. personality routine) has to be called to
  752. // execute any termination routines in this frame.
  753. //
  754. // The control PC, establisher frame pointer, the address
  755. // of the function table entry, and the address of the
  756. // context record are all stored in the dispatcher context.
  757. // This information is used by the unwind linkage routine
  758. // and can be used by the exception handler itself.
  759. //
  760. DispatcherContext.ControlPc = ControlPc;
  761. DispatcherContext.FunctionEntry = FunctionEntry;
  762. DispatcherContext.ImageBase = ImageBase;
  763. DispatcherContext.ContextRecord = CurrentContext;
  764. DispatcherContext.TargetGp = TargetGp;
  765. DispatcherContext.Index = 0;
  766. //
  767. // Call the exception handler.
  768. //
  769. do {
  770. //
  771. // If the establisher frame is the target of the unwind
  772. // operation, then set the target unwind flag.
  773. //
  774. if (IS_SAME_FRAME(EstablisherFrame,TargetFrame)) {
  775. ExceptionFlags |= EXCEPTION_TARGET_UNWIND;
  776. }
  777. ExceptionRecord->ExceptionFlags = ExceptionFlags;
  778. //
  779. // Set the specified return value in case the exception
  780. // handler directly continues execution.
  781. //
  782. CurrentContext->IntV0 = (ULONGLONG)ReturnValue;
  783. //
  784. // A linkage routine written in assembler is used to
  785. // actually call the actual exception handler. This is
  786. // required by the exception handler that is associated
  787. // with the linkage routine so it can have access to two
  788. // sets of dispatcher context when it is called.
  789. //
  790. DispatcherContext.EstablisherFrame = EstablisherFrame;
  791. DispatcherContext.HistoryTable = HistoryTable;
  792. Disposition = RtlpExecuteEmHandlerForUnwind(
  793. ExceptionRecord,
  794. EstablisherFrame.MemoryStackFp,
  795. EstablisherFrame.BackingStoreFp,
  796. CurrentContext,
  797. &DispatcherContext,
  798. TargetGp,
  799. HANDLER(FunctionEntry, ImageBase, TargetGp));
  800. //
  801. // Clear target unwind and collided unwind flags.
  802. //
  803. ExceptionFlags &= ~(EXCEPTION_COLLIDED_UNWIND |
  804. EXCEPTION_TARGET_UNWIND);
  805. //
  806. // Case on the handler disposition.
  807. //
  808. switch (Disposition) {
  809. //
  810. // The disposition is to continue the search.
  811. //
  812. // If the target frame has not been reached, then
  813. // swap context pointers.
  814. //
  815. case ExceptionContinueSearch :
  816. if (!IS_SAME_FRAME(EstablisherFrame, TargetFrame)) {
  817. TempContext = CurrentContext;
  818. CurrentContext = PreviousContext;
  819. PreviousContext = TempContext;
  820. }
  821. break;
  822. //
  823. // The disposition is collided unwind.
  824. //
  825. // Set the target of the current unwind to the context
  826. // record of the previous unwind, and reexecute the
  827. // exception handler from the collided frame with the
  828. // collided unwind flag set in the exception record.
  829. //
  830. // Copy the context of the previous unwind and
  831. // virtually unwind to the caller of the extablisher,
  832. // then set the target of the current unwind to the
  833. // dispatcher context of the previous unwind, and
  834. // reexecute the exception handler from the collided
  835. // frame with the collided unwind flag set in the
  836. // exception record
  837. case ExceptionCollidedUnwind :
  838. ControlPc = DispatcherContext.ControlPc;
  839. FunctionEntry = DispatcherContext.FunctionEntry;
  840. ImageBase = DispatcherContext.ImageBase;
  841. TargetGp = DispatcherContext.TargetGp;
  842. RtlpCopyContext(OriginalContext,
  843. DispatcherContext.ContextRecord);
  844. CurrentContext = OriginalContext;
  845. PreviousContext = &LocalContext;
  846. RtlpCopyContext(PreviousContext,
  847. CurrentContext);
  848. NextPc = RtlVirtualUnwind(ImageBase,
  849. ControlPc,
  850. FunctionEntry,
  851. PreviousContext,
  852. &InFunction,
  853. &EstablisherFrame,
  854. NULL);
  855. HistoryTable = DispatcherContext.HistoryTable;
  856. ExceptionFlags |= EXCEPTION_COLLIDED_UNWIND;
  857. EstablisherFrame = DispatcherContext.EstablisherFrame;
  858. break;
  859. //
  860. // All other disposition values are invalid.
  861. //
  862. // Raise invalid disposition exception.
  863. //
  864. default :
  865. RAISE_EXCEPTION(STATUS_INVALID_DISPOSITION, ExceptionRecord);
  866. }
  867. } while ((ExceptionFlags & EXCEPTION_COLLIDED_UNWIND) != 0);
  868. } else {
  869. //
  870. // If the target frame has not been reached, then swap
  871. // context pointers.
  872. //
  873. if (!IS_SAME_FRAME(EstablisherFrame, TargetFrame) ||
  874. (IS_SAME_FRAME(EstablisherFrame, TargetFrame) &&
  875. CurrentContext->RsBSP != TargetFrame.BackingStoreFp)) {
  876. TempContext = CurrentContext;
  877. CurrentContext = PreviousContext;
  878. PreviousContext = TempContext;
  879. }
  880. }
  881. } else {
  882. //
  883. // No function table entry was found.
  884. //
  885. NextPc = RtlIa64InsertIPSlotNumber((CurrentContext->BrRp-16), 2);
  886. CurrentContext->StIFS = CurrentContext->RsPFS;
  887. CurrentContext->RsBSP = RtlpRseShrinkBySOL (CurrentContext->RsBSP, CurrentContext->StIFS);
  888. CurrentContext->RsBSPSTORE = CurrentContext->RsBSP;
  889. }
  890. //
  891. // Set point at which control left the previous routine.
  892. //
  893. ControlPc = NextPc;
  894. } while (((EstablisherFrame.MemoryStackFp < HighStackLimit) ||
  895. (EstablisherFrame.BackingStoreFp > LowBStoreLimit)) &&
  896. !(IS_SAME_FRAME(EstablisherFrame, TargetFrame)));
  897. //
  898. // If the establisher stack pointer is equal to the target frame
  899. // pointer, then continue execution. Otherwise, an exit unwind was
  900. // performed or the target of the unwind did not exist and the
  901. // debugger and subsystem are given a second chance to handle the
  902. // unwind.
  903. //
  904. if (IS_SAME_FRAME(EstablisherFrame, TargetFrame)) {
  905. CurrentContext->IntGp = TargetGp;
  906. CurrentContext->StIPSR &= ~(0x3i64 << PSR_RI);
  907. CurrentContext->IntV0 = (ULONGLONG)ReturnValue;
  908. CurrentContext->StIIP = (ULONGLONG)TargetIp;
  909. RtlRestoreContext(CurrentContext, ExceptionRecord);
  910. } else {
  911. ZwRaiseException(ExceptionRecord, CurrentContext, FALSE);
  912. }
  913. }
  914. BOOLEAN
  915. RtlDispatchException (
  916. IN PEXCEPTION_RECORD ExceptionRecord,
  917. IN PCONTEXT ContextRecord
  918. )
  919. /*++
  920. Routine Description:
  921. This function attempts to dispatch an exception to a frame based
  922. handler by searching backwards through the call frames by unwinding
  923. the RSE backing store as well as the memory stack. The search begins
  924. with the frame specified in the context record and continues backward
  925. until either a handler is found that handles the exception, the stack
  926. and/or the backing store is found to be invalid (i.e., out of limits
  927. or unaligned), or the end of the call hierarchy is reached.
  928. As each frame is encountered, the PC where control left the corresponding
  929. function is determined and used to lookup exception handler information
  930. in the runtime function table built by the linker. If the respective
  931. routine has an exception handler, then the handler is called. If the
  932. handler does not handle the exception, then the prologue of the routine
  933. is undone to "unwind" the effect of the prologue and then the next
  934. frame is examined.
  935. Arguments:
  936. ExceptionRecord - Supplies a pointer to an exception record.
  937. ContextRecord - Supplies a pointer to a context record.
  938. Return Value:
  939. If the exception is handled by one of the frame based handlers, then
  940. a value of TRUE is returned. Otherwise a value of FALSE is returned.
  941. --*/
  942. {
  943. ULONGLONG TargetGp;
  944. ULONGLONG ImageBase;
  945. CONTEXT ContextRecordEm;
  946. ULONGLONG ControlPc;
  947. ULONGLONG NextPc;
  948. DISPATCHER_CONTEXT DispatcherContext;
  949. EXCEPTION_DISPOSITION Disposition;
  950. ULONG ExceptionFlags;
  951. PRUNTIME_FUNCTION FunctionEntry;
  952. FRAME_POINTERS EstablisherFrame;
  953. FRAME_POINTERS TargetFrame; // to be removed in the future
  954. ULONGLONG HighStackLimit;
  955. ULONGLONG LowStackLimit;
  956. ULONGLONG HighBStoreLimit;
  957. ULONGLONG LowBStoreLimit;
  958. FRAME_POINTERS NestedFrame;
  959. FRAME_POINTERS NullFrame;
  960. ULONG Index;
  961. ULONG Size;
  962. BOOLEAN InFunction;
  963. PUNWIND_HISTORY_TABLE HistoryTable;
  964. UNWIND_HISTORY_TABLE UnwindTable;
  965. #ifndef NTOS_KERNEL_RUNTIME
  966. if (RtlCallVectoredExceptionHandlers(ExceptionRecord,ContextRecord)) {
  967. return TRUE;
  968. }
  969. #endif // NTOS_KERNEL_RUNTIME
  970. //
  971. // Get the current stack limits, make a copy of the context record,
  972. // get the initial PC value, capture the exception flags, and set
  973. // the nested exception frame pointer.
  974. //
  975. Rtlp64GetStackLimits(&LowStackLimit, &HighStackLimit);
  976. Rtlp64GetBStoreLimits(&LowBStoreLimit, &HighBStoreLimit);
  977. RtlpCopyContext(&ContextRecordEm, ContextRecord);
  978. if ( (ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION) &&
  979. (ExceptionRecord->NumberParameters == 5) &&
  980. (ExceptionRecord->ExceptionInformation[4] & (1 << ISR_X)) )
  981. {
  982. ControlPc = ExceptionRecord->ExceptionInformation[3];
  983. ControlPc = RtlIa64InsertIPSlotNumber(ControlPc,
  984. ((ContextRecordEm.StIPSR >> PSR_RI) & 0x3));
  985. } else {
  986. ControlPc = RtlIa64InsertIPSlotNumber(ContextRecordEm.StIIP,
  987. ((ContextRecordEm.StIPSR >> PSR_RI) & 0x3));
  988. }
  989. ExceptionFlags = ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE;
  990. INITIALIZE_FRAME(NestedFrame);
  991. INITIALIZE_FRAME(NullFrame);
  992. //
  993. // Initialize the unwind history table.
  994. //
  995. HistoryTable = &UnwindTable;
  996. HistoryTable->Count = 0;
  997. HistoryTable->Search = UNWIND_HISTORY_TABLE_NONE;
  998. HistoryTable->LowAddress = - 1;
  999. HistoryTable->HighAddress = 0;
  1000. //
  1001. // Start with the frame specified by the context record and search
  1002. // backwards through the chain of call frames attempting to find an
  1003. // exception handler that will handle the exception.
  1004. //
  1005. do {
  1006. //
  1007. // Lookup the function table entry using the point at which control
  1008. // left the procedure.
  1009. //
  1010. FunctionEntry = RtlpLookupFunctionEntry(ControlPc,
  1011. &ImageBase,
  1012. &TargetGp,
  1013. HistoryTable);
  1014. //
  1015. // If there is a function table entry for the routine, then
  1016. // virtually unwind to the caller of the current routine to
  1017. // obtain the virtual frame pointer of the establisher and check
  1018. // if there is an exception handler for the frame.
  1019. //
  1020. if (FunctionEntry != NULL) {
  1021. NextPc = RtlVirtualUnwind(ImageBase,
  1022. ControlPc,
  1023. FunctionEntry,
  1024. &ContextRecordEm,
  1025. &InFunction,
  1026. &EstablisherFrame,
  1027. NULL);
  1028. //
  1029. // If either one or both of the two virtual frame pointers are
  1030. // not within the specified stack limits or unaligned,
  1031. // then set the stack invalid flag in the exception record and
  1032. // return exception not handled. Otherwise, check if the
  1033. // current routine has an exception handler.
  1034. //
  1035. if (CHECK_MSTACK_FRAME(EstablisherFrame, NullFrame)) {
  1036. ExceptionFlags |= EXCEPTION_STACK_INVALID;
  1037. break;
  1038. } else if (CHECK_BSTORE_FRAME(EstablisherFrame, NullFrame)) {
  1039. ExceptionFlags |= EXCEPTION_STACK_INVALID;
  1040. break;
  1041. } else if ((IS_HANDLER_DEFINED(FunctionEntry, ImageBase) && InFunction)) {
  1042. //
  1043. // The handler (i.e. personality routine) has to be called
  1044. // to search for an exception handler in this frame. The
  1045. // handler must be executed by calling a stub routine that
  1046. // is written in assembler. This is required because up
  1047. // level addressing of this routine information is required
  1048. // when a nested exception is encountered.
  1049. //
  1050. DispatcherContext.ControlPc = ControlPc;
  1051. DispatcherContext.FunctionEntry = FunctionEntry;
  1052. DispatcherContext.ImageBase = ImageBase;
  1053. DispatcherContext.TargetGp = TargetGp;
  1054. DispatcherContext.Index = 0;
  1055. do {
  1056. ExceptionRecord->ExceptionFlags = ExceptionFlags;
  1057. if (NtGlobalFlag & FLG_ENABLE_EXCEPTION_LOGGING) {
  1058. Index = RtlpLogExceptionHandler(
  1059. ExceptionRecord,
  1060. ContextRecord,
  1061. (ULONG)ControlPc,
  1062. FunctionEntry,
  1063. sizeof(RUNTIME_FUNCTION));
  1064. }
  1065. DispatcherContext.EstablisherFrame = EstablisherFrame;
  1066. DispatcherContext.ContextRecord = &ContextRecordEm;
  1067. DispatcherContext.HistoryTable = HistoryTable;
  1068. Disposition = RtlpExecuteEmHandlerForException(
  1069. ExceptionRecord,
  1070. EstablisherFrame.MemoryStackFp,
  1071. EstablisherFrame.BackingStoreFp,
  1072. ContextRecord,
  1073. &DispatcherContext,
  1074. TargetGp,
  1075. HANDLER(FunctionEntry, ImageBase, TargetGp));
  1076. if (NtGlobalFlag & FLG_ENABLE_EXCEPTION_LOGGING) {
  1077. RtlpLogLastExceptionDisposition(Index, Disposition);
  1078. }
  1079. ExceptionFlags |=
  1080. (ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE);
  1081. ExceptionFlags &= ~EXCEPTION_COLLIDED_UNWIND;
  1082. //
  1083. // If the current scan is within a nested context and the
  1084. // frame just examined is the end of the nested region,
  1085. // then clear the nested context frame and the nested
  1086. // exception flag in the exception flags.
  1087. //
  1088. if (IS_SAME_FRAME(NestedFrame, EstablisherFrame)) {
  1089. ExceptionFlags &= (~EXCEPTION_NESTED_CALL);
  1090. INITIALIZE_FRAME(NestedFrame);
  1091. }
  1092. //
  1093. // Case on the handler disposition.
  1094. //
  1095. switch (Disposition) {
  1096. //
  1097. // The disposition is to continue execution.
  1098. //
  1099. // If the exception is not continuable, then raise the
  1100. // exception STATUS_NONCONTINUABLE_EXCEPTION. Otherwise,
  1101. // return exception handled.
  1102. //
  1103. case ExceptionContinueExecution:
  1104. if ((ExceptionFlags & EXCEPTION_NONCONTINUABLE) != 0) {
  1105. RAISE_EXCEPTION(STATUS_NONCONTINUABLE_EXCEPTION,
  1106. ExceptionRecord);
  1107. } else {
  1108. return TRUE;
  1109. }
  1110. //
  1111. // The disposition is to continue the search.
  1112. //
  1113. // Get the next frame address and continue the search.
  1114. //
  1115. case ExceptionContinueSearch:
  1116. break;
  1117. //
  1118. // The disposition is nested exception.
  1119. //
  1120. // Set the nested context frame to the establisher frame
  1121. // address and set the nested exception flag in the
  1122. // exception flags.
  1123. //
  1124. case ExceptionNestedException:
  1125. ExceptionFlags |= EXCEPTION_NESTED_CALL;
  1126. if (DispatcherContext.EstablisherFrame.MemoryStackFp > NestedFrame.MemoryStackFp) {
  1127. NestedFrame = DispatcherContext.EstablisherFrame;
  1128. }
  1129. break;
  1130. //
  1131. // The disposition is hitting a frame processed by a
  1132. // previous unwind.
  1133. //
  1134. // Set the target of the current dispatch to the context
  1135. // record of the previous unwind
  1136. //
  1137. case ExceptionCollidedUnwind:
  1138. ControlPc = DispatcherContext.ControlPc;
  1139. NextPc = ControlPc;
  1140. EstablisherFrame = DispatcherContext.EstablisherFrame;
  1141. FunctionEntry = DispatcherContext.FunctionEntry;
  1142. ImageBase = DispatcherContext.ImageBase;
  1143. TargetGp = DispatcherContext.TargetGp;
  1144. RtlpCopyContext(&ContextRecordEm,
  1145. DispatcherContext.ContextRecord);
  1146. HistoryTable = DispatcherContext.HistoryTable;
  1147. ExceptionFlags |= EXCEPTION_COLLIDED_UNWIND;
  1148. break;
  1149. //
  1150. // All other disposition values are invalid.
  1151. //
  1152. // Raise invalid disposition exception.
  1153. //
  1154. default:
  1155. RAISE_EXCEPTION(STATUS_INVALID_DISPOSITION, ExceptionRecord);
  1156. break;
  1157. }
  1158. } while ((ExceptionFlags & EXCEPTION_COLLIDED_UNWIND) != 0);
  1159. }
  1160. } else {
  1161. //
  1162. // No function table entry is found.
  1163. //
  1164. NextPc = RtlIa64InsertIPSlotNumber((ContextRecordEm.BrRp-16), 2);
  1165. ContextRecordEm.StIFS = ContextRecordEm.RsPFS;
  1166. ContextRecordEm.RsBSP = RtlpRseShrinkBySOL (ContextRecordEm.RsBSP, ContextRecordEm.StIFS);
  1167. ContextRecordEm.RsBSPSTORE = ContextRecordEm.RsBSP;
  1168. if (NextPc == ControlPc) {
  1169. break;
  1170. }
  1171. }
  1172. //
  1173. // Set the point at which control left the previous routine.
  1174. //
  1175. ControlPc = NextPc;
  1176. } while ( (ContextRecordEm.IntSp < HighStackLimit) ||
  1177. (ContextRecordEm.RsBSP > LowBStoreLimit) );
  1178. //
  1179. // Could not handle the exception.
  1180. //
  1181. // Set final exception flags and return exception not handled.
  1182. //
  1183. ExceptionRecord->ExceptionFlags = ExceptionFlags;
  1184. return FALSE;
  1185. }
  1186. ULONGLONG
  1187. RtlpVirtualUnwind (
  1188. IN ULONGLONG ImageBase,
  1189. IN ULONGLONG ControlPc,
  1190. IN PRUNTIME_FUNCTION FunctionEntry,
  1191. IN PCONTEXT ContextRecord,
  1192. OUT PBOOLEAN InFunction,
  1193. OUT PFRAME_POINTERS EstablisherFrame,
  1194. IN OUT PKNONVOLATILE_CONTEXT_POINTERS ContextPointers OPTIONAL
  1195. )
  1196. /*++
  1197. Routine Description:
  1198. This function virtually unwinds the specfified function by executing its
  1199. prologue code backwards.
  1200. If the function is a leaf function, then the address where control left
  1201. the previous frame is obtained from the context record. If the function
  1202. is a nested function, but not an exception or interrupt frame, then the
  1203. prologue code is executed backwards and the address where control left
  1204. the previous frame is obtained from the updated context record.
  1205. Otherwise, an exception or interrupt entry to the system is being unwound
  1206. and a specially coded prologue restores the return address twice. Once
  1207. from the fault instruction address and once from the saved return address
  1208. register. The first restore is returned as the function value and the
  1209. second restore is place in the updated context record.
  1210. If a context pointers record is specified, then the address where each
  1211. nonvolatile registers is restored from is recorded in the appropriate
  1212. element of the context pointers record.
  1213. N.B. This function copies the specified context record and only computes
  1214. the establisher frame and whether control is actually in a function.
  1215. Arguments:
  1216. ImageBase - Base address of the module to which the function belongs.
  1217. ControlPc - Supplies the address where control left the specified
  1218. function.
  1219. FunctionEntry - Supplies the address of the function table entry for the
  1220. specified function.
  1221. ContextRecord - Supplies the address of a context record.
  1222. InFunction - Supplies a pointer to a variable that receives whether the
  1223. control PC is within the current function.
  1224. EstablisherFrame - Supplies a pointer to a variable that receives the
  1225. the establisher frame pointer value.
  1226. ContextPointers - Supplies an optional pointer to a context pointers
  1227. record.
  1228. Return Value:
  1229. The address where control left the previous frame is returned as the
  1230. function value.
  1231. --*/
  1232. {
  1233. CONTEXT LocalContext;
  1234. //
  1235. // Copy the context record so updates will not be reflected in the
  1236. // original copy and then virtually unwind to the caller of the
  1237. // specified control point.
  1238. //
  1239. RtlCopyMemory((PVOID)&LocalContext, ContextRecord, sizeof(CONTEXT));
  1240. return RtlVirtualUnwind(ImageBase,
  1241. ControlPc,
  1242. FunctionEntry,
  1243. &LocalContext,
  1244. InFunction,
  1245. EstablisherFrame,
  1246. ContextPointers);
  1247. }
  1248. VOID
  1249. RtlpCopyContext (
  1250. OUT PCONTEXT Destination,
  1251. IN PCONTEXT Source
  1252. )
  1253. /*++
  1254. Routine Description:
  1255. This function copies the nonvolatile context and the volatile integer context
  1256. required for exception dispatch and unwind from the specified source context
  1257. record to the specified destination context record. Note the volatile integer
  1258. context must be included since leaf function may save some of the non-volatile context in
  1259. volatile registers. For example the lc register might be saved in a tempoary register by a
  1260. function like memcpy. The lc needs to be properly restored when an unwind occurs.
  1261. Arguments:
  1262. Destination - Supplies a pointer to the destination context record.
  1263. Source - Supplies a pointer to the source context record.
  1264. Return Value:
  1265. None.
  1266. --*/
  1267. {
  1268. //
  1269. // Copy nonvolatile context required for exception dispatch and unwind.
  1270. //
  1271. Destination->ContextFlags = Source->ContextFlags;
  1272. //
  1273. // Copy FltS0-FltS3
  1274. //
  1275. RtlCopyMemory(&Destination->FltS0,
  1276. &Source->FltS0,
  1277. FIELD_OFFSET(CONTEXT, FltS3) + sizeof(Source->FltS3) - FIELD_OFFSET(CONTEXT, FltS0));
  1278. //
  1279. // Copy FltS4-FltS19
  1280. //
  1281. RtlCopyMemory(&Destination->FltS4,
  1282. &Source->FltS4,
  1283. FIELD_OFFSET(CONTEXT, FltS19) + sizeof(Source->FltS19) - FIELD_OFFSET(CONTEXT, FltS4));
  1284. //
  1285. // Copy StFPSR to StFDR, includes all of the interger conext.
  1286. //
  1287. RtlCopyMemory(&Destination->StFPSR,
  1288. &Source->StFPSR,
  1289. FIELD_OFFSET(CONTEXT, StFDR) + sizeof(Source->StFDR) - FIELD_OFFSET(CONTEXT, StFPSR));
  1290. }
  1291. #if !defined(NTOS_KERNEL_RUNTIME)
  1292. LIST_ENTRY RtlpDynamicFunctionTable;
  1293. PLIST_ENTRY
  1294. RtlGetFunctionTableListHead (
  1295. VOID
  1296. )
  1297. /*++
  1298. Routine Description:
  1299. This function returns the address of the dynamic function table list head.
  1300. Arguments:
  1301. None.
  1302. Return value:
  1303. The address of the dynamic function table list head is returned.
  1304. --*/
  1305. {
  1306. return &RtlpDynamicFunctionTable;
  1307. }
  1308. BOOLEAN
  1309. RtlAddFunctionTable(
  1310. IN PRUNTIME_FUNCTION FunctionTable,
  1311. IN ULONG EntryCount,
  1312. IN ULONGLONG BaseAddress,
  1313. IN ULONGLONG TargetGp
  1314. )
  1315. /*++
  1316. Routine Description:
  1317. Add a dynamic function table to the dynamic function table list. Dynamic
  1318. function tables describe code generated at run-time. The dynamic function
  1319. tables are searched via a call to RtlLookupDynamicFunctionEntry().
  1320. Normally this is only invoked via calls to RtlpLookupFunctionEntry().
  1321. The FunctionTable entries need not be sorted in any particular order. The
  1322. list is scanned for a Min and Max address range and whether or not it is
  1323. sorted. If the latter RtlLookupDynamicFunctionEntry() uses a binary
  1324. search, otherwise it uses a linear search.
  1325. The dynamic function entries will be searched only after a search
  1326. through the static function entries associated with all current
  1327. process images has failed.
  1328. Arguments:
  1329. FunctionTable Address of an array of function entries where
  1330. each element is of type RUNTIME_FUNCTION.
  1331. EntryCount The number of function entries in the array
  1332. BaseAddress Base address to calculate the real address of the function table entry
  1333. TargetGp return back to RtlpLookupFunctionEntry for future query.
  1334. Return value:
  1335. TRUE if RtlAddFunctionTable completed successfully
  1336. FALSE if RtlAddFunctionTable completed unsuccessfully
  1337. --*/
  1338. {
  1339. PDYNAMIC_FUNCTION_TABLE pNew;
  1340. PRUNTIME_FUNCTION FunctionEntry;
  1341. ULONG i;
  1342. if (EntryCount == 0)
  1343. return FALSE;
  1344. //
  1345. // Allocate memory for this link list entry
  1346. //
  1347. pNew = RtlAllocateHeap( RtlProcessHeap(), 0, sizeof(DYNAMIC_FUNCTION_TABLE) );
  1348. if (pNew != NULL) {
  1349. PVOID LockCookie = NULL;
  1350. pNew->FunctionTable = FunctionTable;
  1351. pNew->EntryCount = EntryCount;
  1352. NtQuerySystemTime( &pNew->TimeStamp );
  1353. //
  1354. // Scan the function table for Minimum/Maximum and to determine
  1355. // if it is sorted. If the latter, we can perform a binary search.
  1356. //
  1357. FunctionEntry = FunctionTable;
  1358. pNew->MinimumAddress = RF_BEGIN_ADDRESS( BaseAddress, FunctionEntry);
  1359. pNew->MaximumAddress = RF_END_ADDRESS(BaseAddress, FunctionEntry);
  1360. pNew->Type = RF_SORTED;
  1361. pNew->OutOfProcessCallbackDll = NULL;
  1362. FunctionEntry++;
  1363. for (i = 1; i < EntryCount; FunctionEntry++, i++) {
  1364. if ((pNew->Type == RF_SORTED) &&
  1365. (FunctionEntry->BeginAddress < FunctionTable[i-1].BeginAddress)) {
  1366. pNew->Type = RF_UNSORTED;
  1367. }
  1368. if (RF_BEGIN_ADDRESS(BaseAddress, FunctionEntry) < pNew->MinimumAddress) {
  1369. pNew->MinimumAddress = RF_BEGIN_ADDRESS( BaseAddress, FunctionEntry);
  1370. }
  1371. if (RF_END_ADDRESS( BaseAddress, FunctionEntry) > pNew->MaximumAddress) {
  1372. pNew->MaximumAddress = RF_END_ADDRESS( BaseAddress, FunctionEntry);
  1373. }
  1374. }
  1375. //
  1376. // Insert the new entry in the dynamic function table list.
  1377. // Protect the insertion with the loader lock.
  1378. //
  1379. pNew->BaseAddress = BaseAddress;
  1380. pNew->TargetGp = TargetGp;
  1381. LdrLockLoaderLock(LDR_LOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS, NULL, &LockCookie);
  1382. __try {
  1383. InsertTailList((PLIST_ENTRY)&RtlpDynamicFunctionTable, (PLIST_ENTRY)pNew);
  1384. } __finally {
  1385. LdrUnlockLoaderLock(LDR_UNLOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS, LockCookie);
  1386. }
  1387. return TRUE;
  1388. } else {
  1389. return FALSE;
  1390. }
  1391. }
  1392. BOOLEAN
  1393. RtlInstallFunctionTableCallback (
  1394. IN ULONG64 TableIdentifier,
  1395. IN ULONG64 BaseAddress,
  1396. IN ULONG Length,
  1397. IN ULONG64 TargetGp,
  1398. IN PGET_RUNTIME_FUNCTION_CALLBACK Callback,
  1399. IN PVOID Context,
  1400. IN PCWSTR OutOfProcessCallbackDll OPTIONAL
  1401. )
  1402. /*++
  1403. Routine Description:
  1404. This function adds a dynamic function table to the dynamic function table
  1405. list. A dynamic function table describe code that is generated at runtime.
  1406. Arguments:
  1407. TableIdentifier - Supplies a value that identifies the dynamic function
  1408. table callback.
  1409. N.B. The two low order bits of this value must be set.
  1410. BaseAddress - Supplies the base address of the code region covered by
  1411. callback function.
  1412. Length - Supplies the length of code region covered by the callback
  1413. function.
  1414. TargetGp - Supplies the target GP value for functions covered by the
  1415. callback function.
  1416. Callback - Supplies the address of the callback function that will be
  1417. called to get function table entries for the functions covered by
  1418. the specified region.
  1419. Context - Supplies a context parameter that will be passed to the callback
  1420. routine.
  1421. OutOfProcessCallbackDll - Supplies an optional pointer to the path name of
  1422. a DLL that can be used by the debugger to obtain function table entries
  1423. from outside the process.
  1424. Return Value
  1425. If the function table is successfully installed, then TRUE is returned.
  1426. Otherwise, FALSE is returned.
  1427. --*/
  1428. {
  1429. PDYNAMIC_FUNCTION_TABLE NewTable;
  1430. SIZE_T Size;
  1431. //
  1432. // If the table identifier does not have the two low bits set, then return
  1433. // FALSE.
  1434. //
  1435. // N.B. The two low order bits are required to be set in order to ensure
  1436. // that the table identifier does not collide with an actual address
  1437. // of a function table, i.e., this value is used to delete the entry.
  1438. //
  1439. if ((TableIdentifier & 0x3) != 3) {
  1440. return FALSE;
  1441. }
  1442. //
  1443. // If the length of the code region is greater than 2gb, then return
  1444. // FALSE.
  1445. //
  1446. if ((LONG)Length < 0) {
  1447. return FALSE;
  1448. }
  1449. //
  1450. // Allocate a new dynamic function table.
  1451. //
  1452. Size = 0;
  1453. if (ARGUMENT_PRESENT(OutOfProcessCallbackDll)) {
  1454. Size = (wcslen(OutOfProcessCallbackDll) + 1) * sizeof(WCHAR);
  1455. }
  1456. NewTable = RtlAllocateHeap(RtlProcessHeap(),
  1457. 0,
  1458. sizeof(DYNAMIC_FUNCTION_TABLE) + Size);
  1459. //
  1460. // If the allocation is successful, then add dynamic function table.
  1461. //
  1462. if (NewTable != NULL) {
  1463. //
  1464. // Initialize the dynamic function table callback entry.
  1465. //
  1466. NewTable->FunctionTable = (PRUNTIME_FUNCTION)TableIdentifier;
  1467. NtQuerySystemTime(&NewTable->TimeStamp);
  1468. NewTable->MinimumAddress = BaseAddress;
  1469. NewTable->MaximumAddress = BaseAddress + Length;
  1470. NewTable->BaseAddress = BaseAddress;
  1471. NewTable->TargetGp = TargetGp;
  1472. NewTable->Callback = Callback;
  1473. NewTable->Context = Context;
  1474. NewTable->Type = RF_CALLBACK;
  1475. NewTable->OutOfProcessCallbackDll = NULL;
  1476. if (ARGUMENT_PRESENT(OutOfProcessCallbackDll)) {
  1477. NewTable->OutOfProcessCallbackDll = (PWSTR)(NewTable + 1);
  1478. wcsncpy((PWSTR)(NewTable + 1), OutOfProcessCallbackDll, Size/sizeof(WCHAR));
  1479. *((PWSTR)(((PUCHAR)(NewTable))+sizeof(DYNAMIC_FUNCTION_TABLE) + Size - sizeof(WCHAR))) = L'\0';
  1480. }
  1481. //
  1482. // Insert the new dyanamic function table in the dynamic function table
  1483. // list.
  1484. //
  1485. RtlEnterCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->LoaderLock);
  1486. InsertTailList(&RtlpDynamicFunctionTable, &NewTable->Links);
  1487. RtlLeaveCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->LoaderLock);
  1488. return TRUE;
  1489. } else {
  1490. return FALSE;
  1491. }
  1492. }
  1493. BOOLEAN
  1494. RtlDeleteFunctionTable (
  1495. IN PRUNTIME_FUNCTION FunctionTable
  1496. )
  1497. {
  1498. /*++
  1499. Routine Description:
  1500. Remove a dynamic function table from the dynamic function table list.
  1501. Arguments:
  1502. FunctionTable Address of an array of function entries that
  1503. was passed in a previous call to RtlAddFunctionTable
  1504. Return Value
  1505. TRUE - If function completed successfully
  1506. FALSE - If function completed unsuccessfully
  1507. --*/
  1508. PDYNAMIC_FUNCTION_TABLE CurrentEntry;
  1509. PLIST_ENTRY Head;
  1510. PLIST_ENTRY Next;
  1511. BOOLEAN Status = FALSE;
  1512. PVOID LockCookie = NULL;
  1513. LdrLockLoaderLock(LDR_LOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS, NULL, &LockCookie);
  1514. __try {
  1515. //
  1516. // Search the dynamic function table list for a match on the the function
  1517. // table address.
  1518. //
  1519. Head = &RtlpDynamicFunctionTable;
  1520. for (Next = Head->Blink; Next != Head; Next = Next->Blink) {
  1521. CurrentEntry = CONTAINING_RECORD(Next,DYNAMIC_FUNCTION_TABLE,Links);
  1522. if (CurrentEntry->FunctionTable == FunctionTable) {
  1523. RemoveEntryList((PLIST_ENTRY)CurrentEntry);
  1524. RtlFreeHeap( RtlProcessHeap(), 0, CurrentEntry );
  1525. Status = TRUE;
  1526. break;
  1527. }
  1528. }
  1529. } __finally {
  1530. LdrUnlockLoaderLock(LDR_UNLOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS, LockCookie);
  1531. }
  1532. return Status;
  1533. }
  1534. PRUNTIME_FUNCTION
  1535. RtlLookupDynamicFunctionEntry(
  1536. IN ULONG_PTR ControlPc,
  1537. OUT PULONGLONG ImageBase,
  1538. OUT PULONGLONG TargetGp
  1539. )
  1540. /*++
  1541. Routine Description:
  1542. This function searches through the dynamic function entry
  1543. tables and returns the function entry address that corresponds
  1544. to the specified ControlPc. This routine does NOT perform the
  1545. secondary function entry indirection. That is performed
  1546. by RtlpLookupFunctionEntry().
  1547. Argument:
  1548. ControlPc Supplies a ControlPc.
  1549. ImageBase OUT Base address for dynamic code
  1550. Return Value
  1551. NULL - No function entry found that contains the ControlPc.
  1552. NON-NULL - Address of the function entry that describes the
  1553. code containing ControlPC.
  1554. --*/
  1555. {
  1556. PGET_RUNTIME_FUNCTION_CALLBACK Callback;
  1557. PVOID Context;
  1558. PDYNAMIC_FUNCTION_TABLE CurrentEntry;
  1559. PLIST_ENTRY Next,Head;
  1560. PRUNTIME_FUNCTION FunctionTable;
  1561. PRUNTIME_FUNCTION FunctionEntry = NULL;
  1562. LONG High;
  1563. LONG Low;
  1564. LONG Middle;
  1565. SIZE_T BaseAddress;
  1566. PVOID LockCookie = NULL;
  1567. // R E A D T H I S C O M M E N T R E A D T H I S C O M M E N T
  1568. // R E A D T H I S C O M M E N T R E A D T H I S C O M M E N T
  1569. // R E A D T H I S C O M M E N T R E A D T H I S C O M M E N T
  1570. //
  1571. // This code is not publicly documented, but there are clients of this code
  1572. // that rely on the locking behavior of this function. You cannot change
  1573. // this next line of code to call TryEnter, or change the lock. Both have
  1574. // been done or considered before, both will break something.
  1575. //
  1576. // The TryEnter idea is bad because if a thread calls RtlAddFunctionTable
  1577. // or RtlDeleteFunctionTable, and another thread is attempting an unwind
  1578. // past a dynamic function, the unwind will fail because it will not be
  1579. // able to find a function entry and its associated unwind information.
  1580. // The specific case where this is fatal is one thread is adding or
  1581. // removing a table, while another thread faults, and RtlDispatchException
  1582. // (which indirectly calls this code) cannot find a handler.
  1583. //
  1584. // The lock change idea is bad because clients may need to suspend another
  1585. // thread and inspect its stack. In this case, it needs to ensure that
  1586. // the target thread is outside of the lock before calling
  1587. // RtlLookupFunctionEntry, else it will deadlock if the target thread is
  1588. // already inside of the lock. The unwinding thread can avoid this by
  1589. // entering the lock before suspending the target thread, which is
  1590. // publicly accessible via the PEB.
  1591. //
  1592. // If you need access to the dynamic function table list outside of any
  1593. // locks, you want to either add a NEW api that does this, or use
  1594. // RtlGetFunctionTableListHead and walk through the list yourself. There
  1595. // are several other examples in the nt source base where the latter has
  1596. // been done before.
  1597. //
  1598. // R E A D T H I S C O M M E N T R E A D T H I S C O M M E N T
  1599. // R E A D T H I S C O M M E N T R E A D T H I S C O M M E N T
  1600. // R E A D T H I S C O M M E N T R E A D T H I S C O M M E N T
  1601. LdrLockLoaderLock(LDR_LOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS, NULL, &LockCookie);
  1602. __try {
  1603. //
  1604. // Search the tree starting from the head, continue until the entry
  1605. // is found or we reach the end of the list.
  1606. //
  1607. Head = &RtlpDynamicFunctionTable;
  1608. for (Next = Head->Blink; Next != Head; Next = Next->Blink) {
  1609. CurrentEntry = CONTAINING_RECORD(Next,DYNAMIC_FUNCTION_TABLE,Links);
  1610. FunctionTable = CurrentEntry->FunctionTable;
  1611. //
  1612. // Check if the ControlPC is within the range of this function table
  1613. //
  1614. if ((ControlPc >= CurrentEntry->MinimumAddress) &&
  1615. (ControlPc < CurrentEntry->MaximumAddress) ) {
  1616. // If this function table is sorted do a binary search.
  1617. BaseAddress = CurrentEntry->BaseAddress;
  1618. if (CurrentEntry->Type == RF_SORTED) {
  1619. //
  1620. // Perform binary search on the function table for a function table
  1621. // entry that subsumes the specified PC.
  1622. //
  1623. Low = 0;
  1624. High = CurrentEntry->EntryCount -1 ;
  1625. while (High >= Low) {
  1626. //
  1627. // Compute next probe index and test entry. If the specified PC
  1628. // is greater than of equal to the beginning address and less
  1629. // than the ending address of the function table entry, then
  1630. // return the address of the function table entry. Otherwise,
  1631. // continue the search.
  1632. //
  1633. Middle = (Low + High) >> 1;
  1634. FunctionEntry = &FunctionTable[Middle];
  1635. if (ControlPc < RF_BEGIN_ADDRESS( BaseAddress, FunctionEntry)) {
  1636. High = Middle - 1;
  1637. } else if (ControlPc >= RF_END_ADDRESS( BaseAddress, FunctionEntry)) {
  1638. Low = Middle + 1;
  1639. } else {
  1640. *ImageBase = CurrentEntry->BaseAddress;
  1641. if ( TargetGp != NULL )
  1642. *TargetGp = CurrentEntry->TargetGp;
  1643. __leave;
  1644. }
  1645. }
  1646. } else if (CurrentEntry->Type == RF_UNSORTED) {
  1647. PRUNTIME_FUNCTION LastFunctionEntry = &FunctionTable[CurrentEntry->EntryCount];
  1648. for (FunctionEntry = FunctionTable; FunctionEntry < LastFunctionEntry; FunctionEntry++) {
  1649. if ((ControlPc >= RF_BEGIN_ADDRESS( BaseAddress, FunctionEntry)) &&
  1650. (ControlPc < RF_END_ADDRESS( BaseAddress, FunctionEntry))) {
  1651. *ImageBase = CurrentEntry->BaseAddress;
  1652. if ( TargetGp != NULL )
  1653. *TargetGp = CurrentEntry->TargetGp;
  1654. __leave;
  1655. }
  1656. }
  1657. } else {
  1658. //
  1659. // Perform a callback to obtain the runtime function table
  1660. // entry that contains the specified control PC.
  1661. //
  1662. Callback = CurrentEntry->Callback;
  1663. Context = CurrentEntry->Context;
  1664. *ImageBase = BaseAddress;
  1665. *TargetGp = CurrentEntry->TargetGp;
  1666. FunctionEntry = (Callback)(ControlPc, Context);
  1667. __leave;
  1668. }
  1669. } // if in range
  1670. } // for (... Next != Head ...)
  1671. // Didn't find one...
  1672. FunctionEntry = NULL;
  1673. } __finally {
  1674. LdrUnlockLoaderLock(LDR_UNLOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS, LockCookie);
  1675. }
  1676. return FunctionEntry;
  1677. }
  1678. #endif