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 (cstdstubbuffer_delegating_t
*)((char *)iface
- FIELD_OFFSET(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 };
161 #warning You must implement delegated proxies/stubs for your CPU
166 static const BYTE opcodes
[1];
170 #define BLOCK_SIZE 1024
171 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
173 static const vtbl_method_t
*method_blocks
[MAX_BLOCKS
];
175 static const vtbl_method_t
*allocate_block( unsigned int num
)
178 vtbl_method_t
*prev
, *block
;
180 block
= VirtualAlloc( NULL
, BLOCK_SIZE
* sizeof(*block
),
181 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
182 if (!block
) return NULL
;
184 for (i
= 0; i
< BLOCK_SIZE
; i
++)
186 memcpy( &block
[i
], opcodes
, sizeof(opcodes
) );
187 block
[i
].offset
= (BLOCK_SIZE
* num
+ i
+ 3) * sizeof(void *);
189 VirtualProtect( block
, BLOCK_SIZE
* sizeof(*block
), PAGE_EXECUTE_READ
, NULL
);
190 prev
= InterlockedCompareExchangePointer( (void **)&method_blocks
[num
], block
, NULL
);
191 if (prev
) /* someone beat us to it */
193 VirtualFree( block
, 0, MEM_RELEASE
);
199 static BOOL
fill_delegated_stub_table(IUnknownVtbl
*vtbl
, DWORD num
)
201 const void **entry
= (const void **)(vtbl
+ 1);
204 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
206 FIXME( "%u methods not supported\n", num
);
209 vtbl
->QueryInterface
= delegating_QueryInterface
;
210 vtbl
->AddRef
= delegating_AddRef
;
211 vtbl
->Release
= delegating_Release
;
212 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
214 const vtbl_method_t
*block
= method_blocks
[i
];
215 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
216 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++) *entry
++ = &block
[j
];
221 BOOL
fill_delegated_proxy_table(IUnknownVtbl
*vtbl
, DWORD num
)
223 const void **entry
= (const void **)(vtbl
+ 1);
226 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
228 FIXME( "%u methods not supported\n", num
);
231 vtbl
->QueryInterface
= IUnknown_QueryInterface_Proxy
;
232 vtbl
->AddRef
= IUnknown_AddRef_Proxy
;
233 vtbl
->Release
= IUnknown_Release_Proxy
;
234 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
236 const vtbl_method_t
*block
= method_blocks
[i
];
237 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
238 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
239 if (!*entry
) *entry
= &block
[j
];
244 static IUnknownVtbl
*get_delegating_vtbl(DWORD num_methods
)
248 if (num_methods
< 256) num_methods
= 256; /* avoid frequent reallocations */
250 EnterCriticalSection(&delegating_vtbl_section
);
252 if(!current_vtbl
|| num_methods
> current_vtbl
->size
)
254 ref_counted_vtbl
*table
= HeapAlloc(GetProcessHeap(), 0,
255 FIELD_OFFSET(ref_counted_vtbl
, vtbl
) + num_methods
* sizeof(void*));
258 LeaveCriticalSection(&delegating_vtbl_section
);
263 table
->size
= num_methods
;
264 fill_delegated_stub_table(&table
->vtbl
, num_methods
);
266 if (current_vtbl
&& current_vtbl
->ref
== 0)
268 TRACE("freeing old table\n");
269 HeapFree(GetProcessHeap(), 0, current_vtbl
);
271 current_vtbl
= table
;
275 ret
= ¤t_vtbl
->vtbl
;
276 LeaveCriticalSection(&delegating_vtbl_section
);
280 static void release_delegating_vtbl(IUnknownVtbl
*vtbl
)
282 ref_counted_vtbl
*table
= (ref_counted_vtbl
*)((DWORD
*)vtbl
- 1);
284 EnterCriticalSection(&delegating_vtbl_section
);
286 TRACE("ref now %d\n", table
->ref
);
287 if(table
->ref
== 0 && table
!= current_vtbl
)
289 TRACE("... and we're not current so free'ing\n");
290 HeapFree(GetProcessHeap(), 0, table
);
292 LeaveCriticalSection(&delegating_vtbl_section
);
295 HRESULT
CStdStubBuffer_Delegating_Construct(REFIID riid
,
296 LPUNKNOWN pUnkServer
,
297 PCInterfaceName name
,
298 CInterfaceStubVtbl
*vtbl
,
299 REFIID delegating_iid
,
300 LPPSFACTORYBUFFER pPSFactory
,
301 LPRPCSTUBBUFFER
*ppStub
)
303 cstdstubbuffer_delegating_t
*This
;
307 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer
, vtbl
, pPSFactory
, ppStub
, name
);
308 TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl
->header
.piid
), debugstr_guid(delegating_iid
));
309 TRACE("vtbl=%p\n", &vtbl
->Vtbl
);
311 if (!IsEqualGUID(vtbl
->header
.piid
, riid
))
313 ERR("IID mismatch during stub creation\n");
314 return RPC_E_UNEXPECTED
;
317 r
= IUnknown_QueryInterface(pUnkServer
, riid
, (void**)&pvServer
);
318 if(FAILED(r
)) return r
;
320 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
323 IUnknown_Release(pvServer
);
324 return E_OUTOFMEMORY
;
327 This
->base_obj
= get_delegating_vtbl( vtbl
->header
.DispatchTableCount
);
328 r
= create_stub(delegating_iid
, (IUnknown
*)&This
->base_obj
, &This
->base_stub
);
331 release_delegating_vtbl(This
->base_obj
);
332 HeapFree(GetProcessHeap(), 0, This
);
333 IUnknown_Release(pvServer
);
337 This
->stub_buffer
.lpVtbl
= &vtbl
->Vtbl
;
338 This
->stub_buffer
.RefCount
= 1;
339 This
->stub_buffer
.pvServerObject
= pvServer
;
340 This
->stub_buffer
.pPSFactory
= pPSFactory
;
341 *ppStub
= (LPRPCSTUBBUFFER
)&This
->stub_buffer
;
343 IPSFactoryBuffer_AddRef(pPSFactory
);
347 HRESULT WINAPI
CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface
,
351 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
352 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
354 if (IsEqualIID(&IID_IUnknown
, riid
) ||
355 IsEqualIID(&IID_IRpcStubBuffer
, riid
))
357 IRpcStubBuffer_AddRef(iface
);
362 return E_NOINTERFACE
;
365 ULONG WINAPI
CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface
)
367 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
368 TRACE("(%p)->AddRef()\n",This
);
369 return InterlockedIncrement(&This
->RefCount
);
372 ULONG WINAPI
NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface
,
373 LPPSFACTORYBUFFER pPSF
)
375 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
378 TRACE("(%p)->Release()\n",This
);
380 refs
= InterlockedDecrement(&This
->RefCount
);
383 /* test_Release shows that native doesn't call Disconnect here.
384 We'll leave it in for the time being. */
385 IRpcStubBuffer_Disconnect(iface
);
387 IPSFactoryBuffer_Release(pPSF
);
388 HeapFree(GetProcessHeap(),0,This
);
393 ULONG WINAPI
NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface
,
394 LPPSFACTORYBUFFER pPSF
)
396 cstdstubbuffer_delegating_t
*This
= impl_from_delegating( iface
);
399 TRACE("(%p)->Release()\n", This
);
401 refs
= InterlockedDecrement(&This
->stub_buffer
.RefCount
);
404 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
406 IRpcStubBuffer_Disconnect((IRpcStubBuffer
*)&This
->stub_buffer
);
408 IRpcStubBuffer_Release(This
->base_stub
);
409 release_delegating_vtbl(This
->base_obj
);
411 IPSFactoryBuffer_Release(pPSF
);
412 HeapFree(GetProcessHeap(), 0, This
);
418 HRESULT WINAPI
CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface
,
419 LPUNKNOWN lpUnkServer
)
421 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
423 IUnknown
*new = NULL
;
425 TRACE("(%p)->Connect(%p)\n",This
,lpUnkServer
);
427 r
= IUnknown_QueryInterface(lpUnkServer
, STUB_HEADER(This
).piid
, (void**)&new);
428 new = InterlockedExchangePointer((void**)&This
->pvServerObject
, new);
430 IUnknown_Release(new);
434 void WINAPI
CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface
)
436 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
438 TRACE("(%p)->Disconnect()\n",This
);
440 old
= InterlockedExchangePointer((void**)&This
->pvServerObject
, NULL
);
443 IUnknown_Release(old
);
446 HRESULT WINAPI
CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface
,
448 LPRPCCHANNELBUFFER pChannel
)
450 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
451 DWORD dwPhase
= STUB_UNMARSHAL
;
454 TRACE("(%p)->Invoke(%p,%p)\n",This
,pMsg
,pChannel
);
458 if (STUB_HEADER(This
).pDispatchTable
)
459 STUB_HEADER(This
).pDispatchTable
[pMsg
->iMethod
](iface
, pChannel
, (PRPC_MESSAGE
)pMsg
, &dwPhase
);
460 else /* pure interpreted */
461 NdrStubCall2(iface
, pChannel
, (PRPC_MESSAGE
)pMsg
, &dwPhase
);
463 __EXCEPT(stub_filter
)
465 DWORD dwExceptionCode
= GetExceptionCode();
466 WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode
, dwExceptionCode
);
467 if (FAILED(dwExceptionCode
))
468 hr
= dwExceptionCode
;
470 hr
= HRESULT_FROM_WIN32(dwExceptionCode
);
477 LPRPCSTUBBUFFER WINAPI
CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface
,
480 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
481 TRACE("(%p)->IsIIDSupported(%s)\n",This
,debugstr_guid(riid
));
482 return IsEqualGUID(STUB_HEADER(This
).piid
, riid
) ? iface
: NULL
;
485 ULONG WINAPI
CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface
)
487 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
488 TRACE("(%p)->CountRefs()\n",This
);
489 return This
->RefCount
;
492 HRESULT WINAPI
CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
,
495 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
496 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This
,ppv
);
500 void WINAPI
CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface
,
503 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
504 TRACE("(%p)->DebugServerRelease(%p)\n",This
,pv
);
507 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl
=
509 CStdStubBuffer_QueryInterface
,
510 CStdStubBuffer_AddRef
,
512 CStdStubBuffer_Connect
,
513 CStdStubBuffer_Disconnect
,
514 CStdStubBuffer_Invoke
,
515 CStdStubBuffer_IsIIDSupported
,
516 CStdStubBuffer_CountRefs
,
517 CStdStubBuffer_DebugServerQueryInterface
,
518 CStdStubBuffer_DebugServerRelease
521 static HRESULT WINAPI
CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface
,
522 LPUNKNOWN lpUnkServer
)
524 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
526 TRACE("(%p)->Connect(%p)\n", This
, lpUnkServer
);
528 r
= CStdStubBuffer_Connect(iface
, lpUnkServer
);
530 r
= IRpcStubBuffer_Connect(This
->base_stub
, (IUnknown
*)&This
->base_obj
);
535 static void WINAPI
CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface
)
537 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
538 TRACE("(%p)->Disconnect()\n", This
);
540 IRpcStubBuffer_Disconnect(This
->base_stub
);
541 CStdStubBuffer_Disconnect(iface
);
544 static ULONG WINAPI
CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface
)
546 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
548 TRACE("(%p)->CountRefs()\n", This
);
550 ret
= CStdStubBuffer_CountRefs(iface
);
551 ret
+= IRpcStubBuffer_CountRefs(This
->base_stub
);
556 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl
=
558 CStdStubBuffer_QueryInterface
,
559 CStdStubBuffer_AddRef
,
561 CStdStubBuffer_Delegating_Connect
,
562 CStdStubBuffer_Delegating_Disconnect
,
563 CStdStubBuffer_Invoke
,
564 CStdStubBuffer_IsIIDSupported
,
565 CStdStubBuffer_Delegating_CountRefs
,
566 CStdStubBuffer_DebugServerQueryInterface
,
567 CStdStubBuffer_DebugServerRelease
570 const MIDL_SERVER_INFO
*CStdStubBuffer_GetServerInfo(IRpcStubBuffer
*iface
)
572 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
573 return STUB_HEADER(This
).pServerInfo
;
576 /************************************************************************
577 * NdrStubForwardingFunction [RPCRT4.@]
579 void __RPC_STUB
NdrStubForwardingFunction( IRpcStubBuffer
*iface
, IRpcChannelBuffer
*pChannel
,
580 PRPC_MESSAGE pMsg
, DWORD
*pdwStubPhase
)
582 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
584 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
585 HRESULT r
= IRpcStubBuffer_Invoke(This
->base_stub
, (RPCOLEMESSAGE
*)pMsg
, pChannel
);
586 if(FAILED(r
)) RpcRaiseException(r
);
590 /***********************************************************************
591 * NdrStubInitialize [RPCRT4.@]
593 void WINAPI
NdrStubInitialize(PRPC_MESSAGE pRpcMsg
,
594 PMIDL_STUB_MESSAGE pStubMsg
,
595 PMIDL_STUB_DESC pStubDescriptor
,
596 LPRPCCHANNELBUFFER pRpcChannelBuffer
)
598 TRACE("(%p,%p,%p,%p)\n", pRpcMsg
, pStubMsg
, pStubDescriptor
, pRpcChannelBuffer
);
599 NdrServerInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
);
600 pStubMsg
->pRpcChannelBuffer
= pRpcChannelBuffer
;
601 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
602 &pStubMsg
->dwDestContext
,
603 &pStubMsg
->pvDestContext
);
606 /***********************************************************************
607 * NdrStubGetBuffer [RPCRT4.@]
609 void WINAPI
NdrStubGetBuffer(LPRPCSTUBBUFFER iface
,
610 LPRPCCHANNELBUFFER pRpcChannelBuffer
,
611 PMIDL_STUB_MESSAGE pStubMsg
)
613 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
616 TRACE("(%p, %p, %p)\n", This
, pRpcChannelBuffer
, pStubMsg
);
618 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
619 hr
= IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer
,
620 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
, STUB_HEADER(This
).piid
);
623 RpcRaiseException(hr
);
627 pStubMsg
->Buffer
= pStubMsg
->RpcMsg
->Buffer
;