widl: Raise RPC_X_SS_IN_NULL_CONTEXT exception for NULL in-only context handles inste...
[wine] / tools / widl / server.c
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2005-2006 Eric Kohl
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 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <string.h>
30 #include <ctype.h>
31
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36
37 #include "typegen.h"
38
39 static FILE* server;
40 static int indent = 0;
41
42
43 static void print_server(const char *format, ...)
44 {
45     va_list va;
46     va_start(va, format);
47     print(server, indent, format, va);
48     va_end(va);
49 }
50
51 static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
52 {
53     const func_t *func;
54     const var_t *var;
55     const var_t* explicit_handle_var;
56
57     if (!iface->funcs) return;
58     LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
59     {
60         const var_t *def = func->def;
61         int has_full_pointer = is_full_pointer_function(func);
62
63         /* check for a defined binding handle */
64         explicit_handle_var = get_explicit_handle_var(func);
65
66         fprintf(server, "void __RPC_STUB\n");
67         fprintf(server, "%s_", iface->name);
68         write_name(server, def);
69         fprintf(server, "(\n");
70         indent++;
71         print_server("PRPC_MESSAGE _pRpcMessage)\n");
72         indent--;
73
74         /* write the functions body */
75         fprintf(server, "{\n");
76         indent++;
77
78         /* Declare arguments */
79         declare_stub_args(server, indent, func);
80
81         print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
82         print_server("RPC_STATUS _Status;\n");
83         fprintf(server, "\n");
84
85
86         print_server("((void)(_Status));\n");
87         print_server("NdrServerInitializeNew(\n");
88         indent++;
89         print_server("_pRpcMessage,\n");
90         print_server("&_StubMsg,\n");
91         print_server("&%s_StubDesc);\n", iface->name);
92         indent--;
93         fprintf(server, "\n");
94
95         write_parameters_init(server, indent, func);
96
97         if (explicit_handle_var)
98         {
99             print_server("%s = _pRpcMessage->Handle;\n", explicit_handle_var->name);
100             fprintf(server, "\n");
101         }
102
103         print_server("RpcTryFinally\n");
104         print_server("{\n");
105         indent++;
106         print_server("RpcTryExcept\n");
107         print_server("{\n");
108         indent++;
109
110         if (has_full_pointer)
111             write_full_pointer_init(server, indent, func, TRUE);
112
113         if (func->args)
114         {
115             print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
116             indent++;
117             print_server("NdrConvert(\n");
118             indent++;
119             print_server("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
120             print_server("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", *proc_offset);
121             indent -= 2;
122             fprintf(server, "\n");
123
124             /* unmarshall arguments */
125             write_remoting_arguments(server, indent, func, PASS_IN, PHASE_UNMARSHAL);
126         }
127
128         print_server("if (_StubMsg.Buffer > _StubMsg.BufferEnd)\n");
129         print_server("{\n");
130         indent++;
131         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
132         indent--;
133         print_server("}\n");
134         indent--;
135         print_server("}\n");
136         print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
137         print_server("{\n");
138         indent++;
139         print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
140         indent--;
141         print_server("}\n");
142         print_server("RpcEndExcept\n");
143         fprintf(server, "\n");
144
145         /* Assign 'out' arguments */
146         assign_stub_out_args(server, indent, func);
147
148         /* Call the real server function */
149         if (!is_void(get_func_return_type(func)))
150             print_server("_RetVal = ");
151         else
152             print_server("");
153         write_prefix_name(server, prefix_server, def);
154
155         if (func->args)
156         {
157             int first_arg = 1;
158
159             fprintf(server, "(\n");
160             indent++;
161             LIST_FOR_EACH_ENTRY( var, func->args, const var_t, entry )
162             {
163                 if (first_arg)
164                     first_arg = 0;
165                 else
166                     fprintf(server, ",\n");
167                 if (is_context_handle(var->type))
168                 {
169                     /* if the context_handle attribute appears in the chain of types
170                      * without pointers being followed, then the context handle must
171                      * be direct, otherwise it is a pointer */
172                     int is_ch_ptr = is_aliaschain_attr(var->type, ATTR_CONTEXTHANDLE) ? FALSE : TRUE;
173                     print_server("(");
174                     write_type_decl_left(server, var->type);
175                     fprintf(server, ")%sNDRSContextValue(%s)", is_ch_ptr ? "" : "*", var->name);
176                 }
177                 else
178                 {
179                     print_server("");
180                     if (var->type->declarray)
181                         fprintf(server, "*");
182                     write_name(server, var);
183                 }
184             }
185             fprintf(server, ");\n");
186             indent--;
187         }
188         else
189         {
190             fprintf(server, "();\n");
191         }
192
193         if (has_out_arg_or_return(func))
194         {
195             write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_BUFFERSIZE);
196
197             if (!is_void(get_func_return_type(func)))
198                 write_remoting_arguments(server, indent, func, PASS_RETURN, PHASE_BUFFERSIZE);
199
200             print_server("_pRpcMessage->BufferLength = _StubMsg.BufferLength;\n");
201             fprintf(server, "\n");
202             print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
203             print_server("if (_Status)\n");
204             indent++;
205             print_server("RpcRaiseException(_Status);\n");
206             indent--;
207             fprintf(server, "\n");
208             print_server("_StubMsg.Buffer = (unsigned char *)_pRpcMessage->Buffer;\n");
209             fprintf(server, "\n");
210         }
211
212         /* marshall arguments */
213         write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_MARSHAL);
214
215         /* marshall the return value */
216         if (!is_void(get_func_return_type(func)))
217             write_remoting_arguments(server, indent, func, PASS_RETURN, PHASE_MARSHAL);
218
219         indent--;
220         print_server("}\n");
221         print_server("RpcFinally\n");
222         print_server("{\n");
223         indent++;
224
225         write_remoting_arguments(server, indent, func, PASS_OUT, PHASE_FREE);
226
227         if (has_full_pointer)
228             write_full_pointer_free(server, indent, func);
229
230         indent--;
231         print_server("}\n");
232         print_server("RpcEndFinally\n");
233
234         /* calculate buffer length */
235         fprintf(server, "\n");
236         print_server("_pRpcMessage->BufferLength =\n");
237         indent++;
238         print_server("(unsigned int)(_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer);\n");
239         indent--;
240         indent--;
241         fprintf(server, "}\n");
242         fprintf(server, "\n");
243
244         /* update proc_offset */
245         *proc_offset += get_size_procformatstring_func( func );
246     }
247 }
248
249
250 static void write_dispatchtable(type_t *iface)
251 {
252     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
253     unsigned long method_count = 0;
254     const func_t *func;
255
256     print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
257     print_server("{\n");
258     indent++;
259
260     if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
261     {
262         var_t *def = func->def;
263
264         print_server("%s_", iface->name);
265         write_name(server, def);
266         fprintf(server, ",\n");
267
268         method_count++;
269     }
270     print_server("0\n");
271     indent--;
272     print_server("};\n");
273     print_server("RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
274     print_server("{\n");
275     indent++;
276     print_server("%u,\n", method_count);
277     print_server("%s_table\n", iface->name);
278     indent--;
279     print_server("};\n");
280     fprintf(server, "\n");
281 }
282
283
284 static void write_stubdescdecl(type_t *iface)
285 {
286     print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
287     fprintf(server, "\n");
288 }
289
290
291 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
292 {
293     print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
294     print_server("{\n");
295     indent++;
296     print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
297     print_server("MIDL_user_allocate,\n");
298     print_server("MIDL_user_free,\n");
299     print_server("{\n");
300     indent++;
301     print_server("0,\n");
302     indent--;
303     print_server("},\n");
304     print_server("0,\n");
305     print_server("0,\n");
306     if (expr_eval_routines)
307         print_server("ExprEvalRoutines,\n");
308     else
309         print_server("0,\n");
310     print_server("0,\n");
311     print_server("__MIDL_TypeFormatString.Format,\n");
312     print_server("1, /* -error bounds_check flag */\n");
313     print_server("0x10001, /* Ndr library version */\n");
314     print_server("0,\n");
315     print_server("0x50100a4, /* MIDL Version 5.1.164 */\n");
316     print_server("0,\n");
317     print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
318     print_server("0,  /* notify & notify_flag routine table */\n");
319     print_server("1,  /* Flags */\n");
320     print_server("0,  /* Reserved3 */\n");
321     print_server("0,  /* Reserved4 */\n");
322     print_server("0   /* Reserved5 */\n");
323     indent--;
324     print_server("};\n");
325     fprintf(server, "\n");
326 }
327
328
329 static void write_serverinterfacedecl(type_t *iface)
330 {
331     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
332     UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
333     const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
334
335     if (endpoints) write_endpoints( server, iface->name, endpoints );
336
337     print_server("extern RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
338     fprintf(server, "\n");
339     print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
340     print_server("{\n");
341     indent++;
342     print_server("sizeof(RPC_SERVER_INTERFACE),\n");
343     print_server("{{0x%08lx,0x%04x,0x%04x,{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}},{%d,%d}},\n",
344                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
345                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
346                  uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
347     print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
348     print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
349     if (endpoints)
350     {
351         print_server("%u,\n", list_count(endpoints));
352         print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
353     }
354     else
355     {
356         print_server("0,\n");
357         print_server("0,\n");
358     }
359     print_server("0,\n");
360     print_server("0,\n");
361     print_server("0,\n");
362     indent--;
363     print_server("};\n");
364     if (old_names)
365         print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
366                      iface->name, iface->name);
367     else
368         print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
369                      prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
370     fprintf(server, "\n");
371 }
372
373
374 static void init_server(void)
375 {
376     if (server)
377         return;
378     if (!(server = fopen(server_name, "w")))
379         error("Could not open %s for output\n", server_name);
380
381     print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
382     print_server("#include <string.h>\n");
383     fprintf(server, "\n");
384     print_server("#include \"%s\"\n", header_name);
385     print_server("\n");
386     print_server("#ifndef USE_COMPILER_EXCEPTIONS\n");
387     print_server("\n");
388     print_server("#include \"wine/exception.h\"\n");
389     print_server("#undef RpcTryExcept\n");
390     print_server("#undef RpcExcept\n");
391     print_server("#undef RpcEndExcept\n");
392     print_server("#undef RpcExceptionCode\n");
393     print_server("\n");
394     print_server( "struct __server_frame\n");
395     print_server( "{\n");
396     print_server( "    EXCEPTION_REGISTRATION_RECORD frame;\n");
397     print_server( "    sigjmp_buf                    jmp;\n");
398     print_server( "};\n");
399     print_server( "\n");
400     print_server("static DWORD __server_exception_handler( EXCEPTION_RECORD *record,\n");
401     print_server("                                         EXCEPTION_REGISTRATION_RECORD *frame,\n");
402     print_server("                                         CONTEXT *context,\n");
403     print_server("                                         EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
404     print_server("{\n");
405     print_server("    struct __server_frame *server_frame = (struct __server_frame *)frame;\n");
406     print_server("\n");
407     print_server("    if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
408     print_server("        return ExceptionContinueSearch;\n");
409     print_server("\n");
410     print_server("    if (record->ExceptionCode != STATUS_ACCESS_VIOLATION &&\n");
411     print_server("        record->ExceptionCode != STATUS_DATATYPE_MISALIGNMENT &&\n");
412     print_server("        record->ExceptionCode != RPC_X_BAD_STUB_DATA &&\n");
413     print_server("        record->ExceptionCode != RPC_S_INVALID_BOUND)\n");
414     print_server("        return ExceptionContinueSearch;\n");
415     print_server("\n");
416     print_server("    __wine_rtl_unwind( frame, record );\n");
417     print_server("    __wine_pop_frame( frame );\n");
418     print_server("    siglongjmp( server_frame->jmp, 1 );\n");
419     print_server("}\n");
420     print_server("#define RpcTryExcept \\\n");
421     print_server("    do { \\\n");
422     print_server("        struct __server_frame __server_frame; \\\n");
423     print_server("        __server_frame.frame.Handler = __server_exception_handler; \\\n");
424     print_server("        if (!sigsetjmp( __server_frame.jmp, 0 )) \\\n");
425     print_server("        { \\\n");
426     print_server("            __wine_push_frame( &__server_frame.frame ); \\\n");
427     print_server("            {\n");
428     print_server("\n");
429     print_server("#define RpcExcept(expr) \\\n");
430     print_server("            } \\\n");
431     print_server("            __wine_pop_frame( &__server_frame.frame ); \\\n");
432     print_server("        } \\\n");
433     print_server("        else \\\n");
434     print_server("        {\n");
435     print_server("\n");
436     print_server("#define RpcEndExcept \\\n");
437     print_server("        } \\\n");
438     print_server("    } while(0);\n");
439     print_server("\n");
440     print_server("#endif /* USE_COMPILER_EXCEPTIONS */\n");
441     print_server("\n");
442 }
443
444
445 static void write_server_stmts(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
446 {
447     const statement_t *stmt;
448     if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
449     {
450         if (stmt->type == STMT_LIBRARY)
451             write_server_stmts(stmt->u.lib->stmts, expr_eval_routines, proc_offset);
452         else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
453         {
454             type_t *iface = stmt->u.type;
455             if (!need_stub(iface))
456                 continue;
457
458             fprintf(server, "/*****************************************************************************\n");
459             fprintf(server, " * %s interface\n", iface->name);
460             fprintf(server, " */\n");
461             fprintf(server, "\n");
462
463             if (iface->funcs)
464             {
465                 write_serverinterfacedecl(iface);
466                 write_stubdescdecl(iface);
467
468                 write_function_stubs(iface, proc_offset);
469
470                 print_server("#if !defined(__RPC_WIN32__)\n");
471                 print_server("#error  Invalid build platform for this stub.\n");
472                 print_server("#endif\n");
473
474                 fprintf(server, "\n");
475                 write_stubdescriptor(iface, expr_eval_routines);
476                 write_dispatchtable(iface);
477             }
478         }
479     }
480 }
481
482 void write_server(const statement_list_t *stmts)
483 {
484     unsigned int proc_offset = 0;
485     int expr_eval_routines;
486
487     if (!do_server)
488         return;
489     if (do_everything && !need_stub_files(stmts))
490         return;
491
492     init_server();
493     if (!server)
494         return;
495
496     write_formatstringsdecl(server, indent, stmts, need_stub);
497     expr_eval_routines = write_expr_eval_routines(server, server_token);
498     if (expr_eval_routines)
499         write_expr_eval_routine_list(server, server_token);
500     write_user_quad_list(server);
501
502     write_server_stmts(stmts, expr_eval_routines, &proc_offset);
503
504     fprintf(server, "\n");
505
506     write_procformatstring(server, stmts, need_stub);
507     write_typeformatstring(server, stmts, need_stub);
508
509     fclose(server);
510 }