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 WINAPI 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 };
118 /* remaining entries in vtbl */
123 ref_counted_vtbl *table;
127 static HRESULT WINAPI delegating_QueryInterface(IUnknown *pUnk, REFIID iid, void **ppv)
133 static ULONG WINAPI delegating_AddRef(IUnknown *pUnk)
138 static ULONG WINAPI delegating_Release(IUnknown *pUnk)
143 #if defined(__i386__)
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.
150 #include "pshpack1.h"
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 */
164 static void fill_table(IUnknownVtbl *vtbl, void **methods, DWORD num)
166 vtbl_method_t *method;
170 vtbl->QueryInterface = delegating_QueryInterface;
171 vtbl->AddRef = delegating_AddRef;
172 vtbl->Release = delegating_Release;
174 method = (vtbl_method_t*)methods;
175 entry = (void**)(vtbl + 1);
177 for(i = 3; i < num; i++)
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;
199 typedef struct {int dummy;} vtbl_method_t;
200 static void fill_table(IUnknownVtbl *vtbl, void **methods, DWORD num)
202 ERR("delegated stubs are not supported on this architecture\n");
205 #endif /* __i386__ */
207 void create_delegating_vtbl(DWORD num_methods)
209 TRACE("%d\n", num_methods);
212 ERR("should have more than %d methods\n", num_methods);
216 EnterCriticalSection(&delegating_vtbl_section);
217 if(!current_vtbl.table || num_methods > current_vtbl.table->size)
221 if(current_vtbl.table && current_vtbl.table->ref == 0)
223 TRACE("freeing old table\n");
224 VirtualFree(current_vtbl.table->methods,
225 (current_vtbl.table->size - 3) * sizeof(vtbl_method_t),
227 HeapFree(GetProcessHeap(), 0, current_vtbl.table);
229 size = (num_methods - 3) * sizeof(vtbl_method_t);
230 current_vtbl.table = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(ref_counted_vtbl, vtbl) + num_methods * sizeof(void*));
231 current_vtbl.table->ref = 0;
232 current_vtbl.table->size = num_methods;
233 current_vtbl.table->methods = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
234 fill_table(¤t_vtbl.table->vtbl, current_vtbl.table->methods, num_methods);
235 VirtualProtect(current_vtbl.table->methods, size, PAGE_EXECUTE_READ, &old_protect);
237 LeaveCriticalSection(&delegating_vtbl_section);
240 static IUnknownVtbl *get_delegating_vtbl(void)
244 EnterCriticalSection(&delegating_vtbl_section);
245 current_vtbl.table->ref++;
246 ret = ¤t_vtbl.table->vtbl;
247 LeaveCriticalSection(&delegating_vtbl_section);
251 static void release_delegating_vtbl(IUnknownVtbl *vtbl)
253 ref_counted_vtbl *table = (ref_counted_vtbl*)((DWORD *)vtbl - 1);
255 EnterCriticalSection(&delegating_vtbl_section);
257 TRACE("ref now %d\n", table->ref);
258 if(table->ref == 0 && table != current_vtbl.table)
260 TRACE("... and we're not current so free'ing\n");
261 VirtualFree(current_vtbl.table->methods,
262 (current_vtbl.table->size - 3) * sizeof(vtbl_method_t),
264 HeapFree(GetProcessHeap(), 0, table);
266 LeaveCriticalSection(&delegating_vtbl_section);
269 HRESULT WINAPI CStdStubBuffer_Delegating_Construct(REFIID riid,
270 LPUNKNOWN pUnkServer,
271 PCInterfaceName name,
272 CInterfaceStubVtbl *vtbl,
273 REFIID delegating_iid,
274 LPPSFACTORYBUFFER pPSFactory,
275 LPRPCSTUBBUFFER *ppStub)
277 cstdstubbuffer_delegating_t *This;
281 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer, vtbl, pPSFactory, ppStub, name);
282 TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl->header.piid), debugstr_guid(delegating_iid));
283 TRACE("vtbl=%p\n", &vtbl->Vtbl);
285 if (!IsEqualGUID(vtbl->header.piid, riid))
287 ERR("IID mismatch during stub creation\n");
288 return RPC_E_UNEXPECTED;
291 r = IUnknown_QueryInterface(pUnkServer, riid, (void**)&pvServer);
292 if(FAILED(r)) return r;
294 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
297 IUnknown_Release(pvServer);
298 return E_OUTOFMEMORY;
301 This->base_obj = get_delegating_vtbl();
302 r = create_stub(delegating_iid, (IUnknown*)&This->base_obj, &This->base_stub);
305 release_delegating_vtbl(This->base_obj);
306 HeapFree(GetProcessHeap(), 0, This);
307 IUnknown_Release(pvServer);
311 This->stub_buffer.lpVtbl = &vtbl->Vtbl;
312 This->stub_buffer.RefCount = 1;
313 This->stub_buffer.pvServerObject = pvServer;
314 This->stub_buffer.pPSFactory = pPSFactory;
315 *ppStub = (LPRPCSTUBBUFFER)&This->stub_buffer;
317 IPSFactoryBuffer_AddRef(pPSFactory);
321 HRESULT WINAPI CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface,
325 CStdStubBuffer *This = (CStdStubBuffer *)iface;
326 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
328 if (IsEqualIID(&IID_IUnknown, riid) ||
329 IsEqualIID(&IID_IRpcStubBuffer, riid))
331 IUnknown_AddRef(iface);
336 return E_NOINTERFACE;
339 ULONG WINAPI CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface)
341 CStdStubBuffer *This = (CStdStubBuffer *)iface;
342 TRACE("(%p)->AddRef()\n",This);
343 return InterlockedIncrement(&This->RefCount);
346 ULONG WINAPI NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface,
347 LPPSFACTORYBUFFER pPSF)
349 CStdStubBuffer *This = (CStdStubBuffer *)iface;
352 TRACE("(%p)->Release()\n",This);
354 refs = InterlockedDecrement(&This->RefCount);
357 /* test_Release shows that native doesn't call Disconnect here.
358 We'll leave it in for the time being. */
359 IRpcStubBuffer_Disconnect(iface);
361 IPSFactoryBuffer_Release(pPSF);
362 HeapFree(GetProcessHeap(),0,This);
367 ULONG WINAPI NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface,
368 LPPSFACTORYBUFFER pPSF)
370 cstdstubbuffer_delegating_t *This = impl_from_delegating( iface );
373 TRACE("(%p)->Release()\n", This);
375 refs = InterlockedDecrement(&This->stub_buffer.RefCount);
378 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
380 IRpcStubBuffer_Disconnect((IRpcStubBuffer *)&This->stub_buffer);
382 IRpcStubBuffer_Release(This->base_stub);
383 release_delegating_vtbl(This->base_obj);
385 IPSFactoryBuffer_Release(pPSF);
386 HeapFree(GetProcessHeap(), 0, This);
392 HRESULT WINAPI CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface,
393 LPUNKNOWN lpUnkServer)
395 CStdStubBuffer *This = (CStdStubBuffer *)iface;
397 IUnknown *new = NULL;
399 TRACE("(%p)->Connect(%p)\n",This,lpUnkServer);
401 r = IUnknown_QueryInterface(lpUnkServer, STUB_HEADER(This).piid, (void**)&new);
402 new = InterlockedExchangePointer((void**)&This->pvServerObject, new);
404 IUnknown_Release(new);
408 void WINAPI CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface)
410 CStdStubBuffer *This = (CStdStubBuffer *)iface;
412 TRACE("(%p)->Disconnect()\n",This);
414 old = InterlockedExchangePointer((void**)&This->pvServerObject, NULL);
417 IUnknown_Release(old);
420 HRESULT WINAPI CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface,
422 LPRPCCHANNELBUFFER pChannel)
424 CStdStubBuffer *This = (CStdStubBuffer *)iface;
425 DWORD dwPhase = STUB_UNMARSHAL;
428 TRACE("(%p)->Invoke(%p,%p)\n",This,pMsg,pChannel);
432 if (STUB_HEADER(This).pDispatchTable)
433 STUB_HEADER(This).pDispatchTable[pMsg->iMethod](iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
434 else /* pure interpreted */
435 NdrStubCall2(iface, pChannel, (PRPC_MESSAGE)pMsg, &dwPhase);
437 __EXCEPT(stub_filter)
439 DWORD dwExceptionCode = GetExceptionCode();
440 WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode, dwExceptionCode);
441 if (FAILED(dwExceptionCode))
442 hr = dwExceptionCode;
444 hr = HRESULT_FROM_WIN32(dwExceptionCode);
451 LPRPCSTUBBUFFER WINAPI CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface,
454 CStdStubBuffer *This = (CStdStubBuffer *)iface;
455 TRACE("(%p)->IsIIDSupported(%s)\n",This,debugstr_guid(riid));
456 return IsEqualGUID(STUB_HEADER(This).piid, riid) ? iface : NULL;
459 ULONG WINAPI CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface)
461 CStdStubBuffer *This = (CStdStubBuffer *)iface;
462 TRACE("(%p)->CountRefs()\n",This);
463 return This->RefCount;
466 HRESULT WINAPI CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface,
469 CStdStubBuffer *This = (CStdStubBuffer *)iface;
470 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This,ppv);
474 void WINAPI CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface,
477 CStdStubBuffer *This = (CStdStubBuffer *)iface;
478 TRACE("(%p)->DebugServerRelease(%p)\n",This,pv);
481 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl =
483 CStdStubBuffer_QueryInterface,
484 CStdStubBuffer_AddRef,
486 CStdStubBuffer_Connect,
487 CStdStubBuffer_Disconnect,
488 CStdStubBuffer_Invoke,
489 CStdStubBuffer_IsIIDSupported,
490 CStdStubBuffer_CountRefs,
491 CStdStubBuffer_DebugServerQueryInterface,
492 CStdStubBuffer_DebugServerRelease
495 static HRESULT WINAPI CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface,
496 LPUNKNOWN lpUnkServer)
498 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
500 TRACE("(%p)->Connect(%p)\n", This, lpUnkServer);
502 r = CStdStubBuffer_Connect(iface, lpUnkServer);
504 r = IRpcStubBuffer_Connect(This->base_stub, (IUnknown*)&This->base_obj);
509 static void WINAPI CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface)
511 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
512 TRACE("(%p)->Disconnect()\n", This);
514 IRpcStubBuffer_Disconnect(This->base_stub);
515 CStdStubBuffer_Disconnect(iface);
518 static ULONG WINAPI CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface)
520 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
522 TRACE("(%p)->CountRefs()\n", This);
524 ret = CStdStubBuffer_CountRefs(iface);
525 ret += IRpcStubBuffer_CountRefs(This->base_stub);
530 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl =
532 CStdStubBuffer_QueryInterface,
533 CStdStubBuffer_AddRef,
535 CStdStubBuffer_Delegating_Connect,
536 CStdStubBuffer_Delegating_Disconnect,
537 CStdStubBuffer_Invoke,
538 CStdStubBuffer_IsIIDSupported,
539 CStdStubBuffer_Delegating_CountRefs,
540 CStdStubBuffer_DebugServerQueryInterface,
541 CStdStubBuffer_DebugServerRelease
544 const MIDL_SERVER_INFO *CStdStubBuffer_GetServerInfo(IRpcStubBuffer *iface)
546 CStdStubBuffer *This = (CStdStubBuffer *)iface;
547 return STUB_HEADER(This).pServerInfo;
550 /************************************************************************
551 * NdrStubForwardingFunction [RPCRT4.@]
553 void __RPC_STUB NdrStubForwardingFunction( IRpcStubBuffer *iface, IRpcChannelBuffer *pChannel,
554 PRPC_MESSAGE pMsg, DWORD *pdwStubPhase )
556 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
558 cstdstubbuffer_delegating_t *This = impl_from_delegating(iface);
559 HRESULT r = IRpcStubBuffer_Invoke(This->base_stub, (RPCOLEMESSAGE*)pMsg, pChannel);
560 if(FAILED(r)) RpcRaiseException(r);
564 /***********************************************************************
565 * NdrStubInitialize [RPCRT4.@]
567 void WINAPI NdrStubInitialize(PRPC_MESSAGE pRpcMsg,
568 PMIDL_STUB_MESSAGE pStubMsg,
569 PMIDL_STUB_DESC pStubDescriptor,
570 LPRPCCHANNELBUFFER pRpcChannelBuffer)
572 TRACE("(%p,%p,%p,%p)\n", pRpcMsg, pStubMsg, pStubDescriptor, pRpcChannelBuffer);
573 NdrServerInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor);
574 pStubMsg->pRpcChannelBuffer = pRpcChannelBuffer;
575 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
576 &pStubMsg->dwDestContext,
577 &pStubMsg->pvDestContext);
580 /***********************************************************************
581 * NdrStubGetBuffer [RPCRT4.@]
583 void WINAPI NdrStubGetBuffer(LPRPCSTUBBUFFER iface,
584 LPRPCCHANNELBUFFER pRpcChannelBuffer,
585 PMIDL_STUB_MESSAGE pStubMsg)
587 CStdStubBuffer *This = (CStdStubBuffer *)iface;
590 TRACE("(%p, %p, %p)\n", This, pRpcChannelBuffer, pStubMsg);
592 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
593 hr = IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer,
594 (RPCOLEMESSAGE *)pStubMsg->RpcMsg, STUB_HEADER(This).piid);
597 RpcRaiseException(hr);
601 pStubMsg->Buffer = pStubMsg->RpcMsg->Buffer;