ReactOS 0.4.16-dev-1535-gea189a3
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
66 UNW_FLAG_EHANDLER);
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:666
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
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:717
_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: ntoskrnl.c:15

◆ 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

TODO: Check for DPC stack

Definition at line 717 of file unwind.c.

725{
727 PEXCEPTION_ROUTINE ExceptionRoutine;
729 PRUNTIME_FUNCTION FunctionEntry;
730 ULONG_PTR StackLow, StackHigh;
731 ULONG64 ImageBase, EstablisherFrame;
732 CONTEXT UnwindContext;
733
734 /* Get the current stack limits */
735 RtlpGetStackLimits(&StackLow, &StackHigh);
736
737 /* If we have a target frame, then this is our high limit */
738 if (TargetFrame != NULL)
739 {
740 StackHigh = (ULONG64)TargetFrame + 1;
741 }
742
743 /* Copy the context */
744 UnwindContext = *ContextRecord;
745
746 /* Set up the constant fields of the dispatcher context */
747 DispatcherContext.ContextRecord =
748 (HandlerType == UNW_FLAG_UHANDLER) ? ContextRecord : &UnwindContext;
749 DispatcherContext.HistoryTable = HistoryTable;
750 DispatcherContext.TargetIp = (ULONG64)TargetIp;
751
752 /* Start looping */
753 while (TRUE)
754 {
755 if (!RtlpIsStackPointerValid(UnwindContext.Rsp, StackLow, StackHigh))
756 {
757 return FALSE;
758 }
759
760 /* Lookup the FunctionEntry for the current RIP */
761 FunctionEntry = RtlLookupFunctionEntry(UnwindContext.Rip, &ImageBase, NULL);
762 if (FunctionEntry == NULL)
763 {
764 /* No function entry, so this must be a leaf function. Pop the return address from the stack.
765 Note: this can happen after the first frame as the result of an exception */
766 UnwindContext.Rip = *(DWORD64*)UnwindContext.Rsp;
767 UnwindContext.Rsp += sizeof(DWORD64);
768
769 if (HandlerType == UNW_FLAG_UHANDLER)
770 {
771 /* Copy the context back for the next iteration */
772 *ContextRecord = UnwindContext;
773 }
774 continue;
775 }
776
777 /* Save Rip before the virtual unwind */
778 DispatcherContext.ControlPc = UnwindContext.Rip;
779
780 /* Do a virtual unwind to get the next frame */
781 ExceptionRoutine = RtlVirtualUnwind(HandlerType,
782 ImageBase,
783 UnwindContext.Rip,
784 FunctionEntry,
785 &UnwindContext,
786 &DispatcherContext.HandlerData,
788 NULL);
789
790 /* Check, if we are still within the stack boundaries */
791 if ((EstablisherFrame < StackLow) ||
792 (EstablisherFrame >= StackHigh) ||
793 (EstablisherFrame & 7))
794 {
796
797 /* If we are handling an exception, we are done here. */
798 if (HandlerType == UNW_FLAG_EHANDLER)
799 {
800 ExceptionRecord->ExceptionFlags |= EXCEPTION_STACK_INVALID;
801 return FALSE;
802 }
803
804 __debugbreak();
806 }
807
808 /* Check if we have an exception routine */
809 if (ExceptionRoutine != NULL)
810 {
811 /* Check if this is the target frame */
812 if (EstablisherFrame == (ULONG64)TargetFrame)
813 {
814 /* Set flag to inform the language handler */
815 ExceptionRecord->ExceptionFlags |= EXCEPTION_TARGET_UNWIND;
816 }
817
818 /* Log the exception if it's enabled */
819 RtlpCheckLogException(ExceptionRecord,
820 &UnwindContext,
822 sizeof(DispatcherContext));
823
824 /* Set up the variable fields of the dispatcher context */
825 DispatcherContext.ImageBase = ImageBase;
826 DispatcherContext.FunctionEntry = FunctionEntry;
827 DispatcherContext.LanguageHandler = ExceptionRoutine;
828 DispatcherContext.EstablisherFrame = EstablisherFrame;
829 DispatcherContext.ScopeIndex = 0;
830
831 /* Store the return value in the unwind context */
832 UnwindContext.Rax = (ULONG64)ReturnValue;
833
834 /* Loop all nested handlers */
835 do
836 {
837 /* Call the language specific handler */
842
843 /* Clear exception flags for the next iteration */
844 ExceptionRecord->ExceptionFlags &= ~(EXCEPTION_TARGET_UNWIND |
846
847 /* Check if we do exception handling */
848 if (HandlerType == UNW_FLAG_EHANDLER)
849 {
851 {
852 /* Check if it was non-continuable */
853 if (ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE)
854 {
855 __debugbreak();
857 }
858
859 /* Execution continues */
860 return TRUE;
861 }
863 {
865 __debugbreak();
866 }
867 }
868
870 {
871 /* We collided with another unwind, so we need to continue
872 "after" the handler, skipping the termination handler
873 that resulted in this unwind. The installed handler has
874 already copied the original dispatcher context, we now
875 need to copy back the original context. */
876 UnwindContext = *ContextRecord = *DispatcherContext.ContextRecord;
877
878 /* The original context was from "before" the unwind, so we
879 need to do an additional virtual unwind to restore the
880 unwind contxt. */
882 DispatcherContext.ImageBase,
883 UnwindContext.Rip,
884 DispatcherContext.FunctionEntry,
885 &UnwindContext,
886 &DispatcherContext.HandlerData,
888 NULL);
889
890 /* Restore the context pointer and establisher frame. */
891 DispatcherContext.ContextRecord = &UnwindContext;
892 EstablisherFrame = DispatcherContext.EstablisherFrame;
893
894 /* Set the exception flags to indicate that we collided
895 with an unwind and continue the handler loop, which
896 will run any additional handlers from the previous
897 unwind. */
898 ExceptionRecord->ExceptionFlags |= EXCEPTION_COLLIDED_UNWIND;
899 continue;
900 }
901
902 /* This must be ExceptionContinueSearch now */
904 {
905 __debugbreak();
907 }
908 } while (ExceptionRecord->ExceptionFlags & EXCEPTION_COLLIDED_UNWIND);
909 }
910
911 /* Check, if we have left our stack (8.) */
912 if ((EstablisherFrame < StackLow) ||
913 (EstablisherFrame > StackHigh) ||
914 (EstablisherFrame & 7))
915 {
917 __debugbreak();
918
919 if (UnwindContext.Rip == ContextRecord->Rip)
920 {
922 }
923 else
924 {
925 ZwRaiseException(ExceptionRecord, ContextRecord, FALSE);
926 }
927 }
928
929 if (EstablisherFrame == (ULONG64)TargetFrame)
930 {
931 break;
932 }
933
934 if (HandlerType == UNW_FLAG_UHANDLER)
935 {
936 /* We have successfully unwound a frame. Copy the unwind context back. */
937 *ContextRecord = UnwindContext;
938 }
939 }
940
941 if (ExceptionRecord->ExceptionCode != STATUS_UNWIND_CONSOLIDATE)
942 {
943 ContextRecord->Rip = (ULONG64)TargetIp;
944 }
945
946 /* Set the return value */
948
949 /* Restore the context */
950 RtlRestoreContext(ContextRecord, ExceptionRecord);
951
952 /* Should never get here! */
953 ASSERT(FALSE);
954 return FALSE;
955}
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:665
_IRQL_requires_same_ _In_ PVOID _Inout_ struct _CONTEXT _In_ PVOID DispatcherContext
Definition: ntbasedef.h:667
#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
EXCEPTION_DISPOSITION NTAPI RtlpExecuteHandlerForUnwind(_Inout_ struct _EXCEPTION_RECORD *ExceptionRecord, _In_ PVOID EstablisherFrame, _Inout_ struct _CONTEXT *ContextRecord, _In_ PVOID DispatcherContext)
#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:1284
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. https://learn....
Definition: unwind.c:124
static __inline BOOL RtlpIsStackPointerValid(_In_ ULONG64 StackPointer, _In_ ULONG64 LowLimit, _In_ ULONG64 HighLimit)
Definition: unwind.c:654
#define EXCEPTION_NONCONTINUABLE_EXCEPTION
Definition: winbase.h:361
#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().