Sync with trunk head
[reactos.git] / dll / win32 / rpcrt4 / cstub.c
1 /*
2 * COM stub (CStdStubBuffer) implementation
3 *
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
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 <stdarg.h>
25
26 #define COBJMACROS
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "excpt.h"
32
33 #include "objbase.h"
34 #include "rpcproxy.h"
35
36 #include "wine/debug.h"
37 #include "wine/exception.h"
38
39 #include "cpsf.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42
43 #define STUB_HEADER(This) (((const CInterfaceStubHeader*)((This)->lpVtbl))[-1])
44
45 static LONG WINAPI stub_filter(EXCEPTION_POINTERS *eptr)
46 {
47 if (eptr->ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE)
48 return EXCEPTION_CONTINUE_SEARCH;
49 return EXCEPTION_EXECUTE_HANDLER;
50 }
51
52 typedef struct
53 {
54 IUnknownVtbl *base_obj;
55 IRpcStubBuffer *base_stub;
56 CStdStubBuffer stub_buffer;
57 } cstdstubbuffer_delegating_t;
58
59 static inline cstdstubbuffer_delegating_t *impl_from_delegating( IRpcStubBuffer *iface )
60 {
61 return (cstdstubbuffer_delegating_t*)((char *)iface - FIELD_OFFSET(cstdstubbuffer_delegating_t, stub_buffer));
62 }
63
64 HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
65 LPUNKNOWN pUnkServer,
66 PCInterfaceName name,
67 CInterfaceStubVtbl *vtbl,
68 LPPSFACTORYBUFFER pPSFactory,
69 LPRPCSTUBBUFFER *ppStub)
70 {
71 CStdStubBuffer *This;
72 IUnknown *pvServer;
73 HRESULT r;
74 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
75 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
76 TRACE("vtbl=%p\n", &vtbl->Vtbl);
77
78 if (!IsEqualGUID(vtbl->header.piid, riid)) {
79 ERR("IID mismatch during stub creation\n");
80 return RPC_E_UNEXPECTED;
81 }
82
83 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
84 if(FAILED(r))
85 return r;
86
87 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
88 if (!This) {
89 IUnknown_Release(pvServer);
90 return E_OUTOFMEMORY;
91 }
92
93 This->lpVtbl = &vtbl->Vtbl;
94 This->RefCount = 1;
95 This->pvServerObject = pvServer;
96 This->pPSFactory = pPSFactory;
97 *ppStub = (LPRPCSTUBBUFFER)This;
98
99 IPSFactoryBuffer_AddRef(pPSFactory);
100 return S_OK;
101 }
102
103 static CRITICAL_SECTION delegating_vtbl_section;
104 static CRITICAL_SECTION_DEBUG critsect_debug =
105 {
106 0, 0, &delegating_vtbl_section,
107 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
108 0, 0, { (DWORD_PTR)(__FILE__ ": delegating_vtbl_section") }
109 };
110 static CRITICAL_SECTION delegating_vtbl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
111
112 typedef struct
113 {
114 DWORD ref;
115 DWORD size;
116 void **methods;
117 IUnknownVtbl vtbl;
118 /* remaining entries in vtbl */
119 } ref_counted_vtbl;
120
121 static struct
122 {
123 ref_counted_vtbl *table;
124 } current_vtbl;
125
126
127 static HRESULT WINAPI delegating_QueryInterface(IUnknown *pUnk, REFIID iid, void **ppv)
128 {
129 *ppv = (void *)pUnk;
130 return S_OK;
131 }
132
133 static ULONG WINAPI delegating_AddRef(IUnknown *pUnk)
134 {
135 return 1;
136 }
137
138 static ULONG WINAPI delegating_Release(IUnknown *pUnk)
139 {
140 return 1;
141 }
142
143 #if defined(__i386__)
144
145 /* The idea here is to replace the first param on the stack
146 ie. This (which will point to cstdstubbuffer_delegating_t)
147 with This->stub_buffer.pvServerObject and then jump to the
148 relevant offset in This->stub_buffer.pvServerObject's vtbl.
149 */
150 #include "pshpack1.h"
151 typedef struct {
152 DWORD mov1; /* mov 0x4(%esp), %eax 8b 44 24 04 */
153 WORD mov2; /* mov 0x10(%eax), %eax 8b 40 */
154 BYTE sixteen; /* 10 */
155 DWORD mov3; /* mov %eax, 0x4(%esp) 89 44 24 04 */
156 WORD mov4; /* mov (%eax), %eax 8b 00 */
157 WORD mov5; /* mov offset(%eax), %eax 8b 80 */
158 DWORD offset; /* xx xx xx xx */
159 WORD jmp; /* jmp *%eax ff e0 */
160 BYTE pad[3]; /* lea 0x0(%esi), %esi 8d 76 00 */
161 } vtbl_method_t;
162 #include "poppack.h"
163
164 static void fill_table(IUnknownVtbl *vtbl, void **methods, DWORD num)
165 {
166 vtbl_method_t *method;
167 void **entry;
168 DWORD i;
169
170 vtbl->QueryInterface = delegating_QueryInterface;
171 vtbl->AddRef = delegating_AddRef;
172 vtbl->Release = delegating_Release;
173
174 method = (vtbl_method_t*)methods;
175 entry = (void**)(vtbl + 1);
176
177 for(i = 3; i < num; i++)
178 {
179 *entry = method;
180 method->mov1 = 0x0424448b;
181 method->mov2 = 0x408b;
182 method->sixteen = 0x10;
183 method->mov3 = 0x04244489;
184 method->mov4 = 0x008b;
185 method->mov5 = 0x808b;
186 method->offset = i << 2;
187 method->jmp = 0xe0ff;
188 method->pad[0] = 0x8d;
189 method->pad[1] = 0x76;
190 method->pad[2] = 0x00;
191
192 method++;
193 entry++;
194 }
195 }
196
197 #else /* __i386__ */
198
199 typedef struct {int dummy;} vtbl_method_t;
200 static void fill_table(IUnknownVtbl *vtbl, void **methods, DWORD num)
201 {
202 ERR("delegated stubs are not supported on this architecture\n");
203 }
204
205 #endif /* __i386__ */
206
207 void create_delegating_vtbl(DWORD num_methods)
208 {
209 TRACE("%d\n", num_methods);
210 if(num_methods <= 3)
211 {
212 ERR("should have more than %d methods\n", num_methods);
213 return;
214 }
215
216 EnterCriticalSection(&delegating_vtbl_section);
217 if(!current_vtbl.table || num_methods > current_vtbl.table->size)
218 {
219 DWORD size;
220 DWORD old_protect;
221 if(current_vtbl.table && current_vtbl.table->ref == 0)
222 {
223 TRACE("freeing old table\n");
224 VirtualFree(current_vtbl.table->methods, 0, MEM_RELEASE);
225 HeapFree(GetProcessHeap(), 0, current_vtbl.table);
226 }
227 size = (num_methods - 3) * sizeof(vtbl_method_t);
228 current_vtbl.table = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(ref_counted_vtbl, vtbl) + num_methods * sizeof(void*));
229 current_vtbl.table->ref = 0;
230 current_vtbl.table->size = num_methods;
231 current_vtbl.table->methods = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
232 fill_table(&current_vtbl.table->vtbl, current_vtbl.table->methods, num_methods);
233 VirtualProtect(current_vtbl.table->methods, size, PAGE_EXECUTE_READ, &old_protect);
234 }
235 LeaveCriticalSection(&delegating_vtbl_section);
236 }
237
238 static IUnknownVtbl *get_delegating_vtbl(void)
239 {
240 IUnknownVtbl *ret;
241
242 EnterCriticalSection(&delegating_vtbl_section);
243 current_vtbl.table->ref++;
244 ret = &current_vtbl.table->vtbl;
245 LeaveCriticalSection(&delegating_vtbl_section);
246 return ret;
247 }
248
249 static void release_delegating_vtbl(IUnknownVtbl *vtbl)
250 {
251 ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1);
252
253 EnterCriticalSection(&delegating_vtbl_section);
254 table->ref--;
255 TRACE("ref now %d\n", table->ref);
256 if(table->ref == 0 && table != current_vtbl.table)
257 {
258 TRACE("... and we're not current so free'ing\n");
259 VirtualFree(current_vtbl.table->methods, 0, MEM_RELEASE);
260 HeapFree(GetProcessHeap(), 0, table);
261 }
262 LeaveCriticalSection(&delegating_vtbl_section);
263 }
264
265 HRESULT WINAPI CStdStubBuffer_Delegating_Construct(REFIID riid,
266 LPUNKNOWN pUnkServer,
267 PCInterfaceName name,
268 CInterfaceStubVtbl *vtbl,
269 REFIID delegating_iid,
270 LPPSFACTORYBUFFER pPSFactory,
271 LPRPCSTUBBUFFER *ppStub)
272 {
273 cstdstubbuffer_delegating_t *This;
274 IUnknown *pvServer;
275 HRESULT r;
276
277 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
278 TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl->header.piid), debugstr_guid(delegating_iid));
279 TRACE("vtbl=%p\n", &vtbl->Vtbl);
280
281 if (!IsEqualGUID(vtbl->header.piid, riid))
282 {
283 ERR("IID mismatch during stub creation\n");
284 return RPC_E_UNEXPECTED;
285 }
286
287 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
288 if(FAILED(r)) return r;
289
290 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
291 if (!This)
292 {
293 IUnknown_Release(pvServer);
294 return E_OUTOFMEMORY;
295 }
296
297 This->base_obj = get_delegating_vtbl();
298 r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
299 if(FAILED(r))
300 {
301 release_delegating_vtbl(This->base_obj);
302 HeapFree(GetProcessHeap(), 0, This);
303 IUnknown_Release(pvServer);
304 return r;
305 }
306
307 This->stub_buffer.lpVtbl = &vtbl->Vtbl;
308 This->stub_buffer.RefCount = 1;
309 This->stub_buffer.pvServerObject = pvServer;
310 This->stub_buffer.pPSFactory = pPSFactory;
311 *ppStub = (LPRPCSTUBBUFFER)&This->stub_buffer;
312
313 IPSFactoryBuffer_AddRef(pPSFactory);
314 return S_OK;
315 }
316
317 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
318 REFIID riid,
319 LPVOID *obj)
320 {
321 CStdStubBuffer *This = (CStdStubBuffer *)iface;
322 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
323
324 if (IsEqualIID(&IID_IUnknown, riid) ||
325 IsEqualIID(&IID_IRpcStubBuffer, riid))
326 {
327 IUnknown_AddRef(iface);
328 *obj = iface;
329 return S_OK;
330 }
331 *obj = NULL;
332 return E_NOINTERFACE;
333 }
334
335 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
336 {
337 CStdStubBuffer *This = (CStdStubBuffer *)iface;
338 TRACE("(%p)->AddRef()\n",This);
339 return InterlockedIncrement(&This->RefCount);
340 }
341
342 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
343 LPPSFACTORYBUFFER pPSF)
344 {
345 CStdStubBuffer *This = (CStdStubBuffer *)iface;
346 ULONG refs;
347
348 TRACE("(%p)->Release()\n",This);
349
350 refs = InterlockedDecrement(&This->RefCount);
351 if (!refs)
352 {
353 /* test_Release shows that native doesn't call Disconnect here.
354 We'll leave it in for the time being. */
355 IRpcStubBuffer_Disconnect(iface);
356
357 IPSFactoryBuffer_Release(pPSF);
358 HeapFree(GetProcessHeap(),0,This);
359 }
360 return refs;
361 }
362
363 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
364 LPPSFACTORYBUFFER pPSF)
365 {
366 cstdstubbuffer_delegating_t *This = impl_from_delegating( iface );
367 ULONG refs;
368
369 TRACE("(%p)->Release()\n", This);
370
371 refs = InterlockedDecrement(&This->stub_buffer.RefCount);
372 if (!refs)
373 {
374 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
375 Disconnect here */
376 IRpcStubBuffer_Disconnect((IRpcStubBuffer *)&This->stub_buffer);
377
378 IRpcStubBuffer_Release(This->base_stub);
379 release_delegating_vtbl(This->base_obj);
380
381 IPSFactoryBuffer_Release(pPSF);
382 HeapFree(GetProcessHeap(), 0, This);
383 }
384
385 return refs;
386 }
387
388 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
389 LPUNKNOWN lpUnkServer)
390 {
391 CStdStubBuffer *This = (CStdStubBuffer *)iface;
392 HRESULT r;
393 IUnknown *new = NULL;
394
395 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
396
397 r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
398 new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
399 if(new)
400 IUnknown_Release(new);
401 return r;
402 }
403
404 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
405 {
406 CStdStubBuffer *This = (CStdStubBuffer *)iface;
407 IUnknown *old;
408 TRACE("(%p)->Disconnect()\n",This);
409
410 old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
411
412 if(old)
413 IUnknown_Release(old);
414 }
415
416 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
417 PRPCOLEMESSAGE pMsg,
418 LPRPCCHANNELBUFFER pChannel)
419 {
420 CStdStubBuffer *This = (CStdStubBuffer *)iface;
421 DWORD dwPhase = STUB_UNMARSHAL;
422 HRESULT hr = S_OK;
423
424 TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
425
426 __TRY
427 {
428 if (STUB_HEADER(This).pDispatchTable)
429 STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
430 else /* pure interpreted */
431 NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
432 }
433 __EXCEPT(stub_filter)
434 {
435 DWORD dwExceptionCode = GetExceptionCode();
436 WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
437 if (FAILED(dwExceptionCode))
438 hr = dwExceptionCode;
439 else
440 hr = HRESULT_FROM_WIN32(dwExceptionCode);
441 }
442 __ENDTRY
443
444 return hr;
445 }
446
447 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
448 REFIID riid)
449 {
450 CStdStubBuffer *This = (CStdStubBuffer *)iface;
451 TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
452 return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
453 }
454
455 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
456 {
457 CStdStubBuffer *This = (CStdStubBuffer *)iface;
458 TRACE("(%p)->CountRefs()\n",This);
459 return This->RefCount;
460 }
461
462 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
463 LPVOID *ppv)
464 {
465 CStdStubBuffer *This = (CStdStubBuffer *)iface;
466 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
467 return S_OK;
468 }
469
470 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
471 LPVOID pv)
472 {
473 CStdStubBuffer *This = (CStdStubBuffer *)iface;
474 TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
475 }
476
477 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
478 {
479 CStdStubBuffer_QueryInterface,
480 CStdStubBuffer_AddRef,
481 NULL,
482 CStdStubBuffer_Connect,
483 CStdStubBuffer_Disconnect,
484 CStdStubBuffer_Invoke,
485 CStdStubBuffer_IsIIDSupported,
486 CStdStubBuffer_CountRefs,
487 CStdStubBuffer_DebugServerQueryInterface,
488 CStdStubBuffer_DebugServerRelease
489 };
490
491 static HRESULT WINAPI CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface,
492 LPUNKNOWN lpUnkServer)
493 {
494 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
495 HRESULT r;
496 TRACE("(%p)->Connect(%p)\n", This, lpUnkServer);
497
498 r = CStdStubBuffer_Connect(iface, lpUnkServer);
499 if(SUCCEEDED(r))
500 r = IRpcStubBuffer_Connect(This->base_stub, (IUnknown*)&This->base_obj);
501
502 return r;
503 }
504
505 static void WINAPI CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface)
506 {
507 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
508 TRACE("(%p)->Disconnect()\n", This);
509
510 IRpcStubBuffer_Disconnect(This->base_stub);
511 CStdStubBuffer_Disconnect(iface);
512 }
513
514 static ULONG WINAPI CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface)
515 {
516 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
517 ULONG ret;
518 TRACE("(%p)->CountRefs()\n", This);
519
520 ret = CStdStubBuffer_CountRefs(iface);
521 ret += IRpcStubBuffer_CountRefs(This->base_stub);
522
523 return ret;
524 }
525
526 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl =
527 {
528 CStdStubBuffer_QueryInterface,
529 CStdStubBuffer_AddRef,
530 NULL,
531 CStdStubBuffer_Delegating_Connect,
532 CStdStubBuffer_Delegating_Disconnect,
533 CStdStubBuffer_Invoke,
534 CStdStubBuffer_IsIIDSupported,
535 CStdStubBuffer_Delegating_CountRefs,
536 CStdStubBuffer_DebugServerQueryInterface,
537 CStdStubBuffer_DebugServerRelease
538 };
539
540 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
541 {
542 CStdStubBuffer *This = (CStdStubBuffer *)iface;
543 return STUB_HEADER(This).pServerInfo;
544 }
545
546 /************************************************************************
547 * NdrStubForwardingFunction [RPCRT4.@]
548 */
549 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *iface, IRpcChannelBuffer *pChannel,
550 PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
551 {
552 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
553
554 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
555 HRESULT r = IRpcStubBuffer_Invoke(This->base_stub, (RPCOLEMESSAGE*)pMsg, pChannel);
556 if(FAILED(r)) RpcRaiseException(r);
557 return;
558 }
559
560 /***********************************************************************
561 * NdrStubInitialize [RPCRT4.@]
562 */
563 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
564 PMIDL_STUB_MESSAGE pStubMsg,
565 PMIDL_STUB_DESC pStubDescriptor,
566 LPRPCCHANNELBUFFER pRpcChannelBuffer)
567 {
568 TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
569 NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
570 pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
571 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
572 &pStubMsg->dwDestContext,
573 &pStubMsg->pvDestContext);
574 }
575
576 /***********************************************************************
577 * NdrStubGetBuffer [RPCRT4.@]
578 */
579 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER iface,
580 LPRPCCHANNELBUFFER pRpcChannelBuffer,
581 PMIDL_STUB_MESSAGE pStubMsg)
582 {
583 CStdStubBuffer *This = (CStdStubBuffer *)iface;
584 HRESULT hr;
585
586 TRACE("(%p, %p, %p)\n", This, pRpcChannelBuffer, pStubMsg);
587
588 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
589 hr = IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer,
590 (RPCOLEMESSAGE *)pStubMsg->RpcMsg, STUB_HEADER(This).piid);
591 if (FAILED(hr))
592 {
593 RpcRaiseException(hr);
594 return;
595 }
596
597 pStubMsg->Buffer = pStubMsg->RpcMsg->Buffer;
598 }