ReactOS 0.4.16-dev-889-g9563c07
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:662
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:677
_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: call RtlpExecuteHandlerForUnwind instead

TODO

TODO

TODO: Check for DPC stack

Definition at line 677 of file unwind.c.

685{
687 PEXCEPTION_ROUTINE ExceptionRoutine;
689 PRUNTIME_FUNCTION FunctionEntry;
690 ULONG_PTR StackLow, StackHigh;
691 ULONG64 ImageBase, EstablisherFrame;
692 CONTEXT UnwindContext;
693
694 /* Get the current stack limits */
695 RtlpGetStackLimits(&StackLow, &StackHigh);
696
697 /* If we have a target frame, then this is our high limit */
698 if (TargetFrame != NULL)
699 {
700 StackHigh = (ULONG64)TargetFrame + 1;
701 }
702
703 /* Copy the context */
704 UnwindContext = *ContextRecord;
705
706 /* Set up the constant fields of the dispatcher context */
707 DispatcherContext.ContextRecord = &UnwindContext;
708 DispatcherContext.HistoryTable = HistoryTable;
709 DispatcherContext.TargetIp = (ULONG64)TargetIp;
710
711 /* Start looping */
712 while (TRUE)
713 {
714 if (!RtlpIsStackPointerValid(UnwindContext.Rsp, StackLow, StackHigh))
715 {
716 return FALSE;
717 }
718
719 /* Lookup the FunctionEntry for the current RIP */
720 FunctionEntry = RtlLookupFunctionEntry(UnwindContext.Rip, &ImageBase, NULL);
721 if (FunctionEntry == NULL)
722 {
723 /* No function entry, so this must be a leaf function. Pop the return address from the stack.
724 Note: this can happen after the first frame as the result of an exception */
725 UnwindContext.Rip = *(DWORD64*)UnwindContext.Rsp;
726 UnwindContext.Rsp += sizeof(DWORD64);
727
728 if (HandlerType == UNW_FLAG_UHANDLER)
729 {
730 /* Copy the context back for the next iteration */
731 *ContextRecord = UnwindContext;
732 }
733 continue;
734 }
735
736 /* Save Rip before the virtual unwind */
737 DispatcherContext.ControlPc = UnwindContext.Rip;
738
739 /* Do a virtual unwind to get the next frame */
740 ExceptionRoutine = RtlVirtualUnwind(HandlerType,
741 ImageBase,
742 UnwindContext.Rip,
743 FunctionEntry,
744 &UnwindContext,
745 &DispatcherContext.HandlerData,
747 NULL);
748
749 /* Check, if we are still within the stack boundaries */
750 if ((EstablisherFrame < StackLow) ||
751 (EstablisherFrame >= StackHigh) ||
752 (EstablisherFrame & 7))
753 {
755
756 /* If we are handling an exception, we are done here. */
757 if (HandlerType == UNW_FLAG_EHANDLER)
758 {
759 ExceptionRecord->ExceptionFlags |= EXCEPTION_STACK_INVALID;
760 return FALSE;
761 }
762
763 __debugbreak();
765 }
766
767 /* Check if we have an exception routine */
768 if (ExceptionRoutine != NULL)
769 {
770 /* Check if this is the target frame */
771 if (EstablisherFrame == (ULONG64)TargetFrame)
772 {
773 /* Set flag to inform the language handler */
774 ExceptionRecord->ExceptionFlags |= EXCEPTION_TARGET_UNWIND;
775 }
776
777 /* Log the exception if it's enabled */
778 RtlpCheckLogException(ExceptionRecord,
779 &UnwindContext,
781 sizeof(DispatcherContext));
782
783 /* Set up the variable fields of the dispatcher context */
784 DispatcherContext.ImageBase = ImageBase;
785 DispatcherContext.FunctionEntry = FunctionEntry;
786 DispatcherContext.LanguageHandler = ExceptionRoutine;
787 DispatcherContext.EstablisherFrame = EstablisherFrame;
788 DispatcherContext.ScopeIndex = 0;
789
790 /* Store the return value in the unwind context */
791 UnwindContext.Rax = (ULONG64)ReturnValue;
792
793 /* Loop all nested handlers */
794 do
795 {
797 /* Call the language specific handler */
798 Disposition = ExceptionRoutine(ExceptionRecord,
802
803 /* Clear exception flags for the next iteration */
804 ExceptionRecord->ExceptionFlags &= ~(EXCEPTION_TARGET_UNWIND |
806
807 /* Check if we do exception handling */
808 if (HandlerType == UNW_FLAG_EHANDLER)
809 {
811 {
812 /* Check if it was non-continuable */
813 if (ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE)
814 {
815 __debugbreak();
817 }
818
819 /* Execution continues */
820 return TRUE;
821 }
823 {
825 __debugbreak();
826 }
827 }
828
830 {
832 __debugbreak();
833 }
834
835 /* This must be ExceptionContinueSearch now */
837 {
838 __debugbreak();
840 }
841 } while (ExceptionRecord->ExceptionFlags & EXCEPTION_COLLIDED_UNWIND);
842 }
843
844 /* Check, if we have left our stack (8.) */
845 if ((EstablisherFrame < StackLow) ||
846 (EstablisherFrame > StackHigh) ||
847 (EstablisherFrame & 7))
848 {
850 __debugbreak();
851
852 if (UnwindContext.Rip == ContextRecord->Rip)
853 {
855 }
856 else
857 {
858 ZwRaiseException(ExceptionRecord, ContextRecord, FALSE);
859 }
860 }
861
862 if (EstablisherFrame == (ULONG64)TargetFrame)
863 {
864 break;
865 }
866
867 if (HandlerType == UNW_FLAG_UHANDLER)
868 {
869 /* We have successfully unwound a frame. Copy the unwind context back. */
870 *ContextRecord = UnwindContext;
871 }
872 }
873
874 if (ExceptionRecord->ExceptionCode != STATUS_UNWIND_CONSOLIDATE)
875 {
876 ContextRecord->Rip = (ULONG64)TargetIp;
877 }
878
879 /* Set the return value */
881
882 /* Restore the context */
883 RtlRestoreContext(ContextRecord, ExceptionRecord);
884
885 /* Should never get here! */
886 ASSERT(FALSE);
887 return FALSE;
888}
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:661
_IRQL_requires_same_ _In_ PVOID _Inout_ struct _CONTEXT _In_ PVOID DispatcherContext
Definition: ntbasedef.h:663
#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:1170
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:354
#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().