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 (cstdstubbuffer_delegating_t
*)((char *)iface
- FIELD_OFFSET(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
;
198 block
= VirtualAlloc( NULL
, BLOCK_SIZE
* sizeof(*block
),
199 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
200 if (!block
) return NULL
;
202 for (i
= 0; i
< BLOCK_SIZE
; i
++)
204 memcpy( &block
[i
], opcodes
, sizeof(opcodes
) );
205 block
[i
].offset
= (BLOCK_SIZE
* num
+ i
+ 3) * sizeof(void *);
207 VirtualProtect( block
, BLOCK_SIZE
* sizeof(*block
), PAGE_EXECUTE_READ
, NULL
);
208 prev
= InterlockedCompareExchangePointer( (void **)&method_blocks
[num
], block
, NULL
);
209 if (prev
) /* someone beat us to it */
211 VirtualFree( block
, 0, MEM_RELEASE
);
217 static BOOL
fill_delegated_stub_table(IUnknownVtbl
*vtbl
, DWORD num
)
219 const void **entry
= (const void **)(vtbl
+ 1);
222 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
224 FIXME( "%u methods not supported\n", num
);
227 vtbl
->QueryInterface
= delegating_QueryInterface
;
228 vtbl
->AddRef
= delegating_AddRef
;
229 vtbl
->Release
= delegating_Release
;
230 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
232 const vtbl_method_t
*block
= method_blocks
[i
];
233 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
234 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++) *entry
++ = &block
[j
];
239 BOOL
fill_delegated_proxy_table(IUnknownVtbl
*vtbl
, DWORD num
)
241 const void **entry
= (const void **)(vtbl
+ 1);
244 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
246 FIXME( "%u methods not supported\n", num
);
249 vtbl
->QueryInterface
= IUnknown_QueryInterface_Proxy
;
250 vtbl
->AddRef
= IUnknown_AddRef_Proxy
;
251 vtbl
->Release
= IUnknown_Release_Proxy
;
252 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
254 const vtbl_method_t
*block
= method_blocks
[i
];
255 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
256 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
257 if (!*entry
) *entry
= &block
[j
];
262 static IUnknownVtbl
*get_delegating_vtbl(DWORD num_methods
)
266 if (num_methods
< 256) num_methods
= 256; /* avoid frequent reallocations */
268 EnterCriticalSection(&delegating_vtbl_section
);
270 if(!current_vtbl
|| num_methods
> current_vtbl
->size
)
272 ref_counted_vtbl
*table
= HeapAlloc(GetProcessHeap(), 0,
273 FIELD_OFFSET(ref_counted_vtbl
, vtbl
) + num_methods
* sizeof(void*));
276 LeaveCriticalSection(&delegating_vtbl_section
);
281 table
->size
= num_methods
;
282 fill_delegated_stub_table(&table
->vtbl
, num_methods
);
284 if (current_vtbl
&& current_vtbl
->ref
== 0)
286 TRACE("freeing old table\n");
287 HeapFree(GetProcessHeap(), 0, current_vtbl
);
289 current_vtbl
= table
;
293 ret
= ¤t_vtbl
->vtbl
;
294 LeaveCriticalSection(&delegating_vtbl_section
);
298 static void release_delegating_vtbl(IUnknownVtbl
*vtbl
)
300 ref_counted_vtbl
*table
= (ref_counted_vtbl
*)((DWORD
*)vtbl
- 1);
302 EnterCriticalSection(&delegating_vtbl_section
);
304 TRACE("ref now %d\n", table
->ref
);
305 if(table
->ref
== 0 && table
!= current_vtbl
)
307 TRACE("... and we're not current so free'ing\n");
308 HeapFree(GetProcessHeap(), 0, table
);
310 LeaveCriticalSection(&delegating_vtbl_section
);
313 HRESULT
CStdStubBuffer_Delegating_Construct(REFIID riid
,
314 LPUNKNOWN pUnkServer
,
315 PCInterfaceName name
,
316 CInterfaceStubVtbl
*vtbl
,
317 REFIID delegating_iid
,
318 LPPSFACTORYBUFFER pPSFactory
,
319 LPRPCSTUBBUFFER
*ppStub
)
321 cstdstubbuffer_delegating_t
*This
;
325 TRACE("(%p,%p,%p,%p) %s\n", pUnkServer
, vtbl
, pPSFactory
, ppStub
, name
);
326 TRACE("iid=%s delegating to %s\n", debugstr_guid(vtbl
->header
.piid
), debugstr_guid(delegating_iid
));
327 TRACE("vtbl=%p\n", &vtbl
->Vtbl
);
329 if (!IsEqualGUID(vtbl
->header
.piid
, riid
))
331 ERR("IID mismatch during stub creation\n");
332 return RPC_E_UNEXPECTED
;
335 r
= IUnknown_QueryInterface(pUnkServer
, riid
, (void**)&pvServer
);
336 if(FAILED(r
)) return r
;
338 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
341 IUnknown_Release(pvServer
);
342 return E_OUTOFMEMORY
;
345 This
->base_obj
= get_delegating_vtbl( vtbl
->header
.DispatchTableCount
);
346 r
= create_stub(delegating_iid
, (IUnknown
*)&This
->base_obj
, &This
->base_stub
);
349 release_delegating_vtbl(This
->base_obj
);
350 HeapFree(GetProcessHeap(), 0, This
);
351 IUnknown_Release(pvServer
);
355 This
->stub_buffer
.lpVtbl
= &vtbl
->Vtbl
;
356 This
->stub_buffer
.RefCount
= 1;
357 This
->stub_buffer
.pvServerObject
= pvServer
;
358 This
->stub_buffer
.pPSFactory
= pPSFactory
;
359 *ppStub
= (LPRPCSTUBBUFFER
)&This
->stub_buffer
;
361 IPSFactoryBuffer_AddRef(pPSFactory
);
365 HRESULT WINAPI
CStdStubBuffer_QueryInterface(LPRPCSTUBBUFFER iface
,
369 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
370 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
372 if (IsEqualIID(&IID_IUnknown
, riid
) ||
373 IsEqualIID(&IID_IRpcStubBuffer
, riid
))
375 IUnknown_AddRef(iface
);
380 return E_NOINTERFACE
;
383 ULONG WINAPI
CStdStubBuffer_AddRef(LPRPCSTUBBUFFER iface
)
385 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
386 TRACE("(%p)->AddRef()\n",This
);
387 return InterlockedIncrement(&This
->RefCount
);
390 ULONG WINAPI
NdrCStdStubBuffer_Release(LPRPCSTUBBUFFER iface
,
391 LPPSFACTORYBUFFER pPSF
)
393 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
396 TRACE("(%p)->Release()\n",This
);
398 refs
= InterlockedDecrement(&This
->RefCount
);
401 /* test_Release shows that native doesn't call Disconnect here.
402 We'll leave it in for the time being. */
403 IRpcStubBuffer_Disconnect(iface
);
405 IPSFactoryBuffer_Release(pPSF
);
406 HeapFree(GetProcessHeap(),0,This
);
411 ULONG WINAPI
NdrCStdStubBuffer2_Release(LPRPCSTUBBUFFER iface
,
412 LPPSFACTORYBUFFER pPSF
)
414 cstdstubbuffer_delegating_t
*This
= impl_from_delegating( iface
);
417 TRACE("(%p)->Release()\n", This
);
419 refs
= InterlockedDecrement(&This
->stub_buffer
.RefCount
);
422 /* Just like NdrCStdStubBuffer_Release, we shouldn't call
424 IRpcStubBuffer_Disconnect((IRpcStubBuffer
*)&This
->stub_buffer
);
426 IRpcStubBuffer_Release(This
->base_stub
);
427 release_delegating_vtbl(This
->base_obj
);
429 IPSFactoryBuffer_Release(pPSF
);
430 HeapFree(GetProcessHeap(), 0, This
);
436 HRESULT WINAPI
CStdStubBuffer_Connect(LPRPCSTUBBUFFER iface
,
437 LPUNKNOWN lpUnkServer
)
439 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
441 IUnknown
*new = NULL
;
443 TRACE("(%p)->Connect(%p)\n",This
,lpUnkServer
);
445 r
= IUnknown_QueryInterface(lpUnkServer
, STUB_HEADER(This
).piid
, (void**)&new);
446 new = InterlockedExchangePointer((void**)&This
->pvServerObject
, new);
448 IUnknown_Release(new);
452 void WINAPI
CStdStubBuffer_Disconnect(LPRPCSTUBBUFFER iface
)
454 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
456 TRACE("(%p)->Disconnect()\n",This
);
458 old
= InterlockedExchangePointer((void**)&This
->pvServerObject
, NULL
);
461 IUnknown_Release(old
);
464 HRESULT WINAPI
CStdStubBuffer_Invoke(LPRPCSTUBBUFFER iface
,
466 LPRPCCHANNELBUFFER pChannel
)
468 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
469 DWORD dwPhase
= STUB_UNMARSHAL
;
472 TRACE("(%p)->Invoke(%p,%p)\n",This
,pMsg
,pChannel
);
476 if (STUB_HEADER(This
).pDispatchTable
)
477 STUB_HEADER(This
).pDispatchTable
[pMsg
->iMethod
](iface
, pChannel
, (PRPC_MESSAGE
)pMsg
, &dwPhase
);
478 else /* pure interpreted */
479 NdrStubCall2(iface
, pChannel
, (PRPC_MESSAGE
)pMsg
, &dwPhase
);
481 __EXCEPT(stub_filter
)
483 DWORD dwExceptionCode
= GetExceptionCode();
484 WARN("a stub call failed with exception 0x%08x (%d)\n", dwExceptionCode
, dwExceptionCode
);
485 if (FAILED(dwExceptionCode
))
486 hr
= dwExceptionCode
;
488 hr
= HRESULT_FROM_WIN32(dwExceptionCode
);
495 LPRPCSTUBBUFFER WINAPI
CStdStubBuffer_IsIIDSupported(LPRPCSTUBBUFFER iface
,
498 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
499 TRACE("(%p)->IsIIDSupported(%s)\n",This
,debugstr_guid(riid
));
500 return IsEqualGUID(STUB_HEADER(This
).piid
, riid
) ? iface
: NULL
;
503 ULONG WINAPI
CStdStubBuffer_CountRefs(LPRPCSTUBBUFFER iface
)
505 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
506 TRACE("(%p)->CountRefs()\n",This
);
507 return This
->RefCount
;
510 HRESULT WINAPI
CStdStubBuffer_DebugServerQueryInterface(LPRPCSTUBBUFFER iface
,
513 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
514 TRACE("(%p)->DebugServerQueryInterface(%p)\n",This
,ppv
);
518 void WINAPI
CStdStubBuffer_DebugServerRelease(LPRPCSTUBBUFFER iface
,
521 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
522 TRACE("(%p)->DebugServerRelease(%p)\n",This
,pv
);
525 const IRpcStubBufferVtbl CStdStubBuffer_Vtbl
=
527 CStdStubBuffer_QueryInterface
,
528 CStdStubBuffer_AddRef
,
530 CStdStubBuffer_Connect
,
531 CStdStubBuffer_Disconnect
,
532 CStdStubBuffer_Invoke
,
533 CStdStubBuffer_IsIIDSupported
,
534 CStdStubBuffer_CountRefs
,
535 CStdStubBuffer_DebugServerQueryInterface
,
536 CStdStubBuffer_DebugServerRelease
539 static HRESULT WINAPI
CStdStubBuffer_Delegating_Connect(LPRPCSTUBBUFFER iface
,
540 LPUNKNOWN lpUnkServer
)
542 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
544 TRACE("(%p)->Connect(%p)\n", This
, lpUnkServer
);
546 r
= CStdStubBuffer_Connect(iface
, lpUnkServer
);
548 r
= IRpcStubBuffer_Connect(This
->base_stub
, (IUnknown
*)&This
->base_obj
);
553 static void WINAPI
CStdStubBuffer_Delegating_Disconnect(LPRPCSTUBBUFFER iface
)
555 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
556 TRACE("(%p)->Disconnect()\n", This
);
558 IRpcStubBuffer_Disconnect(This
->base_stub
);
559 CStdStubBuffer_Disconnect(iface
);
562 static ULONG WINAPI
CStdStubBuffer_Delegating_CountRefs(LPRPCSTUBBUFFER iface
)
564 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
566 TRACE("(%p)->CountRefs()\n", This
);
568 ret
= CStdStubBuffer_CountRefs(iface
);
569 ret
+= IRpcStubBuffer_CountRefs(This
->base_stub
);
574 const IRpcStubBufferVtbl CStdStubBuffer_Delegating_Vtbl
=
576 CStdStubBuffer_QueryInterface
,
577 CStdStubBuffer_AddRef
,
579 CStdStubBuffer_Delegating_Connect
,
580 CStdStubBuffer_Delegating_Disconnect
,
581 CStdStubBuffer_Invoke
,
582 CStdStubBuffer_IsIIDSupported
,
583 CStdStubBuffer_Delegating_CountRefs
,
584 CStdStubBuffer_DebugServerQueryInterface
,
585 CStdStubBuffer_DebugServerRelease
588 const MIDL_SERVER_INFO
*CStdStubBuffer_GetServerInfo(IRpcStubBuffer
*iface
)
590 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
591 return STUB_HEADER(This
).pServerInfo
;
594 /************************************************************************
595 * NdrStubForwardingFunction [RPCRT4.@]
597 void __RPC_STUB
NdrStubForwardingFunction( IRpcStubBuffer
*iface
, IRpcChannelBuffer
*pChannel
,
598 PRPC_MESSAGE pMsg
, DWORD
*pdwStubPhase
)
600 /* Note pMsg is passed intact since RPCOLEMESSAGE is basically a RPC_MESSAGE. */
602 cstdstubbuffer_delegating_t
*This
= impl_from_delegating(iface
);
603 HRESULT r
= IRpcStubBuffer_Invoke(This
->base_stub
, (RPCOLEMESSAGE
*)pMsg
, pChannel
);
604 if(FAILED(r
)) RpcRaiseException(r
);
608 /***********************************************************************
609 * NdrStubInitialize [RPCRT4.@]
611 void WINAPI
NdrStubInitialize(PRPC_MESSAGE pRpcMsg
,
612 PMIDL_STUB_MESSAGE pStubMsg
,
613 PMIDL_STUB_DESC pStubDescriptor
,
614 LPRPCCHANNELBUFFER pRpcChannelBuffer
)
616 TRACE("(%p,%p,%p,%p)\n", pRpcMsg
, pStubMsg
, pStubDescriptor
, pRpcChannelBuffer
);
617 NdrServerInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
);
618 pStubMsg
->pRpcChannelBuffer
= pRpcChannelBuffer
;
619 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
620 &pStubMsg
->dwDestContext
,
621 &pStubMsg
->pvDestContext
);
624 /***********************************************************************
625 * NdrStubGetBuffer [RPCRT4.@]
627 void WINAPI
NdrStubGetBuffer(LPRPCSTUBBUFFER iface
,
628 LPRPCCHANNELBUFFER pRpcChannelBuffer
,
629 PMIDL_STUB_MESSAGE pStubMsg
)
631 CStdStubBuffer
*This
= (CStdStubBuffer
*)iface
;
634 TRACE("(%p, %p, %p)\n", This
, pRpcChannelBuffer
, pStubMsg
);
636 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
637 hr
= IRpcChannelBuffer_GetBuffer(pRpcChannelBuffer
,
638 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
, STUB_HEADER(This
).piid
);
641 RpcRaiseException(hr
);
645 pStubMsg
->Buffer
= pStubMsg
->RpcMsg
->Buffer
;