ReactOS 0.4.15-dev-8227-g32d615f
except.c File Reference
#include <rtl.h>
#include <debug.h>
Include dependency graph for except.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

PVOID NTAPI RtlpGetExceptionAddress (VOID)
 
BOOLEAN NTAPI RtlpUnwindInternal (_In_opt_ PVOID TargetFrame, _In_opt_ PVOID TargetIp, _In_ PEXCEPTION_RECORD ExceptionRecord, _In_ PVOID ReturnValue, _In_ PCONTEXT ContextRecord, _In_opt_ struct _UNWIND_HISTORY_TABLE *HistoryTable, _In_ ULONG Flags)
 
BOOLEAN NTAPI RtlDispatchException (_In_ PEXCEPTION_RECORD ExceptionRecord, _In_ PCONTEXT ContextRecord)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 11 of file except.c.

Function Documentation

◆ RtlDispatchException()

BOOLEAN NTAPI RtlDispatchException ( _In_ PEXCEPTION_RECORD  ExceptionRecord,
_In_ PCONTEXT  ContextRecord 
)

Definition at line 43 of file except.c.

46{
48
49 /* Perform vectored exception handling for user mode */
51 {
52 /* Exception handled, now call vectored continue handlers */
54
55 /* Continue execution */
56 return TRUE;
57 }
58
59 /* Call the internal unwind routine */
60 Handled = RtlpUnwindInternal(NULL, // TargetFrame
61 NULL, // TargetIp
62 ExceptionRecord,
63 0, // ReturnValue
65 NULL, // HistoryTable
67
68 /* In user mode, call any registered vectored continue handlers */
70
71 return Handled;
72}
unsigned char BOOLEAN
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
_IRQL_requires_same_ _In_ PVOID _Inout_ struct _CONTEXT * ContextRecord
Definition: ntbasedef.h:654
VOID NTAPI RtlCallVectoredContinueHandlers(_In_ PEXCEPTION_RECORD ExceptionRecord, _In_ PCONTEXT Context)
Definition: libsupp.c:822
BOOLEAN NTAPI RtlCallVectoredExceptionHandlers(_In_ PEXCEPTION_RECORD ExceptionRecord, _In_ PCONTEXT Context)
Definition: libsupp.c:813
@ UNW_FLAG_EHANDLER
Definition: rsym64.h:109
BOOLEAN NTAPI RtlpUnwindInternal(_In_opt_ PVOID TargetFrame, _In_opt_ PVOID TargetIp, _In_ PEXCEPTION_RECORD ExceptionRecord, _In_ PVOID ReturnValue, _In_ PCONTEXT ContextRecord, _In_opt_ struct _UNWIND_HISTORY_TABLE *HistoryTable, _In_ ULONG Flags)
Definition: unwind.c:665
_In_ BOOLEAN Handled
Definition: ketypes.h:349

◆ RtlpGetExceptionAddress()

PVOID NTAPI RtlpGetExceptionAddress ( VOID  )

Definition at line 21 of file except.c.

22{
24 return NULL;
25}
#define UNIMPLEMENTED
Definition: debug.h:118

◆ RtlpUnwindInternal()

BOOLEAN NTAPI RtlpUnwindInternal ( _In_opt_ PVOID  TargetFrame,
_In_opt_ PVOID  TargetIp,
_In_ PEXCEPTION_RECORD  ExceptionRecord,
_In_ PVOID  ReturnValue,
_In_ PCONTEXT  ContextRecord,
_In_opt_ struct _UNWIND_HISTORY_TABLE *  HistoryTable,
_In_ ULONG  HandlerType 
)
Remarks
The implementation is based on the description in this blog: http://www.nynaeve.net/?p=106
Differences to the desciption:
- Instead of using 2 pointers to the unwind context and previous context,
  that are being swapped and the context copied, the unwind context is
  kept in the local context and copied back into the context passed in
  by the caller.
See also
http://www.nynaeve.net/?p=106

TODO: Handle DPC stack

TODO: call RtlpExecuteHandlerForUnwind instead

TODO

TODO

TODO: Check for DPC stack

Definition at line 665 of file unwind.c.

673{
675 PEXCEPTION_ROUTINE ExceptionRoutine;
677 PRUNTIME_FUNCTION FunctionEntry;
678 ULONG_PTR StackLow, StackHigh;
679 ULONG64 ImageBase, EstablisherFrame;
680 CONTEXT UnwindContext;
681
682 /* Get the current stack limits and registration frame */
683 RtlpGetStackLimits(&StackLow, &StackHigh);
684
685 /* If we have a target frame, then this is our high limit */
686 if (TargetFrame != NULL)
687 {
688 StackHigh = (ULONG64)TargetFrame + 1;
689 }
690
691 /* Copy the context */
692 UnwindContext = *ContextRecord;
693
694 /* Set up the constant fields of the dispatcher context */
695 DispatcherContext.ContextRecord = &UnwindContext;
696 DispatcherContext.HistoryTable = HistoryTable;
697 DispatcherContext.TargetIp = (ULONG64)TargetIp;
698
699 /* Start looping */
700 while (TRUE)
701 {
702 /* Lookup the FunctionEntry for the current RIP */
703 FunctionEntry = RtlLookupFunctionEntry(UnwindContext.Rip, &ImageBase, NULL);
704 if (FunctionEntry == NULL)
705 {
706 /* No function entry, so this must be a leaf function. Pop the return address from the stack.
707 Note: this can happen after the first frame as the result of an exception */
708 UnwindContext.Rip = *(DWORD64*)UnwindContext.Rsp;
709 UnwindContext.Rsp += sizeof(DWORD64);
710
711 /* Copy the context back for the next iteration */
712 *ContextRecord = UnwindContext;
713 continue;
714 }
715
716 /* Save Rip before the virtual unwind */
717 DispatcherContext.ControlPc = UnwindContext.Rip;
718
719 /* Do a virtual unwind to get the next frame */
720 ExceptionRoutine = RtlVirtualUnwind(HandlerType,
721 ImageBase,
722 UnwindContext.Rip,
723 FunctionEntry,
724 &UnwindContext,
725 &DispatcherContext.HandlerData,
727 NULL);
728
729 /* Check, if we are still within the stack boundaries */
730 if ((EstablisherFrame < StackLow) ||
731 (EstablisherFrame >= StackHigh) ||
732 (EstablisherFrame & 7))
733 {
735
736 /* If we are handling an exception, we are done here. */
738 {
739 ExceptionRecord->ExceptionFlags |= EXCEPTION_STACK_INVALID;
740 return FALSE;
741 }
742
743 __debugbreak();
745 }
746
747 /* Check if we have an exception routine */
748 if (ExceptionRoutine != NULL)
749 {
750 /* Check if this is the target frame */
751 if (EstablisherFrame == (ULONG64)TargetFrame)
752 {
753 /* Set flag to inform the language handler */
754 ExceptionRecord->ExceptionFlags |= EXCEPTION_TARGET_UNWIND;
755 }
756
757 /* Log the exception if it's enabled */
758 RtlpCheckLogException(ExceptionRecord,
761 sizeof(DispatcherContext));
762
763 /* Set up the variable fields of the dispatcher context */
764 DispatcherContext.ImageBase = ImageBase;
765 DispatcherContext.FunctionEntry = FunctionEntry;
766 DispatcherContext.LanguageHandler = ExceptionRoutine;
767 DispatcherContext.EstablisherFrame = EstablisherFrame;
768 DispatcherContext.ScopeIndex = 0;
769
770 /* Store the return value in the unwind context */
771 UnwindContext.Rax = (ULONG64)ReturnValue;
772
773 /* Loop all nested handlers */
774 do
775 {
777 /* Call the language specific handler */
778 Disposition = ExceptionRoutine(ExceptionRecord,
782
783 /* Clear exception flags for the next iteration */
784 ExceptionRecord->ExceptionFlags &= ~(EXCEPTION_TARGET_UNWIND |
786
787 /* Check if we do exception handling */
789 {
791 {
792 /* Check if it was non-continuable */
793 if (ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE)
794 {
795 __debugbreak();
797 }
798
799 /* Execution continues */
800 return TRUE;
801 }
803 {
805 __debugbreak();
806 }
807 }
808
810 {
812 __debugbreak();
813 }
814
815 /* This must be ExceptionContinueSearch now */
817 {
818 __debugbreak();
820 }
821 } while (ExceptionRecord->ExceptionFlags & EXCEPTION_COLLIDED_UNWIND);
822 }
823
824 /* Check, if we have left our stack (8.) */
825 if ((EstablisherFrame < StackLow) ||
826 (EstablisherFrame > StackHigh) ||
827 (EstablisherFrame & 7))
828 {
830 __debugbreak();
831
832 if (UnwindContext.Rip == ContextRecord->Rip)
833 {
835 }
836 else
837 {
838 ZwRaiseException(ExceptionRecord, ContextRecord, FALSE);
839 }
840 }
841
842 if (EstablisherFrame == (ULONG64)TargetFrame)
843 {
844 break;
845 }
846
847 /* We have successfully unwound a frame. Copy the unwind context back. */
848 *ContextRecord = UnwindContext;
849 }
850
851 if (ExceptionRecord->ExceptionCode != STATUS_UNWIND_CONSOLIDATE)
852 {
853 ContextRecord->Rip = (ULONG64)TargetIp;
854 }
855
856 /* Set the return value */
858
859 /* Restore the context */
860 RtlRestoreContext(ContextRecord, ExceptionRecord);
861
862 /* Should never get here! */
863 ASSERT(FALSE);
864 return FALSE;
865}
UINT32 void void ** ReturnValue
Definition: acevents.h:216
ACPI_PHYSICAL_ADDRESS ACPI_SIZE BOOLEAN Warn UINT32 *TableIdx UINT32 ACPI_TABLE_HEADER *OutTableHeader ACPI_TABLE_HEADER **OutTable ACPI_HANDLE UINT32 ACPI_WALK_CALLBACK ACPI_WALK_CALLBACK void void **ReturnValue UINT32 ACPI_BUFFER *RetPathPtr ACPI_OBJECT_HANDLER void *Data ACPI_OBJECT_HANDLER void **Data ACPI_STRING ACPI_OBJECT_LIST ACPI_BUFFER *ReturnObjectBuffer ACPI_DEVICE_INFO **ReturnBuffer ACPI_HANDLE ACPI_HANDLE ACPI_HANDLE *OutHandle ACPI_HANDLE *OutHandle void *Context void *Context ACPI_EVENT_HANDLER Handler UINT32 UINT32 ACPI_GPE_HANDLER void *Context UINT32 HandlerType
Definition: acpixf.h:817
#define FALSE
Definition: types.h:117
VOID NTAPI RtlpCheckLogException(IN PEXCEPTION_RECORD ExceptionRecord, IN PCONTEXT ContextRecord, IN PVOID ContextData, IN ULONG Size)
Definition: libsupp.c:203
VOID NTAPI RtlpGetStackLimits(OUT PULONG_PTR LowLimit, OUT PULONG_PTR HighLimit)
Definition: libsupp.c:337
@ ExceptionContinueSearch
Definition: compat.h:91
@ ExceptionCollidedUnwind
Definition: compat.h:93
@ ExceptionNestedException
Definition: compat.h:92
@ ExceptionContinueExecution
Definition: compat.h:90
EXCEPTION_ROUTINE * PEXCEPTION_ROUTINE
Definition: compat.h:709
enum _EXCEPTION_DISPOSITION EXCEPTION_DISPOSITION
void __cdecl __debugbreak(void)
Definition: intrin_ppc.h:698
#define ASSERT(a)
Definition: mode.c:44
unsigned __int64 ULONG64
Definition: imports.h:198
_In_ ACCESS_MASK _In_ POBJECT_ATTRIBUTES _Reserved_ ULONG _In_opt_ PUNICODE_STRING _In_ ULONG _Out_opt_ PULONG Disposition
Definition: cmfuncs.h:56
NTSYSAPI NTSTATUS NTAPI ZwRaiseException(_In_ PEXCEPTION_RECORD ExceptionRecord, _In_ PCONTEXT Context, _In_ BOOLEAN SearchFrames)
DECLSPEC_NORETURN NTSYSAPI VOID NTAPI RtlRaiseStatus(_In_ NTSTATUS Status)
_IRQL_requires_same_ _In_ PVOID EstablisherFrame
Definition: ntbasedef.h:653
_IRQL_requires_same_ _In_ PVOID _Inout_ struct _CONTEXT _In_ PVOID DispatcherContext
Definition: ntbasedef.h:655
#define STATUS_INVALID_DISPOSITION
Definition: ntstatus.h:275
#define STATUS_UNWIND_CONSOLIDATE
Definition: ntstatus.h:220
#define STATUS_BAD_FUNCTION_TABLE
Definition: ntstatus.h:491
#define STATUS_BAD_STACK
Definition: ntstatus.h:277
#define EXCEPTION_NONCONTINUABLE
Definition: stubs.h:23
uint64_t DWORD64
Definition: typedefs.h:67
uint32_t ULONG_PTR
Definition: typedefs.h:65
PEXCEPTION_ROUTINE NTAPI RtlVirtualUnwind(_In_ ULONG HandlerType, _In_ ULONG64 ImageBase, _In_ ULONG64 ControlPc, _In_ PRUNTIME_FUNCTION FunctionEntry, _Inout_ PCONTEXT Context, _Outptr_ PVOID *HandlerData, _Out_ PULONG64 EstablisherFrame, _Inout_opt_ PKNONVOLATILE_CONTEXT_POINTERS ContextPointers)
Definition: unwind.c:478
VOID RtlRestoreContext(_In_ PCONTEXT ContextRecord, _In_ PEXCEPTION_RECORD ExceptionRecord)
Definition: unwind.c:1140
PRUNTIME_FUNCTION NTAPI RtlLookupFunctionEntry(IN DWORD64 ControlPc, OUT PDWORD64 ImageBase, OUT PUNWIND_HISTORY_TABLE HistoryTable)
Locates the RUNTIME_FUNCTION entry corresponding to a code address. http://msdn.microsoft....
Definition: unwind.c:124
#define EXCEPTION_NONCONTINUABLE_EXCEPTION
Definition: winbase.h:328
#define EXCEPTION_STACK_INVALID
Definition: rtltypes.h:157
#define EXCEPTION_TARGET_UNWIND
Definition: rtltypes.h:159
#define EXCEPTION_COLLIDED_UNWIND
Definition: rtltypes.h:160

Referenced by RtlDispatchException(), and RtlUnwindEx().