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 CONTAINING_RECORD(iface
, 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 /* The idea here is to replace the first param on the stack
141 ie. This (which will point to cstdstubbuffer_delegating_t)
142 with This->stub_buffer.pvServerObject and then jump to the
143 relevant offset in This->stub_buffer.pvServerObject's vtbl.
147 #include "pshpack1.h"
149 BYTE mov1
[4]; /* mov 0x4(%esp),%eax 8b 44 24 04 */
150 BYTE mov2
[3]; /* mov 0x10(%eax),%eax 8b 40 10 */
151 BYTE mov3
[4]; /* mov %eax,0x4(%esp) 89 44 24 04 */
152 BYTE mov4
[2]; /* mov (%eax),%eax 8b 00 */
153 BYTE mov5
[2]; /* jmp *offset(%eax) ff a0 offset */
155 BYTE pad
[1]; /* nop 90 */
159 static const BYTE opcodes
[20] = { 0x8b, 0x44, 0x24, 0x04, 0x8b, 0x40, 0x10, 0x89, 0x44, 0x24, 0x04,
160 0x8b, 0x00, 0xff, 0xa0, 0, 0, 0, 0, 0x90 };
162 #elif defined(__x86_64__)
164 #include "pshpack1.h"
167 BYTE mov1
[4]; /* movq 0x20(%rcx),%rcx 48 8b 49 20 */
168 BYTE mov2
[3]; /* movq (%rcx),%rax 48 8b 01 */
169 BYTE jmp
[2]; /* jmp *offset(%rax) ff a0 offset */
171 BYTE pad
[3]; /* lea 0x0(%rsi),%rsi 48 8d 36 */
175 static const BYTE opcodes
[16] = { 0x48, 0x8b, 0x49, 0x20, 0x48, 0x8b, 0x01,
176 0xff, 0xa0, 0, 0, 0, 0, 0x48, 0x8d, 0x36 };
179 #warning You must implement delegated proxies/stubs for your CPU
184 static const BYTE opcodes
[1];
188 #define BLOCK_SIZE 1024
189 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
191 static const vtbl_method_t
*method_blocks
[MAX_BLOCKS
];
193 static const vtbl_method_t
*allocate_block( unsigned int num
)
196 vtbl_method_t
*prev
, *block
;
199 block
= VirtualAlloc( NULL
, BLOCK_SIZE
* sizeof(*block
),
200 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
201 if (!block
) return NULL
;
203 for (i
= 0; i
< BLOCK_SIZE
; i
++)
205 memcpy( &block
[i
], opcodes
, sizeof(opcodes
) );
206 block
[i
].offset
= (BLOCK_SIZE
* num
+ i
+ 3) * sizeof(void *);
208 VirtualProtect( block
, BLOCK_SIZE
* sizeof(*block
), PAGE_EXECUTE_READ
, &oldprot
);
209 prev
= InterlockedCompareExchangePointer( (void **)&method_blocks
[num
], block
, NULL
);
210 if (prev
) /* someone beat us to it */
212 VirtualFree( block
, 0, MEM_RELEASE
);
218 static BOOL
fill_delegated_stub_table(IUnknownVtbl
*vtbl
, DWORD num
)
220 const void **entry
= (const void **)(vtbl
+ 1);
223 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
225 FIXME( "%u methods not supported\n", num
);
228 vtbl
->QueryInterface
= delegating_QueryInterface
;
229 vtbl
->AddRef
= delegating_AddRef
;
230 vtbl
->Release
= delegating_Release
;
231 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
233 const vtbl_method_t
*block
= method_blocks
[i
];
234 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
235 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++) *entry
++ = &block
[j
];
240 BOOL
fill_delegated_proxy_table(IUnknownVtbl
*vtbl
, DWORD num
)
242 const void **entry
= (const void **)(vtbl
+ 1);
245 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
247 FIXME( "%u methods not supported\n", num
);
250 vtbl
->QueryInterface
= IUnknown_QueryInterface_Proxy
;
251 vtbl
->AddRef
= IUnknown_AddRef_Proxy
;
252 vtbl
->Release
= IUnknown_Release_Proxy
;
253 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
255 const vtbl_method_t
*block
= method_blocks
[i
];
256 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
257 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
258 if (!*entry
) *entry
= &block
[j
];
263 static IUnknownVtbl
*get_delegating_vtbl(DWORD num_methods
)
267 if (num_methods
< 256) num_methods
= 256; /* avoid frequent reallocations */
269 EnterCriticalSection(&delegating_vtbl_section
);
271 if(!current_vtbl
|| num_methods
> current_vtbl
->size
)
273 ref_counted_vtbl
*table
= HeapAlloc(GetProcessHeap(), 0,
274 FIELD_OFFSET(ref_counted_vtbl
, vtbl
) + num_methods
* sizeof(void*));
277 LeaveCriticalSection(&delegating_vtbl_section
);
282 table
->size
= num_methods
;
283 fill_delegated_stub_table(&table
->vtbl
, num_methods
);
285 if (current_vtbl
&& current_vtbl
->ref
== 0)
287 TRACE("freeing old table\n");
288 HeapFree(GetProcessHeap(), 0, current_vtbl
);
290 current_vtbl
= table
;
294 ret
= ¤t_vtbl
->vtbl
;
295 LeaveCriticalSection(&delegating_vtbl_section
);
299 static void release_delegating_vtbl(IUnknownVtbl
*vtbl
)
301 ref_counted_vtbl
*table
= (ref_counted_vtbl
*)((DWORD
*)vtbl
- 1);
303 EnterCriticalSection(&delegating_vtbl_section
);
305 TRACE("ref now %d\n", table
->ref
);
306 if(table
->ref
== 0 && table
!= current_vtbl
)
308 TRACE("... and we're not current so free'ing\n");
309 HeapFree(GetProcessHeap(), 0, table
);
311 LeaveCriticalSection(&delegating_vtbl_section
);
314 HRESULT
CStdStubBuffer_Delegating_Construct(REFIID riid
,
315 LPUNKNOWN pUnkServer
,
316 PCInterfaceName name
,
317 CInterfaceStubVtbl
*vtbl
,
318 REFIID delegating_iid
,
319 LPPSFACTORYBUFFER pPSFactory
,
320 LPRPCSTUBBUFFER
*ppStub
)
322 cstdstubbuffer_delegating_t
*This
;
326 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer
, vtbl
, pPSFactory
, ppStub
, name
);
327 TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl
->header
.piid
), debugstr_guid(delegating_iid
));
328 TRACE("vtbl=%p\n", &vtbl
->Vtbl
);
330 if (!IsEqualGUID(vtbl
->header
.piid
, riid
))
332 ERR("IID mismatch during stub creation\n");
333 return RPC_E_UNEXPECTED
;
336 r
= IUnknown_QueryInterface(pUnkServer
, riid
, (void**)&pvServer
);
337 if(FAILED(r
)) return r
;
339 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
342 IUnknown_Release(pvServer
);
343 return E_OUTOFMEMORY
;
346 This
->base_obj
= get_delegating_vtbl( vtbl
->header
.DispatchTableCount
);
347 r
= create_stub(delegating_iid
, (IUnknown
*)&This
->base_obj
, &This
->base_stub
);
350 release_delegating_vtbl(This
->base_obj
);
351 HeapFree(GetProcessHeap(), 0, This
);
352 IUnknown_Release(pvServer
);
356 This
->stub_buffer
.lpVtbl
= &vtbl
->Vtbl
;
357 This
->stub_buffer
.RefCount
= 1;
358 This
->stub_buffer
.pvServerObject
= pvServer
;
359 This
->stub_buffer
.pPSFactory
= pPSFactory
;
360 *ppStub
= (LPRPCSTUBBUFFER
)&This
->stub_buffer
;
362 IPSFactoryBuffer_AddRef(pPSFactory
);
366 HRESULT WINAPI
CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface
,
370 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
371 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
373 if (IsEqualIID(&IID_IUnknown
, riid
) ||
374 IsEqualIID(&IID_IRpcStubBuffer
, riid
))
376 IRpcStubBuffer_AddRef(iface
);
381 return E_NOINTERFACE
;
384 ULONG WINAPI
CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface
)
386 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
387 TRACE("(%p)->AddRef()\n",This
);
388 return InterlockedIncrement(&This
->RefCount
);
391 ULONG WINAPI
NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface
,
392 LPPSFACTORYBUFFER pPSF
)
394 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
397 TRACE("(%p)->Release()\n",This
);
399 refs
= InterlockedDecrement(&This
->RefCount
);
402 /* test_Release shows that native doesn't call Disconnect here.
403 We'll leave it in for the time being. */
404 IRpcStubBuffer_Disconnect(iface
);
406 IPSFactoryBuffer_Release(pPSF
);
407 HeapFree(GetProcessHeap(),0,This
);
412 ULONG WINAPI
NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface
,
413 LPPSFACTORYBUFFER pPSF
)
415 cstdstubbuffer_delegating_t
*This
= impl_from_delegating( iface
);
418 TRACE("(%p)->Release()\n", This
);
420 refs
= InterlockedDecrement(&This
->stub_buffer
.RefCount
);
423 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
425 IRpcStubBuffer_Disconnect((IRpcStubBuffer
*)&This
->stub_buffer
);
427 IRpcStubBuffer_Release(This
->base_stub
);
428 release_delegating_vtbl(This
->base_obj
);
430 IPSFactoryBuffer_Release(pPSF
);
431 HeapFree(GetProcessHeap(), 0, This
);
437 HRESULT WINAPI
CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface
,
438 LPUNKNOWN lpUnkServer
)
440 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
442 IUnknown
*new = NULL
;
444 TRACE("(%p)->Connect(%p)\n",This
,lpUnkServer
);
446 r
= IUnknown_QueryInterface(lpUnkServer
, STUB_HEADER(This
).piid
, (void**)&new);
447 new = InterlockedExchangePointer((void**)&This
->pvServerObject
, new);
449 IUnknown_Release(new);
453 void WINAPI
CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface
)
455 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
457 TRACE("(%p)->Disconnect()\n",This
);
459 old
= InterlockedExchangePointer((void**)&This
->pvServerObject
, NULL
);
462 IUnknown_Release(old
);
465 HRESULT WINAPI
CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface
,
467 LPRPCCHANNELBUFFER pChannel
)
469 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
470 DWORD dwPhase
= STUB_UNMARSHAL
;
473 TRACE("(%p)->Invoke(%p,%p)\n",This
,pMsg
,pChannel
);
477 if (STUB_HEADER(This
).pDispatchTable
)
478 STUB_HEADER(This
).pDispatchTable
[pMsg
->iMethod
](iface
, pChannel
, (PRPC_MESSAGE
)pMsg
, &dwPhase
);
479 else /* pure interpreted */
480 NdrStubCall2(iface
, pChannel
, (PRPC_MESSAGE
)pMsg
, &dwPhase
);
482 __EXCEPT(stub_filter
)
484 DWORD dwExceptionCode
= GetExceptionCode();
485 WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode
, dwExceptionCode
);
486 if (FAILED(dwExceptionCode
))
487 hr
= dwExceptionCode
;
489 hr
= HRESULT_FROM_WIN32(dwExceptionCode
);
496 LPRPCSTUBBUFFER WINAPI
CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface
,
499 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
500 TRACE("(%p)->IsIIDSupported(%s)\n",This
,debugstr_guid(riid
));
501 return IsEqualGUID(STUB_HEADER(This
).piid
, riid
) ? iface
: NULL
;
504 ULONG WINAPI
CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface
)
506 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
507 TRACE("(%p)->CountRefs()\n",This
);
508 return This
->RefCount
;
511 HRESULT WINAPI
CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
,
514 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
515 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This
,ppv
);
519 void WINAPI
CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface
,
522 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
523 TRACE("(%p)->DebugServerRelease(%p)\n",This
,pv
);
526 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl
=
528 CStdStubBuffer_QueryInterface
,
529 CStdStubBuffer_AddRef
,
531 CStdStubBuffer_Connect
,
532 CStdStubBuffer_Disconnect
,
533 CStdStubBuffer_Invoke
,
534 CStdStubBuffer_IsIIDSupported
,
535 CStdStubBuffer_CountRefs
,
536 CStdStubBuffer_DebugServerQueryInterface
,
537 CStdStubBuffer_DebugServerRelease
540 static HRESULT WINAPI
CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface
,
541 LPUNKNOWN lpUnkServer
)
543 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
545 TRACE("(%p)->Connect(%p)\n", This
, lpUnkServer
);
547 r
= CStdStubBuffer_Connect(iface
, lpUnkServer
);
549 r
= IRpcStubBuffer_Connect(This
->base_stub
, (IUnknown
*)&This
->base_obj
);
554 static void WINAPI
CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface
)
556 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
557 TRACE("(%p)->Disconnect()\n", This
);
559 IRpcStubBuffer_Disconnect(This
->base_stub
);
560 CStdStubBuffer_Disconnect(iface
);
563 static ULONG WINAPI
CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface
)
565 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
567 TRACE("(%p)->CountRefs()\n", This
);
569 ret
= CStdStubBuffer_CountRefs(iface
);
570 ret
+= IRpcStubBuffer_CountRefs(This
->base_stub
);
575 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl
=
577 CStdStubBuffer_QueryInterface
,
578 CStdStubBuffer_AddRef
,
580 CStdStubBuffer_Delegating_Connect
,
581 CStdStubBuffer_Delegating_Disconnect
,
582 CStdStubBuffer_Invoke
,
583 CStdStubBuffer_IsIIDSupported
,
584 CStdStubBuffer_Delegating_CountRefs
,
585 CStdStubBuffer_DebugServerQueryInterface
,
586 CStdStubBuffer_DebugServerRelease
589 const MIDL_SERVER_INFO
*CStdStubBuffer_GetServerInfo(IRpcStubBuffer
*iface
)
591 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
592 return STUB_HEADER(This
).pServerInfo
;
595 /************************************************************************
596 * NdrStubForwardingFunction [RPCRT4.@]
598 void __RPC_STUB
NdrStubForwardingFunction( IRpcStubBuffer
*iface
, IRpcChannelBuffer
*pChannel
,
599 PRPC_MESSAGE pMsg
, DWORD
*pdwStubPhase
)
601 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
603 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
604 HRESULT r
= IRpcStubBuffer_Invoke(This
->base_stub
, (RPCOLEMESSAGE
*)pMsg
, pChannel
);
605 if(FAILED(r
)) RpcRaiseException(r
);
609 /***********************************************************************
610 * NdrStubInitialize [RPCRT4.@]
612 void WINAPI
NdrStubInitialize(PRPC_MESSAGE pRpcMsg
,
613 PMIDL_STUB_MESSAGE pStubMsg
,
614 PMIDL_STUB_DESC pStubDescriptor
,
615 LPRPCCHANNELBUFFER pRpcChannelBuffer
)
617 TRACE("(%p,%p,%p,%p)\n", pRpcMsg
, pStubMsg
, pStubDescriptor
, pRpcChannelBuffer
);
618 NdrServerInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
);
619 pStubMsg
->pRpcChannelBuffer
= pRpcChannelBuffer
;
620 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
621 &pStubMsg
->dwDestContext
,
622 &pStubMsg
->pvDestContext
);
625 /***********************************************************************
626 * NdrStubGetBuffer [RPCRT4.@]
628 void WINAPI
NdrStubGetBuffer(LPRPCSTUBBUFFER iface
,
629 LPRPCCHANNELBUFFER pRpcChannelBuffer
,
630 PMIDL_STUB_MESSAGE pStubMsg
)
632 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
635 TRACE("(%p, %p, %p)\n", This
, pRpcChannelBuffer
, pStubMsg
);
637 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
638 hr
= IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer
,
639 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
, STUB_HEADER(This
).piid
);
642 RpcRaiseException(hr
);
646 pStubMsg
->Buffer
= pStubMsg
->RpcMsg
->Buffer
;