ReactOS 0.4.16-dev-457-g087979e
pseh3.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS system libraries
3 * LICENSE: GNU GPL - See COPYING in the top level directory
4 * PURPOSE: Support library for PSEH3
5 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
6 */
7
8/*
9 * - Naming: To avoid naming conflicts, all internal identifiers are prefixed
10 * with _SEH3$_.
11 * - Frame graph: PSEH3 uses the same registration frame for every trylevel.
12 * Only the top trylevel is registered in FS:0, the inner trylevels are linked
13 * to the first trylevel frame. Only the first trylevel frame has the Handler
14 * member set, it's 0 for all others as an identification. The EndOfChain
15 * member of the FS:0 registered frame points to the last internal frame,
16 * which is the frame itself, when only 1 trylevel is present.
17 *
18 * The registration graph looks like this:
19 *
20 * newer handlers
21 * ---------------->
22 *
23 * fs:0 /----------------\
24 * |-----------|<-\ |-----------|<-\ / |----------|<-\ \->|----------|
25 * | <Next> | \-| <Next> | \--/--| <Next> | \---| <Next> |
26 * | <Handler> | | <Handler> | / | <NULL> | | <NULL> |
27 * |-----------| |-----------| / |----------| |----------|
28 * |EndOfChain |---/
29 * | ... |
30 * |-----------|
31 */
32
33/* We need the full structure with all non-volatile */
34#define _SEH3$_FRAME_ALL_NONVOLATILES 1
35
36#include <stdarg.h>
37#include <windef.h>
38#include <winnt.h>
39
40#include "pseh3.h"
41#include "pseh3_asmdef.h"
42
43/* Make sure the asm definitions match the structures */
53#ifdef _SEH3$_FRAME_ALL_NONVOLATILES
57#endif
58#ifdef __clang__
60#endif
63
64void
65__attribute__((regparm(1)))
66_SEH3$_Unregister(
68{
69 if (Frame->Handler)
70 _SEH3$_UnregisterFrame(Frame);
71 else
72 _SEH3$_UnregisterTryLevel(Frame);
73}
74
75static inline
76LONG
78 volatile SEH3$_REGISTRATION_FRAME *RegistrationFrame,
80{
81 LONG FilterResult;
82
83 asm volatile (
84 /* First call with param = 0 to get the frame layout */
85 "xorl %%ecx, %%ecx\n\t"
86 "xorl %%eax, %%eax\n\t"
87 "call *%[Filter]\n\t"
88
89 /* The result is the frame base address that we passed in (0) plus the
90 offset to the registration record. */
91 "negl %%eax\n\t"
92 "addl %[RegistrationFrame], %%eax\n\t"
93
94 /* Second call to get the filter result */
95 "mov $1, %%ecx\n\t"
96 "call *%[Filter]"
97 : "=a" (FilterResult)
98 : [RegistrationFrame] "m" (RegistrationFrame), [Filter] "m" (Filter)
99 : "ecx", "edx");
100
101 return FilterResult;
102}
103
104long
105__attribute__((regparm(1)))
106_SEH3$_InvokeEmbeddedFilter(
107 volatile SEH3$_REGISTRATION_FRAME *RegistrationFrame);
108
109long
110__attribute__((regparm(1)))
111_SEH3$_InvokeEmbeddedFilterFromRegistration(
112 volatile SEH3$_REGISTRATION_FRAME *RegistrationFrame);
113
114static inline
115LONG
116_SEH3$_InvokeFilter(
117 volatile SEH3$_REGISTRATION_FRAME *RegistrationFrame,
119{
120 LONG FilterResult;
121
122 if (RegistrationFrame->ScopeTable->HandlerType == _SEH3$_NESTED_HANDLER)
123 {
124 return _SEH3$_InvokeNestedFunctionFilter(RegistrationFrame, Filter);
125 }
126 else if (RegistrationFrame->ScopeTable->HandlerType == _SEH3$_CPP_HANDLER)
127 {
128 /* Call the embedded filter function */
129 return _SEH3$_InvokeEmbeddedFilter(RegistrationFrame);
130 }
131 else if (RegistrationFrame->ScopeTable->HandlerType == _SEH3$_CLANG_HANDLER)
132 {
133 return _SEH3$_InvokeEmbeddedFilterFromRegistration(RegistrationFrame);
134 }
135 else
136 {
137 /* Should not happen! Skip this handler */
138 FilterResult = EXCEPTION_CONTINUE_SEARCH;
139 }
140
141 return FilterResult;
142}
143
144void
145__attribute__((regparm(1)))
146_SEH3$_AutoCleanup(
148{
149 if (Frame->Handler)
150 _SEH3$_UnregisterFrame(Frame);
151 else
152 _SEH3$_UnregisterTryLevel(Frame);
153
154 /* Check for __finally frames */
155 if (Frame->ScopeTable->Target == NULL)
156 {
157 _SEH3$_InvokeFilter(Frame, Frame->ScopeTable->Filter);
158 }
159
160}
161
162static inline
163LONG
166{
167 PVOID Filter = Record->ScopeTable->Filter;
168 LONG Result;
169
170 /* Check for __finally frames */
171 if (Record->ScopeTable->Target == NULL)
172 {
174 }
175
176 /* Check if we have a constant filter */
177 if (((ULONG)Filter & 0xFFFFFF00) == 0)
178 {
179 /* Lowest 8 bit are sign extended to give the result */
181 }
182 else
183 {
184 /* Call the filter function */
185 Result = _SEH3$_InvokeFilter(Record, Filter);
186 }
187
188 /* Normalize the result */
189 if (Result < 0) return EXCEPTION_CONTINUE_EXECUTION;
190 else if (Result > 0) return EXCEPTION_EXECUTE_HANDLER;
191 else return EXCEPTION_CONTINUE_SEARCH;
192}
193
194static inline
195VOID
198{
199 _SEH3$_InvokeFilter(Record, Record->ScopeTable->Filter);
200}
201
202__attribute__((noreturn))
203static inline
204void
205_SEH3$_JumpToTarget(
206 PSEH3$_REGISTRATION_FRAME RegistrationFrame)
207{
208 if (RegistrationFrame->ScopeTable->HandlerType == _SEH3$_CLANG_HANDLER)
209 {
210 asm volatile (
211 /* Load the registers */
212 "movl 24(%%ecx), %%esp\n\t"
213 "movl 28(%%ecx), %%ebp\n\t"
214
215 "movl 36(%%ecx), %%ebx\n\t"
216 "movl 40(%%ecx), %%esi\n\t"
217 "movl 44(%%ecx), %%edi\n\t"
218
219 /* Stack pointer is 4 off from the call to __SEH3$_RegisterFrame */
220 "addl $4, %%esp\n\t"
221
222 /* Jump into the exception handler */
223 "jmp *%[Target]"
224 : :
225 "c" (RegistrationFrame),
226 "a" (RegistrationFrame->ScopeTable),
227 [Target] "m" (RegistrationFrame->ScopeTable->Target)
228 );
229 }
230 else
231 {
232 asm volatile (
233 /* Load the registers */
234 "movl 24(%%ecx), %%esp\n\t"
235 "movl 28(%%ecx), %%ebp\n\t"
236
237 /* Stack pointer is 4 off from the call to __SEH3$_RegisterFrame */
238 "addl $4, %%esp\n\t"
239
240 /* Jump into the exception handler */
241 "jmp *%[Target]"
242 : :
243 "c" (RegistrationFrame),
244 "a" (RegistrationFrame->ScopeTable),
245 [Target] "m" (RegistrationFrame->ScopeTable->Target)
246 );
247 }
248
249 __builtin_unreachable();
250}
251
252void
255 PSEH3$_REGISTRATION_FRAME RegistrationFrame);
256
257
260#ifndef __clang__
261__attribute__ ((__target__ ("cld")))
262#endif
263_SEH3$_except_handler(
264 struct _EXCEPTION_RECORD * ExceptionRecord,
266 struct _CONTEXT * ContextRecord,
267 void * DispatcherContext)
268{
269 PSEH3$_REGISTRATION_FRAME CurrentFrame, TargetFrame;
270 SEH3$_EXCEPTION_POINTERS ExceptionPointers;
271 LONG FilterResult;
272
273 /* Clear the direction flag. */
274 asm volatile ("cld" : : : "memory");
275
276 /* Save the exception pointers on the stack */
277 ExceptionPointers.ExceptionRecord = ExceptionRecord;
278 ExceptionPointers.ContextRecord = ContextRecord;
279
280 /* Check if this is an unwind */
281 if (ExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING)
282 {
283 /* Unwind all local frames */
284 TargetFrame = EstablisherFrame->Next;
285 }
286 else
287 {
288 /* Loop all frames for this registration */
289 CurrentFrame = EstablisherFrame->EndOfChain;
290 for (;;)
291 {
292 /* Check if we have an exception handler */
293 if (CurrentFrame->ScopeTable->Target != NULL)
294 {
295 /* Set exception pointers and code for this frame */
296 CurrentFrame->ExceptionPointers = &ExceptionPointers;
297 CurrentFrame->ExceptionCode = ExceptionRecord->ExceptionCode;
298
299 /* Get the filter result */
300 FilterResult = _SEH3$_GetFilterResult(CurrentFrame);
301
302 /* Check, if continuuing is requested */
303 if (FilterResult == EXCEPTION_CONTINUE_EXECUTION)
304 {
306 }
307
308 /* Check if the except handler shall be executed */
309 if (FilterResult == EXCEPTION_EXECUTE_HANDLER) break;
310 }
311
312 /* Bail out if this is the last handler */
313 if (CurrentFrame == EstablisherFrame)
315
316 /* Go to the next frame */
317 CurrentFrame = CurrentFrame->Next;
318 }
319
320 /* Call RtlUnwind to unwind the frames below this one */
322
323 /* Do a local unwind up to this frame */
324 TargetFrame = CurrentFrame;
325 }
326
327 /* Loop frames up to the target frame */
328 for (CurrentFrame = EstablisherFrame->EndOfChain;
329 CurrentFrame != TargetFrame;
330 CurrentFrame = CurrentFrame->Next)
331 {
332 /* Manually unregister the frame */
333 _SEH3$_Unregister(CurrentFrame);
334
335 /* Check if this is an unwind frame */
336 if (CurrentFrame->ScopeTable->Target == NULL)
337 {
338 /* Set exception pointers and code for this frame */
339 CurrentFrame->ExceptionPointers = &ExceptionPointers;
340 CurrentFrame->ExceptionCode = ExceptionRecord->ExceptionCode;
341
342 /* Call the finally function */
343 _SEH3$_CallFinally(CurrentFrame);
344 }
345 }
346
347 /* Check if this was an unwind */
348 if (ExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING)
349 {
351 }
352
353 /* Unregister the frame. It will be unregistered again at the end of the
354 __except block, due to auto cleanup, but that doesn't hurt.
355 All we do is set either fs:[0] or EstablisherFrame->EndOfChain to
356 CurrentFrame->Next, which will not change it's value. */
357 _SEH3$_Unregister(CurrentFrame);
358
359 /* Jump to the __except block (does not return) */
360 _SEH3$_JumpToTarget(CurrentFrame);
361}
362
#define __cdecl
Definition: accygwin.h:79
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 Handler
Definition: acpixf.h:672
_Inout_ PIRP _In_ NTSTATUS ExceptionCode
Definition: cdprocs.h:1774
#define NULL
Definition: types.h:112
#define __attribute__(x)
Definition: wpp_private.h:207
@ ExceptionContinueSearch
Definition: compat.h:91
@ ExceptionContinueExecution
Definition: compat.h:90
enum _EXCEPTION_DISPOSITION EXCEPTION_DISPOSITION
_Must_inspect_result_ _In_opt_ PFLT_FILTER Filter
Definition: fltkernel.h:1801
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
#define EXCEPTION_CONTINUE_SEARCH
Definition: excpt.h:91
#define EXCEPTION_CONTINUE_EXECUTION
Definition: excpt.h:92
#define C_ASSERT(e)
Definition: intsafe.h:73
#define __fastcall
Definition: sync.c:38
_IRQL_requires_same_ _In_ PVOID EstablisherFrame
Definition: ntbasedef.h:661
_IRQL_requires_same_ _In_ PVOID _Inout_ struct _CONTEXT * ContextRecord
Definition: ntbasedef.h:662
_IRQL_requires_same_ _In_ PVOID _Inout_ struct _CONTEXT _In_ PVOID DispatcherContext
Definition: ntbasedef.h:663
long LONG
Definition: pedump.c:60
#define volatile
Definition: prototyp.h:117
void __fastcall _SEH3$_CallRtlUnwind(PSEH3$_REGISTRATION_FRAME RegistrationFrame)
static VOID _SEH3$_CallFinally(PSEH3$_REGISTRATION_FRAME Record)
Definition: pseh3.c:196
static LONG _SEH3$_GetFilterResult(PSEH3$_REGISTRATION_FRAME Record)
Definition: pseh3.c:164
static LONG _SEH3$_InvokeNestedFunctionFilter(volatile SEH3$_REGISTRATION_FRAME *RegistrationFrame, PVOID Filter)
Definition: pseh3.c:77
int const SEH3$_SCOPE_TABLE void * AllocaFrame
Definition: pseh3.h:128
int const SEH3$_SCOPE_TABLE * ScopeTable
Definition: pseh3.h:127
@ _SEH3$_CLANG_HANDLER
Definition: pseh3.h:28
@ _SEH3$_CPP_HANDLER
Definition: pseh3.h:27
@ _SEH3$_NESTED_HANDLER
Definition: pseh3.h:26
#define SEH3_REGISTRATION_FRAME_AllocaFrame
Definition: pseh3_asmdef.h:11
#define SEH3_REGISTRATION_FRAME_ExceptionPointers
Definition: pseh3_asmdef.h:7
#define SEH3_SCOPE_TABLE_Target
Definition: pseh3_asmdef.h:17
#define SEH3_REGISTRATION_FRAME_Handler
Definition: pseh3_asmdef.h:4
#define SEH3_REGISTRATION_FRAME_Esp
Definition: pseh3_asmdef.h:9
#define SEH3_REGISTRATION_FRAME_EndOfChain
Definition: pseh3_asmdef.h:5
#define SEH3_REGISTRATION_FRAME_Ebp
Definition: pseh3_asmdef.h:10
#define SEH3_SCOPE_TABLE_Filter
Definition: pseh3_asmdef.h:18
#define SEH3_REGISTRATION_FRAME_Edi
Definition: pseh3_asmdef.h:14
#define SEH3_REGISTRATION_FRAME_Esi
Definition: pseh3_asmdef.h:13
#define SEH3_REGISTRATION_FRAME_ReturnAddress
Definition: pseh3_asmdef.h:15
#define SEH3_REGISTRATION_FRAME_Next
Definition: pseh3_asmdef.h:3
#define SEH3_REGISTRATION_FRAME_ScopeTable
Definition: pseh3_asmdef.h:6
#define SEH3_REGISTRATION_FRAME_Ebx
Definition: pseh3_asmdef.h:12
#define SEH3_REGISTRATION_FRAME_ExceptionCode
Definition: pseh3_asmdef.h:8
#define inline
Definition: compat.h:23
DWORD ExceptionCode
Definition: compat.h:208
DWORD ExceptionFlags
Definition: compat.h:209
struct _CONTEXT * ContextRecord
Definition: pseh3.h:49
struct _EXCEPTION_RECORD * ExceptionRecord
Definition: pseh3.h:48
PSEH3$_EXCEPTION_POINTERS volatile ExceptionPointers
Definition: pseh3.h:65
struct _SEH3$_REGISTRATION_FRAME * EndOfChain
Definition: pseh3.h:59
PSEH3$_SCOPE_TABLE ScopeTable
Definition: pseh3.h:62
unsigned long ExceptionCode
Definition: pseh3.h:68
struct _SEH3$_REGISTRATION_FRAME * Next
Definition: pseh3.h:55
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
uint32_t ULONG
Definition: typedefs.h:59
_In_ WDFIOTARGET Target
Definition: wdfrequest.h:306
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
_In_ struct _KBUGCHECK_REASON_CALLBACK_RECORD * Record
Definition: ketypes.h:268
#define EXCEPTION_UNWINDING
Definition: rtltypes.h:155
char CHAR
Definition: xmlstorage.h:175