2 * COM proxy 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
21 * TODO: Handle non-i386 architectures
26 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
28 /* I don't know what MS's std proxy structure looks like,
29 so this probably doesn't match, but that shouldn't matter */
31 IRpcProxyBuffer IRpcProxyBuffer_iface
;
36 IUnknown
*base_object
; /* must be at offset 0x10 from PVtbl */
37 IRpcProxyBuffer
*base_proxy
;
39 LPPSFACTORYBUFFER pPSFactory
;
40 LPRPCCHANNELBUFFER pChannel
;
43 static const IRpcProxyBufferVtbl StdProxy_Vtbl
;
45 static inline StdProxyImpl
*impl_from_IRpcProxyBuffer(IRpcProxyBuffer
*iface
)
47 return CONTAINING_RECORD(iface
, StdProxyImpl
, IRpcProxyBuffer_iface
);
50 static inline StdProxyImpl
*impl_from_proxy_obj( void *iface
)
52 return CONTAINING_RECORD(iface
, StdProxyImpl
, PVtbl
);
57 extern void call_stubless_func(void);
58 __ASM_GLOBAL_FUNC(call_stubless_func
,
59 "movl 4(%esp),%ecx\n\t" /* This pointer */
60 "movl (%ecx),%ecx\n\t" /* This->lpVtbl */
61 "movl -8(%ecx),%ecx\n\t" /* MIDL_STUBLESS_PROXY_INFO */
62 "movl 8(%ecx),%edx\n\t" /* info->FormatStringOffset */
63 "movzwl (%edx,%eax,2),%edx\n\t" /* FormatStringOffset[index] */
64 "addl 4(%ecx),%edx\n\t" /* info->ProcFormatString + offset */
65 "movzwl 8(%edx),%eax\n\t" /* arguments size */
67 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
68 "leal 8(%esp),%eax\n\t" /* &This */
70 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
71 "pushl %edx\n\t" /* format string */
72 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
73 "pushl (%ecx)\n\t" /* info->pStubDesc */
74 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
75 "call " __ASM_NAME("ndr_client_call") "\n\t"
76 "leal 12(%esp),%esp\n\t"
77 __ASM_CFI(".cfi_adjust_cfa_offset -12\n\t")
78 "popl %edx\n\t" /* arguments size */
79 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
80 "movl (%esp),%ecx\n\t" /* return address */
94 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
96 thunk
->mov_eax
= 0xb8; /* movl $n,%eax */
98 thunk
->jmp
= 0xe9; /* jmp */
99 thunk
->handler
= (char *)call_stubless_func
- (char *)(&thunk
->handler
+ 1);
102 #elif defined(__x86_64__)
104 extern void call_stubless_func(void);
105 __ASM_GLOBAL_FUNC(call_stubless_func
,
106 "movq %rcx,0x8(%rsp)\n\t"
107 "movq %rdx,0x10(%rsp)\n\t"
108 "movq %r8,0x18(%rsp)\n\t"
109 "movq %r9,0x20(%rsp)\n\t"
110 "leaq 0x8(%rsp),%r8\n\t" /* &This */
111 "movq (%rcx),%rcx\n\t" /* This->lpVtbl */
112 "movq -0x10(%rcx),%rcx\n\t" /* MIDL_STUBLESS_PROXY_INFO */
113 "movq 0x10(%rcx),%rdx\n\t" /* info->FormatStringOffset */
114 "movzwq (%rdx,%r10,2),%rdx\n\t" /* FormatStringOffset[index] */
115 "addq 8(%rcx),%rdx\n\t" /* info->ProcFormatString + offset */
116 "movq (%rcx),%rcx\n\t" /* info->pStubDesc */
117 "subq $0x38,%rsp\n\t"
118 __ASM_CFI(".cfi_adjust_cfa_offset 0x38\n\t")
119 "movq %xmm1,0x20(%rsp)\n\t"
120 "movq %xmm2,0x28(%rsp)\n\t"
121 "movq %xmm3,0x30(%rsp)\n\t"
122 "leaq 0x18(%rsp),%r9\n\t" /* fpu_args */
123 "call " __ASM_NAME("ndr_client_call") "\n\t"
124 "addq $0x38,%rsp\n\t"
125 __ASM_CFI(".cfi_adjust_cfa_offset -0x38\n\t")
128 #include "pshpack1.h"
139 static const struct thunk thunk_template
=
141 { 0x49, 0xc7, 0xc2 }, 0, /* movq $index,%r10 */
142 { 0x48, 0xb8 }, 0, /* movq $call_stubless_func,%rax */
143 { 0xff, 0xe0 } /* jmp *%rax */
146 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
148 *thunk
= thunk_template
;
149 thunk
->index
= index
;
150 thunk
->call_stubless
= call_stubless_func
;
155 #warning You must implement stubless proxies for your CPU
162 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
164 thunk
->index
= index
;
167 #endif /* __i386__ */
169 #define BLOCK_SIZE 1024
170 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
172 static const struct thunk
*method_blocks
[MAX_BLOCKS
];
174 static const struct thunk
*allocate_block( unsigned int num
)
177 struct thunk
*prev
, *block
;
179 block
= VirtualAlloc( NULL
, BLOCK_SIZE
* sizeof(*block
),
180 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
181 if (!block
) return NULL
;
183 for (i
= 0; i
< BLOCK_SIZE
; i
++) init_thunk( &block
[i
], BLOCK_SIZE
* num
+ i
+ 3 );
184 VirtualProtect( block
, BLOCK_SIZE
* sizeof(*block
), PAGE_EXECUTE_READ
, NULL
);
185 prev
= InterlockedCompareExchangePointer( (void **)&method_blocks
[num
], block
, NULL
);
186 if (prev
) /* someone beat us to it */
188 VirtualFree( block
, 0, MEM_RELEASE
);
194 static BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
196 const void **entry
= (const void **)(vtbl
+ 1);
199 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
201 FIXME( "%u methods not supported\n", num
);
204 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
206 const struct thunk
*block
= method_blocks
[i
];
207 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
208 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
209 if (*entry
== (LPVOID
)-1) *entry
= &block
[j
];
214 HRESULT
StdProxy_Construct(REFIID riid
,
216 const ProxyFileInfo
*ProxyInfo
,
218 LPPSFACTORYBUFFER pPSFactory
,
219 LPRPCPROXYBUFFER
*ppProxy
,
223 PCInterfaceName name
= ProxyInfo
->pNamesArray
[Index
];
224 CInterfaceProxyVtbl
*vtbl
= ProxyInfo
->pProxyVtblList
[Index
];
226 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
228 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
229 if (ProxyInfo
->TableVersion
> 1) {
230 ULONG count
= ProxyInfo
->pStubVtblList
[Index
]->header
.DispatchTableCount
;
231 vtbl
= (CInterfaceProxyVtbl
*)((const void **)vtbl
+ 1);
232 TRACE("stubless vtbl %p: count=%d\n", vtbl
->Vtbl
, count
);
233 fill_stubless_table( (IUnknownVtbl
*)vtbl
->Vtbl
, count
);
236 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
237 ERR("IID mismatch during proxy creation\n");
238 return RPC_E_UNEXPECTED
;
241 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(StdProxyImpl
));
242 if (!This
) return E_OUTOFMEMORY
;
244 if (!pUnkOuter
) pUnkOuter
= (IUnknown
*)This
;
245 This
->IRpcProxyBuffer_iface
.lpVtbl
= &StdProxy_Vtbl
;
246 This
->PVtbl
= vtbl
->Vtbl
;
247 /* one reference for the proxy */
249 This
->piid
= vtbl
->header
.piid
;
250 This
->base_object
= NULL
;
251 This
->base_proxy
= NULL
;
252 This
->pUnkOuter
= pUnkOuter
;
254 This
->pPSFactory
= pPSFactory
;
255 This
->pChannel
= NULL
;
257 if(ProxyInfo
->pDelegatedIIDs
&& ProxyInfo
->pDelegatedIIDs
[Index
])
259 HRESULT r
= create_proxy( ProxyInfo
->pDelegatedIIDs
[Index
], NULL
,
260 &This
->base_proxy
, (void **)&This
->base_object
);
263 HeapFree( GetProcessHeap(), 0, This
);
268 *ppProxy
= &This
->IRpcProxyBuffer_iface
;
269 *ppvObj
= &This
->PVtbl
;
270 IUnknown_AddRef((IUnknown
*)*ppvObj
);
271 IPSFactoryBuffer_AddRef(pPSFactory
);
273 TRACE( "iid=%s this %p proxy %p obj %p vtbl %p base proxy %p base obj %p\n",
274 debugstr_guid(riid
), This
, *ppProxy
, *ppvObj
, This
->PVtbl
, This
->base_proxy
, This
->base_object
);
278 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
282 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
283 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
285 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
286 IsEqualGUID(This
->piid
,riid
)) {
288 InterlockedIncrement(&This
->RefCount
);
292 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
293 *obj
= &This
->IRpcProxyBuffer_iface
;
294 InterlockedIncrement(&This
->RefCount
);
298 return E_NOINTERFACE
;
301 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
303 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
304 TRACE("(%p)->AddRef()\n",This
);
306 return InterlockedIncrement(&This
->RefCount
);
309 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
312 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
313 TRACE("(%p)->Release()\n",This
);
315 refs
= InterlockedDecrement(&This
->RefCount
);
319 IRpcProxyBuffer_Disconnect(&This
->IRpcProxyBuffer_iface
);
321 if (This
->base_object
) IUnknown_Release( This
->base_object
);
322 if (This
->base_proxy
) IRpcProxyBuffer_Release( This
->base_proxy
);
324 IPSFactoryBuffer_Release(This
->pPSFactory
);
325 HeapFree(GetProcessHeap(),0,This
);
331 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
332 LPRPCCHANNELBUFFER pChannel
)
334 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
335 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
337 This
->pChannel
= pChannel
;
338 IRpcChannelBuffer_AddRef(pChannel
);
339 if (This
->base_proxy
) IRpcProxyBuffer_Connect( This
->base_proxy
, pChannel
);
343 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
345 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
346 TRACE("(%p)->Disconnect()\n",This
);
348 if (This
->base_proxy
) IRpcProxyBuffer_Disconnect( This
->base_proxy
);
350 IRpcChannelBuffer_Release(This
->pChannel
);
351 This
->pChannel
= NULL
;
354 static const IRpcProxyBufferVtbl StdProxy_Vtbl
=
356 StdProxy_QueryInterface
,
363 static void StdProxy_GetChannel(LPVOID iface
,
364 LPRPCCHANNELBUFFER
*ppChannel
)
366 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
367 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
369 *ppChannel
= This
->pChannel
;
372 static void StdProxy_GetIID(LPVOID iface
,
375 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
376 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
381 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
385 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
386 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This
,debugstr_guid(riid
),ppvObj
,This
->name
);
387 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
390 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
392 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
393 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
394 return IUnknown_AddRef(This
->pUnkOuter
);
397 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
399 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
400 TRACE("(%p)->Release() %s\n",This
,This
->name
);
401 return IUnknown_Release(This
->pUnkOuter
);
404 /***********************************************************************
405 * NdrProxyInitialize [RPCRT4.@]
407 void WINAPI
NdrProxyInitialize(void *This
,
408 PRPC_MESSAGE pRpcMsg
,
409 PMIDL_STUB_MESSAGE pStubMsg
,
410 PMIDL_STUB_DESC pStubDescriptor
,
411 unsigned int ProcNum
)
413 TRACE("(%p,%p,%p,%p,%d)\n", This
, pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
414 NdrClientInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
415 StdProxy_GetChannel(This
, &pStubMsg
->pRpcChannelBuffer
);
416 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
417 &pStubMsg
->dwDestContext
,
418 &pStubMsg
->pvDestContext
);
419 TRACE("channel=%p\n", pStubMsg
->pRpcChannelBuffer
);
422 /***********************************************************************
423 * NdrProxyGetBuffer [RPCRT4.@]
425 void WINAPI
NdrProxyGetBuffer(void *This
,
426 PMIDL_STUB_MESSAGE pStubMsg
)
429 const IID
*riid
= NULL
;
431 TRACE("(%p,%p)\n", This
, pStubMsg
);
432 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
433 pStubMsg
->dwStubPhase
= PROXY_GETBUFFER
;
434 StdProxy_GetIID(This
, &riid
);
435 hr
= IRpcChannelBuffer_GetBuffer(pStubMsg
->pRpcChannelBuffer
,
436 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
440 RpcRaiseException(hr
);
443 pStubMsg
->fBufferValid
= TRUE
;
444 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
445 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
446 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
447 pStubMsg
->dwStubPhase
= PROXY_MARSHAL
;
450 /***********************************************************************
451 * NdrProxySendReceive [RPCRT4.@]
453 void WINAPI
NdrProxySendReceive(void *This
,
454 PMIDL_STUB_MESSAGE pStubMsg
)
459 TRACE("(%p,%p)\n", This
, pStubMsg
);
461 if (!pStubMsg
->pRpcChannelBuffer
)
463 WARN("Trying to use disconnected proxy %p\n", This
);
464 RpcRaiseException(RPC_E_DISCONNECTED
);
467 pStubMsg
->dwStubPhase
= PROXY_SENDRECEIVE
;
468 /* avoid sending uninitialised parts of the buffer on the wire */
469 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->Buffer
- (unsigned char *)pStubMsg
->RpcMsg
->Buffer
;
470 hr
= IRpcChannelBuffer_SendReceive(pStubMsg
->pRpcChannelBuffer
,
471 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
473 pStubMsg
->dwStubPhase
= PROXY_UNMARSHAL
;
474 pStubMsg
->BufferLength
= pStubMsg
->RpcMsg
->BufferLength
;
475 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
476 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
477 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
479 /* raise exception if call failed */
480 if (hr
== RPC_S_CALL_FAILED
) RpcRaiseException(*(DWORD
*)pStubMsg
->Buffer
);
481 else if (FAILED(hr
)) RpcRaiseException(hr
);
484 /***********************************************************************
485 * NdrProxyFreeBuffer [RPCRT4.@]
487 void WINAPI
NdrProxyFreeBuffer(void *This
,
488 PMIDL_STUB_MESSAGE pStubMsg
)
490 TRACE("(%p,%p)\n", This
, pStubMsg
);
492 if (pStubMsg
->fBufferValid
)
494 IRpcChannelBuffer_FreeBuffer(pStubMsg
->pRpcChannelBuffer
,
495 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
);
496 pStubMsg
->fBufferValid
= TRUE
;
500 /***********************************************************************
501 * NdrProxyErrorHandler [RPCRT4.@]
503 HRESULT WINAPI
NdrProxyErrorHandler(DWORD dwExceptionCode
)
505 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode
);
507 if (FAILED(dwExceptionCode
))
508 return dwExceptionCode
;
510 return HRESULT_FROM_WIN32(dwExceptionCode
);
514 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo
, LPUNKNOWN pUnkOuter
, REFIID riid
,
515 LPRPCPROXYBUFFER
*ppProxy
, LPVOID
*ppv
)
517 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
518 HMODULE hUser32
= LoadLibraryA("user32");
519 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
521 FIXME("%p %p %s %p %p\n", pTypeInfo
, pUnkOuter
, debugstr_guid(riid
), ppProxy
, ppv
);
525 "The native implementation of OLEAUT32.DLL cannot be used "
526 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
527 "Wine: Unimplemented CreateProxyFromTypeInfo",
535 CreateStubFromTypeInfo(ITypeInfo
*pTypeInfo
, REFIID riid
, IUnknown
*pUnkServer
,
536 IRpcStubBuffer
**ppStub
)
538 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
539 HMODULE hUser32
= LoadLibraryA("user32");
540 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
542 FIXME("%p %s %p %p\n", pTypeInfo
, debugstr_guid(riid
), pUnkServer
, ppStub
);
546 "The native implementation of OLEAUT32.DLL cannot be used "
547 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
548 "Wine: Unimplemented CreateProxyFromTypeInfo",