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
35 #include "ndr_stubless.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
41 static const IRpcProxyBufferVtbl StdProxy_Vtbl
;
43 static inline StdProxyImpl
*impl_from_IRpcProxyBuffer(IRpcProxyBuffer
*iface
)
45 return CONTAINING_RECORD(iface
, StdProxyImpl
, IRpcProxyBuffer_iface
);
48 static inline StdProxyImpl
*impl_from_proxy_obj( void *iface
)
50 return CONTAINING_RECORD(iface
, StdProxyImpl
, PVtbl
);
55 extern void call_stubless_func(void);
56 __ASM_GLOBAL_FUNC(call_stubless_func
,
57 "movl 4(%esp),%ecx\n\t" /* This pointer */
58 "movl (%ecx),%ecx\n\t" /* This->lpVtbl */
59 "movl -8(%ecx),%ecx\n\t" /* MIDL_STUBLESS_PROXY_INFO */
60 "movl 8(%ecx),%edx\n\t" /* info->FormatStringOffset */
61 "movzwl (%edx,%eax,2),%edx\n\t" /* FormatStringOffset[index] */
62 "addl 4(%ecx),%edx\n\t" /* info->ProcFormatString + offset */
63 "movzbl 1(%edx),%eax\n\t" /* Oi_flags */
64 "andl $0x08,%eax\n\t" /* Oi_HAS_RPCFLAGS */
66 "movzwl 4(%edx,%eax),%eax\n\t" /* arguments size */
68 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
69 "leal 8(%esp),%eax\n\t" /* &This */
71 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
72 "pushl %edx\n\t" /* format string */
73 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
74 "pushl (%ecx)\n\t" /* info->pStubDesc */
75 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
76 "call " __ASM_NAME("ndr_client_call") "\n\t"
77 "leal 12(%esp),%esp\n\t"
78 __ASM_CFI(".cfi_adjust_cfa_offset -12\n\t")
79 "popl %edx\n\t" /* arguments size */
80 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
81 "movl (%esp),%ecx\n\t" /* return address */
95 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
97 thunk
->mov_eax
= 0xb8; /* movl $n,%eax */
99 thunk
->jmp
= 0xe9; /* jmp */
100 thunk
->handler
= (char *)call_stubless_func
- (char *)(&thunk
->handler
+ 1);
103 #elif defined(__x86_64__)
105 extern void call_stubless_func(void);
106 __ASM_GLOBAL_FUNC(call_stubless_func
,
107 "subq $0x38,%rsp\n\t"
108 __ASM_SEH(".seh_stackalloc 0x38\n\t")
109 __ASM_SEH(".seh_endprologue\n\t")
110 __ASM_CFI(".cfi_adjust_cfa_offset 0x38\n\t")
111 "movq %rcx,0x40(%rsp)\n\t"
112 "movq %rdx,0x48(%rsp)\n\t"
113 "movq %r8,0x50(%rsp)\n\t"
114 "movq %r9,0x58(%rsp)\n\t"
115 "leaq 0x40(%rsp),%r8\n\t" /* &This */
116 "movq (%rcx),%rcx\n\t" /* This->lpVtbl */
117 "movq -0x10(%rcx),%rcx\n\t" /* MIDL_STUBLESS_PROXY_INFO */
118 "movq 0x10(%rcx),%rdx\n\t" /* info->FormatStringOffset */
119 "movzwq (%rdx,%r10,2),%rdx\n\t" /* FormatStringOffset[index] */
120 "addq 8(%rcx),%rdx\n\t" /* info->ProcFormatString + offset */
121 "movq (%rcx),%rcx\n\t" /* info->pStubDesc */
122 "movq %xmm1,0x20(%rsp)\n\t"
123 "movq %xmm2,0x28(%rsp)\n\t"
124 "movq %xmm3,0x30(%rsp)\n\t"
125 "leaq 0x18(%rsp),%r9\n\t" /* fpu_args */
126 "call " __ASM_NAME("ndr_client_call") "\n\t"
127 "addq $0x38,%rsp\n\t"
128 __ASM_CFI(".cfi_adjust_cfa_offset -0x38\n\t")
131 #include "pshpack1.h"
142 static const struct thunk thunk_template
=
144 { 0x49, 0xc7, 0xc2 }, 0, /* movq $index,%r10 */
145 { 0x48, 0xb8 }, 0, /* movq $call_stubless_func,%rax */
146 { 0xff, 0xe0 } /* jmp *%rax */
149 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
151 *thunk
= thunk_template
;
152 thunk
->index
= index
;
153 thunk
->call_stubless
= call_stubless_func
;
156 #elif defined(__arm__)
158 extern void call_stubless_func(void);
159 __ASM_GLOBAL_FUNC(call_stubless_func
,
161 "mov r2, sp\n\t" /* stack_top */
164 "ldr r0, [r0]\n\t" /* This->lpVtbl */
165 "ldr r0, [r0,#-8]\n\t" /* MIDL_STUBLESS_PROXY_INFO */
166 "ldr r1, [r0,#8]\n\t" /* info->FormatStringOffset */
167 "ldrh r1, [r1,ip]\n\t" /* info->FormatStringOffset[index] */
168 "ldr ip, [r0,#4]\n\t" /* info->ProcFormatString */
169 "add r1, ip\n\t" /* info->ProcFormatString + offset */
170 "ldr r0, [r0]\n\t" /* info->pStubDesc */
174 "vpush {s0-s15}\n\t" /* store the s0-s15/d0-d7 arguments */
175 "mov r3, sp\n\t" /* fpu_stack */
177 "bl " __ASM_NAME("ndr_client_call") "\n\t"
185 DWORD ldr_ip
; /* ldr ip,[pc] */
186 DWORD ldr_pc
; /* ldr pc,[pc] */
191 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
193 thunk
->ldr_ip
= 0xe59fc000; /* ldr ip,[pc] */
194 thunk
->ldr_pc
= 0xe59ff000; /* ldr pc,[pc] */
195 thunk
->index
= index
* sizeof(unsigned short);
196 thunk
->func
= call_stubless_func
;
199 #elif defined(__aarch64__)
201 extern void call_stubless_func(void);
202 __ASM_GLOBAL_FUNC( call_stubless_func
,
203 "stp x29, x30, [sp, #-0x90]!\n\t"
204 __ASM_SEH(".seh_save_fplr_x 0x90\n\t")
206 __ASM_SEH(".seh_set_fp\n\t")
207 __ASM_SEH(".seh_endprologue\n\t")
208 "stp d0, d1, [sp, #0x10]\n\t"
209 "stp d2, d3, [sp, #0x20]\n\t"
210 "stp d4, d5, [sp, #0x30]\n\t"
211 "stp d6, d7, [sp, #0x40]\n\t"
212 "stp x0, x1, [sp, #0x50]\n\t"
213 "stp x2, x3, [sp, #0x60]\n\t"
214 "stp x4, x5, [sp, #0x70]\n\t"
215 "stp x6, x7, [sp, #0x80]\n\t"
216 "ldr x0, [x0]\n\t" /* This->lpVtbl */
217 "ldr x0, [x0, #-16]\n\t" /* MIDL_STUBLESS_PROXY_INFO */
218 "ldp x1, x4, [x0, #8]\n\t" /* info->ProcFormatString, FormatStringOffset */
219 "ldrh w4, [x4, x16, lsl #1]\n\t" /* info->FormatStringOffset[index] */
220 "add x1, x1, x4\n\t" /* info->ProcFormatString + offset */
221 "ldr x0, [x0]\n\t" /* info->pStubDesc */
222 "add x2, sp, #0x50\n\t" /* stack */
223 "add x3, sp, #0x10\n\t" /* fpu_stack */
224 "bl " __ASM_NAME("ndr_client_call") "\n\t"
225 "ldp x29, x30, [sp], #0x90\n\t"
230 DWORD ldr_index
; /* ldr w16, index */
231 DWORD ldr_func
; /* ldr x17, func */
232 DWORD br
; /* br x17 */
237 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
239 thunk
->ldr_index
= 0x18000070; /* ldr w16,index */
240 thunk
->ldr_func
= 0x58000071; /* ldr x17,func */
241 thunk
->br
= 0xd61f0220; /* br x17 */
242 thunk
->index
= index
;
243 thunk
->func
= call_stubless_func
;
248 #warning You must implement stubless proxies for your CPU
255 static inline void init_thunk( struct thunk
*thunk
, unsigned int index
)
257 thunk
->index
= index
;
260 #endif /* __i386__ */
262 #define BLOCK_SIZE 1024
263 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
265 static const struct thunk
*method_blocks
[MAX_BLOCKS
];
267 static const struct thunk
*allocate_block( unsigned int num
)
270 struct thunk
*prev
, *block
;
273 block
= VirtualAlloc( NULL
, BLOCK_SIZE
* sizeof(*block
),
274 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
275 if (!block
) return NULL
;
277 for (i
= 0; i
< BLOCK_SIZE
; i
++) init_thunk( &block
[i
], BLOCK_SIZE
* num
+ i
+ 3 );
278 VirtualProtect( block
, BLOCK_SIZE
* sizeof(*block
), PAGE_EXECUTE_READ
, &oldprot
);
279 prev
= InterlockedCompareExchangePointer( (void **)&method_blocks
[num
], block
, NULL
);
280 if (prev
) /* someone beat us to it */
282 VirtualFree( block
, 0, MEM_RELEASE
);
288 BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
290 const void **entry
= (const void **)(vtbl
+ 1);
293 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
295 FIXME( "%lu methods not supported\n", num
);
298 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
300 const struct thunk
*block
= method_blocks
[i
];
301 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
302 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
303 if (*entry
== (LPVOID
)-1) *entry
= &block
[j
];
308 HRESULT
StdProxy_Construct(REFIID riid
,
310 const ProxyFileInfo
*ProxyInfo
,
312 LPPSFACTORYBUFFER pPSFactory
,
313 LPRPCPROXYBUFFER
*ppProxy
,
317 PCInterfaceName name
= ProxyInfo
->pNamesArray
[Index
];
318 CInterfaceProxyVtbl
*vtbl
= ProxyInfo
->pProxyVtblList
[Index
];
320 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
322 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
323 if (ProxyInfo
->TableVersion
> 1) {
324 ULONG count
= ProxyInfo
->pStubVtblList
[Index
]->header
.DispatchTableCount
;
325 vtbl
= (CInterfaceProxyVtbl
*)((const void **)vtbl
+ 1);
326 TRACE("stubless vtbl %p: count=%ld\n", vtbl
->Vtbl
, count
);
327 fill_stubless_table( (IUnknownVtbl
*)vtbl
->Vtbl
, count
);
330 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
331 ERR("IID mismatch during proxy creation\n");
332 return RPC_E_UNEXPECTED
;
335 This
= calloc(1, sizeof(StdProxyImpl
));
336 if (!This
) return E_OUTOFMEMORY
;
338 if (!pUnkOuter
) pUnkOuter
= (IUnknown
*)&This
->IRpcProxyBuffer_iface
;
339 This
->IRpcProxyBuffer_iface
.lpVtbl
= &StdProxy_Vtbl
;
340 This
->PVtbl
= vtbl
->Vtbl
;
341 /* one reference for the proxy */
343 This
->piid
= vtbl
->header
.piid
;
344 This
->base_object
= NULL
;
345 This
->base_proxy
= NULL
;
346 This
->pUnkOuter
= pUnkOuter
;
348 This
->pPSFactory
= pPSFactory
;
349 This
->pChannel
= NULL
;
351 if(ProxyInfo
->pDelegatedIIDs
&& ProxyInfo
->pDelegatedIIDs
[Index
])
353 HRESULT r
= create_proxy( ProxyInfo
->pDelegatedIIDs
[Index
], NULL
,
354 &This
->base_proxy
, (void **)&This
->base_object
);
362 *ppProxy
= &This
->IRpcProxyBuffer_iface
;
363 *ppvObj
= &This
->PVtbl
;
364 IUnknown_AddRef((IUnknown
*)*ppvObj
);
365 IPSFactoryBuffer_AddRef(pPSFactory
);
367 TRACE( "iid=%s this %p proxy %p obj %p vtbl %p base proxy %p base obj %p\n",
368 debugstr_guid(riid
), This
, *ppProxy
, *ppvObj
, This
->PVtbl
, This
->base_proxy
, This
->base_object
);
372 HRESULT WINAPI
StdProxy_QueryInterface(IRpcProxyBuffer
*iface
, REFIID riid
, void **obj
)
374 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
375 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
377 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
378 IsEqualGUID(This
->piid
,riid
)) {
380 InterlockedIncrement(&This
->RefCount
);
384 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
385 *obj
= &This
->IRpcProxyBuffer_iface
;
386 InterlockedIncrement(&This
->RefCount
);
390 return E_NOINTERFACE
;
393 ULONG WINAPI
StdProxy_AddRef(IRpcProxyBuffer
*iface
)
395 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
396 TRACE("(%p)->AddRef()\n",This
);
398 return InterlockedIncrement(&This
->RefCount
);
401 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
404 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
405 TRACE("(%p)->Release()\n",This
);
407 refs
= InterlockedDecrement(&This
->RefCount
);
411 IRpcProxyBuffer_Disconnect(&This
->IRpcProxyBuffer_iface
);
413 if (This
->base_object
) IUnknown_Release( This
->base_object
);
414 if (This
->base_proxy
) IRpcProxyBuffer_Release( This
->base_proxy
);
416 IPSFactoryBuffer_Release(This
->pPSFactory
);
423 HRESULT WINAPI
StdProxy_Connect(IRpcProxyBuffer
*iface
, IRpcChannelBuffer
*pChannel
)
425 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
426 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
428 This
->pChannel
= pChannel
;
429 IRpcChannelBuffer_AddRef(pChannel
);
430 if (This
->base_proxy
) IRpcProxyBuffer_Connect( This
->base_proxy
, pChannel
);
434 void WINAPI
StdProxy_Disconnect(IRpcProxyBuffer
*iface
)
436 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
437 TRACE("(%p)->Disconnect()\n",This
);
439 if (This
->base_proxy
) IRpcProxyBuffer_Disconnect( This
->base_proxy
);
441 IRpcChannelBuffer_Release(This
->pChannel
);
442 This
->pChannel
= NULL
;
445 static const IRpcProxyBufferVtbl StdProxy_Vtbl
=
447 StdProxy_QueryInterface
,
454 static void StdProxy_GetChannel(LPVOID iface
,
455 LPRPCCHANNELBUFFER
*ppChannel
)
457 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
458 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
461 IRpcChannelBuffer_AddRef(This
->pChannel
);
463 *ppChannel
= This
->pChannel
;
466 static void StdProxy_GetIID(LPVOID iface
,
469 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
470 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
475 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
479 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
480 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This
,debugstr_guid(riid
),ppvObj
,This
->name
);
481 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
484 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
486 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
487 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
488 return IUnknown_AddRef(This
->pUnkOuter
);
491 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
493 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
494 TRACE("(%p)->Release() %s\n",This
,This
->name
);
495 return IUnknown_Release(This
->pUnkOuter
);
498 /***********************************************************************
499 * NdrProxyInitialize [RPCRT4.@]
501 void WINAPI
NdrProxyInitialize(void *This
,
502 PRPC_MESSAGE pRpcMsg
,
503 PMIDL_STUB_MESSAGE pStubMsg
,
504 PMIDL_STUB_DESC pStubDescriptor
,
505 unsigned int ProcNum
)
507 TRACE("(%p,%p,%p,%p,%d)\n", This
, pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
508 NdrClientInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
509 StdProxy_GetChannel(This
, &pStubMsg
->pRpcChannelBuffer
);
510 if (!pStubMsg
->pRpcChannelBuffer
)
511 RpcRaiseException(CO_E_OBJNOTCONNECTED
);
512 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
513 &pStubMsg
->dwDestContext
,
514 &pStubMsg
->pvDestContext
);
515 TRACE("channel=%p\n", pStubMsg
->pRpcChannelBuffer
);
518 /***********************************************************************
519 * NdrProxyGetBuffer [RPCRT4.@]
521 void WINAPI
NdrProxyGetBuffer(void *This
,
522 PMIDL_STUB_MESSAGE pStubMsg
)
525 const IID
*riid
= NULL
;
527 TRACE("(%p,%p)\n", This
, pStubMsg
);
528 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
529 pStubMsg
->dwStubPhase
= PROXY_GETBUFFER
;
530 StdProxy_GetIID(This
, &riid
);
531 hr
= IRpcChannelBuffer_GetBuffer(pStubMsg
->pRpcChannelBuffer
,
532 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
536 RpcRaiseException(hr
);
539 pStubMsg
->fBufferValid
= TRUE
;
540 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
541 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
542 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
543 pStubMsg
->dwStubPhase
= PROXY_MARSHAL
;
546 /***********************************************************************
547 * NdrProxySendReceive [RPCRT4.@]
549 void WINAPI
NdrProxySendReceive(void *This
,
550 PMIDL_STUB_MESSAGE pStubMsg
)
555 TRACE("(%p,%p)\n", This
, pStubMsg
);
557 if (!pStubMsg
->pRpcChannelBuffer
)
559 WARN("Trying to use disconnected proxy %p\n", This
);
560 RpcRaiseException(RPC_E_DISCONNECTED
);
563 pStubMsg
->dwStubPhase
= PROXY_SENDRECEIVE
;
564 /* avoid sending uninitialised parts of the buffer on the wire */
565 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->Buffer
- (unsigned char *)pStubMsg
->RpcMsg
->Buffer
;
566 hr
= IRpcChannelBuffer_SendReceive(pStubMsg
->pRpcChannelBuffer
,
567 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
569 pStubMsg
->dwStubPhase
= PROXY_UNMARSHAL
;
570 pStubMsg
->BufferLength
= pStubMsg
->RpcMsg
->BufferLength
;
571 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
572 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
573 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
575 /* raise exception if call failed */
576 if (hr
== RPC_S_CALL_FAILED
) RpcRaiseException(*(DWORD
*)pStubMsg
->Buffer
);
577 else if (FAILED(hr
)) RpcRaiseException(hr
);
580 /***********************************************************************
581 * NdrProxyFreeBuffer [RPCRT4.@]
583 void WINAPI
NdrProxyFreeBuffer(void *This
,
584 PMIDL_STUB_MESSAGE pStubMsg
)
586 TRACE("(%p,%p)\n", This
, pStubMsg
);
588 if (pStubMsg
->fBufferValid
)
590 IRpcChannelBuffer_FreeBuffer(pStubMsg
->pRpcChannelBuffer
,
591 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
);
592 pStubMsg
->fBufferValid
= FALSE
;
594 IRpcChannelBuffer_Release(pStubMsg
->pRpcChannelBuffer
);
595 pStubMsg
->pRpcChannelBuffer
= NULL
;
598 /***********************************************************************
599 * NdrProxyErrorHandler [RPCRT4.@]
601 HRESULT WINAPI
NdrProxyErrorHandler(DWORD dwExceptionCode
)
603 WARN("(0x%08lx): a proxy call failed\n", dwExceptionCode
);
605 if (FAILED(dwExceptionCode
))
606 return dwExceptionCode
;
608 return HRESULT_FROM_WIN32(dwExceptionCode
);