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