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
25 #include "wine/port.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 */
48 IRpcProxyBuffer IRpcProxyBuffer_iface
;
53 IUnknown
*base_object
; /* must be at offset 0x10 from PVtbl */
54 IRpcProxyBuffer
*base_proxy
;
56 LPPSFACTORYBUFFER pPSFactory
;
57 LPRPCCHANNELBUFFER pChannel
;
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
);
85 extern void call_stubless_func(void);
86 __ASM_GLOBAL_FUNC(call_stubless_func
,
87 "pushl %eax\n\t" /* method index */
88 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
89 "pushl %esp\n\t" /* pointer to index */
90 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
91 "call " __ASM_NAME("ObjectStubless") __ASM_STDCALL(4) "\n\t"
92 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
93 "popl %edx\n\t" /* args size */
94 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
95 "movl (%esp),%ecx\n\t" /* return address */
99 LONG_PTR WINAPI
ObjectStubless(void **args
)
101 DWORD index
= (DWORD
)args
[0];
102 void **iface
= (void **)args
[2];
103 const void **vtbl
= (const void **)*iface
;
104 const MIDL_STUBLESS_PROXY_INFO
*stubless
= *(const MIDL_STUBLESS_PROXY_INFO
**)(vtbl
- 2);
105 const PFORMAT_STRING fs
= stubless
->ProcFormatString
+ stubless
->FormatStringOffset
[index
];
106 DWORD arg_size
= *(const WORD
*)(fs
+ 8);
108 /* store bytes to remove from stack */
109 args
[0] = (void *)arg_size
;
110 TRACE("(%p)->(%d)([%d bytes]) ret=%p\n", iface
, index
, arg_size
, args
[1]);
112 return ndr_client_call(stubless
->pStubDesc
, fs
, args
+ 2);
115 #define BLOCK_SIZE 1024
116 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
118 static const struct thunk
*method_blocks
[MAX_BLOCKS
];
120 static const struct thunk
*allocate_block( unsigned int num
)
123 struct thunk
*prev
, *block
;
125 block
= VirtualAlloc( NULL
, BLOCK_SIZE
* sizeof(*block
),
126 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
127 if (!block
) return NULL
;
129 for (i
= 0; i
< BLOCK_SIZE
; i
++)
131 block
[i
].mov_eax
= 0xb8; /* movl $n,%eax */
132 block
[i
].index
= BLOCK_SIZE
* num
+ i
+ 3;
133 block
[i
].jmp
= 0xe9; /* jmp */
134 block
[i
].handler
= (char *)call_stubless_func
- (char *)(&block
[i
].handler
+ 1);
136 VirtualProtect( block
, BLOCK_SIZE
* sizeof(*block
), PAGE_EXECUTE_READ
, NULL
);
137 prev
= InterlockedCompareExchangePointer( (void **)&method_blocks
[num
], block
, NULL
);
138 if (prev
) /* someone beat us to it */
140 VirtualFree( block
, 0, MEM_RELEASE
);
146 static BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
148 const void **entry
= (const void **)(vtbl
+ 1);
151 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
153 FIXME( "%u methods not supported\n", num
);
156 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
158 const struct thunk
*block
= method_blocks
[i
];
159 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
160 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
161 if (*entry
== (LPVOID
)-1) *entry
= &block
[j
];
168 static BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
170 ERR("stubless proxies are not supported on this architecture\n");
174 #endif /* __i386__ */
176 HRESULT
StdProxy_Construct(REFIID riid
,
178 const ProxyFileInfo
*ProxyInfo
,
180 LPPSFACTORYBUFFER pPSFactory
,
181 LPRPCPROXYBUFFER
*ppProxy
,
185 PCInterfaceName name
= ProxyInfo
->pNamesArray
[Index
];
186 CInterfaceProxyVtbl
*vtbl
= ProxyInfo
->pProxyVtblList
[Index
];
188 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
190 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
191 if (ProxyInfo
->TableVersion
> 1) {
192 ULONG count
= ProxyInfo
->pStubVtblList
[Index
]->header
.DispatchTableCount
;
193 vtbl
= (CInterfaceProxyVtbl
*)((const void **)vtbl
+ 1);
194 TRACE("stubless vtbl %p: count=%d\n", vtbl
->Vtbl
, count
);
195 fill_stubless_table( (IUnknownVtbl
*)vtbl
->Vtbl
, count
);
198 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
199 ERR("IID mismatch during proxy creation\n");
200 return RPC_E_UNEXPECTED
;
203 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(StdProxyImpl
));
204 if (!This
) return E_OUTOFMEMORY
;
206 if (!pUnkOuter
) pUnkOuter
= (IUnknown
*)This
;
207 This
->IRpcProxyBuffer_iface
.lpVtbl
= &StdProxy_Vtbl
;
208 This
->PVtbl
= vtbl
->Vtbl
;
209 /* one reference for the proxy */
211 This
->piid
= vtbl
->header
.piid
;
212 This
->base_object
= NULL
;
213 This
->base_proxy
= NULL
;
214 This
->pUnkOuter
= pUnkOuter
;
216 This
->pPSFactory
= pPSFactory
;
217 This
->pChannel
= NULL
;
219 if(ProxyInfo
->pDelegatedIIDs
&& ProxyInfo
->pDelegatedIIDs
[Index
])
221 HRESULT r
= create_proxy( ProxyInfo
->pDelegatedIIDs
[Index
], NULL
,
222 &This
->base_proxy
, (void **)&This
->base_object
);
225 HeapFree( GetProcessHeap(), 0, This
);
230 *ppProxy
= &This
->IRpcProxyBuffer_iface
;
231 *ppvObj
= &This
->PVtbl
;
232 IUnknown_AddRef((IUnknown
*)*ppvObj
);
233 IPSFactoryBuffer_AddRef(pPSFactory
);
235 TRACE( "iid=%s this %p proxy %p obj %p vtbl %p base proxy %p base obj %p\n",
236 debugstr_guid(riid
), This
, *ppProxy
, *ppvObj
, This
->PVtbl
, This
->base_proxy
, This
->base_object
);
240 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
244 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
245 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
247 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
248 IsEqualGUID(This
->piid
,riid
)) {
250 InterlockedIncrement(&This
->RefCount
);
254 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
255 *obj
= &This
->IRpcProxyBuffer_iface
;
256 InterlockedIncrement(&This
->RefCount
);
260 return E_NOINTERFACE
;
263 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
265 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
266 TRACE("(%p)->AddRef()\n",This
);
268 return InterlockedIncrement(&This
->RefCount
);
271 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
274 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
275 TRACE("(%p)->Release()\n",This
);
277 refs
= InterlockedDecrement(&This
->RefCount
);
281 IRpcProxyBuffer_Disconnect(&This
->IRpcProxyBuffer_iface
);
283 if (This
->base_object
) IUnknown_Release( This
->base_object
);
284 if (This
->base_proxy
) IRpcProxyBuffer_Release( This
->base_proxy
);
286 IPSFactoryBuffer_Release(This
->pPSFactory
);
287 HeapFree(GetProcessHeap(),0,This
);
293 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
294 LPRPCCHANNELBUFFER pChannel
)
296 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
297 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
299 This
->pChannel
= pChannel
;
300 IRpcChannelBuffer_AddRef(pChannel
);
301 if (This
->base_proxy
) IRpcProxyBuffer_Connect( This
->base_proxy
, pChannel
);
305 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
307 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
308 TRACE("(%p)->Disconnect()\n",This
);
310 if (This
->base_proxy
) IRpcProxyBuffer_Disconnect( This
->base_proxy
);
312 IRpcChannelBuffer_Release(This
->pChannel
);
313 This
->pChannel
= NULL
;
316 static const IRpcProxyBufferVtbl StdProxy_Vtbl
=
318 StdProxy_QueryInterface
,
325 static void StdProxy_GetChannel(LPVOID iface
,
326 LPRPCCHANNELBUFFER
*ppChannel
)
328 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
329 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
331 *ppChannel
= This
->pChannel
;
334 static void StdProxy_GetIID(LPVOID iface
,
337 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
338 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
343 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
347 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
348 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This
,debugstr_guid(riid
),ppvObj
,This
->name
);
349 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
352 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
354 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
355 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
356 return IUnknown_AddRef(This
->pUnkOuter
);
359 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
361 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
362 TRACE("(%p)->Release() %s\n",This
,This
->name
);
363 return IUnknown_Release(This
->pUnkOuter
);
366 /***********************************************************************
367 * NdrProxyInitialize [RPCRT4.@]
369 void WINAPI
NdrProxyInitialize(void *This
,
370 PRPC_MESSAGE pRpcMsg
,
371 PMIDL_STUB_MESSAGE pStubMsg
,
372 PMIDL_STUB_DESC pStubDescriptor
,
373 unsigned int ProcNum
)
375 TRACE("(%p,%p,%p,%p,%d)\n", This
, pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
376 NdrClientInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
377 StdProxy_GetChannel(This
, &pStubMsg
->pRpcChannelBuffer
);
378 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
379 &pStubMsg
->dwDestContext
,
380 &pStubMsg
->pvDestContext
);
381 TRACE("channel=%p\n", pStubMsg
->pRpcChannelBuffer
);
384 /***********************************************************************
385 * NdrProxyGetBuffer [RPCRT4.@]
387 void WINAPI
NdrProxyGetBuffer(void *This
,
388 PMIDL_STUB_MESSAGE pStubMsg
)
391 const IID
*riid
= NULL
;
393 TRACE("(%p,%p)\n", This
, pStubMsg
);
394 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
395 pStubMsg
->dwStubPhase
= PROXY_GETBUFFER
;
396 StdProxy_GetIID(This
, &riid
);
397 hr
= IRpcChannelBuffer_GetBuffer(pStubMsg
->pRpcChannelBuffer
,
398 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
402 RpcRaiseException(hr
);
405 pStubMsg
->fBufferValid
= TRUE
;
406 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
407 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
408 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
409 pStubMsg
->dwStubPhase
= PROXY_MARSHAL
;
412 /***********************************************************************
413 * NdrProxySendReceive [RPCRT4.@]
415 void WINAPI
NdrProxySendReceive(void *This
,
416 PMIDL_STUB_MESSAGE pStubMsg
)
421 TRACE("(%p,%p)\n", This
, pStubMsg
);
423 if (!pStubMsg
->pRpcChannelBuffer
)
425 WARN("Trying to use disconnected proxy %p\n", This
);
426 RpcRaiseException(RPC_E_DISCONNECTED
);
429 pStubMsg
->dwStubPhase
= PROXY_SENDRECEIVE
;
430 /* avoid sending uninitialised parts of the buffer on the wire */
431 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->Buffer
- (unsigned char *)pStubMsg
->RpcMsg
->Buffer
;
432 hr
= IRpcChannelBuffer_SendReceive(pStubMsg
->pRpcChannelBuffer
,
433 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
435 pStubMsg
->dwStubPhase
= PROXY_UNMARSHAL
;
436 pStubMsg
->BufferLength
= pStubMsg
->RpcMsg
->BufferLength
;
437 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
438 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
439 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
441 /* raise exception if call failed */
442 if (hr
== RPC_S_CALL_FAILED
) RpcRaiseException(*(DWORD
*)pStubMsg
->Buffer
);
443 else if (FAILED(hr
)) RpcRaiseException(hr
);
446 /***********************************************************************
447 * NdrProxyFreeBuffer [RPCRT4.@]
449 void WINAPI
NdrProxyFreeBuffer(void *This
,
450 PMIDL_STUB_MESSAGE pStubMsg
)
452 TRACE("(%p,%p)\n", This
, pStubMsg
);
454 if (pStubMsg
->fBufferValid
)
456 IRpcChannelBuffer_FreeBuffer(pStubMsg
->pRpcChannelBuffer
,
457 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
);
458 pStubMsg
->fBufferValid
= TRUE
;
462 /***********************************************************************
463 * NdrProxyErrorHandler [RPCRT4.@]
465 HRESULT WINAPI
NdrProxyErrorHandler(DWORD dwExceptionCode
)
467 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode
);
469 if (FAILED(dwExceptionCode
))
470 return dwExceptionCode
;
472 return HRESULT_FROM_WIN32(dwExceptionCode
);
476 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo
, LPUNKNOWN pUnkOuter
, REFIID riid
,
477 LPRPCPROXYBUFFER
*ppProxy
, LPVOID
*ppv
)
479 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
480 HMODULE hUser32
= LoadLibraryA("user32");
481 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
483 FIXME("%p %p %s %p %p\n", pTypeInfo
, pUnkOuter
, debugstr_guid(riid
), ppProxy
, ppv
);
487 "The native implementation of OLEAUT32.DLL cannot be used "
488 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
489 "Wine: Unimplemented CreateProxyFromTypeInfo",
497 CreateStubFromTypeInfo(ITypeInfo
*pTypeInfo
, REFIID riid
, IUnknown
*pUnkServer
,
498 IRpcStubBuffer
**ppStub
)
500 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
501 HMODULE hUser32
= LoadLibraryA("user32");
502 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
504 FIXME("%p %s %p %p\n", pTypeInfo
, debugstr_guid(riid
), pUnkServer
, ppStub
);
508 "The native implementation of OLEAUT32.DLL cannot be used "
509 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
510 "Wine: Unimplemented CreateProxyFromTypeInfo",