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
24 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
26 #define STUB_HEADER(This) (((const CInterfaceStubHeader*)((This)->lpVtbl))[-1])
28 static LONG WINAPI
stub_filter(EXCEPTION_POINTERS
*eptr
)
30 if (eptr
->ExceptionRecord
->ExceptionFlags
& EXCEPTION_NONCONTINUABLE
)
31 return EXCEPTION_CONTINUE_SEARCH
;
32 return EXCEPTION_EXECUTE_HANDLER
;
37 IUnknownVtbl
*base_obj
;
38 IRpcStubBuffer
*base_stub
;
39 CStdStubBuffer stub_buffer
;
40 } cstdstubbuffer_delegating_t
;
42 static inline cstdstubbuffer_delegating_t
*impl_from_delegating( IRpcStubBuffer
*iface
)
44 return CONTAINING_RECORD((void *)iface
, cstdstubbuffer_delegating_t
, stub_buffer
);
47 HRESULT
CStdStubBuffer_Construct(REFIID riid
,
50 CInterfaceStubVtbl
*vtbl
,
51 LPPSFACTORYBUFFER pPSFactory
,
52 LPRPCSTUBBUFFER
*ppStub
)
57 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer
, vtbl
, pPSFactory
, ppStub
, name
);
58 TRACE("iid=%s\n", debugstr_guid(vtbl
->header
.piid
));
59 TRACE("vtbl=%p\n", &vtbl
->Vtbl
);
61 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
62 ERR("IID mismatch during stub creation\n");
63 return RPC_E_UNEXPECTED
;
66 r
= IUnknown_QueryInterface(pUnkServer
, riid
, (void**)&pvServer
);
70 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(CStdStubBuffer
));
72 IUnknown_Release(pvServer
);
76 This
->lpVtbl
= &vtbl
->Vtbl
;
78 This
->pvServerObject
= pvServer
;
79 This
->pPSFactory
= pPSFactory
;
80 *ppStub
= (LPRPCSTUBBUFFER
)This
;
82 IPSFactoryBuffer_AddRef(pPSFactory
);
86 static CRITICAL_SECTION delegating_vtbl_section
;
87 static CRITICAL_SECTION_DEBUG critsect_debug
=
89 0, 0, &delegating_vtbl_section
,
90 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
91 0, 0, { (DWORD_PTR
)(__FILE__
": delegating_vtbl_section") }
93 static CRITICAL_SECTION delegating_vtbl_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
100 /* remaining entries in vtbl */
103 static ref_counted_vtbl
*current_vtbl
;
106 static HRESULT WINAPI
delegating_QueryInterface(IUnknown
*pUnk
, REFIID iid
, void **ppv
)
112 static ULONG WINAPI
delegating_AddRef(IUnknown
*pUnk
)
117 static ULONG WINAPI
delegating_Release(IUnknown
*pUnk
)
122 /* The idea here is to replace the first param on the stack
123 ie. This (which will point to cstdstubbuffer_delegating_t)
124 with This->stub_buffer.pvServerObject and then jump to the
125 relevant offset in This->stub_buffer.pvServerObject's vtbl.
129 #include "pshpack1.h"
131 BYTE mov1
[4]; /* mov 0x4(%esp),%eax 8b 44 24 04 */
132 BYTE mov2
[3]; /* mov 0x10(%eax),%eax 8b 40 10 */
133 BYTE mov3
[4]; /* mov %eax,0x4(%esp) 89 44 24 04 */
134 BYTE mov4
[2]; /* mov (%eax),%eax 8b 00 */
135 BYTE mov5
[2]; /* jmp *offset(%eax) ff a0 offset */
137 BYTE pad
[1]; /* nop 90 */
141 static const BYTE opcodes
[20] = { 0x8b, 0x44, 0x24, 0x04, 0x8b, 0x40, 0x10, 0x89, 0x44, 0x24, 0x04,
142 0x8b, 0x00, 0xff, 0xa0, 0, 0, 0, 0, 0x90 };
144 #elif defined(__x86_64__)
146 #include "pshpack1.h"
149 BYTE mov1
[4]; /* movq 0x20(%rcx),%rcx 48 8b 49 20 */
150 BYTE mov2
[3]; /* movq (%rcx),%rax 48 8b 01 */
151 BYTE jmp
[2]; /* jmp *offset(%rax) ff a0 offset */
153 BYTE pad
[3]; /* lea 0x0(%rsi),%rsi 48 8d 36 */
157 static const BYTE opcodes
[16] = { 0x48, 0x8b, 0x49, 0x20, 0x48, 0x8b, 0x01,
158 0xff, 0xa0, 0, 0, 0, 0, 0x48, 0x8d, 0x36 };
159 #elif defined(__arm__)
164 static const BYTE opcodes
[1];
168 #warning You must implement delegated proxies/stubs for your CPU
173 static const BYTE opcodes
[1];
177 #define BLOCK_SIZE 1024
178 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
180 static const vtbl_method_t
*method_blocks
[MAX_BLOCKS
];
182 static const vtbl_method_t
*allocate_block( unsigned int num
)
185 vtbl_method_t
*prev
, *block
;
188 block
= VirtualAlloc( NULL
, BLOCK_SIZE
* sizeof(*block
),
189 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
190 if (!block
) return NULL
;
192 for (i
= 0; i
< BLOCK_SIZE
; i
++)
194 memcpy( &block
[i
], opcodes
, sizeof(opcodes
) );
195 block
[i
].offset
= (BLOCK_SIZE
* num
+ i
+ 3) * sizeof(void *);
197 VirtualProtect( block
, BLOCK_SIZE
* sizeof(*block
), PAGE_EXECUTE_READ
, &oldprot
);
198 prev
= InterlockedCompareExchangePointer( (void **)&method_blocks
[num
], block
, NULL
);
199 if (prev
) /* someone beat us to it */
201 VirtualFree( block
, 0, MEM_RELEASE
);
207 static BOOL
fill_delegated_stub_table(IUnknownVtbl
*vtbl
, DWORD num
)
209 const void **entry
= (const void **)(vtbl
+ 1);
212 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
214 FIXME( "%u methods not supported\n", num
);
217 vtbl
->QueryInterface
= delegating_QueryInterface
;
218 vtbl
->AddRef
= delegating_AddRef
;
219 vtbl
->Release
= delegating_Release
;
220 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
222 const vtbl_method_t
*block
= method_blocks
[i
];
223 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
224 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++) *entry
++ = &block
[j
];
229 BOOL
fill_delegated_proxy_table(IUnknownVtbl
*vtbl
, DWORD num
)
231 const void **entry
= (const void **)(vtbl
+ 1);
234 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
236 FIXME( "%u methods not supported\n", num
);
239 vtbl
->QueryInterface
= IUnknown_QueryInterface_Proxy
;
240 vtbl
->AddRef
= IUnknown_AddRef_Proxy
;
241 vtbl
->Release
= IUnknown_Release_Proxy
;
242 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
244 const vtbl_method_t
*block
= method_blocks
[i
];
245 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
246 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
247 if (!*entry
) *entry
= &block
[j
];
252 static IUnknownVtbl
*get_delegating_vtbl(DWORD num_methods
)
256 if (num_methods
< 256) num_methods
= 256; /* avoid frequent reallocations */
258 EnterCriticalSection(&delegating_vtbl_section
);
260 if(!current_vtbl
|| num_methods
> current_vtbl
->size
)
262 ref_counted_vtbl
*table
= HeapAlloc(GetProcessHeap(), 0,
263 FIELD_OFFSET(ref_counted_vtbl
, vtbl
) + num_methods
* sizeof(void*));
266 LeaveCriticalSection(&delegating_vtbl_section
);
271 table
->size
= num_methods
;
272 fill_delegated_stub_table(&table
->vtbl
, num_methods
);
274 if (current_vtbl
&& current_vtbl
->ref
== 0)
276 TRACE("freeing old table\n");
277 HeapFree(GetProcessHeap(), 0, current_vtbl
);
279 current_vtbl
= table
;
283 ret
= ¤t_vtbl
->vtbl
;
284 LeaveCriticalSection(&delegating_vtbl_section
);
288 static void release_delegating_vtbl(IUnknownVtbl
*vtbl
)
290 ref_counted_vtbl
*table
= (ref_counted_vtbl
*)((DWORD
*)vtbl
- 1);
292 EnterCriticalSection(&delegating_vtbl_section
);
294 TRACE("ref now %d\n", table
->ref
);
295 if(table
->ref
== 0 && table
!= current_vtbl
)
297 TRACE("... and we're not current so free'ing\n");
298 HeapFree(GetProcessHeap(), 0, table
);
300 LeaveCriticalSection(&delegating_vtbl_section
);
303 HRESULT
CStdStubBuffer_Delegating_Construct(REFIID riid
,
304 LPUNKNOWN pUnkServer
,
305 PCInterfaceName name
,
306 CInterfaceStubVtbl
*vtbl
,
307 REFIID delegating_iid
,
308 LPPSFACTORYBUFFER pPSFactory
,
309 LPRPCSTUBBUFFER
*ppStub
)
311 cstdstubbuffer_delegating_t
*This
;
315 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer
, vtbl
, pPSFactory
, ppStub
, name
);
316 TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl
->header
.piid
), debugstr_guid(delegating_iid
));
317 TRACE("vtbl=%p\n", &vtbl
->Vtbl
);
319 if (!IsEqualGUID(vtbl
->header
.piid
, riid
))
321 ERR("IID mismatch during stub creation\n");
322 return RPC_E_UNEXPECTED
;
325 r
= IUnknown_QueryInterface(pUnkServer
, riid
, (void**)&pvServer
);
326 if(FAILED(r
)) return r
;
328 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
331 IUnknown_Release(pvServer
);
332 return E_OUTOFMEMORY
;
335 This
->base_obj
= get_delegating_vtbl( vtbl
->header
.DispatchTableCount
);
336 r
= create_stub(delegating_iid
, (IUnknown
*)&This
->base_obj
, &This
->base_stub
);
339 release_delegating_vtbl(This
->base_obj
);
340 HeapFree(GetProcessHeap(), 0, This
);
341 IUnknown_Release(pvServer
);
345 This
->stub_buffer
.lpVtbl
= &vtbl
->Vtbl
;
346 This
->stub_buffer
.RefCount
= 1;
347 This
->stub_buffer
.pvServerObject
= pvServer
;
348 This
->stub_buffer
.pPSFactory
= pPSFactory
;
349 *ppStub
= (LPRPCSTUBBUFFER
)&This
->stub_buffer
;
351 IPSFactoryBuffer_AddRef(pPSFactory
);
355 HRESULT WINAPI
CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface
,
359 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
360 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
362 if (IsEqualIID(&IID_IUnknown
, riid
) ||
363 IsEqualIID(&IID_IRpcStubBuffer
, riid
))
365 IRpcStubBuffer_AddRef(iface
);
370 return E_NOINTERFACE
;
373 ULONG WINAPI
CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface
)
375 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
376 TRACE("(%p)->AddRef()\n",This
);
377 return InterlockedIncrement(&This
->RefCount
);
380 ULONG WINAPI
NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface
,
381 LPPSFACTORYBUFFER pPSF
)
383 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
386 TRACE("(%p)->Release()\n",This
);
388 refs
= InterlockedDecrement(&This
->RefCount
);
391 /* test_Release shows that native doesn't call Disconnect here.
392 We'll leave it in for the time being. */
393 IRpcStubBuffer_Disconnect(iface
);
395 IPSFactoryBuffer_Release(pPSF
);
396 HeapFree(GetProcessHeap(),0,This
);
401 ULONG WINAPI
NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface
,
402 LPPSFACTORYBUFFER pPSF
)
404 cstdstubbuffer_delegating_t
*This
= impl_from_delegating( iface
);
407 TRACE("(%p)->Release()\n", This
);
409 refs
= InterlockedDecrement(&This
->stub_buffer
.RefCount
);
412 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
414 IRpcStubBuffer_Disconnect((IRpcStubBuffer
*)&This
->stub_buffer
);
416 IRpcStubBuffer_Release(This
->base_stub
);
417 release_delegating_vtbl(This
->base_obj
);
419 IPSFactoryBuffer_Release(pPSF
);
420 HeapFree(GetProcessHeap(), 0, This
);
426 HRESULT WINAPI
CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface
,
427 LPUNKNOWN lpUnkServer
)
429 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
431 IUnknown
*new = NULL
;
433 TRACE("(%p)->Connect(%p)\n",This
,lpUnkServer
);
435 r
= IUnknown_QueryInterface(lpUnkServer
, STUB_HEADER(This
).piid
, (void**)&new);
436 new = InterlockedExchangePointer((void**)&This
->pvServerObject
, new);
438 IUnknown_Release(new);
442 void WINAPI
CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface
)
444 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
446 TRACE("(%p)->Disconnect()\n",This
);
448 old
= InterlockedExchangePointer((void**)&This
->pvServerObject
, NULL
);
451 IUnknown_Release(old
);
454 HRESULT WINAPI
CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface
,
456 LPRPCCHANNELBUFFER pChannel
)
458 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
459 DWORD dwPhase
= STUB_UNMARSHAL
;
462 TRACE("(%p)->Invoke(%p,%p)\n",This
,pMsg
,pChannel
);
466 if (STUB_HEADER(This
).pDispatchTable
)
467 STUB_HEADER(This
).pDispatchTable
[pMsg
->iMethod
](iface
, pChannel
, (PRPC_MESSAGE
)pMsg
, &dwPhase
);
468 else /* pure interpreted */
469 NdrStubCall2(iface
, pChannel
, (PRPC_MESSAGE
)pMsg
, &dwPhase
);
471 __EXCEPT(stub_filter
)
473 DWORD dwExceptionCode
= GetExceptionCode();
474 WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode
, dwExceptionCode
);
475 if (FAILED(dwExceptionCode
))
476 hr
= dwExceptionCode
;
478 hr
= HRESULT_FROM_WIN32(dwExceptionCode
);
485 LPRPCSTUBBUFFER WINAPI
CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface
,
488 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
489 TRACE("(%p)->IsIIDSupported(%s)\n",This
,debugstr_guid(riid
));
490 return IsEqualGUID(STUB_HEADER(This
).piid
, riid
) ? iface
: NULL
;
493 ULONG WINAPI
CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface
)
495 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
496 TRACE("(%p)->CountRefs()\n",This
);
497 return This
->RefCount
;
500 HRESULT WINAPI
CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
,
503 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
504 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This
,ppv
);
508 void WINAPI
CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface
,
511 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
512 TRACE("(%p)->DebugServerRelease(%p)\n",This
,pv
);
515 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl
=
517 CStdStubBuffer_QueryInterface
,
518 CStdStubBuffer_AddRef
,
520 CStdStubBuffer_Connect
,
521 CStdStubBuffer_Disconnect
,
522 CStdStubBuffer_Invoke
,
523 CStdStubBuffer_IsIIDSupported
,
524 CStdStubBuffer_CountRefs
,
525 CStdStubBuffer_DebugServerQueryInterface
,
526 CStdStubBuffer_DebugServerRelease
529 static HRESULT WINAPI
CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface
,
530 LPUNKNOWN lpUnkServer
)
532 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
534 TRACE("(%p)->Connect(%p)\n", This
, lpUnkServer
);
536 r
= CStdStubBuffer_Connect(iface
, lpUnkServer
);
538 r
= IRpcStubBuffer_Connect(This
->base_stub
, (IUnknown
*)&This
->base_obj
);
543 static void WINAPI
CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface
)
545 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
546 TRACE("(%p)->Disconnect()\n", This
);
548 IRpcStubBuffer_Disconnect(This
->base_stub
);
549 CStdStubBuffer_Disconnect(iface
);
552 static ULONG WINAPI
CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface
)
554 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
556 TRACE("(%p)->CountRefs()\n", This
);
558 ret
= CStdStubBuffer_CountRefs(iface
);
559 ret
+= IRpcStubBuffer_CountRefs(This
->base_stub
);
564 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl
=
566 CStdStubBuffer_QueryInterface
,
567 CStdStubBuffer_AddRef
,
569 CStdStubBuffer_Delegating_Connect
,
570 CStdStubBuffer_Delegating_Disconnect
,
571 CStdStubBuffer_Invoke
,
572 CStdStubBuffer_IsIIDSupported
,
573 CStdStubBuffer_Delegating_CountRefs
,
574 CStdStubBuffer_DebugServerQueryInterface
,
575 CStdStubBuffer_DebugServerRelease
578 const MIDL_SERVER_INFO
*CStdStubBuffer_GetServerInfo(IRpcStubBuffer
*iface
)
580 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
581 return STUB_HEADER(This
).pServerInfo
;
584 /************************************************************************
585 * NdrStubForwardingFunction [RPCRT4.@]
587 void __RPC_STUB
NdrStubForwardingFunction( IRpcStubBuffer
*iface
, IRpcChannelBuffer
*pChannel
,
588 PRPC_MESSAGE pMsg
, DWORD
*pdwStubPhase
)
590 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
592 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
593 HRESULT r
= IRpcStubBuffer_Invoke(This
->base_stub
, (RPCOLEMESSAGE
*)pMsg
, pChannel
);
594 if(FAILED(r
)) RpcRaiseException(r
);
598 /***********************************************************************
599 * NdrStubInitialize [RPCRT4.@]
601 void WINAPI
NdrStubInitialize(PRPC_MESSAGE pRpcMsg
,
602 PMIDL_STUB_MESSAGE pStubMsg
,
603 PMIDL_STUB_DESC pStubDescriptor
,
604 LPRPCCHANNELBUFFER pRpcChannelBuffer
)
606 TRACE("(%p,%p,%p,%p)\n", pRpcMsg
, pStubMsg
, pStubDescriptor
, pRpcChannelBuffer
);
607 NdrServerInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
);
608 pStubMsg
->pRpcChannelBuffer
= pRpcChannelBuffer
;
609 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
610 &pStubMsg
->dwDestContext
,
611 &pStubMsg
->pvDestContext
);
614 /***********************************************************************
615 * NdrStubGetBuffer [RPCRT4.@]
617 void WINAPI
NdrStubGetBuffer(LPRPCSTUBBUFFER iface
,
618 LPRPCCHANNELBUFFER pRpcChannelBuffer
,
619 PMIDL_STUB_MESSAGE pStubMsg
)
621 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
624 TRACE("(%p, %p, %p)\n", This
, pRpcChannelBuffer
, pStubMsg
);
626 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
627 hr
= IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer
,
628 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
, STUB_HEADER(This
).piid
);
631 RpcRaiseException(hr
);
635 pStubMsg
->Buffer
= pStubMsg
->RpcMsg
->Buffer
;