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
33 #include "wine/debug.h"
34 #include "wine/exception.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 #define STUB_HEADER(This) (((const CInterfaceStubHeader*)((This)->lpVtbl))[-1])
42 static WINE_EXCEPTION_FILTER(stub_filter)
44 if (GetExceptionInformation()->ExceptionRecord->ExceptionFlags & EXCEPTION_NONCONTINUABLE)
45 return EXCEPTION_CONTINUE_SEARCH;
46 return EXCEPTION_EXECUTE_HANDLER;
51 IUnknownVtbl *base_obj;
52 IRpcStubBuffer *base_stub;
53 CStdStubBuffer stub_buffer;
54 } cstdstubbuffer_delegating_t;
56 static inline cstdstubbuffer_delegating_t *impl_from_delegating( IRpcStubBuffer *iface )
58 return (cstdstubbuffer_delegating_t*)((char *)iface - FIELD_OFFSET(cstdstubbuffer_delegating_t, stub_buffer));
61 HRESULT WINAPI CStdStubBuffer_Construct(REFIID riid,
64 CInterfaceStubVtbl *vtbl,
65 LPPSFACTORYBUFFER pPSFactory,
66 LPRPCSTUBBUFFER *ppStub)
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);
75 if (!IsEqualGUID(vtbl->header.piid, riid)) {
76 ERR("IID mismatch during stub creation\n");
77 return RPC_E_UNEXPECTED;
80 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
84 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CStdStubBuffer));
86 IUnknown_Release(pvServer);
90 This->lpVtbl = &vtbl->Vtbl;
92 This->pvServerObject = pvServer;
93 This->pPSFactory = pPSFactory;
94 *ppStub = (LPRPCSTUBBUFFER)This;
96 IPSFactoryBuffer_AddRef(pPSFactory);
100 static CRITICAL_SECTION delegating_vtbl_section;
101 static CRITICAL_SECTION_DEBUG critsect_debug =
103 0, 0, &delegating_vtbl_section,
104 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
105 0, 0, { (DWORD_PTR)(__FILE__ ": delegating_vtbl_section") }
107 static CRITICAL_SECTION delegating_vtbl_section = { &critsect_debug, -1, 0, 0, 0, 0 };
115 /* remaining entries in vtbl */
120 ref_counted_vtbl *table;
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 static void fill_table(IUnknownVtbl *vtbl, void **methods, DWORD num)
163 vtbl_method_t *method;
167 vtbl->QueryInterface = delegating_QueryInterface;
168 vtbl->AddRef = delegating_AddRef;
169 vtbl->Release = delegating_Release;
171 method = (vtbl_method_t*)methods;
172 entry = (void**)(vtbl + 1);
174 for(i = 3; i < num; i++)
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;
196 typedef struct {int dummy;} vtbl_method_t;
197 static void fill_table(IUnknownVtbl *vtbl, DWORD num)
199 ERR("delegated stubs are not supported on this architecture\n");
202 #endif /* __i386__ */
204 void create_delegating_vtbl(DWORD num_methods)
206 TRACE("%d\n", num_methods);
209 ERR("should have more than %d methods\n", num_methods);
213 EnterCriticalSection(&delegating_vtbl_section);
214 if(!current_vtbl.table || num_methods > current_vtbl.table->size)
218 if(current_vtbl.table && current_vtbl.table->ref == 0)
220 TRACE("freeing old table\n");
221 VirtualFree(current_vtbl.table->methods,
222 (current_vtbl.table->size - 3) * sizeof(vtbl_method_t),
224 HeapFree(GetProcessHeap(), 0, current_vtbl.table);
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(¤t_vtbl.table->vtbl, current_vtbl.table->methods, num_methods);
232 VirtualProtect(current_vtbl.table->methods, size, PAGE_EXECUTE_READ, &old_protect);
234 LeaveCriticalSection(&delegating_vtbl_section);
237 static IUnknownVtbl *get_delegating_vtbl(void)
241 EnterCriticalSection(&delegating_vtbl_section);
242 current_vtbl.table->ref++;
243 ret = ¤t_vtbl.table->vtbl;
244 LeaveCriticalSection(&delegating_vtbl_section);
248 static void release_delegating_vtbl(IUnknownVtbl *vtbl)
250 ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1);
252 EnterCriticalSection(&delegating_vtbl_section);
254 TRACE("ref now %d\n", table->ref);
255 if(table->ref == 0 && table != current_vtbl.table)
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),
261 HeapFree(GetProcessHeap(), 0, table);
263 LeaveCriticalSection(&delegating_vtbl_section);
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)
274 cstdstubbuffer_delegating_t *This;
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);
282 if (!IsEqualGUID(vtbl->header.piid, riid))
284 ERR("IID mismatch during stub creation\n");
285 return RPC_E_UNEXPECTED;
288 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
289 if(FAILED(r)) return r;
291 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
294 IUnknown_Release(pvServer);
295 return E_OUTOFMEMORY;
298 This->base_obj = get_delegating_vtbl();
299 r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
302 release_delegating_vtbl(This->base_obj);
303 HeapFree(GetProcessHeap(), 0, This);
304 IUnknown_Release(pvServer);
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;
314 IPSFactoryBuffer_AddRef(pPSFactory);
318 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
322 CStdStubBuffer *This = (CStdStubBuffer *)iface;
323 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
325 if (IsEqualIID(&IID_IUnknown, riid) ||
326 IsEqualIID(&IID_IRpcStubBuffer, riid))
328 IUnknown_AddRef(iface);
333 return E_NOINTERFACE;
336 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
338 CStdStubBuffer *This = (CStdStubBuffer *)iface;
339 TRACE("(%p)->AddRef()\n",This);
340 return InterlockedIncrement(&This->RefCount);
343 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
344 LPPSFACTORYBUFFER pPSF)
346 CStdStubBuffer *This = (CStdStubBuffer *)iface;
349 TRACE("(%p)->Release()\n",This);
351 refs = InterlockedDecrement(&This->RefCount);
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);
358 IPSFactoryBuffer_Release(pPSF);
359 HeapFree(GetProcessHeap(),0,This);
364 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
365 LPPSFACTORYBUFFER pPSF)
367 cstdstubbuffer_delegating_t *This = impl_from_delegating( iface );
370 TRACE("(%p)->Release()\n", This);
372 refs = InterlockedDecrement(&This->stub_buffer.RefCount);
375 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
377 IRpcStubBuffer_Disconnect((IRpcStubBuffer *)&This->stub_buffer);
379 IRpcStubBuffer_Release(This->base_stub);
380 release_delegating_vtbl(This->base_obj);
382 IPSFactoryBuffer_Release(pPSF);
383 HeapFree(GetProcessHeap(), 0, This);
389 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
390 LPUNKNOWN lpUnkServer)
392 CStdStubBuffer *This = (CStdStubBuffer *)iface;
394 IUnknown *new = NULL;
396 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
398 r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
399 new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
401 IUnknown_Release(new);
405 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
407 CStdStubBuffer *This = (CStdStubBuffer *)iface;
409 TRACE("(%p)->Disconnect()\n",This);
411 old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
414 IUnknown_Release(old);
417 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
419 LPRPCCHANNELBUFFER pChannel)
421 CStdStubBuffer *This = (CStdStubBuffer *)iface;
422 DWORD dwPhase = STUB_UNMARSHAL;
425 TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
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);
434 __EXCEPT(stub_filter)
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;
441 hr = HRESULT_FROM_WIN32(dwExceptionCode);
448 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
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;
456 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
458 CStdStubBuffer *This = (CStdStubBuffer *)iface;
459 TRACE("(%p)->CountRefs()\n",This);
460 return This->RefCount;
463 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
466 CStdStubBuffer *This = (CStdStubBuffer *)iface;
467 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
471 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
474 CStdStubBuffer *This = (CStdStubBuffer *)iface;
475 TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
478 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
480 CStdStubBuffer_QueryInterface,
481 CStdStubBuffer_AddRef,
483 CStdStubBuffer_Connect,
484 CStdStubBuffer_Disconnect,
485 CStdStubBuffer_Invoke,
486 CStdStubBuffer_IsIIDSupported,
487 CStdStubBuffer_CountRefs,
488 CStdStubBuffer_DebugServerQueryInterface,
489 CStdStubBuffer_DebugServerRelease
492 static HRESULT WINAPI CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface,
493 LPUNKNOWN lpUnkServer)
495 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
497 TRACE("(%p)->Connect(%p)\n", This, lpUnkServer);
499 r = CStdStubBuffer_Connect(iface, lpUnkServer);
501 r = IRpcStubBuffer_Connect(This->base_stub, (IUnknown*)&This->base_obj);
506 static void WINAPI CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface)
508 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
509 TRACE("(%p)->Disconnect()\n", This);
511 IRpcStubBuffer_Disconnect(This->base_stub);
512 CStdStubBuffer_Disconnect(iface);
515 static ULONG WINAPI CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface)
517 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
519 TRACE("(%p)->CountRefs()\n", This);
521 ret = CStdStubBuffer_CountRefs(iface);
522 ret += IRpcStubBuffer_CountRefs(This->base_stub);
527 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl =
529 CStdStubBuffer_QueryInterface,
530 CStdStubBuffer_AddRef,
532 CStdStubBuffer_Delegating_Connect,
533 CStdStubBuffer_Delegating_Disconnect,
534 CStdStubBuffer_Invoke,
535 CStdStubBuffer_IsIIDSupported,
536 CStdStubBuffer_Delegating_CountRefs,
537 CStdStubBuffer_DebugServerQueryInterface,
538 CStdStubBuffer_DebugServerRelease
541 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
543 CStdStubBuffer *This = (CStdStubBuffer *)iface;
544 return STUB_HEADER(This).pServerInfo;
547 /************************************************************************
548 * NdrStubForwardingFunction [RPCRT4.@]
550 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *iface, IRpcChannelBuffer *pChannel,
551 PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
553 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
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);
561 /***********************************************************************
562 * NdrStubInitialize [RPCRT4.@]
564 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
565 PMIDL_STUB_MESSAGE pStubMsg,
566 PMIDL_STUB_DESC pStubDescriptor,
567 LPRPCCHANNELBUFFER pRpcChannelBuffer)
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);
577 /***********************************************************************
578 * NdrStubGetBuffer [RPCRT4.@]
580 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER iface,
581 LPRPCCHANNELBUFFER pRpcChannelBuffer,
582 PMIDL_STUB_MESSAGE pStubMsg)
584 CStdStubBuffer *This = (CStdStubBuffer *)iface;
587 TRACE("(%p, %p, %p)\n", This, pRpcChannelBuffer, pStubMsg);
589 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
590 hr = IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer,
591 (RPCOLEMESSAGE *)pStubMsg->RpcMsg, STUB_HEADER(This).piid);
594 RpcRaiseException(hr);
598 pStubMsg->Buffer = pStubMsg->RpcMsg->Buffer;