2 * COM stub (CStdStubBuffer) implementation
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
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.
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.
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
22 #include "wine/port.h"
36 #include "wine/debug.h"
37 #include "wine/exception.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 #define STUB_HEADER(This) (((const CInterfaceStubHeader*)((This)->lpVtbl))[-1])
45 static LONG WINAPI stub_filter(EXCEPTION_POINTERS *eptr)
47 if (eptr->ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE)
48 return EXCEPTION_CONTINUE_SEARCH;
49 return EXCEPTION_EXECUTE_HANDLER;
54 IUnknownVtbl *base_obj;
55 IRpcStubBuffer *base_stub;
56 CStdStubBuffer stub_buffer;
57 } cstdstubbuffer_delegating_t;
59 static inline cstdstubbuffer_delegating_t *impl_from_delegating( IRpcStubBuffer *iface )
61 return (cstdstubbuffer_delegating_t*)((char *)iface - FIELD_OFFSET(cstdstubbuffer_delegating_t, stub_buffer));
64 HRESULT CStdStubBuffer_Construct(REFIID riid,
67 CInterfaceStubVtbl *vtbl,
68 LPPSFACTORYBUFFER pPSFactory,
69 LPRPCSTUBBUFFER *ppStub)
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);
78 if (!IsEqualGUID(vtbl->header.piid, riid)) {
79 ERR("IID mismatch during stub creation\n");
80 return RPC_E_UNEXPECTED;
83 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
87 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
89 IUnknown_Release(pvServer);
93 This->lpVtbl = &vtbl->Vtbl;
95 This->pvServerObject = pvServer;
96 This->pPSFactory = pPSFactory;
97 *ppStub = (LPRPCSTUBBUFFER)This;
99 IPSFactoryBuffer_AddRef(pPSFactory);
103 static CRITICAL_SECTION delegating_vtbl_section;
104 static CRITICAL_SECTION_DEBUG critsect_debug =
106 0, 0, &delegating_vtbl_section,
107 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
108 0, 0, { (DWORD_PTR)(__FILE__ ": delegating_vtbl_section") }
110 static CRITICAL_SECTION delegating_vtbl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
117 /* remaining entries in vtbl */
120 static ref_counted_vtbl *current_vtbl;
123 static HRESULT WINAPI delegating_QueryInterface(IUnknown *pUnk, REFIID iid, void **ppv)
129 static ULONG WINAPI delegating_AddRef(IUnknown *pUnk)
134 static ULONG WINAPI delegating_Release(IUnknown *pUnk)
139 #if defined(__i386__)
141 /* The idea here is to replace the first param on the stack
142 ie. This (which will point to cstdstubbuffer_delegating_t)
143 with This->stub_buffer.pvServerObject and then jump to the
144 relevant offset in This->stub_buffer.pvServerObject's vtbl.
146 #include "pshpack1.h"
148 DWORD mov1; /* mov 0x4(%esp), %eax 8b 44 24 04 */
149 WORD mov2; /* mov 0x10(%eax), %eax 8b 40 */
150 BYTE sixteen; /* 10 */
151 DWORD mov3; /* mov %eax, 0x4(%esp) 89 44 24 04 */
152 WORD mov4; /* mov (%eax), %eax 8b 00 */
153 WORD mov5; /* mov offset(%eax), %eax 8b 80 */
154 DWORD offset; /* xx xx xx xx */
155 WORD jmp; /* jmp *%eax ff e0 */
156 BYTE pad[3]; /* lea 0x0(%esi), %esi 8d 76 00 */
160 #define BLOCK_SIZE 1024
161 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
163 static const vtbl_method_t *method_blocks[MAX_BLOCKS];
165 static const vtbl_method_t *allocate_block( unsigned int num )
168 vtbl_method_t *prev, *block;
170 block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
171 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
172 if (!block) return NULL;
174 for (i = 0; i < BLOCK_SIZE; i++)
176 block[i].mov1 = 0x0424448b;
177 block[i].mov2 = 0x408b;
178 block[i].sixteen = 0x10;
179 block[i].mov3 = 0x04244489;
180 block[i].mov4 = 0x008b;
181 block[i].mov5 = 0x808b;
182 block[i].offset = (BLOCK_SIZE * num + i + 3) << 2;
183 block[i].jmp = 0xe0ff;
184 block[i].pad[0] = 0x8d;
185 block[i].pad[1] = 0x76;
186 block[i].pad[2] = 0x00;
188 VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, NULL );
189 prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
190 if (prev) /* someone beat us to it */
192 VirtualFree( block, 0, MEM_RELEASE );
198 static BOOL fill_delegated_stub_table(IUnknownVtbl *vtbl, DWORD num)
200 const void **entry = (const void **)(vtbl + 1);
203 vtbl->QueryInterface = delegating_QueryInterface;
204 vtbl->AddRef = delegating_AddRef;
205 vtbl->Release = delegating_Release;
206 for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
208 const vtbl_method_t *block = method_blocks[i];
209 if (!block && !(block = allocate_block( i ))) return FALSE;
210 for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++) *entry++ = &block[j];
217 static BOOL fill_delegated_stub_table(IUnknownVtbl *vtbl, DWORD num)
219 ERR("delegated stubs are not supported on this architecture\n");
223 #endif /* __i386__ */
225 static IUnknownVtbl *get_delegating_vtbl(DWORD num_methods)
229 if (num_methods < 256) num_methods = 256; /* avoid frequent reallocations */
231 EnterCriticalSection(&delegating_vtbl_section);
233 if(!current_vtbl || num_methods > current_vtbl->size)
235 ref_counted_vtbl *table = HeapAlloc(GetProcessHeap(), 0,
236 FIELD_OFFSET(ref_counted_vtbl, vtbl) + num_methods * sizeof(void*));
239 LeaveCriticalSection(&delegating_vtbl_section);
244 table->size = num_methods;
245 fill_delegated_stub_table(&table->vtbl, num_methods);
247 if (current_vtbl && current_vtbl->ref == 0)
249 TRACE("freeing old table\n");
250 HeapFree(GetProcessHeap(), 0, current_vtbl);
252 current_vtbl = table;
256 ret = ¤t_vtbl->vtbl;
257 LeaveCriticalSection(&delegating_vtbl_section);
261 static void release_delegating_vtbl(IUnknownVtbl *vtbl)
263 ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1);
265 EnterCriticalSection(&delegating_vtbl_section);
267 TRACE("ref now %d\n", table->ref);
268 if(table->ref == 0 && table != current_vtbl)
270 TRACE("... and we're not current so free'ing\n");
271 HeapFree(GetProcessHeap(), 0, table);
273 LeaveCriticalSection(&delegating_vtbl_section);
276 HRESULT CStdStubBuffer_Delegating_Construct(REFIID riid,
277 LPUNKNOWN pUnkServer,
278 PCInterfaceName name,
279 CInterfaceStubVtbl *vtbl,
280 REFIID delegating_iid,
281 LPPSFACTORYBUFFER pPSFactory,
282 LPRPCSTUBBUFFER *ppStub)
284 cstdstubbuffer_delegating_t *This;
288 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
289 TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl->header.piid), debugstr_guid(delegating_iid));
290 TRACE("vtbl=%p\n", &vtbl->Vtbl);
292 if (!IsEqualGUID(vtbl->header.piid, riid))
294 ERR("IID mismatch during stub creation\n");
295 return RPC_E_UNEXPECTED;
298 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
299 if(FAILED(r)) return r;
301 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
304 IUnknown_Release(pvServer);
305 return E_OUTOFMEMORY;
308 This->base_obj = get_delegating_vtbl( vtbl->header.DispatchTableCount );
309 r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
312 release_delegating_vtbl(This->base_obj);
313 HeapFree(GetProcessHeap(), 0, This);
314 IUnknown_Release(pvServer);
318 This->stub_buffer.lpVtbl = &vtbl->Vtbl;
319 This->stub_buffer.RefCount = 1;
320 This->stub_buffer.pvServerObject = pvServer;
321 This->stub_buffer.pPSFactory = pPSFactory;
322 *ppStub = (LPRPCSTUBBUFFER)&This->stub_buffer;
324 IPSFactoryBuffer_AddRef(pPSFactory);
328 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
332 CStdStubBuffer *This = (CStdStubBuffer *)iface;
333 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
335 if (IsEqualIID(&IID_IUnknown, riid) ||
336 IsEqualIID(&IID_IRpcStubBuffer, riid))
338 IUnknown_AddRef(iface);
343 return E_NOINTERFACE;
346 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
348 CStdStubBuffer *This = (CStdStubBuffer *)iface;
349 TRACE("(%p)->AddRef()\n",This);
350 return InterlockedIncrement(&This->RefCount);
353 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
354 LPPSFACTORYBUFFER pPSF)
356 CStdStubBuffer *This = (CStdStubBuffer *)iface;
359 TRACE("(%p)->Release()\n",This);
361 refs = InterlockedDecrement(&This->RefCount);
364 /* test_Release shows that native doesn't call Disconnect here.
365 We'll leave it in for the time being. */
366 IRpcStubBuffer_Disconnect(iface);
368 IPSFactoryBuffer_Release(pPSF);
369 HeapFree(GetProcessHeap(),0,This);
374 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
375 LPPSFACTORYBUFFER pPSF)
377 cstdstubbuffer_delegating_t *This = impl_from_delegating( iface );
380 TRACE("(%p)->Release()\n", This);
382 refs = InterlockedDecrement(&This->stub_buffer.RefCount);
385 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
387 IRpcStubBuffer_Disconnect((IRpcStubBuffer *)&This->stub_buffer);
389 IRpcStubBuffer_Release(This->base_stub);
390 release_delegating_vtbl(This->base_obj);
392 IPSFactoryBuffer_Release(pPSF);
393 HeapFree(GetProcessHeap(), 0, This);
399 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
400 LPUNKNOWN lpUnkServer)
402 CStdStubBuffer *This = (CStdStubBuffer *)iface;
404 IUnknown *new = NULL;
406 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
408 r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
409 new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
411 IUnknown_Release(new);
415 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
417 CStdStubBuffer *This = (CStdStubBuffer *)iface;
419 TRACE("(%p)->Disconnect()\n",This);
421 old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
424 IUnknown_Release(old);
427 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
429 LPRPCCHANNELBUFFER pChannel)
431 CStdStubBuffer *This = (CStdStubBuffer *)iface;
432 DWORD dwPhase = STUB_UNMARSHAL;
435 TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
439 if (STUB_HEADER(This).pDispatchTable)
440 STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
441 else /* pure interpreted */
442 NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
444 __EXCEPT(stub_filter)
446 DWORD dwExceptionCode = GetExceptionCode();
447 WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
448 if (FAILED(dwExceptionCode))
449 hr = dwExceptionCode;
451 hr = HRESULT_FROM_WIN32(dwExceptionCode);
458 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
461 CStdStubBuffer *This = (CStdStubBuffer *)iface;
462 TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
463 return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
466 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
468 CStdStubBuffer *This = (CStdStubBuffer *)iface;
469 TRACE("(%p)->CountRefs()\n",This);
470 return This->RefCount;
473 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
476 CStdStubBuffer *This = (CStdStubBuffer *)iface;
477 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
481 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
484 CStdStubBuffer *This = (CStdStubBuffer *)iface;
485 TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
488 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
490 CStdStubBuffer_QueryInterface,
491 CStdStubBuffer_AddRef,
493 CStdStubBuffer_Connect,
494 CStdStubBuffer_Disconnect,
495 CStdStubBuffer_Invoke,
496 CStdStubBuffer_IsIIDSupported,
497 CStdStubBuffer_CountRefs,
498 CStdStubBuffer_DebugServerQueryInterface,
499 CStdStubBuffer_DebugServerRelease
502 static HRESULT WINAPI CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface,
503 LPUNKNOWN lpUnkServer)
505 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
507 TRACE("(%p)->Connect(%p)\n", This, lpUnkServer);
509 r = CStdStubBuffer_Connect(iface, lpUnkServer);
511 r = IRpcStubBuffer_Connect(This->base_stub, (IUnknown*)&This->base_obj);
516 static void WINAPI CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface)
518 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
519 TRACE("(%p)->Disconnect()\n", This);
521 IRpcStubBuffer_Disconnect(This->base_stub);
522 CStdStubBuffer_Disconnect(iface);
525 static ULONG WINAPI CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface)
527 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
529 TRACE("(%p)->CountRefs()\n", This);
531 ret = CStdStubBuffer_CountRefs(iface);
532 ret += IRpcStubBuffer_CountRefs(This->base_stub);
537 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl =
539 CStdStubBuffer_QueryInterface,
540 CStdStubBuffer_AddRef,
542 CStdStubBuffer_Delegating_Connect,
543 CStdStubBuffer_Delegating_Disconnect,
544 CStdStubBuffer_Invoke,
545 CStdStubBuffer_IsIIDSupported,
546 CStdStubBuffer_Delegating_CountRefs,
547 CStdStubBuffer_DebugServerQueryInterface,
548 CStdStubBuffer_DebugServerRelease
551 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
553 CStdStubBuffer *This = (CStdStubBuffer *)iface;
554 return STUB_HEADER(This).pServerInfo;
557 /************************************************************************
558 * NdrStubForwardingFunction [RPCRT4.@]
560 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *iface, IRpcChannelBuffer *pChannel,
561 PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
563 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
565 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
566 HRESULT r = IRpcStubBuffer_Invoke(This->base_stub, (RPCOLEMESSAGE*)pMsg, pChannel);
567 if(FAILED(r)) RpcRaiseException(r);
571 /***********************************************************************
572 * NdrStubInitialize [RPCRT4.@]
574 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
575 PMIDL_STUB_MESSAGE pStubMsg,
576 PMIDL_STUB_DESC pStubDescriptor,
577 LPRPCCHANNELBUFFER pRpcChannelBuffer)
579 TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
580 NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
581 pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
582 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
583 &pStubMsg->dwDestContext,
584 &pStubMsg->pvDestContext);
587 /***********************************************************************
588 * NdrStubGetBuffer [RPCRT4.@]
590 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER iface,
591 LPRPCCHANNELBUFFER pRpcChannelBuffer,
592 PMIDL_STUB_MESSAGE pStubMsg)
594 CStdStubBuffer *This = (CStdStubBuffer *)iface;
597 TRACE("(%p, %p, %p)\n", This, pRpcChannelBuffer, pStubMsg);
599 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
600 hr = IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer,
601 (RPCOLEMESSAGE *)pStubMsg->RpcMsg, STUB_HEADER(This).piid);
604 RpcRaiseException(hr);
608 pStubMsg->Buffer = pStubMsg->RpcMsg->Buffer;