wined3d: Remove fbo_entry->d3d_depth_stencil.
[wine.git] / dlls / rpcrt4 / cproxy.c
blob7513b196cfefcf67ef5463afe2c24581132fdd20
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
21 * TODO: Handle non-i386 architectures
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
29 #define COBJMACROS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
35 #include "objbase.h"
36 #include "rpcproxy.h"
38 #include "cpsf.h"
39 #include "ndr_misc.h"
40 #include "ndr_stubless.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(ole);
45 /* I don't know what MS's std proxy structure looks like,
46 so this probably doesn't match, but that shouldn't matter */
47 typedef struct {
48 IRpcProxyBuffer IRpcProxyBuffer_iface;
49 LPVOID *PVtbl;
50 LONG RefCount;
51 const IID* piid;
52 LPUNKNOWN pUnkOuter;
53 IUnknown *base_object; /* must be at offset 0x10 from PVtbl */
54 IRpcProxyBuffer *base_proxy;
55 PCInterfaceName name;
56 LPPSFACTORYBUFFER pPSFactory;
57 LPRPCCHANNELBUFFER pChannel;
58 } StdProxyImpl;
60 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
62 static inline StdProxyImpl *impl_from_IRpcProxyBuffer(IRpcProxyBuffer *iface)
64 return CONTAINING_RECORD(iface, StdProxyImpl, IRpcProxyBuffer_iface);
67 static inline StdProxyImpl *impl_from_proxy_obj( void *iface )
69 return CONTAINING_RECORD(iface, StdProxyImpl, PVtbl);
72 #ifdef __i386__
74 extern void call_stubless_func(void);
75 __ASM_GLOBAL_FUNC(call_stubless_func,
76 "movl 4(%esp),%ecx\n\t" /* This pointer */
77 "movl (%ecx),%ecx\n\t" /* This->lpVtbl */
78 "movl -8(%ecx),%ecx\n\t" /* MIDL_STUBLESS_PROXY_INFO */
79 "movl 8(%ecx),%edx\n\t" /* info->FormatStringOffset */
80 "movzwl (%edx,%eax,2),%edx\n\t" /* FormatStringOffset[index] */
81 "addl 4(%ecx),%edx\n\t" /* info->ProcFormatString + offset */
82 "movzwl 8(%edx),%eax\n\t" /* arguments size */
83 "pushl %eax\n\t"
84 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
85 "leal 8(%esp),%eax\n\t" /* &This */
86 "pushl %eax\n\t"
87 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
88 "pushl %edx\n\t" /* format string */
89 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
90 "pushl (%ecx)\n\t" /* info->pStubDesc */
91 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
92 "call " __ASM_NAME("ndr_client_call") "\n\t"
93 "leal 12(%esp),%esp\n\t"
94 __ASM_CFI(".cfi_adjust_cfa_offset -12\n\t")
95 "popl %edx\n\t" /* arguments size */
96 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
97 "movl (%esp),%ecx\n\t" /* return address */
98 "addl %edx,%esp\n\t"
99 "jmp *%ecx" );
101 #include "pshpack1.h"
102 struct thunk
104 BYTE mov_eax;
105 DWORD index;
106 BYTE jmp;
107 LONG handler;
109 #include "poppack.h"
111 static inline void init_thunk( struct thunk *thunk, unsigned int index )
113 thunk->mov_eax = 0xb8; /* movl $n,%eax */
114 thunk->index = index;
115 thunk->jmp = 0xe9; /* jmp */
116 thunk->handler = (char *)call_stubless_func - (char *)(&thunk->handler + 1);
119 #elif defined(__x86_64__)
121 extern void call_stubless_func(void);
122 __ASM_GLOBAL_FUNC(call_stubless_func,
123 "movq %rcx,0x8(%rsp)\n\t"
124 "movq %rdx,0x10(%rsp)\n\t"
125 "movq %r8,0x18(%rsp)\n\t"
126 "movq %r9,0x20(%rsp)\n\t"
127 "leaq 0x8(%rsp),%r8\n\t" /* &This */
128 "movq (%rcx),%rcx\n\t" /* This->lpVtbl */
129 "movq -0x10(%rcx),%rcx\n\t" /* MIDL_STUBLESS_PROXY_INFO */
130 "movq 0x10(%rcx),%rdx\n\t" /* info->FormatStringOffset */
131 "movzwq (%rdx,%r10,2),%rdx\n\t" /* FormatStringOffset[index] */
132 "addq 8(%rcx),%rdx\n\t" /* info->ProcFormatString + offset */
133 "movq (%rcx),%rcx\n\t" /* info->pStubDesc */
134 "subq $0x38,%rsp\n\t"
135 __ASM_CFI(".cfi_adjust_cfa_offset 0x38\n\t")
136 "movq %xmm1,0x20(%rsp)\n\t"
137 "movq %xmm2,0x28(%rsp)\n\t"
138 "movq %xmm3,0x30(%rsp)\n\t"
139 "leaq 0x18(%rsp),%r9\n\t" /* fpu_args */
140 "call " __ASM_NAME("ndr_client_call") "\n\t"
141 "addq $0x38,%rsp\n\t"
142 __ASM_CFI(".cfi_adjust_cfa_offset -0x38\n\t")
143 "ret" );
145 #include "pshpack1.h"
146 struct thunk
148 BYTE mov_r10[3];
149 DWORD index;
150 BYTE mov_rax[2];
151 void *call_stubless;
152 BYTE jmp_rax[2];
154 #include "poppack.h"
156 static const struct thunk thunk_template =
158 { 0x49, 0xc7, 0xc2 }, 0, /* movq $index,%r10 */
159 { 0x48, 0xb8 }, 0, /* movq $call_stubless_func,%rax */
160 { 0xff, 0xe0 } /* jmp *%rax */
163 static inline void init_thunk( struct thunk *thunk, unsigned int index )
165 *thunk = thunk_template;
166 thunk->index = index;
167 thunk->call_stubless = call_stubless_func;
170 #else /* __i386__ */
172 #warning You must implement stubless proxies for your CPU
174 struct thunk
176 DWORD index;
179 static inline void init_thunk( struct thunk *thunk, unsigned int index )
181 thunk->index = index;
184 #endif /* __i386__ */
186 #define BLOCK_SIZE 1024
187 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
189 static const struct thunk *method_blocks[MAX_BLOCKS];
191 static const struct thunk *allocate_block( unsigned int num )
193 unsigned int i;
194 struct thunk *prev, *block;
195 DWORD oldprot;
197 block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
198 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
199 if (!block) return NULL;
201 for (i = 0; i < BLOCK_SIZE; i++) init_thunk( &block[i], BLOCK_SIZE * num + i + 3 );
202 VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, &oldprot );
203 prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
204 if (prev) /* someone beat us to it */
206 VirtualFree( block, 0, MEM_RELEASE );
207 block = prev;
209 return block;
212 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
214 const void **entry = (const void **)(vtbl + 1);
215 DWORD i, j;
217 if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
219 FIXME( "%u methods not supported\n", num );
220 return FALSE;
222 for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
224 const struct thunk *block = method_blocks[i];
225 if (!block && !(block = allocate_block( i ))) return FALSE;
226 for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
227 if (*entry == (LPVOID)-1) *entry = &block[j];
229 return TRUE;
232 HRESULT StdProxy_Construct(REFIID riid,
233 LPUNKNOWN pUnkOuter,
234 const ProxyFileInfo *ProxyInfo,
235 int Index,
236 LPPSFACTORYBUFFER pPSFactory,
237 LPRPCPROXYBUFFER *ppProxy,
238 LPVOID *ppvObj)
240 StdProxyImpl *This;
241 PCInterfaceName name = ProxyInfo->pNamesArray[Index];
242 CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
244 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
246 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
247 if (ProxyInfo->TableVersion > 1) {
248 ULONG count = ProxyInfo->pStubVtblList[Index]->header.DispatchTableCount;
249 vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
250 TRACE("stubless vtbl %p: count=%d\n", vtbl->Vtbl, count );
251 fill_stubless_table( (IUnknownVtbl *)vtbl->Vtbl, count );
254 if (!IsEqualGUID(vtbl->header.piid, riid)) {
255 ERR("IID mismatch during proxy creation\n");
256 return RPC_E_UNEXPECTED;
259 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
260 if (!This) return E_OUTOFMEMORY;
262 if (!pUnkOuter) pUnkOuter = (IUnknown *)This;
263 This->IRpcProxyBuffer_iface.lpVtbl = &StdProxy_Vtbl;
264 This->PVtbl = vtbl->Vtbl;
265 /* one reference for the proxy */
266 This->RefCount = 1;
267 This->piid = vtbl->header.piid;
268 This->base_object = NULL;
269 This->base_proxy = NULL;
270 This->pUnkOuter = pUnkOuter;
271 This->name = name;
272 This->pPSFactory = pPSFactory;
273 This->pChannel = NULL;
275 if(ProxyInfo->pDelegatedIIDs && ProxyInfo->pDelegatedIIDs[Index])
277 HRESULT r = create_proxy( ProxyInfo->pDelegatedIIDs[Index], NULL,
278 &This->base_proxy, (void **)&This->base_object );
279 if (FAILED(r))
281 HeapFree( GetProcessHeap(), 0, This );
282 return r;
286 *ppProxy = &This->IRpcProxyBuffer_iface;
287 *ppvObj = &This->PVtbl;
288 IUnknown_AddRef((IUnknown *)*ppvObj);
289 IPSFactoryBuffer_AddRef(pPSFactory);
291 TRACE( "iid=%s this %p proxy %p obj %p vtbl %p base proxy %p base obj %p\n",
292 debugstr_guid(riid), This, *ppProxy, *ppvObj, This->PVtbl, This->base_proxy, This->base_object );
293 return S_OK;
296 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
297 REFIID riid,
298 LPVOID *obj)
300 StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
301 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
303 if (IsEqualGUID(&IID_IUnknown,riid) ||
304 IsEqualGUID(This->piid,riid)) {
305 *obj = &This->PVtbl;
306 InterlockedIncrement(&This->RefCount);
307 return S_OK;
310 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
311 *obj = &This->IRpcProxyBuffer_iface;
312 InterlockedIncrement(&This->RefCount);
313 return S_OK;
316 return E_NOINTERFACE;
319 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
321 StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
322 TRACE("(%p)->AddRef()\n",This);
324 return InterlockedIncrement(&This->RefCount);
327 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
329 ULONG refs;
330 StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
331 TRACE("(%p)->Release()\n",This);
333 refs = InterlockedDecrement(&This->RefCount);
334 if (!refs)
336 if (This->pChannel)
337 IRpcProxyBuffer_Disconnect(&This->IRpcProxyBuffer_iface);
339 if (This->base_object) IUnknown_Release( This->base_object );
340 if (This->base_proxy) IRpcProxyBuffer_Release( This->base_proxy );
342 IPSFactoryBuffer_Release(This->pPSFactory);
343 HeapFree(GetProcessHeap(),0,This);
346 return refs;
349 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
350 LPRPCCHANNELBUFFER pChannel)
352 StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
353 TRACE("(%p)->Connect(%p)\n",This,pChannel);
355 This->pChannel = pChannel;
356 IRpcChannelBuffer_AddRef(pChannel);
357 if (This->base_proxy) IRpcProxyBuffer_Connect( This->base_proxy, pChannel );
358 return S_OK;
361 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
363 StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
364 TRACE("(%p)->Disconnect()\n",This);
366 if (This->base_proxy) IRpcProxyBuffer_Disconnect( This->base_proxy );
368 IRpcChannelBuffer_Release(This->pChannel);
369 This->pChannel = NULL;
372 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
374 StdProxy_QueryInterface,
375 StdProxy_AddRef,
376 StdProxy_Release,
377 StdProxy_Connect,
378 StdProxy_Disconnect
381 static void StdProxy_GetChannel(LPVOID iface,
382 LPRPCCHANNELBUFFER *ppChannel)
384 StdProxyImpl *This = impl_from_proxy_obj( iface );
385 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
387 *ppChannel = This->pChannel;
390 static void StdProxy_GetIID(LPVOID iface,
391 const IID **ppiid)
393 StdProxyImpl *This = impl_from_proxy_obj( iface );
394 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
396 *ppiid = This->piid;
399 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
400 REFIID riid,
401 LPVOID *ppvObj)
403 StdProxyImpl *This = impl_from_proxy_obj( iface );
404 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
405 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
408 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
410 StdProxyImpl *This = impl_from_proxy_obj( iface );
411 TRACE("(%p)->AddRef() %s\n",This,This->name);
412 return IUnknown_AddRef(This->pUnkOuter);
415 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
417 StdProxyImpl *This = impl_from_proxy_obj( iface );
418 TRACE("(%p)->Release() %s\n",This,This->name);
419 return IUnknown_Release(This->pUnkOuter);
422 /***********************************************************************
423 * NdrProxyInitialize [RPCRT4.@]
425 void WINAPI NdrProxyInitialize(void *This,
426 PRPC_MESSAGE pRpcMsg,
427 PMIDL_STUB_MESSAGE pStubMsg,
428 PMIDL_STUB_DESC pStubDescriptor,
429 unsigned int ProcNum)
431 TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
432 NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
433 StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
434 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
435 &pStubMsg->dwDestContext,
436 &pStubMsg->pvDestContext);
437 TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
440 /***********************************************************************
441 * NdrProxyGetBuffer [RPCRT4.@]
443 void WINAPI NdrProxyGetBuffer(void *This,
444 PMIDL_STUB_MESSAGE pStubMsg)
446 HRESULT hr;
447 const IID *riid = NULL;
449 TRACE("(%p,%p)\n", This, pStubMsg);
450 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
451 pStubMsg->dwStubPhase = PROXY_GETBUFFER;
452 StdProxy_GetIID(This, &riid);
453 hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
454 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
455 riid);
456 if (FAILED(hr))
458 RpcRaiseException(hr);
459 return;
461 pStubMsg->fBufferValid = TRUE;
462 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
463 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
464 pStubMsg->Buffer = pStubMsg->BufferStart;
465 pStubMsg->dwStubPhase = PROXY_MARSHAL;
468 /***********************************************************************
469 * NdrProxySendReceive [RPCRT4.@]
471 void WINAPI NdrProxySendReceive(void *This,
472 PMIDL_STUB_MESSAGE pStubMsg)
474 ULONG Status = 0;
475 HRESULT hr;
477 TRACE("(%p,%p)\n", This, pStubMsg);
479 if (!pStubMsg->pRpcChannelBuffer)
481 WARN("Trying to use disconnected proxy %p\n", This);
482 RpcRaiseException(RPC_E_DISCONNECTED);
485 pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
486 /* avoid sending uninitialised parts of the buffer on the wire */
487 pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
488 hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
489 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
490 &Status);
491 pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
492 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
493 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
494 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
495 pStubMsg->Buffer = pStubMsg->BufferStart;
497 /* raise exception if call failed */
498 if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
499 else if (FAILED(hr)) RpcRaiseException(hr);
502 /***********************************************************************
503 * NdrProxyFreeBuffer [RPCRT4.@]
505 void WINAPI NdrProxyFreeBuffer(void *This,
506 PMIDL_STUB_MESSAGE pStubMsg)
508 TRACE("(%p,%p)\n", This, pStubMsg);
510 if (pStubMsg->fBufferValid)
512 IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
513 (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
514 pStubMsg->fBufferValid = TRUE;
518 /***********************************************************************
519 * NdrProxyErrorHandler [RPCRT4.@]
521 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
523 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode);
525 if (FAILED(dwExceptionCode))
526 return dwExceptionCode;
527 else
528 return HRESULT_FROM_WIN32(dwExceptionCode);
531 HRESULT WINAPI
532 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
533 LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
535 typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
536 HMODULE hUser32 = LoadLibraryA("user32");
537 MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
539 FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
540 if (pMessageBoxA)
542 pMessageBoxA(NULL,
543 "The native implementation of OLEAUT32.DLL cannot be used "
544 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
545 "Wine: Unimplemented CreateProxyFromTypeInfo",
546 0x10);
547 ExitProcess(1);
549 return E_NOTIMPL;
552 HRESULT WINAPI
553 CreateStubFromTypeInfo(ITypeInfo *pTypeInfo, REFIID riid, IUnknown *pUnkServer,
554 IRpcStubBuffer **ppStub )
556 typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
557 HMODULE hUser32 = LoadLibraryA("user32");
558 MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
560 FIXME("%p %s %p %p\n", pTypeInfo, debugstr_guid(riid), pUnkServer, ppStub);
561 if (pMessageBoxA)
563 pMessageBoxA(NULL,
564 "The native implementation of OLEAUT32.DLL cannot be used "
565 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
566 "Wine: Unimplemented CreateProxyFromTypeInfo",
567 0x10);
568 ExitProcess(1);
570 return E_NOTIMPL;