2 * COM stub (CStdStubBuffer) implementation
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2009 Alexandre Julliard
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
23 #include "wine/port.h"
37 #include "wine/debug.h"
38 #include "wine/exception.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44 #define STUB_HEADER(This) (((const CInterfaceStubHeader*)((This)->lpVtbl))[-1])
46 static LONG WINAPI stub_filter(EXCEPTION_POINTERS *eptr)
48 if (eptr->ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE)
49 return EXCEPTION_CONTINUE_SEARCH;
50 return EXCEPTION_EXECUTE_HANDLER;
55 IUnknownVtbl *base_obj;
56 IRpcStubBuffer *base_stub;
57 CStdStubBuffer stub_buffer;
58 } cstdstubbuffer_delegating_t;
60 static inline cstdstubbuffer_delegating_t *impl_from_delegating( IRpcStubBuffer *iface )
62 return (cstdstubbuffer_delegating_t*)((char *)iface - FIELD_OFFSET(cstdstubbuffer_delegating_t, stub_buffer));
65 HRESULT CStdStubBuffer_Construct(REFIID riid,
68 CInterfaceStubVtbl *vtbl,
69 LPPSFACTORYBUFFER pPSFactory,
70 LPRPCSTUBBUFFER *ppStub)
75 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
76 TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
77 TRACE("vtbl=%p\n", &vtbl->Vtbl);
79 if (!IsEqualGUID(vtbl->header.piid, riid)) {
80 ERR("IID mismatch during stub creation\n");
81 return RPC_E_UNEXPECTED;
84 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
88 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
90 IUnknown_Release(pvServer);
94 This->lpVtbl = &vtbl->Vtbl;
96 This->pvServerObject = pvServer;
97 This->pPSFactory = pPSFactory;
98 *ppStub = (LPRPCSTUBBUFFER)This;
100 IPSFactoryBuffer_AddRef(pPSFactory);
104 static CRITICAL_SECTION delegating_vtbl_section;
105 static CRITICAL_SECTION_DEBUG critsect_debug =
107 0, 0, &delegating_vtbl_section,
108 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
109 0, 0, { (DWORD_PTR)(__FILE__ ": delegating_vtbl_section") }
111 static CRITICAL_SECTION delegating_vtbl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
118 /* remaining entries in vtbl */
121 static ref_counted_vtbl *current_vtbl;
124 static HRESULT WINAPI delegating_QueryInterface(IUnknown *pUnk, REFIID iid, void **ppv)
130 static ULONG WINAPI delegating_AddRef(IUnknown *pUnk)
135 static ULONG WINAPI delegating_Release(IUnknown *pUnk)
140 #if defined(__i386__)
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.
147 #include "pshpack1.h"
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 */
161 #define BLOCK_SIZE 1024
162 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
164 static const vtbl_method_t *method_blocks[MAX_BLOCKS];
166 static const vtbl_method_t *allocate_block( unsigned int num )
169 vtbl_method_t *prev, *block;
171 block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
172 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
173 if (!block) return NULL;
175 for (i = 0; i < BLOCK_SIZE; i++)
177 block[i].mov1 = 0x0424448b;
178 block[i].mov2 = 0x408b;
179 block[i].sixteen = 0x10;
180 block[i].mov3 = 0x04244489;
181 block[i].mov4 = 0x008b;
182 block[i].mov5 = 0x808b;
183 block[i].offset = (BLOCK_SIZE * num + i + 3) << 2;
184 block[i].jmp = 0xe0ff;
185 block[i].pad[0] = 0x8d;
186 block[i].pad[1] = 0x76;
187 block[i].pad[2] = 0x00;
189 VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, NULL );
190 prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
191 if (prev) /* someone beat us to it */
193 VirtualFree( block, 0, MEM_RELEASE );
199 static BOOL fill_delegated_stub_table(IUnknownVtbl *vtbl, DWORD num)
201 const void **entry = (const void **)(vtbl + 1);
204 if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
206 FIXME( "%u methods not supported\n", num );
209 vtbl->QueryInterface = delegating_QueryInterface;
210 vtbl->AddRef = delegating_AddRef;
211 vtbl->Release = delegating_Release;
212 for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
214 const vtbl_method_t *block = method_blocks[i];
215 if (!block && !(block = allocate_block( i ))) return FALSE;
216 for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++) *entry++ = &block[j];
221 BOOL fill_delegated_proxy_table(IUnknownVtbl *vtbl, DWORD num)
223 const void **entry = (const void **)(vtbl + 1);
226 if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
228 FIXME( "%u methods not supported\n", num );
231 vtbl->QueryInterface = IUnknown_QueryInterface_Proxy;
232 vtbl->AddRef = IUnknown_AddRef_Proxy;
233 vtbl->Release = IUnknown_Release_Proxy;
234 for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
236 const vtbl_method_t *block = method_blocks[i];
237 if (!block && !(block = allocate_block( i ))) return FALSE;
238 for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
239 if (!*entry) *entry = &block[j];
246 static BOOL fill_delegated_stub_table(IUnknownVtbl *vtbl, DWORD num)
248 ERR("delegated stubs are not supported on this architecture\n");
252 BOOL fill_delegated_proxy_table(IUnknownVtbl *vtbl, DWORD num)
254 ERR("delegated proxies are not supported on this architecture\n");
258 #endif /* __i386__ */
260 static IUnknownVtbl *get_delegating_vtbl(DWORD num_methods)
264 if (num_methods < 256) num_methods = 256; /* avoid frequent reallocations */
266 EnterCriticalSection(&delegating_vtbl_section);
268 if(!current_vtbl || num_methods > current_vtbl->size)
270 ref_counted_vtbl *table = HeapAlloc(GetProcessHeap(), 0,
271 FIELD_OFFSET(ref_counted_vtbl, vtbl) + num_methods * sizeof(void*));
274 LeaveCriticalSection(&delegating_vtbl_section);
279 table->size = num_methods;
280 fill_delegated_stub_table(&table->vtbl, num_methods);
282 if (current_vtbl && current_vtbl->ref == 0)
284 TRACE("freeing old table\n");
285 HeapFree(GetProcessHeap(), 0, current_vtbl);
287 current_vtbl = table;
291 ret = ¤t_vtbl->vtbl;
292 LeaveCriticalSection(&delegating_vtbl_section);
296 static void release_delegating_vtbl(IUnknownVtbl *vtbl)
298 ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1);
300 EnterCriticalSection(&delegating_vtbl_section);
302 TRACE("ref now %d\n", table->ref);
303 if(table->ref == 0 && table != current_vtbl)
305 TRACE("... and we're not current so free'ing\n");
306 HeapFree(GetProcessHeap(), 0, table);
308 LeaveCriticalSection(&delegating_vtbl_section);
311 HRESULT CStdStubBuffer_Delegating_Construct(REFIID riid,
312 LPUNKNOWN pUnkServer,
313 PCInterfaceName name,
314 CInterfaceStubVtbl *vtbl,
315 REFIID delegating_iid,
316 LPPSFACTORYBUFFER pPSFactory,
317 LPRPCSTUBBUFFER *ppStub)
319 cstdstubbuffer_delegating_t *This;
323 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
324 TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl->header.piid), debugstr_guid(delegating_iid));
325 TRACE("vtbl=%p\n", &vtbl->Vtbl);
327 if (!IsEqualGUID(vtbl->header.piid, riid))
329 ERR("IID mismatch during stub creation\n");
330 return RPC_E_UNEXPECTED;
333 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
334 if(FAILED(r)) return r;
336 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
339 IUnknown_Release(pvServer);
340 return E_OUTOFMEMORY;
343 This->base_obj = get_delegating_vtbl( vtbl->header.DispatchTableCount );
344 r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
347 release_delegating_vtbl(This->base_obj);
348 HeapFree(GetProcessHeap(), 0, This);
349 IUnknown_Release(pvServer);
353 This->stub_buffer.lpVtbl = &vtbl->Vtbl;
354 This->stub_buffer.RefCount = 1;
355 This->stub_buffer.pvServerObject = pvServer;
356 This->stub_buffer.pPSFactory = pPSFactory;
357 *ppStub = (LPRPCSTUBBUFFER)&This->stub_buffer;
359 IPSFactoryBuffer_AddRef(pPSFactory);
363 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
367 CStdStubBuffer *This = (CStdStubBuffer *)iface;
368 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
370 if (IsEqualIID(&IID_IUnknown, riid) ||
371 IsEqualIID(&IID_IRpcStubBuffer, riid))
373 IUnknown_AddRef(iface);
378 return E_NOINTERFACE;
381 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
383 CStdStubBuffer *This = (CStdStubBuffer *)iface;
384 TRACE("(%p)->AddRef()\n",This);
385 return InterlockedIncrement(&This->RefCount);
388 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
389 LPPSFACTORYBUFFER pPSF)
391 CStdStubBuffer *This = (CStdStubBuffer *)iface;
394 TRACE("(%p)->Release()\n",This);
396 refs = InterlockedDecrement(&This->RefCount);
399 /* test_Release shows that native doesn't call Disconnect here.
400 We'll leave it in for the time being. */
401 IRpcStubBuffer_Disconnect(iface);
403 IPSFactoryBuffer_Release(pPSF);
404 HeapFree(GetProcessHeap(),0,This);
409 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
410 LPPSFACTORYBUFFER pPSF)
412 cstdstubbuffer_delegating_t *This = impl_from_delegating( iface );
415 TRACE("(%p)->Release()\n", This);
417 refs = InterlockedDecrement(&This->stub_buffer.RefCount);
420 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
422 IRpcStubBuffer_Disconnect((IRpcStubBuffer *)&This->stub_buffer);
424 IRpcStubBuffer_Release(This->base_stub);
425 release_delegating_vtbl(This->base_obj);
427 IPSFactoryBuffer_Release(pPSF);
428 HeapFree(GetProcessHeap(), 0, This);
434 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
435 LPUNKNOWN lpUnkServer)
437 CStdStubBuffer *This = (CStdStubBuffer *)iface;
439 IUnknown *new = NULL;
441 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
443 r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
444 new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
446 IUnknown_Release(new);
450 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
452 CStdStubBuffer *This = (CStdStubBuffer *)iface;
454 TRACE("(%p)->Disconnect()\n",This);
456 old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
459 IUnknown_Release(old);
462 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
464 LPRPCCHANNELBUFFER pChannel)
466 CStdStubBuffer *This = (CStdStubBuffer *)iface;
467 DWORD dwPhase = STUB_UNMARSHAL;
470 TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
474 if (STUB_HEADER(This).pDispatchTable)
475 STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
476 else /* pure interpreted */
477 NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
479 __EXCEPT(stub_filter)
481 DWORD dwExceptionCode = GetExceptionCode();
482 WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
483 if (FAILED(dwExceptionCode))
484 hr = dwExceptionCode;
486 hr = HRESULT_FROM_WIN32(dwExceptionCode);
493 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
496 CStdStubBuffer *This = (CStdStubBuffer *)iface;
497 TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
498 return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
501 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
503 CStdStubBuffer *This = (CStdStubBuffer *)iface;
504 TRACE("(%p)->CountRefs()\n",This);
505 return This->RefCount;
508 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
511 CStdStubBuffer *This = (CStdStubBuffer *)iface;
512 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
516 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
519 CStdStubBuffer *This = (CStdStubBuffer *)iface;
520 TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
523 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
525 CStdStubBuffer_QueryInterface,
526 CStdStubBuffer_AddRef,
528 CStdStubBuffer_Connect,
529 CStdStubBuffer_Disconnect,
530 CStdStubBuffer_Invoke,
531 CStdStubBuffer_IsIIDSupported,
532 CStdStubBuffer_CountRefs,
533 CStdStubBuffer_DebugServerQueryInterface,
534 CStdStubBuffer_DebugServerRelease
537 static HRESULT WINAPI CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface,
538 LPUNKNOWN lpUnkServer)
540 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
542 TRACE("(%p)->Connect(%p)\n", This, lpUnkServer);
544 r = CStdStubBuffer_Connect(iface, lpUnkServer);
546 r = IRpcStubBuffer_Connect(This->base_stub, (IUnknown*)&This->base_obj);
551 static void WINAPI CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface)
553 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
554 TRACE("(%p)->Disconnect()\n", This);
556 IRpcStubBuffer_Disconnect(This->base_stub);
557 CStdStubBuffer_Disconnect(iface);
560 static ULONG WINAPI CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface)
562 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
564 TRACE("(%p)->CountRefs()\n", This);
566 ret = CStdStubBuffer_CountRefs(iface);
567 ret += IRpcStubBuffer_CountRefs(This->base_stub);
572 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl =
574 CStdStubBuffer_QueryInterface,
575 CStdStubBuffer_AddRef,
577 CStdStubBuffer_Delegating_Connect,
578 CStdStubBuffer_Delegating_Disconnect,
579 CStdStubBuffer_Invoke,
580 CStdStubBuffer_IsIIDSupported,
581 CStdStubBuffer_Delegating_CountRefs,
582 CStdStubBuffer_DebugServerQueryInterface,
583 CStdStubBuffer_DebugServerRelease
586 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
588 CStdStubBuffer *This = (CStdStubBuffer *)iface;
589 return STUB_HEADER(This).pServerInfo;
592 /************************************************************************
593 * NdrStubForwardingFunction [RPCRT4.@]
595 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *iface, IRpcChannelBuffer *pChannel,
596 PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
598 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
600 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
601 HRESULT r = IRpcStubBuffer_Invoke(This->base_stub, (RPCOLEMESSAGE*)pMsg, pChannel);
602 if(FAILED(r)) RpcRaiseException(r);
606 /***********************************************************************
607 * NdrStubInitialize [RPCRT4.@]
609 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
610 PMIDL_STUB_MESSAGE pStubMsg,
611 PMIDL_STUB_DESC pStubDescriptor,
612 LPRPCCHANNELBUFFER pRpcChannelBuffer)
614 TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
615 NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
616 pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
617 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
618 &pStubMsg->dwDestContext,
619 &pStubMsg->pvDestContext);
622 /***********************************************************************
623 * NdrStubGetBuffer [RPCRT4.@]
625 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER iface,
626 LPRPCCHANNELBUFFER pRpcChannelBuffer,
627 PMIDL_STUB_MESSAGE pStubMsg)
629 CStdStubBuffer *This = (CStdStubBuffer *)iface;
632 TRACE("(%p, %p, %p)\n", This, pRpcChannelBuffer, pStubMsg);
634 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
635 hr = IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer,
636 (RPCOLEMESSAGE *)pStubMsg->RpcMsg, STUB_HEADER(This).piid);
639 RpcRaiseException(hr);
643 pStubMsg->Buffer = pStubMsg->RpcMsg->Buffer;