ReactOS 0.4.16-dev-289-g096a551
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: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
@ 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: 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 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 */
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
712 {
713 /* Copy the context back for the next iteration */
714 *ContextRecord = UnwindContext;
715 }
716 continue;
717 }
718
719 /* Save Rip before the virtual unwind */
720 DispatcherContext.ControlPc = UnwindContext.Rip;
721
722 /* Do a virtual unwind to get the next frame */
723 ExceptionRoutine = RtlVirtualUnwind(HandlerType,
724 ImageBase,
725 UnwindContext.Rip,
726 FunctionEntry,
727 &UnwindContext,
728 &DispatcherContext.HandlerData,
730 NULL);
731
732 /* Check, if we are still within the stack boundaries */
733 if ((EstablisherFrame < StackLow) ||
734 (EstablisherFrame >= StackHigh) ||
735 (EstablisherFrame & 7))
736 {
738
739 /* If we are handling an exception, we are done here. */
741 {
742 ExceptionRecord->ExceptionFlags |= EXCEPTION_STACK_INVALID;
743 return FALSE;
744 }
745
746 __debugbreak();
748 }
749
750 /* Check if we have an exception routine */
751 if (ExceptionRoutine != NULL)
752 {
753 /* Check if this is the target frame */
754 if (EstablisherFrame == (ULONG64)TargetFrame)
755 {
756 /* Set flag to inform the language handler */
757 ExceptionRecord->ExceptionFlags |= EXCEPTION_TARGET_UNWIND;
758 }
759
760 /* Log the exception if it's enabled */
761 RtlpCheckLogException(ExceptionRecord,
762 &UnwindContext,
764 sizeof(DispatcherContext));
765
766 /* Set up the variable fields of the dispatcher context */
767 DispatcherContext.ImageBase = ImageBase;
768 DispatcherContext.FunctionEntry = FunctionEntry;
769 DispatcherContext.LanguageHandler = ExceptionRoutine;
770 DispatcherContext.EstablisherFrame = EstablisherFrame;
771 DispatcherContext.ScopeIndex = 0;
772
773 /* Store the return value in the unwind context */
774 UnwindContext.Rax = (ULONG64)ReturnValue;
775
776 /* Loop all nested handlers */
777 do
778 {
780 /* Call the language specific handler */
781 Disposition = ExceptionRoutine(ExceptionRecord,
785
786 /* Clear exception flags for the next iteration */
787 ExceptionRecord->ExceptionFlags &= ~(EXCEPTION_TARGET_UNWIND |
789
790 /* Check if we do exception handling */
792 {
794 {
795 /* Check if it was non-continuable */
796 if (ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE)
797 {
798 __debugbreak();
800 }
801
802 /* Execution continues */
803 return TRUE;
804 }
806 {
808 __debugbreak();
809 }
810 }
811
813 {
815 __debugbreak();
816 }
817
818 /* This must be ExceptionContinueSearch now */
820 {
821 __debugbreak();
823 }
824 } while (ExceptionRecord->ExceptionFlags & EXCEPTION_COLLIDED_UNWIND);
825 }
826
827 /* Check, if we have left our stack (8.) */
828 if ((EstablisherFrame < StackLow) ||
829 (EstablisherFrame > StackHigh) ||
830 (EstablisherFrame & 7))
831 {
833 __debugbreak();
834
835 if (UnwindContext.Rip == ContextRecord->Rip)
836 {
838 }
839 else
840 {
841 ZwRaiseException(ExceptionRecord, ContextRecord, FALSE);
842 }
843 }
844
845 if (EstablisherFrame == (ULONG64)TargetFrame)
846 {
847 break;
848 }
849
851 {
852 /* We have successfully unwound a frame. Copy the unwind context back. */
853 *ContextRecord = UnwindContext;
854 }
855 }
856
857 if (ExceptionRecord->ExceptionCode != STATUS_UNWIND_CONSOLIDATE)
858 {
859 ContextRecord->Rip = (ULONG64)TargetIp;
860 }
861
862 /* Set the return value */
864
865 /* Restore the context */
866 RtlRestoreContext(ContextRecord, ExceptionRecord);
867
868 /* Should never get here! */
869 ASSERT(FALSE);
870 return FALSE;
871}
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
@ UNW_FLAG_UHANDLER
Definition: rsym64.h:110
#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:1146
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: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().