msvcrt: NULL terminate program arguments list in __getmainargs.
[wine] / dlls / msvcrt / except_x86_64.c
1 /*
2  * msvcrt C++ exception handling
3  *
4  * Copyright 2011 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #ifdef __x86_64__
25
26 #include <stdarg.h>
27
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winternl.h"
33 #include "msvcrt.h"
34 #include "wine/exception.h"
35 #include "excpt.h"
36 #include "wine/debug.h"
37
38 #include "cppexcept.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(seh);
41
42 struct _DISPATCHER_CONTEXT;
43
44 typedef LONG (WINAPI *PC_LANGUAGE_EXCEPTION_HANDLER)( EXCEPTION_POINTERS *ptrs, ULONG64 frame );
45 typedef EXCEPTION_DISPOSITION (WINAPI *PEXCEPTION_ROUTINE)( EXCEPTION_RECORD *rec,
46                                                             ULONG64 frame,
47                                                             CONTEXT *context,
48                                                             struct _DISPATCHER_CONTEXT *dispatch );
49
50 typedef struct _DISPATCHER_CONTEXT
51 {
52     ULONG64               ControlPc;
53     ULONG64               ImageBase;
54     PRUNTIME_FUNCTION     FunctionEntry;
55     ULONG64               EstablisherFrame;
56     ULONG64               TargetIp;
57     PCONTEXT              ContextRecord;
58     PEXCEPTION_ROUTINE    LanguageHandler;
59     PVOID                 HandlerData;
60     PUNWIND_HISTORY_TABLE HistoryTable;
61     ULONG                 ScopeIndex;
62 } DISPATCHER_CONTEXT;
63
64
65 /*********************************************************************
66  *              __CxxFrameHandler (MSVCRT.@)
67  */
68 EXCEPTION_DISPOSITION CDECL __CxxFrameHandler( EXCEPTION_RECORD *rec, ULONG64 frame,
69                                                CONTEXT *context, DISPATCHER_CONTEXT *dispatch )
70 {
71     FIXME( "%p %lx %p %p: not implemented\n", rec, frame, context, dispatch );
72     return ExceptionContinueSearch;
73 }
74
75
76 /*********************************************************************
77  *              __CppXcptFilter (MSVCRT.@)
78  */
79 int CDECL __CppXcptFilter(NTSTATUS ex, PEXCEPTION_POINTERS ptr)
80 {
81     /* only filter c++ exceptions */
82     if (ex != CXX_EXCEPTION) return EXCEPTION_CONTINUE_SEARCH;
83     return _XcptFilter( ex, ptr );
84 }
85
86
87 /*********************************************************************
88  *              __CxxDetectRethrow (MSVCRT.@)
89  */
90 BOOL CDECL __CxxDetectRethrow(PEXCEPTION_POINTERS ptrs)
91 {
92     PEXCEPTION_RECORD rec;
93
94     if (!ptrs)
95         return FALSE;
96
97     rec = ptrs->ExceptionRecord;
98
99     if (rec->ExceptionCode == CXX_EXCEPTION &&
100         rec->NumberParameters == 3 &&
101         rec->ExceptionInformation[0] == CXX_FRAME_MAGIC_VC6 &&
102         rec->ExceptionInformation[2])
103     {
104         ptrs->ExceptionRecord = msvcrt_get_thread_data()->exc_record;
105         return TRUE;
106     }
107     return (msvcrt_get_thread_data()->exc_record == rec);
108 }
109
110
111 /*********************************************************************
112  *              __CxxQueryExceptionSize (MSVCRT.@)
113  */
114 unsigned int CDECL __CxxQueryExceptionSize(void)
115 {
116     return sizeof(cxx_exception_type);
117 }
118
119
120 /*******************************************************************
121  *              _setjmp (MSVCRT.@)
122  */
123 __ASM_GLOBAL_FUNC( MSVCRT__setjmp,
124                    "xorq %rdx,%rdx\n\t"  /* frame */
125                    "jmp " __ASM_NAME("MSVCRT__setjmpex") );
126
127 /*******************************************************************
128  *              _setjmpex (MSVCRT.@)
129  */
130 __ASM_GLOBAL_FUNC( MSVCRT__setjmpex,
131                    "movq %rdx,(%rcx)\n\t"          /* jmp_buf->Frame */
132                    "movq %rbx,0x8(%rcx)\n\t"       /* jmp_buf->Rbx */
133                    "leaq 0x8(%rsp),%rax\n\t"
134                    "movq %rax,0x10(%rcx)\n\t"      /* jmp_buf->Rsp */
135                    "movq %rbp,0x18(%rcx)\n\t"      /* jmp_buf->Rbp */
136                    "movq %rsi,0x20(%rcx)\n\t"      /* jmp_buf->Rsi */
137                    "movq %rdi,0x28(%rcx)\n\t"      /* jmp_buf->Rdi */
138                    "movq %r12,0x30(%rcx)\n\t"      /* jmp_buf->R12 */
139                    "movq %r13,0x38(%rcx)\n\t"      /* jmp_buf->R13 */
140                    "movq %r14,0x40(%rcx)\n\t"      /* jmp_buf->R14 */
141                    "movq %r15,0x48(%rcx)\n\t"      /* jmp_buf->R15 */
142                    "movq (%rsp),%rax\n\t"
143                    "movq %rax,0x50(%rcx)\n\t"      /* jmp_buf->Rip */
144                    "movdqa %xmm6,0x60(%rcx)\n\t"   /* jmp_buf->Xmm6 */
145                    "movdqa %xmm7,0x70(%rcx)\n\t"   /* jmp_buf->Xmm7 */
146                    "movdqa %xmm8,0x80(%rcx)\n\t"   /* jmp_buf->Xmm8 */
147                    "movdqa %xmm9,0x90(%rcx)\n\t"   /* jmp_buf->Xmm9 */
148                    "movdqa %xmm10,0xa0(%rcx)\n\t"  /* jmp_buf->Xmm10 */
149                    "movdqa %xmm11,0xb0(%rcx)\n\t"  /* jmp_buf->Xmm11 */
150                    "movdqa %xmm12,0xc0(%rcx)\n\t"  /* jmp_buf->Xmm12 */
151                    "movdqa %xmm13,0xd0(%rcx)\n\t"  /* jmp_buf->Xmm13 */
152                    "movdqa %xmm14,0xe0(%rcx)\n\t"  /* jmp_buf->Xmm14 */
153                    "movdqa %xmm15,0xf0(%rcx)\n\t"  /* jmp_buf->Xmm15 */
154                    "xorq %rax,%rax\n\t"
155                    "retq" );
156
157
158 extern void DECLSPEC_NORETURN CDECL longjmp_set_regs( struct MSVCRT___JUMP_BUFFER *jmp, int retval );
159 __ASM_GLOBAL_FUNC( longjmp_set_regs,
160                    "movq %rdx,%rax\n\t"            /* retval */
161                    "movq 0x8(%rcx),%rbx\n\t"       /* jmp_buf->Rbx */
162                    "movq 0x18(%rcx),%rbp\n\t"      /* jmp_buf->Rbp */
163                    "movq 0x20(%rcx),%rsi\n\t"      /* jmp_buf->Rsi */
164                    "movq 0x28(%rcx),%rdi\n\t"      /* jmp_buf->Rdi */
165                    "movq 0x30(%rcx),%r12\n\t"      /* jmp_buf->R12 */
166                    "movq 0x38(%rcx),%r13\n\t"      /* jmp_buf->R13 */
167                    "movq 0x40(%rcx),%r14\n\t"      /* jmp_buf->R14 */
168                    "movq 0x48(%rcx),%r15\n\t"      /* jmp_buf->R15 */
169                    "movdqa 0x60(%rcx),%xmm6\n\t"   /* jmp_buf->Xmm6 */
170                    "movdqa 0x70(%rcx),%xmm7\n\t"   /* jmp_buf->Xmm7 */
171                    "movdqa 0x80(%rcx),%xmm8\n\t"   /* jmp_buf->Xmm8 */
172                    "movdqa 0x90(%rcx),%xmm9\n\t"   /* jmp_buf->Xmm9 */
173                    "movdqa 0xa0(%rcx),%xmm10\n\t"  /* jmp_buf->Xmm10 */
174                    "movdqa 0xb0(%rcx),%xmm11\n\t"  /* jmp_buf->Xmm11 */
175                    "movdqa 0xc0(%rcx),%xmm12\n\t"  /* jmp_buf->Xmm12 */
176                    "movdqa 0xd0(%rcx),%xmm13\n\t"  /* jmp_buf->Xmm13 */
177                    "movdqa 0xe0(%rcx),%xmm14\n\t"  /* jmp_buf->Xmm14 */
178                    "movdqa 0xf0(%rcx),%xmm15\n\t"  /* jmp_buf->Xmm15 */
179                    "movq 0x50(%rcx),%rdx\n\t"      /* jmp_buf->Rip */
180                    "movq 0x10(%rcx),%rsp\n\t"      /* jmp_buf->Rsp */
181                    "jmp *%rdx" );
182
183 /*******************************************************************
184  *              longjmp (MSVCRT.@)
185  */
186 void __cdecl MSVCRT_longjmp( struct MSVCRT___JUMP_BUFFER *jmp, int retval )
187 {
188     EXCEPTION_RECORD rec;
189
190     if (!retval) retval = 1;
191     if (jmp->Frame)
192     {
193         rec.ExceptionCode = STATUS_LONGJUMP;
194         rec.ExceptionFlags = 0;
195         rec.ExceptionRecord = NULL;
196         rec.ExceptionAddress = NULL;
197         rec.NumberParameters = 1;
198         rec.ExceptionInformation[0] = (DWORD_PTR)jmp;
199         RtlUnwind( (void *)jmp->Frame, (void *)jmp->Rip, &rec, IntToPtr(retval) );
200     }
201     longjmp_set_regs( jmp, retval );
202 }
203
204 /*******************************************************************
205  *              _local_unwind (MSVCRT.@)
206  */
207 void __cdecl _local_unwind( void *frame, void *target )
208 {
209     RtlUnwind( frame, target, NULL, 0 );
210 }
211
212 #endif  /* __x86_64__ */