2 * msvcrt.dll exception handling
4 * Copyright 2000 Jon Griffiths
5 * Copyright 2005 Juan Lang
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * FIXME: Incomplete support for nested exceptions/try block cleanup.
25 #include "wine/port.h"
30 #define WIN32_NO_STATUS
34 #include "wine/exception.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(seh);
42 static MSVCRT_security_error_handler security_error_handler;
44 /* VC++ extensions to Win32 SEH */
45 typedef struct _SCOPETABLE
48 int (*lpfnFilter)(PEXCEPTION_POINTERS);
49 int (*lpfnHandler)(void);
50 } SCOPETABLE, *PSCOPETABLE;
52 typedef struct _MSVCRT_EXCEPTION_FRAME
54 EXCEPTION_REGISTRATION_RECORD *prev;
55 void (*handler)(PEXCEPTION_RECORD, EXCEPTION_REGISTRATION_RECORD*,
56 PCONTEXT, PEXCEPTION_RECORD);
57 PSCOPETABLE scopetable;
60 PEXCEPTION_POINTERS xpointers;
61 } MSVCRT_EXCEPTION_FRAME;
69 SCOPETABLE entries[1];
72 #define TRYLEVEL_END (-1) /* End of trylevel list */
74 #if defined(__GNUC__) && defined(__i386__)
75 static inline void call_finally_block( void *code_block, void *base_ptr )
77 __asm__ __volatile__ ("movl %1,%%ebp; call *%%eax"
78 : : "a" (code_block), "g" (base_ptr));
81 static inline int call_filter( int (*func)(PEXCEPTION_POINTERS), void *arg, void *ebp )
84 __asm__ __volatile__ ("pushl %%ebp; pushl %3; movl %2,%%ebp; call *%%eax; popl %%ebp; popl %%ebp"
86 : "0" (func), "r" (ebp), "r" (arg)
87 : "ecx", "edx", "memory" );
91 static inline int call_unwind_func( int (*func)(void), void *ebp )
94 __asm__ __volatile__ ("pushl %%ebp\n\t"
105 : "0" (func), "r" (ebp)
106 : "ecx", "edx", "memory" );
115 static const SCOPETABLE_V4 *get_scopetable_v4( MSVCRT_EXCEPTION_FRAME *frame, ULONG_PTR cookie )
117 return (const SCOPETABLE_V4 *)((ULONG_PTR)frame->scopetable ^ cookie);
120 static DWORD MSVCRT_nested_handler(PEXCEPTION_RECORD rec,
121 EXCEPTION_REGISTRATION_RECORD* frame,
123 EXCEPTION_REGISTRATION_RECORD** dispatch)
125 if (!(rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))
126 return ExceptionContinueSearch;
128 return ExceptionCollidedUnwind;
132 /*********************************************************************
133 * _EH_prolog (MSVCRT.@)
136 /* Provided for VC++ binary compatibility only */
137 __ASM_GLOBAL_FUNC(_EH_prolog,
138 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") /* skip ret addr */
140 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
142 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
144 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
145 "movl %esp, %fs:0\n\t"
146 "movl 12(%esp), %eax\n\t"
147 "movl %ebp, 12(%esp)\n\t"
148 "leal 12(%esp), %ebp\n\t"
150 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
153 static void msvcrt_local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp)
155 EXCEPTION_REGISTRATION_RECORD reg;
157 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
159 /* Register a handler in case of a nested exception */
160 reg.Handler = MSVCRT_nested_handler;
161 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
162 __wine_push_frame(®);
164 while (frame->trylevel != TRYLEVEL_END && frame->trylevel != trylevel)
166 int level = frame->trylevel;
167 frame->trylevel = frame->scopetable[level].previousTryLevel;
168 if (!frame->scopetable[level].lpfnFilter)
170 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
171 level, frame->scopetable[level].lpfnHandler, ebp );
172 call_unwind_func( frame->scopetable[level].lpfnHandler, ebp );
175 __wine_pop_frame(®);
176 TRACE("unwound OK\n");
179 static void msvcrt_local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel, void *ebp )
181 EXCEPTION_REGISTRATION_RECORD reg;
182 const SCOPETABLE_V4 *scopetable = get_scopetable_v4( frame, *cookie );
184 TRACE("(%p,%d,%d)\n",frame, frame->trylevel, trylevel);
186 /* Register a handler in case of a nested exception */
187 reg.Handler = MSVCRT_nested_handler;
188 reg.Prev = NtCurrentTeb()->Tib.ExceptionList;
189 __wine_push_frame(®);
191 while (frame->trylevel != -2 && frame->trylevel != trylevel)
193 int level = frame->trylevel;
194 frame->trylevel = scopetable->entries[level].previousTryLevel;
195 if (!scopetable->entries[level].lpfnFilter)
197 TRACE( "__try block cleanup level %d handler %p ebp %p\n",
198 level, scopetable->entries[level].lpfnHandler, ebp );
199 call_unwind_func( scopetable->entries[level].lpfnHandler, ebp );
202 __wine_pop_frame(®);
203 TRACE("unwound OK\n");
206 /*******************************************************************
207 * _local_unwind2 (MSVCRT.@)
209 void CDECL _local_unwind2(MSVCRT_EXCEPTION_FRAME* frame, int trylevel)
211 msvcrt_local_unwind2( frame, trylevel, &frame->_ebp );
214 /*******************************************************************
215 * _local_unwind4 (MSVCRT.@)
217 void CDECL _local_unwind4( ULONG *cookie, MSVCRT_EXCEPTION_FRAME* frame, int trylevel )
219 msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
222 /*******************************************************************
223 * _global_unwind2 (MSVCRT.@)
225 void CDECL _global_unwind2(EXCEPTION_REGISTRATION_RECORD* frame)
227 TRACE("(%p)\n",frame);
228 RtlUnwind( frame, 0, 0, 0 );
231 /*********************************************************************
232 * _except_handler2 (MSVCRT.@)
234 int CDECL _except_handler2(PEXCEPTION_RECORD rec,
235 EXCEPTION_REGISTRATION_RECORD* frame,
237 EXCEPTION_REGISTRATION_RECORD** dispatcher)
239 FIXME("exception %x flags=%x at %p handler=%p %p %p stub\n",
240 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
241 frame->Handler, context, dispatcher);
242 return ExceptionContinueSearch;
245 /*********************************************************************
246 * _except_handler3 (MSVCRT.@)
248 int CDECL _except_handler3(PEXCEPTION_RECORD rec,
249 MSVCRT_EXCEPTION_FRAME* frame,
250 PCONTEXT context, void* dispatcher)
252 int retval, trylevel;
253 EXCEPTION_POINTERS exceptPtrs;
254 PSCOPETABLE pScopeTable;
256 TRACE("exception %x flags=%x at %p handler=%p %p %p semi-stub\n",
257 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
258 frame->handler, context, dispatcher);
260 __asm__ __volatile__ ("cld");
262 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
264 /* Unwinding the current frame */
265 msvcrt_local_unwind2(frame, TRYLEVEL_END, &frame->_ebp);
266 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
267 return ExceptionContinueSearch;
271 /* Hunting for handler */
272 exceptPtrs.ExceptionRecord = rec;
273 exceptPtrs.ContextRecord = context;
274 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
275 trylevel = frame->trylevel;
276 pScopeTable = frame->scopetable;
278 while (trylevel != TRYLEVEL_END)
280 TRACE( "level %d prev %d filter %p\n", trylevel, pScopeTable[trylevel].previousTryLevel,
281 pScopeTable[trylevel].lpfnFilter );
282 if (pScopeTable[trylevel].lpfnFilter)
284 retval = call_filter( pScopeTable[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
286 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
287 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
288 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
290 if (retval == EXCEPTION_CONTINUE_EXECUTION)
291 return ExceptionContinueExecution;
293 if (retval == EXCEPTION_EXECUTE_HANDLER)
295 /* Unwind all higher frames, this one will handle the exception */
296 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
297 msvcrt_local_unwind2(frame, trylevel, &frame->_ebp);
299 /* Set our trylevel to the enclosing block, and call the __finally
300 * code, which won't return
302 frame->trylevel = pScopeTable[trylevel].previousTryLevel;
303 TRACE("__finally block %p\n",pScopeTable[trylevel].lpfnHandler);
304 call_finally_block(pScopeTable[trylevel].lpfnHandler, &frame->_ebp);
305 ERR("Returned from __finally block - expect crash!\n");
308 trylevel = pScopeTable[trylevel].previousTryLevel;
311 TRACE("reached TRYLEVEL_END, returning ExceptionContinueSearch\n");
312 return ExceptionContinueSearch;
315 /*********************************************************************
316 * _except_handler4_common (MSVCRT.@)
318 int CDECL _except_handler4_common( ULONG *cookie, void (*check_cookie)(void),
319 EXCEPTION_RECORD *rec, MSVCRT_EXCEPTION_FRAME *frame,
320 CONTEXT *context, EXCEPTION_REGISTRATION_RECORD **dispatcher )
322 int retval, trylevel;
323 EXCEPTION_POINTERS exceptPtrs;
324 const SCOPETABLE_V4 *scope_table = get_scopetable_v4( frame, *cookie );
326 TRACE( "exception %x flags=%x at %p handler=%p %p %p cookie=%x scope table=%p cookies=%d/%x,%d/%x\n",
327 rec->ExceptionCode, rec->ExceptionFlags, rec->ExceptionAddress,
328 frame->handler, context, dispatcher, *cookie, scope_table,
329 scope_table->gs_cookie_offset, scope_table->gs_cookie_xor,
330 scope_table->eh_cookie_offset, scope_table->eh_cookie_xor );
332 /* FIXME: no cookie validation yet */
334 if (rec->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND))
336 /* Unwinding the current frame */
337 msvcrt_local_unwind4( cookie, frame, -2, &frame->_ebp );
338 TRACE("unwound current frame, returning ExceptionContinueSearch\n");
339 return ExceptionContinueSearch;
343 /* Hunting for handler */
344 exceptPtrs.ExceptionRecord = rec;
345 exceptPtrs.ContextRecord = context;
346 *((DWORD *)frame-1) = (DWORD)&exceptPtrs;
347 trylevel = frame->trylevel;
349 while (trylevel != -2)
351 TRACE( "level %d prev %d filter %p\n", trylevel,
352 scope_table->entries[trylevel].previousTryLevel,
353 scope_table->entries[trylevel].lpfnFilter );
354 if (scope_table->entries[trylevel].lpfnFilter)
356 retval = call_filter( scope_table->entries[trylevel].lpfnFilter, &exceptPtrs, &frame->_ebp );
358 TRACE("filter returned %s\n", retval == EXCEPTION_CONTINUE_EXECUTION ?
359 "CONTINUE_EXECUTION" : retval == EXCEPTION_EXECUTE_HANDLER ?
360 "EXECUTE_HANDLER" : "CONTINUE_SEARCH");
362 if (retval == EXCEPTION_CONTINUE_EXECUTION)
363 return ExceptionContinueExecution;
365 if (retval == EXCEPTION_EXECUTE_HANDLER)
367 /* Unwind all higher frames, this one will handle the exception */
368 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)frame);
369 msvcrt_local_unwind4( cookie, frame, trylevel, &frame->_ebp );
371 /* Set our trylevel to the enclosing block, and call the __finally
372 * code, which won't return
374 frame->trylevel = scope_table->entries[trylevel].previousTryLevel;
375 TRACE("__finally block %p\n",scope_table->entries[trylevel].lpfnHandler);
376 call_finally_block(scope_table->entries[trylevel].lpfnHandler, &frame->_ebp);
377 ERR("Returned from __finally block - expect crash!\n");
380 trylevel = scope_table->entries[trylevel].previousTryLevel;
383 TRACE("reached -2, returning ExceptionContinueSearch\n");
384 return ExceptionContinueSearch;
389 * setjmp/longjmp implementation
392 #define MSVCRT_JMP_MAGIC 0x56433230 /* ID value for new jump structure */
393 typedef void (__stdcall *MSVCRT_unwind_function)(const struct MSVCRT___JUMP_BUFFER *);
395 /* define an entrypoint for setjmp/setjmp3 that stores the registers in the jmp buf */
396 /* and then jumps to the C backend function */
397 #define DEFINE_SETJMP_ENTRYPOINT(name) \
398 __ASM_GLOBAL_FUNC( name, \
399 "movl 4(%esp),%ecx\n\t" /* jmp_buf */ \
400 "movl %ebp,0(%ecx)\n\t" /* jmp_buf.Ebp */ \
401 "movl %ebx,4(%ecx)\n\t" /* jmp_buf.Ebx */ \
402 "movl %edi,8(%ecx)\n\t" /* jmp_buf.Edi */ \
403 "movl %esi,12(%ecx)\n\t" /* jmp_buf.Esi */ \
404 "movl %esp,16(%ecx)\n\t" /* jmp_buf.Esp */ \
405 "movl 0(%esp),%eax\n\t" \
406 "movl %eax,20(%ecx)\n\t" /* jmp_buf.Eip */ \
407 "jmp " __ASM_NAME("__regs_") # name )
409 /* restore the registers from the jmp buf upon longjmp */
410 extern void DECLSPEC_NORETURN longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
411 __ASM_GLOBAL_FUNC( longjmp_set_regs,
412 "movl 4(%esp),%ecx\n\t" /* jmp_buf */
413 "movl 8(%esp),%eax\n\t" /* retval */
414 "movl 0(%ecx),%ebp\n\t" /* jmp_buf.Ebp */
415 "movl 4(%ecx),%ebx\n\t" /* jmp_buf.Ebx */
416 "movl 8(%ecx),%edi\n\t" /* jmp_buf.Edi */
417 "movl 12(%ecx),%esi\n\t" /* jmp_buf.Esi */
418 "movl 16(%ecx),%esp\n\t" /* jmp_buf.Esp */
419 "addl $4,%esp\n\t" /* get rid of return address */
420 "jmp *20(%ecx)\n\t" /* jmp_buf.Eip */ )
423 * The signatures of the setjmp/longjmp functions do not match that
424 * declared in the setjmp header so they don't follow the regular naming
425 * convention to avoid conflicts.
428 /*******************************************************************
431 DEFINE_SETJMP_ENTRYPOINT(MSVCRT__setjmp)
432 int CDECL __regs_MSVCRT__setjmp(struct MSVCRT___JUMP_BUFFER *jmp)
434 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
435 if (jmp->Registration == ~0UL)
436 jmp->TryLevel = TRYLEVEL_END;
438 jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
440 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
441 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
445 /*******************************************************************
446 * _setjmp3 (MSVCRT.@)
448 DEFINE_SETJMP_ENTRYPOINT( MSVCRT__setjmp3 )
449 int CDECL __regs_MSVCRT__setjmp3(struct MSVCRT___JUMP_BUFFER *jmp, int nb_args, ...)
451 jmp->Cookie = MSVCRT_JMP_MAGIC;
453 jmp->Registration = (unsigned long)NtCurrentTeb()->Tib.ExceptionList;
454 if (jmp->Registration == ~0UL)
456 jmp->TryLevel = TRYLEVEL_END;
463 va_start( args, nb_args );
464 if (nb_args > 0) jmp->UnwindFunc = va_arg( args, unsigned long );
465 if (nb_args > 1) jmp->TryLevel = va_arg( args, unsigned long );
466 else jmp->TryLevel = ((MSVCRT_EXCEPTION_FRAME*)jmp->Registration)->trylevel;
467 for (i = 0; i < 6 && i < nb_args - 2; i++)
468 jmp->UnwindData[i] = va_arg( args, unsigned long );
472 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx\n",
473 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration );
477 /*********************************************************************
480 void CDECL MSVCRT_longjmp(struct MSVCRT___JUMP_BUFFER *jmp, int retval)
482 unsigned long cur_frame = 0;
484 TRACE("buf=%p ebx=%08lx esi=%08lx edi=%08lx ebp=%08lx esp=%08lx eip=%08lx frame=%08lx retval=%08x\n",
485 jmp, jmp->Ebx, jmp->Esi, jmp->Edi, jmp->Ebp, jmp->Esp, jmp->Eip, jmp->Registration, retval );
487 cur_frame=(unsigned long)NtCurrentTeb()->Tib.ExceptionList;
488 TRACE("cur_frame=%lx\n",cur_frame);
490 if (cur_frame != jmp->Registration)
491 _global_unwind2((EXCEPTION_REGISTRATION_RECORD*)jmp->Registration);
493 if (jmp->Registration)
495 if (!IsBadReadPtr(&jmp->Cookie, sizeof(long)) &&
496 jmp->Cookie == MSVCRT_JMP_MAGIC && jmp->UnwindFunc)
498 MSVCRT_unwind_function unwind_func;
500 unwind_func=(MSVCRT_unwind_function)jmp->UnwindFunc;
504 msvcrt_local_unwind2((MSVCRT_EXCEPTION_FRAME*)jmp->Registration,
505 jmp->TryLevel, (void *)jmp->Ebp);
511 longjmp_set_regs( jmp, retval );
514 /*********************************************************************
515 * _seh_longjmp_unwind (MSVCRT.@)
517 void __stdcall _seh_longjmp_unwind(struct MSVCRT___JUMP_BUFFER *jmp)
519 msvcrt_local_unwind2( (MSVCRT_EXCEPTION_FRAME *)jmp->Registration, jmp->TryLevel, (void *)jmp->Ebp );
522 #elif defined(__x86_64__)
524 /*******************************************************************
527 __ASM_GLOBAL_FUNC( MSVCRT__setjmp,
528 "xorq %rdx,%rdx\n\t" /* frame */
529 "jmp " __ASM_NAME("MSVCRT__setjmpex") );
531 /*******************************************************************
532 * _setjmpex (MSVCRT.@)
534 __ASM_GLOBAL_FUNC( MSVCRT__setjmpex,
535 "movq %rdx,(%rcx)\n\t" /* jmp_buf->Frame */
536 "movq %rbx,0x8(%rcx)\n\t" /* jmp_buf->Rbx */
537 "leaq 0x8(%rsp),%rax\n\t"
538 "movq %rax,0x10(%rcx)\n\t" /* jmp_buf->Rsp */
539 "movq %rbp,0x18(%rcx)\n\t" /* jmp_buf->Rbp */
540 "movq %rsi,0x20(%rcx)\n\t" /* jmp_buf->Rsi */
541 "movq %rdi,0x28(%rcx)\n\t" /* jmp_buf->Rdi */
542 "movq %r12,0x30(%rcx)\n\t" /* jmp_buf->R12 */
543 "movq %r13,0x38(%rcx)\n\t" /* jmp_buf->R13 */
544 "movq %r14,0x40(%rcx)\n\t" /* jmp_buf->R14 */
545 "movq %r15,0x48(%rcx)\n\t" /* jmp_buf->R15 */
546 "movq (%rsp),%rax\n\t"
547 "movq %rax,0x50(%rcx)\n\t" /* jmp_buf->Rip */
548 "movdqa %xmm6,0x60(%rcx)\n\t" /* jmp_buf->Xmm6 */
549 "movdqa %xmm7,0x70(%rcx)\n\t" /* jmp_buf->Xmm7 */
550 "movdqa %xmm8,0x80(%rcx)\n\t" /* jmp_buf->Xmm8 */
551 "movdqa %xmm9,0x90(%rcx)\n\t" /* jmp_buf->Xmm9 */
552 "movdqa %xmm10,0xa0(%rcx)\n\t" /* jmp_buf->Xmm10 */
553 "movdqa %xmm11,0xb0(%rcx)\n\t" /* jmp_buf->Xmm11 */
554 "movdqa %xmm12,0xc0(%rcx)\n\t" /* jmp_buf->Xmm12 */
555 "movdqa %xmm13,0xd0(%rcx)\n\t" /* jmp_buf->Xmm13 */
556 "movdqa %xmm14,0xe0(%rcx)\n\t" /* jmp_buf->Xmm14 */
557 "movdqa %xmm15,0xf0(%rcx)\n\t" /* jmp_buf->Xmm15 */
562 extern void DECLSPEC_NORETURN CDECL longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
563 __ASM_GLOBAL_FUNC( longjmp_set_regs,
564 "movq %rdx,%rax\n\t" /* retval */
565 "movq 0x8(%rcx),%rbx\n\t" /* jmp_buf->Rbx */
566 "movq 0x18(%rcx),%rbp\n\t" /* jmp_buf->Rbp */
567 "movq 0x20(%rcx),%rsi\n\t" /* jmp_buf->Rsi */
568 "movq 0x28(%rcx),%rdi\n\t" /* jmp_buf->Rdi */
569 "movq 0x30(%rcx),%r12\n\t" /* jmp_buf->R12 */
570 "movq 0x38(%rcx),%r13\n\t" /* jmp_buf->R13 */
571 "movq 0x40(%rcx),%r14\n\t" /* jmp_buf->R14 */
572 "movq 0x48(%rcx),%r15\n\t" /* jmp_buf->R15 */
573 "movdqa 0x60(%rcx),%xmm6\n\t" /* jmp_buf->Xmm6 */
574 "movdqa 0x70(%rcx),%xmm7\n\t" /* jmp_buf->Xmm7 */
575 "movdqa 0x80(%rcx),%xmm8\n\t" /* jmp_buf->Xmm8 */
576 "movdqa 0x90(%rcx),%xmm9\n\t" /* jmp_buf->Xmm9 */
577 "movdqa 0xa0(%rcx),%xmm10\n\t" /* jmp_buf->Xmm10 */
578 "movdqa 0xb0(%rcx),%xmm11\n\t" /* jmp_buf->Xmm11 */
579 "movdqa 0xc0(%rcx),%xmm12\n\t" /* jmp_buf->Xmm12 */
580 "movdqa 0xd0(%rcx),%xmm13\n\t" /* jmp_buf->Xmm13 */
581 "movdqa 0xe0(%rcx),%xmm14\n\t" /* jmp_buf->Xmm14 */
582 "movdqa 0xf0(%rcx),%xmm15\n\t" /* jmp_buf->Xmm15 */
583 "movq 0x50(%rcx),%rdx\n\t" /* jmp_buf->Rip */
584 "movq 0x10(%rcx),%rsp\n\t" /* jmp_buf->Rsp */
587 /*******************************************************************
590 void __cdecl MSVCRT_longjmp( struct MSVCRT___JUMP_BUFFER *jmp, int retval )
592 EXCEPTION_RECORD rec;
594 if (!retval) retval = 1;
597 rec.ExceptionCode = STATUS_LONGJUMP;
598 rec.ExceptionFlags = 0;
599 rec.ExceptionRecord = NULL;
600 rec.ExceptionAddress = NULL;
601 rec.NumberParameters = 1;
602 rec.ExceptionInformation[0] = (DWORD_PTR)jmp;
603 RtlUnwind( (void *)jmp->Frame, (void *)jmp->Rip, &rec, IntToPtr(retval) );
605 longjmp_set_regs( jmp, retval );
608 #endif /* __x86_64__ */
610 static MSVCRT___sighandler_t sighandlers[MSVCRT_NSIG] = { MSVCRT_SIG_DFL };
612 static BOOL WINAPI msvcrt_console_handler(DWORD ctrlType)
619 if (sighandlers[MSVCRT_SIGINT])
621 if (sighandlers[MSVCRT_SIGINT] != MSVCRT_SIG_IGN)
622 sighandlers[MSVCRT_SIGINT](MSVCRT_SIGINT);
630 typedef void (CDECL *float_handler)(int, int);
632 /* The exception codes are actually NTSTATUS values */
637 } float_exception_map[] = {
638 { EXCEPTION_FLT_DENORMAL_OPERAND, MSVCRT__FPE_DENORMAL },
639 { EXCEPTION_FLT_DIVIDE_BY_ZERO, MSVCRT__FPE_ZERODIVIDE },
640 { EXCEPTION_FLT_INEXACT_RESULT, MSVCRT__FPE_INEXACT },
641 { EXCEPTION_FLT_INVALID_OPERATION, MSVCRT__FPE_INVALID },
642 { EXCEPTION_FLT_OVERFLOW, MSVCRT__FPE_OVERFLOW },
643 { EXCEPTION_FLT_STACK_CHECK, MSVCRT__FPE_STACKOVERFLOW },
644 { EXCEPTION_FLT_UNDERFLOW, MSVCRT__FPE_UNDERFLOW },
647 static LONG WINAPI msvcrt_exception_filter(struct _EXCEPTION_POINTERS *except)
649 LONG ret = EXCEPTION_CONTINUE_SEARCH;
650 MSVCRT___sighandler_t handler;
652 if (!except || !except->ExceptionRecord)
653 return EXCEPTION_CONTINUE_SEARCH;
655 switch (except->ExceptionRecord->ExceptionCode)
657 case EXCEPTION_ACCESS_VIOLATION:
658 if ((handler = sighandlers[MSVCRT_SIGSEGV]) != MSVCRT_SIG_DFL)
660 if (handler != MSVCRT_SIG_IGN)
662 sighandlers[MSVCRT_SIGSEGV] = MSVCRT_SIG_DFL;
663 handler(MSVCRT_SIGSEGV);
665 ret = EXCEPTION_CONTINUE_EXECUTION;
668 /* According to msdn,
669 * the FPE signal handler takes as a second argument the type of
670 * floating point exception.
672 case EXCEPTION_FLT_DENORMAL_OPERAND:
673 case EXCEPTION_FLT_DIVIDE_BY_ZERO:
674 case EXCEPTION_FLT_INEXACT_RESULT:
675 case EXCEPTION_FLT_INVALID_OPERATION:
676 case EXCEPTION_FLT_OVERFLOW:
677 case EXCEPTION_FLT_STACK_CHECK:
678 case EXCEPTION_FLT_UNDERFLOW:
679 if ((handler = sighandlers[MSVCRT_SIGFPE]) != MSVCRT_SIG_DFL)
681 if (handler != MSVCRT_SIG_IGN)
684 int float_signal = MSVCRT__FPE_INVALID;
686 sighandlers[MSVCRT_SIGFPE] = MSVCRT_SIG_DFL;
687 for (i = 0; i < sizeof(float_exception_map) /
688 sizeof(float_exception_map[0]); i++)
690 if (float_exception_map[i].status ==
691 except->ExceptionRecord->ExceptionCode)
693 float_signal = float_exception_map[i].signal;
697 ((float_handler)handler)(MSVCRT_SIGFPE, float_signal);
699 ret = EXCEPTION_CONTINUE_EXECUTION;
702 case EXCEPTION_ILLEGAL_INSTRUCTION:
703 case EXCEPTION_PRIV_INSTRUCTION:
704 if ((handler = sighandlers[MSVCRT_SIGILL]) != MSVCRT_SIG_DFL)
706 if (handler != MSVCRT_SIG_IGN)
708 sighandlers[MSVCRT_SIGILL] = MSVCRT_SIG_DFL;
709 handler(MSVCRT_SIGILL);
711 ret = EXCEPTION_CONTINUE_EXECUTION;
718 void msvcrt_init_signals(void)
720 SetConsoleCtrlHandler(msvcrt_console_handler, TRUE);
721 SetUnhandledExceptionFilter(msvcrt_exception_filter);
724 void msvcrt_free_signals(void)
726 SetConsoleCtrlHandler(msvcrt_console_handler, FALSE);
727 SetUnhandledExceptionFilter(NULL);
730 /*********************************************************************
732 * Some signals may never be generated except through an explicit call to
735 MSVCRT___sighandler_t CDECL MSVCRT_signal(int sig, MSVCRT___sighandler_t func)
737 MSVCRT___sighandler_t ret = MSVCRT_SIG_ERR;
739 TRACE("(%d, %p)\n", sig, func);
741 if (func == MSVCRT_SIG_ERR) return MSVCRT_SIG_ERR;
745 /* Cases handled internally. Note SIGTERM is never generated by Windows,
746 * so we effectively mask it.
754 case MSVCRT_SIGBREAK:
755 ret = sighandlers[sig];
756 sighandlers[sig] = func;
759 ret = MSVCRT_SIG_ERR;
764 /*********************************************************************
767 int CDECL MSVCRT_raise(int sig)
769 MSVCRT___sighandler_t handler;
771 TRACE("(%d)\n", sig);
781 case MSVCRT_SIGBREAK:
782 handler = sighandlers[sig];
783 if (handler == MSVCRT_SIG_DFL) MSVCRT__exit(3);
784 if (handler != MSVCRT_SIG_IGN)
786 sighandlers[sig] = MSVCRT_SIG_DFL;
787 if (sig == MSVCRT_SIGFPE)
788 ((float_handler)handler)(sig, MSVCRT__FPE_EXPLICITGEN);
799 /*********************************************************************
800 * _XcptFilter (MSVCRT.@)
802 int CDECL _XcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
804 TRACE("(%08x,%p)\n", ex, ptr);
805 /* I assume ptr->ExceptionRecord->ExceptionCode is the same as ex */
806 return msvcrt_exception_filter(ptr);
809 /*********************************************************************
810 * _abnormal_termination (MSVCRT.@)
812 int CDECL _abnormal_termination(void)
814 FIXME("(void)stub\n");
818 /******************************************************************
819 * MSVCRT___uncaught_exception
821 BOOL CDECL MSVCRT___uncaught_exception(void)
826 /* _set_security_error_handler - not exported in native msvcrt, added in msvcr70 */
827 MSVCRT_security_error_handler CDECL _set_security_error_handler(
828 MSVCRT_security_error_handler handler )
830 MSVCRT_security_error_handler old = security_error_handler;
832 TRACE("(%p)\n", handler);
834 security_error_handler = handler;