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.

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