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
24 #define WIN32_NO_STATUS
28 //#include "wine/port.h"
36 //#include "winerror.h"
42 //#include "ndr_misc.h"
43 //#include "ndr_stubless.h"
44 #include <wine/debug.h>
46 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
48 /* I don't know what MS's std proxy structure looks like,
49 so this probably doesn't match, but that shouldn't matter */
51 IRpcProxyBuffer IRpcProxyBuffer_iface
;
56 IUnknown
*base_object
; /* must be at offset 0x10 from PVtbl */
57 IRpcProxyBuffer
*base_proxy
;
59 LPPSFACTORYBUFFER pPSFactory
;
60 LPRPCCHANNELBUFFER pChannel
;
63 static const IRpcProxyBufferVtbl StdProxy_Vtbl
;
65 static inline StdProxyImpl
*impl_from_IRpcProxyBuffer(IRpcProxyBuffer
*iface
)
67 return CONTAINING_RECORD(iface
, StdProxyImpl
, IRpcProxyBuffer_iface
);
70 static inline StdProxyImpl
*impl_from_proxy_obj( void *iface
)
72 return CONTAINING_RECORD(iface
, StdProxyImpl
, PVtbl
);
77 extern void call_stubless_func(void);
78 __ASM_GLOBAL_FUNC(call_stubless_func
,
79 "movl 4(%esp),%ecx\n\t" /* This pointer */
80 "movl (%ecx),%ecx\n\t" /* This->lpVtbl */
81 "movl -8(%ecx),%ecx\n\t" /* MIDL_STUBLESS_PROXY_INFO */
82 "movl 8(%ecx),%edx\n\t" /* info->FormatStringOffset */
83 "movzwl (%edx,%eax,2),%edx\n\t" /* FormatStringOffset[index] */
84 "addl 4(%ecx),%edx\n\t" /* info->ProcFormatString + offset */
85 "movzwl 8(%edx),%eax\n\t" /* arguments size */
87 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
88 "leal 8(%esp),%eax\n\t" /* &This */
90 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
91 "pushl %edx\n\t" /* format string */
92 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
93 "pushl (%ecx)\n\t" /* info->pStubDesc */
94 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
95 "call " __ASM_NAME("ndr_client_call") "\n\t"
96 "leal 12(%esp),%esp\n\t"
97 __ASM_CFI(".cfi_adjust_cfa_offset -12\n\t")
98 "popl %edx\n\t" /* arguments size */
99 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
100 "movl (%esp),%ecx\n\t" /* return address */
104 #include "pshpack1.h"
114 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
116 thunk
->mov_eax
= 0xb8; /* movl $n,%eax */
117 thunk
->index
= index
;
118 thunk
->jmp
= 0xe9; /* jmp */
119 thunk
->handler
= (char *)call_stubless_func
- (char *)(&thunk
->handler
+ 1);
122 #elif defined(__x86_64__)
124 extern void call_stubless_func(void);
125 __ASM_GLOBAL_FUNC(call_stubless_func
,
126 "movq %rcx,0x8(%rsp)\n\t"
127 "movq %rdx,0x10(%rsp)\n\t"
128 "movq %r8,0x18(%rsp)\n\t"
129 "movq %r9,0x20(%rsp)\n\t"
130 "leaq 0x8(%rsp),%r8\n\t" /* &This */
131 "movq (%rcx),%rcx\n\t" /* This->lpVtbl */
132 "movq -0x10(%rcx),%rcx\n\t" /* MIDL_STUBLESS_PROXY_INFO */
133 "movq 0x10(%rcx),%rdx\n\t" /* info->FormatStringOffset */
134 "movzwq (%rdx,%r10,2),%rdx\n\t" /* FormatStringOffset[index] */
135 "addq 8(%rcx),%rdx\n\t" /* info->ProcFormatString + offset */
136 "movq (%rcx),%rcx\n\t" /* info->pStubDesc */
137 "subq $0x38,%rsp\n\t"
138 __ASM_CFI(".cfi_adjust_cfa_offset 0x38\n\t")
139 "movq %xmm1,0x20(%rsp)\n\t"
140 "movq %xmm2,0x28(%rsp)\n\t"
141 "movq %xmm3,0x30(%rsp)\n\t"
142 "leaq 0x18(%rsp),%r9\n\t" /* fpu_args */
143 "call " __ASM_NAME("ndr_client_call") "\n\t"
144 "addq $0x38,%rsp\n\t"
145 __ASM_CFI(".cfi_adjust_cfa_offset -0x38\n\t")
148 #include "pshpack1.h"
159 static const struct thunk thunk_template
=
161 { 0x49, 0xc7, 0xc2 }, 0, /* movq $index,%r10 */
162 { 0x48, 0xb8 }, 0, /* movq $call_stubless_func,%rax */
163 { 0xff, 0xe0 } /* jmp *%rax */
166 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
168 *thunk
= thunk_template
;
169 thunk
->index
= index
;
170 thunk
->call_stubless
= call_stubless_func
;
175 #warning You must implement stubless proxies for your CPU
182 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
184 thunk
->index
= index
;
187 #endif /* __i386__ */
189 #define BLOCK_SIZE 1024
190 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
192 static const struct thunk
*method_blocks
[MAX_BLOCKS
];
194 static const struct thunk
*allocate_block( unsigned int num
)
197 struct thunk
*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
++) init_thunk( &block
[i
], BLOCK_SIZE
* num
+ i
+ 3 );
204 VirtualProtect( block
, BLOCK_SIZE
* sizeof(*block
), PAGE_EXECUTE_READ
, NULL
);
205 prev
= InterlockedCompareExchangePointer( (void **)&method_blocks
[num
], block
, NULL
);
206 if (prev
) /* someone beat us to it */
208 VirtualFree( block
, 0, MEM_RELEASE
);
214 static BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
216 const void **entry
= (const void **)(vtbl
+ 1);
219 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
221 FIXME( "%u methods not supported\n", num
);
224 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
226 const struct thunk
*block
= method_blocks
[i
];
227 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
228 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
229 if (*entry
== (LPVOID
)-1) *entry
= &block
[j
];
234 HRESULT
StdProxy_Construct(REFIID riid
,
236 const ProxyFileInfo
*ProxyInfo
,
238 LPPSFACTORYBUFFER pPSFactory
,
239 LPRPCPROXYBUFFER
*ppProxy
,
243 PCInterfaceName name
= ProxyInfo
->pNamesArray
[Index
];
244 CInterfaceProxyVtbl
*vtbl
= ProxyInfo
->pProxyVtblList
[Index
];
246 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
248 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
249 if (ProxyInfo
->TableVersion
> 1) {
250 ULONG count
= ProxyInfo
->pStubVtblList
[Index
]->header
.DispatchTableCount
;
251 vtbl
= (CInterfaceProxyVtbl
*)((const void **)vtbl
+ 1);
252 TRACE("stubless vtbl %p: count=%d\n", vtbl
->Vtbl
, count
);
253 fill_stubless_table( (IUnknownVtbl
*)vtbl
->Vtbl
, count
);
256 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
257 ERR("IID mismatch during proxy creation\n");
258 return RPC_E_UNEXPECTED
;
261 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(StdProxyImpl
));
262 if (!This
) return E_OUTOFMEMORY
;
264 if (!pUnkOuter
) pUnkOuter
= (IUnknown
*)This
;
265 This
->IRpcProxyBuffer_iface
.lpVtbl
= &StdProxy_Vtbl
;
266 This
->PVtbl
= vtbl
->Vtbl
;
267 /* one reference for the proxy */
269 This
->piid
= vtbl
->header
.piid
;
270 This
->base_object
= NULL
;
271 This
->base_proxy
= NULL
;
272 This
->pUnkOuter
= pUnkOuter
;
274 This
->pPSFactory
= pPSFactory
;
275 This
->pChannel
= NULL
;
277 if(ProxyInfo
->pDelegatedIIDs
&& ProxyInfo
->pDelegatedIIDs
[Index
])
279 HRESULT r
= create_proxy( ProxyInfo
->pDelegatedIIDs
[Index
], NULL
,
280 &This
->base_proxy
, (void **)&This
->base_object
);
283 HeapFree( GetProcessHeap(), 0, This
);
288 *ppProxy
= &This
->IRpcProxyBuffer_iface
;
289 *ppvObj
= &This
->PVtbl
;
290 IUnknown_AddRef((IUnknown
*)*ppvObj
);
291 IPSFactoryBuffer_AddRef(pPSFactory
);
293 TRACE( "iid=%s this %p proxy %p obj %p vtbl %p base proxy %p base obj %p\n",
294 debugstr_guid(riid
), This
, *ppProxy
, *ppvObj
, This
->PVtbl
, This
->base_proxy
, This
->base_object
);
298 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
302 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
303 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
305 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
306 IsEqualGUID(This
->piid
,riid
)) {
308 InterlockedIncrement(&This
->RefCount
);
312 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
313 *obj
= &This
->IRpcProxyBuffer_iface
;
314 InterlockedIncrement(&This
->RefCount
);
318 return E_NOINTERFACE
;
321 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
323 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
324 TRACE("(%p)->AddRef()\n",This
);
326 return InterlockedIncrement(&This
->RefCount
);
329 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
332 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
333 TRACE("(%p)->Release()\n",This
);
335 refs
= InterlockedDecrement(&This
->RefCount
);
339 IRpcProxyBuffer_Disconnect(&This
->IRpcProxyBuffer_iface
);
341 if (This
->base_object
) IUnknown_Release( This
->base_object
);
342 if (This
->base_proxy
) IRpcProxyBuffer_Release( This
->base_proxy
);
344 IPSFactoryBuffer_Release(This
->pPSFactory
);
345 HeapFree(GetProcessHeap(),0,This
);
351 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
352 LPRPCCHANNELBUFFER pChannel
)
354 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
355 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
357 This
->pChannel
= pChannel
;
358 IRpcChannelBuffer_AddRef(pChannel
);
359 if (This
->base_proxy
) IRpcProxyBuffer_Connect( This
->base_proxy
, pChannel
);
363 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
365 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
366 TRACE("(%p)->Disconnect()\n",This
);
368 if (This
->base_proxy
) IRpcProxyBuffer_Disconnect( This
->base_proxy
);
370 IRpcChannelBuffer_Release(This
->pChannel
);
371 This
->pChannel
= NULL
;
374 static const IRpcProxyBufferVtbl StdProxy_Vtbl
=
376 StdProxy_QueryInterface
,
383 static void StdProxy_GetChannel(LPVOID iface
,
384 LPRPCCHANNELBUFFER
*ppChannel
)
386 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
387 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
389 *ppChannel
= This
->pChannel
;
392 static void StdProxy_GetIID(LPVOID iface
,
395 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
396 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
401 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
405 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
406 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This
,debugstr_guid(riid
),ppvObj
,This
->name
);
407 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
410 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
412 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
413 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
414 return IUnknown_AddRef(This
->pUnkOuter
);
417 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
419 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
420 TRACE("(%p)->Release() %s\n",This
,This
->name
);
421 return IUnknown_Release(This
->pUnkOuter
);
424 /***********************************************************************
425 * NdrProxyInitialize [RPCRT4.@]
427 void WINAPI
NdrProxyInitialize(void *This
,
428 PRPC_MESSAGE pRpcMsg
,
429 PMIDL_STUB_MESSAGE pStubMsg
,
430 PMIDL_STUB_DESC pStubDescriptor
,
431 unsigned int ProcNum
)
433 TRACE("(%p,%p,%p,%p,%d)\n", This
, pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
434 NdrClientInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
435 StdProxy_GetChannel(This
, &pStubMsg
->pRpcChannelBuffer
);
436 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
437 &pStubMsg
->dwDestContext
,
438 &pStubMsg
->pvDestContext
);
439 TRACE("channel=%p\n", pStubMsg
->pRpcChannelBuffer
);
442 /***********************************************************************
443 * NdrProxyGetBuffer [RPCRT4.@]
445 void WINAPI
NdrProxyGetBuffer(void *This
,
446 PMIDL_STUB_MESSAGE pStubMsg
)
449 const IID
*riid
= NULL
;
451 TRACE("(%p,%p)\n", This
, pStubMsg
);
452 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
453 pStubMsg
->dwStubPhase
= PROXY_GETBUFFER
;
454 StdProxy_GetIID(This
, &riid
);
455 hr
= IRpcChannelBuffer_GetBuffer(pStubMsg
->pRpcChannelBuffer
,
456 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
460 RpcRaiseException(hr
);
463 pStubMsg
->fBufferValid
= TRUE
;
464 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
465 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
466 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
467 pStubMsg
->dwStubPhase
= PROXY_MARSHAL
;
470 /***********************************************************************
471 * NdrProxySendReceive [RPCRT4.@]
473 void WINAPI
NdrProxySendReceive(void *This
,
474 PMIDL_STUB_MESSAGE pStubMsg
)
479 TRACE("(%p,%p)\n", This
, pStubMsg
);
481 if (!pStubMsg
->pRpcChannelBuffer
)
483 WARN("Trying to use disconnected proxy %p\n", This
);
484 RpcRaiseException(RPC_E_DISCONNECTED
);
487 pStubMsg
->dwStubPhase
= PROXY_SENDRECEIVE
;
488 /* avoid sending uninitialised parts of the buffer on the wire */
489 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->Buffer
- (unsigned char *)pStubMsg
->RpcMsg
->Buffer
;
490 hr
= IRpcChannelBuffer_SendReceive(pStubMsg
->pRpcChannelBuffer
,
491 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
493 pStubMsg
->dwStubPhase
= PROXY_UNMARSHAL
;
494 pStubMsg
->BufferLength
= pStubMsg
->RpcMsg
->BufferLength
;
495 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
496 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
497 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
499 /* raise exception if call failed */
500 if (hr
== RPC_S_CALL_FAILED
) RpcRaiseException(*(DWORD
*)pStubMsg
->Buffer
);
501 else if (FAILED(hr
)) RpcRaiseException(hr
);
504 /***********************************************************************
505 * NdrProxyFreeBuffer [RPCRT4.@]
507 void WINAPI
NdrProxyFreeBuffer(void *This
,
508 PMIDL_STUB_MESSAGE pStubMsg
)
510 TRACE("(%p,%p)\n", This
, pStubMsg
);
512 if (pStubMsg
->fBufferValid
)
514 IRpcChannelBuffer_FreeBuffer(pStubMsg
->pRpcChannelBuffer
,
515 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
);
516 pStubMsg
->fBufferValid
= TRUE
;
520 /***********************************************************************
521 * NdrProxyErrorHandler [RPCRT4.@]
523 HRESULT WINAPI
NdrProxyErrorHandler(DWORD dwExceptionCode
)
525 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode
);
527 if (FAILED(dwExceptionCode
))
528 return dwExceptionCode
;
530 return HRESULT_FROM_WIN32(dwExceptionCode
);
534 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo
, LPUNKNOWN pUnkOuter
, REFIID riid
,
535 LPRPCPROXYBUFFER
*ppProxy
, LPVOID
*ppv
)
537 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
538 HMODULE hUser32
= LoadLibraryA("user32");
539 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
541 FIXME("%p %p %s %p %p\n", pTypeInfo
, pUnkOuter
, debugstr_guid(riid
), ppProxy
, ppv
);
545 "The native implementation of OLEAUT32.DLL cannot be used "
546 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
547 "Wine: Unimplemented CreateProxyFromTypeInfo",
555 CreateStubFromTypeInfo(ITypeInfo
*pTypeInfo
, REFIID riid
, IUnknown
*pUnkServer
,
556 IRpcStubBuffer
**ppStub
)
558 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
559 HMODULE hUser32
= LoadLibraryA("user32");
560 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
562 FIXME("%p %s %p %p\n", pTypeInfo
, debugstr_guid(riid
), pUnkServer
, ppStub
);
566 "The native implementation of OLEAUT32.DLL cannot be used "
567 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
568 "Wine: Unimplemented CreateProxyFromTypeInfo",