windowscodecs: Silence fixme for IID_CMetaBitmapRenderTarget.
[wine.git] / dlls / rpcrt4 / cproxy.c
blob2fa064b17307930b0816db4835fe225a9fb700da
1 /*
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
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
30 #include "objbase.h"
31 #include "rpcproxy.h"
33 #include "cpsf.h"
34 #include "ndr_misc.h"
35 #include "ndr_stubless.h"
36 #include "wine/asm.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);
53 #ifdef __i386__
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 */
65 "shrl $1,%eax\n\t"
66 "movzwl 4(%edx,%eax),%eax\n\t" /* arguments size */
67 "pushl %eax\n\t"
68 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
69 "leal 8(%esp),%eax\n\t" /* &This */
70 "pushl %eax\n\t"
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 */
82 "addl %edx,%esp\n\t"
83 "jmp *%ecx" );
85 #define THUNK_ENTRY_SIZE 12
86 #define THUNK_ENTRY(num) \
87 ".balign 4\n\t" \
88 "movl $("#num"),%eax\n\t" \
89 ".byte 0xe9\n\t" /* jmp */ \
90 ".long " __ASM_NAME("call_stubless_func") "-1f\n" \
91 "1:\n\t"
93 #elif defined(__x86_64__)
95 extern void call_stubless_func(void);
96 __ASM_GLOBAL_FUNC(call_stubless_func,
97 "subq $0x38,%rsp\n\t"
98 __ASM_SEH(".seh_stackalloc 0x38\n\t")
99 __ASM_SEH(".seh_endprologue\n\t")
100 __ASM_CFI(".cfi_adjust_cfa_offset 0x38\n\t")
101 "movq %rcx,0x40(%rsp)\n\t"
102 "movq %rdx,0x48(%rsp)\n\t"
103 "movq %r8,0x50(%rsp)\n\t"
104 "movq %r9,0x58(%rsp)\n\t"
105 "leaq 0x40(%rsp),%r8\n\t" /* &This */
106 "movq (%rcx),%rcx\n\t" /* This->lpVtbl */
107 "movq -0x10(%rcx),%rcx\n\t" /* MIDL_STUBLESS_PROXY_INFO */
108 "movq 0x10(%rcx),%rdx\n\t" /* info->FormatStringOffset */
109 "movzwq (%rdx,%r10,2),%rdx\n\t" /* FormatStringOffset[index] */
110 "addq 8(%rcx),%rdx\n\t" /* info->ProcFormatString + offset */
111 "movq (%rcx),%rcx\n\t" /* info->pStubDesc */
112 "movq %xmm1,0x20(%rsp)\n\t"
113 "movq %xmm2,0x28(%rsp)\n\t"
114 "movq %xmm3,0x30(%rsp)\n\t"
115 "leaq 0x18(%rsp),%r9\n\t" /* fpu_args */
116 "call " __ASM_NAME("ndr_client_call") "\n\t"
117 "addq $0x38,%rsp\n\t"
118 __ASM_CFI(".cfi_adjust_cfa_offset -0x38\n\t")
119 "ret" );
121 #define THUNK_ENTRY_SIZE 12
122 #define THUNK_ENTRY(num) \
123 ".balign 4\n\t" \
124 "movl $("#num"),%r10d\n\t" \
125 ".byte 0xe9\n\t" /* jmp */ \
126 ".long " __ASM_NAME("call_stubless_func") "-1f\n" \
127 "1:\n\t"
129 #elif defined(__arm__)
131 extern void call_stubless_func(void);
132 __ASM_GLOBAL_FUNC(call_stubless_func,
133 "push {r0-r3}\n\t"
134 "mov r2, sp\n\t" /* stack_top */
135 "push {fp,lr}\n\t"
136 "mov fp, sp\n\t"
137 "ldr r0, [r0]\n\t" /* This->lpVtbl */
138 "ldr r0, [r0,#-8]\n\t" /* MIDL_STUBLESS_PROXY_INFO */
139 "ldr r1, [r0,#8]\n\t" /* info->FormatStringOffset */
140 "ldrh r1, [r1,ip]\n\t" /* info->FormatStringOffset[index] */
141 "ldr ip, [r0,#4]\n\t" /* info->ProcFormatString */
142 "add r1, ip\n\t" /* info->ProcFormatString + offset */
143 "ldr r0, [r0]\n\t" /* info->pStubDesc */
144 #ifdef __SOFTFP__
145 "mov r3, #0\n\t"
146 #else
147 "vpush {s0-s15}\n\t" /* store the s0-s15/d0-d7 arguments */
148 "mov r3, sp\n\t" /* fpu_stack */
149 #endif
150 "bl " __ASM_NAME("ndr_client_call") "\n\t"
151 "mov sp, fp\n\t"
152 "pop {fp,lr}\n\t"
153 "add sp, #16\n\t"
154 "bx lr" );
156 #define THUNK_ENTRY_SIZE 12
157 #define THUNK_ENTRY(num) \
158 "ldr ip,1f\n\t" \
159 "b.w " __ASM_NAME("call_stubless_func") "\n" \
160 "1:\t.long "#num"\n\t"
162 #elif defined(__aarch64__)
164 extern void call_stubless_func(void);
165 __ASM_GLOBAL_FUNC( call_stubless_func,
166 "stp x29, x30, [sp, #-0x90]!\n\t"
167 __ASM_SEH(".seh_save_fplr_x 0x90\n\t")
168 "mov x29, sp\n\t"
169 __ASM_SEH(".seh_set_fp\n\t")
170 __ASM_SEH(".seh_endprologue\n\t")
171 "stp d0, d1, [sp, #0x10]\n\t"
172 "stp d2, d3, [sp, #0x20]\n\t"
173 "stp d4, d5, [sp, #0x30]\n\t"
174 "stp d6, d7, [sp, #0x40]\n\t"
175 "stp x0, x1, [sp, #0x50]\n\t"
176 "stp x2, x3, [sp, #0x60]\n\t"
177 "stp x4, x5, [sp, #0x70]\n\t"
178 "stp x6, x7, [sp, #0x80]\n\t"
179 "ldr x0, [x0]\n\t" /* This->lpVtbl */
180 "ldr x0, [x0, #-16]\n\t" /* MIDL_STUBLESS_PROXY_INFO */
181 "ldp x1, x4, [x0, #8]\n\t" /* info->ProcFormatString, FormatStringOffset */
182 "ldrh w4, [x4, x16, lsl #1]\n\t" /* info->FormatStringOffset[index] */
183 "add x1, x1, x4\n\t" /* info->ProcFormatString + offset */
184 "ldr x0, [x0]\n\t" /* info->pStubDesc */
185 "add x2, sp, #0x50\n\t" /* stack */
186 "add x3, sp, #0x10\n\t" /* fpu_stack */
187 "bl " __ASM_NAME("ndr_client_call") "\n\t"
188 "ldp x29, x30, [sp], #0x90\n\t"
189 "ret" )
191 #define THUNK_ENTRY_SIZE 8
192 #define THUNK_ENTRY(num) \
193 "mov w16,#("#num")\n\t" \
194 "b " __ASM_NAME("call_stubless_func") "\n\t"
196 #else /* __i386__ */
198 #warning You must implement stubless proxies for your CPU
200 #define THUNK_ENTRY_SIZE 0
201 #define THUNK_ENTRY(num) ""
203 #endif /* __i386__ */
205 extern void stubless_thunks(void);
206 __ASM_GLOBAL_FUNC( stubless_thunks, ALL_THUNK_ENTRIES )
207 #undef THUNK_ENTRY
209 BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
211 const void **entry = (const void **)(vtbl + 1);
212 DWORD i;
214 if (num >= NB_THUNK_ENTRIES)
216 FIXME( "%lu methods not supported\n", num );
217 return FALSE;
219 for (i = 0; i < num - 3; i++, entry++)
220 if (*entry == (void *)-1) *entry = (char *)stubless_thunks + i * THUNK_ENTRY_SIZE;
222 return TRUE;
225 HRESULT StdProxy_Construct(REFIID riid,
226 LPUNKNOWN pUnkOuter,
227 const ProxyFileInfo *ProxyInfo,
228 int Index,
229 LPPSFACTORYBUFFER pPSFactory,
230 LPRPCPROXYBUFFER *ppProxy,
231 LPVOID *ppvObj)
233 StdProxyImpl *This;
234 PCInterfaceName name = ProxyInfo->pNamesArray[Index];
235 CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
237 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
239 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
240 if (ProxyInfo->TableVersion > 1) {
241 ULONG count = ProxyInfo->pStubVtblList[Index]->header.DispatchTableCount;
242 vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
243 TRACE("stubless vtbl %p: count=%ld\n", vtbl->Vtbl, count );
244 fill_stubless_table( (IUnknownVtbl *)vtbl->Vtbl, count );
247 if (!IsEqualGUID(vtbl->header.piid, riid)) {
248 ERR("IID mismatch during proxy creation\n");
249 return RPC_E_UNEXPECTED;
252 This = calloc(1, sizeof(StdProxyImpl));
253 if (!This) return E_OUTOFMEMORY;
255 if (!pUnkOuter) pUnkOuter = (IUnknown *)&This->IRpcProxyBuffer_iface;
256 This->IRpcProxyBuffer_iface.lpVtbl = &StdProxy_Vtbl;
257 This->PVtbl = vtbl->Vtbl;
258 /* one reference for the proxy */
259 This->RefCount = 1;
260 This->piid = vtbl->header.piid;
261 This->base_object = NULL;
262 This->base_proxy = NULL;
263 This->pUnkOuter = pUnkOuter;
264 This->name = name;
265 This->pPSFactory = pPSFactory;
266 This->pChannel = NULL;
268 if(ProxyInfo->pDelegatedIIDs && ProxyInfo->pDelegatedIIDs[Index])
270 HRESULT r = create_proxy( ProxyInfo->pDelegatedIIDs[Index], NULL,
271 &This->base_proxy, (void **)&This->base_object );
272 if (FAILED(r))
274 free( This );
275 return r;
279 *ppProxy = &This->IRpcProxyBuffer_iface;
280 *ppvObj = &This->PVtbl;
281 IUnknown_AddRef((IUnknown *)*ppvObj);
282 IPSFactoryBuffer_AddRef(pPSFactory);
284 TRACE( "iid=%s this %p proxy %p obj %p vtbl %p base proxy %p base obj %p\n",
285 debugstr_guid(riid), This, *ppProxy, *ppvObj, This->PVtbl, This->base_proxy, This->base_object );
286 return S_OK;
289 HRESULT WINAPI StdProxy_QueryInterface(IRpcProxyBuffer *iface, REFIID riid, void **obj)
291 StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
292 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
294 if (IsEqualGUID(&IID_IUnknown,riid) ||
295 IsEqualGUID(This->piid,riid)) {
296 *obj = &This->PVtbl;
297 InterlockedIncrement(&This->RefCount);
298 return S_OK;
301 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
302 *obj = &This->IRpcProxyBuffer_iface;
303 InterlockedIncrement(&This->RefCount);
304 return S_OK;
307 return E_NOINTERFACE;
310 ULONG WINAPI StdProxy_AddRef(IRpcProxyBuffer *iface)
312 StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
313 TRACE("(%p)->AddRef()\n",This);
315 return InterlockedIncrement(&This->RefCount);
318 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
320 ULONG refs;
321 StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
322 TRACE("(%p)->Release()\n",This);
324 refs = InterlockedDecrement(&This->RefCount);
325 if (!refs)
327 if (This->pChannel)
328 IRpcProxyBuffer_Disconnect(&This->IRpcProxyBuffer_iface);
330 if (This->base_object) IUnknown_Release( This->base_object );
331 if (This->base_proxy) IRpcProxyBuffer_Release( This->base_proxy );
333 IPSFactoryBuffer_Release(This->pPSFactory);
334 free(This);
337 return refs;
340 HRESULT WINAPI StdProxy_Connect(IRpcProxyBuffer *iface, IRpcChannelBuffer *pChannel)
342 StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
343 TRACE("(%p)->Connect(%p)\n",This,pChannel);
345 This->pChannel = pChannel;
346 IRpcChannelBuffer_AddRef(pChannel);
347 if (This->base_proxy) IRpcProxyBuffer_Connect( This->base_proxy, pChannel );
348 return S_OK;
351 void WINAPI StdProxy_Disconnect(IRpcProxyBuffer *iface)
353 StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
354 TRACE("(%p)->Disconnect()\n",This);
356 if (This->base_proxy) IRpcProxyBuffer_Disconnect( This->base_proxy );
358 IRpcChannelBuffer_Release(This->pChannel);
359 This->pChannel = NULL;
362 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
364 StdProxy_QueryInterface,
365 StdProxy_AddRef,
366 StdProxy_Release,
367 StdProxy_Connect,
368 StdProxy_Disconnect
371 static void StdProxy_GetChannel(LPVOID iface,
372 LPRPCCHANNELBUFFER *ppChannel)
374 StdProxyImpl *This = impl_from_proxy_obj( iface );
375 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
377 if(This->pChannel)
378 IRpcChannelBuffer_AddRef(This->pChannel);
380 *ppChannel = This->pChannel;
383 static void StdProxy_GetIID(LPVOID iface,
384 const IID **ppiid)
386 StdProxyImpl *This = impl_from_proxy_obj( iface );
387 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
389 *ppiid = This->piid;
392 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
393 REFIID riid,
394 LPVOID *ppvObj)
396 StdProxyImpl *This = impl_from_proxy_obj( iface );
397 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
398 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
401 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
403 StdProxyImpl *This = impl_from_proxy_obj( iface );
404 TRACE("(%p)->AddRef() %s\n",This,This->name);
405 return IUnknown_AddRef(This->pUnkOuter);
408 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
410 StdProxyImpl *This = impl_from_proxy_obj( iface );
411 TRACE("(%p)->Release() %s\n",This,This->name);
412 return IUnknown_Release(This->pUnkOuter);
415 /***********************************************************************
416 * NdrProxyInitialize [RPCRT4.@]
418 void WINAPI NdrProxyInitialize(void *This,
419 PRPC_MESSAGE pRpcMsg,
420 PMIDL_STUB_MESSAGE pStubMsg,
421 PMIDL_STUB_DESC pStubDescriptor,
422 unsigned int ProcNum)
424 TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
425 NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
426 StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
427 if (!pStubMsg->pRpcChannelBuffer)
428 RpcRaiseException(CO_E_OBJNOTCONNECTED);
429 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
430 &pStubMsg->dwDestContext,
431 &pStubMsg->pvDestContext);
432 TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
435 /***********************************************************************
436 * NdrProxyGetBuffer [RPCRT4.@]
438 void WINAPI NdrProxyGetBuffer(void *This,
439 PMIDL_STUB_MESSAGE pStubMsg)
441 HRESULT hr;
442 const IID *riid = NULL;
444 TRACE("(%p,%p)\n", This, pStubMsg);
445 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
446 pStubMsg->dwStubPhase = PROXY_GETBUFFER;
447 StdProxy_GetIID(This, &riid);
448 hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
449 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
450 riid);
451 if (FAILED(hr))
453 RpcRaiseException(hr);
454 return;
456 pStubMsg->fBufferValid = TRUE;
457 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
458 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
459 pStubMsg->Buffer = pStubMsg->BufferStart;
460 pStubMsg->dwStubPhase = PROXY_MARSHAL;
463 /***********************************************************************
464 * NdrProxySendReceive [RPCRT4.@]
466 void WINAPI NdrProxySendReceive(void *This,
467 PMIDL_STUB_MESSAGE pStubMsg)
469 ULONG Status = 0;
470 HRESULT hr;
472 TRACE("(%p,%p)\n", This, pStubMsg);
474 if (!pStubMsg->pRpcChannelBuffer)
476 WARN("Trying to use disconnected proxy %p\n", This);
477 RpcRaiseException(RPC_E_DISCONNECTED);
480 pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
481 /* avoid sending uninitialised parts of the buffer on the wire */
482 pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
483 hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
484 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
485 &Status);
486 pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
487 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
488 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
489 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
490 pStubMsg->Buffer = pStubMsg->BufferStart;
492 /* raise exception if call failed */
493 if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
494 else if (FAILED(hr)) RpcRaiseException(hr);
497 /***********************************************************************
498 * NdrProxyFreeBuffer [RPCRT4.@]
500 void WINAPI NdrProxyFreeBuffer(void *This,
501 PMIDL_STUB_MESSAGE pStubMsg)
503 TRACE("(%p,%p)\n", This, pStubMsg);
505 if (pStubMsg->fBufferValid)
507 IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
508 (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
509 pStubMsg->fBufferValid = FALSE;
511 IRpcChannelBuffer_Release(pStubMsg->pRpcChannelBuffer);
512 pStubMsg->pRpcChannelBuffer = NULL;
515 /***********************************************************************
516 * NdrProxyErrorHandler [RPCRT4.@]
518 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
520 WARN("(0x%08lx): a proxy call failed\n", dwExceptionCode);
522 if (FAILED(dwExceptionCode))
523 return dwExceptionCode;
524 else
525 return HRESULT_FROM_WIN32(dwExceptionCode);