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 "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
44 /* I don't know what MS's std proxy structure looks like,
45 so this probably doesn't match, but that shouldn't matter */
47 IRpcProxyBuffer IRpcProxyBuffer_iface
;
52 IUnknown
*base_object
; /* must be at offset 0x10 from PVtbl */
53 IRpcProxyBuffer
*base_proxy
;
55 LPPSFACTORYBUFFER pPSFactory
;
56 LPRPCCHANNELBUFFER pChannel
;
59 static const IRpcProxyBufferVtbl StdProxy_Vtbl
;
61 static inline StdProxyImpl
*impl_from_IRpcProxyBuffer(IRpcProxyBuffer
*iface
)
63 return CONTAINING_RECORD(iface
, StdProxyImpl
, IRpcProxyBuffer_iface
);
66 static inline StdProxyImpl
*impl_from_proxy_obj( void *iface
)
68 return CONTAINING_RECORD(iface
, StdProxyImpl
, PVtbl
);
84 extern void call_stubless_func(void);
85 __ASM_GLOBAL_FUNC(call_stubless_func
,
86 "pushl %eax\n\t" /* method index */
87 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
88 "pushl %esp\n\t" /* pointer to index */
89 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
90 "call " __ASM_NAME("ObjectStubless") __ASM_STDCALL(4) "\n\t"
91 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
92 "popl %edx\n\t" /* args size */
93 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
94 "movl (%esp),%ecx\n\t" /* return address */
98 CLIENT_CALL_RETURN WINAPI
ObjectStubless(DWORD
*args
)
100 DWORD index
= args
[0];
101 void **iface
= (void **)args
[2];
102 const void **vtbl
= (const void **)*iface
;
103 const MIDL_STUBLESS_PROXY_INFO
*stubless
= *(const MIDL_STUBLESS_PROXY_INFO
**)(vtbl
- 2);
104 const PFORMAT_STRING fs
= stubless
->ProcFormatString
+ stubless
->FormatStringOffset
[index
];
106 /* store bytes to remove from stack */
107 args
[0] = *(const WORD
*)(fs
+ 8);
108 TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface
, index
, args
[0], args
[1]);
110 return NdrClientCall2(stubless
->pStubDesc
, fs
, args
+ 2);
113 #define BLOCK_SIZE 1024
114 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
116 static const struct thunk
*method_blocks
[MAX_BLOCKS
];
118 static const struct thunk
*allocate_block( unsigned int num
)
121 struct thunk
*prev
, *block
;
123 block
= VirtualAlloc( NULL
, BLOCK_SIZE
* sizeof(*block
),
124 MEM_COMMIT
| MEM_RESERVE
, PAGE_EXECUTE_READWRITE
);
125 if (!block
) return NULL
;
127 for (i
= 0; i
< BLOCK_SIZE
; i
++)
129 block
[i
].mov_eax
= 0xb8; /* movl $n,%eax */
130 block
[i
].index
= BLOCK_SIZE
* num
+ i
+ 3;
131 block
[i
].jmp
= 0xe9; /* jmp */
132 block
[i
].handler
= (char *)call_stubless_func
- (char *)(&block
[i
].handler
+ 1);
134 VirtualProtect( block
, BLOCK_SIZE
* sizeof(*block
), PAGE_EXECUTE_READ
, NULL
);
135 prev
= InterlockedCompareExchangePointer( (void **)&method_blocks
[num
], block
, NULL
);
136 if (prev
) /* someone beat us to it */
138 VirtualFree( block
, 0, MEM_RELEASE
);
144 static BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
146 const void **entry
= (const void **)(vtbl
+ 1);
149 if (num
- 3 > BLOCK_SIZE
* MAX_BLOCKS
)
151 FIXME( "%u methods not supported\n", num
);
154 for (i
= 0; i
< (num
- 3 + BLOCK_SIZE
- 1) / BLOCK_SIZE
; i
++)
156 const struct thunk
*block
= method_blocks
[i
];
157 if (!block
&& !(block
= allocate_block( i
))) return FALSE
;
158 for (j
= 0; j
< BLOCK_SIZE
&& j
< num
- 3 - i
* BLOCK_SIZE
; j
++, entry
++)
159 if (*entry
== (LPVOID
)-1) *entry
= &block
[j
];
166 static BOOL
fill_stubless_table( IUnknownVtbl
*vtbl
, DWORD num
)
168 ERR("stubless proxies are not supported on this architecture\n");
172 #endif /* __i386__ */
174 HRESULT
StdProxy_Construct(REFIID riid
,
176 const ProxyFileInfo
*ProxyInfo
,
178 LPPSFACTORYBUFFER pPSFactory
,
179 LPRPCPROXYBUFFER
*ppProxy
,
183 PCInterfaceName name
= ProxyInfo
->pNamesArray
[Index
];
184 CInterfaceProxyVtbl
*vtbl
= ProxyInfo
->pProxyVtblList
[Index
];
186 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter
, vtbl
, pPSFactory
, ppProxy
, ppvObj
, name
);
188 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
189 if (ProxyInfo
->TableVersion
> 1) {
190 ULONG count
= ProxyInfo
->pStubVtblList
[Index
]->header
.DispatchTableCount
;
191 vtbl
= (CInterfaceProxyVtbl
*)((const void **)vtbl
+ 1);
192 TRACE("stubless vtbl %p: count=%d\n", vtbl
->Vtbl
, count
);
193 fill_stubless_table( (IUnknownVtbl
*)vtbl
->Vtbl
, count
);
196 if (!IsEqualGUID(vtbl
->header
.piid
, riid
)) {
197 ERR("IID mismatch during proxy creation\n");
198 return RPC_E_UNEXPECTED
;
201 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(StdProxyImpl
));
202 if (!This
) return E_OUTOFMEMORY
;
204 if (!pUnkOuter
) pUnkOuter
= (IUnknown
*)This
;
205 This
->IRpcProxyBuffer_iface
.lpVtbl
= &StdProxy_Vtbl
;
206 This
->PVtbl
= vtbl
->Vtbl
;
207 /* one reference for the proxy */
209 This
->piid
= vtbl
->header
.piid
;
210 This
->base_object
= NULL
;
211 This
->base_proxy
= NULL
;
212 This
->pUnkOuter
= pUnkOuter
;
214 This
->pPSFactory
= pPSFactory
;
215 This
->pChannel
= NULL
;
217 if(ProxyInfo
->pDelegatedIIDs
&& ProxyInfo
->pDelegatedIIDs
[Index
])
219 HRESULT r
= create_proxy( ProxyInfo
->pDelegatedIIDs
[Index
], NULL
,
220 &This
->base_proxy
, (void **)&This
->base_object
);
223 HeapFree( GetProcessHeap(), 0, This
);
228 *ppProxy
= &This
->IRpcProxyBuffer_iface
;
229 *ppvObj
= &This
->PVtbl
;
230 IUnknown_AddRef((IUnknown
*)*ppvObj
);
231 IPSFactoryBuffer_AddRef(pPSFactory
);
233 TRACE( "iid=%s this %p proxy %p obj %p vtbl %p base proxy %p base obj %p\n",
234 debugstr_guid(riid
), This
, *ppProxy
, *ppvObj
, This
->PVtbl
, This
->base_proxy
, This
->base_object
);
238 static HRESULT WINAPI
StdProxy_QueryInterface(LPRPCPROXYBUFFER iface
,
242 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
243 TRACE("(%p)->QueryInterface(%s,%p)\n",This
,debugstr_guid(riid
),obj
);
245 if (IsEqualGUID(&IID_IUnknown
,riid
) ||
246 IsEqualGUID(This
->piid
,riid
)) {
248 InterlockedIncrement(&This
->RefCount
);
252 if (IsEqualGUID(&IID_IRpcProxyBuffer
,riid
)) {
253 *obj
= &This
->IRpcProxyBuffer_iface
;
254 InterlockedIncrement(&This
->RefCount
);
258 return E_NOINTERFACE
;
261 static ULONG WINAPI
StdProxy_AddRef(LPRPCPROXYBUFFER iface
)
263 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
264 TRACE("(%p)->AddRef()\n",This
);
266 return InterlockedIncrement(&This
->RefCount
);
269 static ULONG WINAPI
StdProxy_Release(LPRPCPROXYBUFFER iface
)
272 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
273 TRACE("(%p)->Release()\n",This
);
275 refs
= InterlockedDecrement(&This
->RefCount
);
279 IRpcProxyBuffer_Disconnect(&This
->IRpcProxyBuffer_iface
);
281 if (This
->base_object
) IUnknown_Release( This
->base_object
);
282 if (This
->base_proxy
) IRpcProxyBuffer_Release( This
->base_proxy
);
284 IPSFactoryBuffer_Release(This
->pPSFactory
);
285 HeapFree(GetProcessHeap(),0,This
);
291 static HRESULT WINAPI
StdProxy_Connect(LPRPCPROXYBUFFER iface
,
292 LPRPCCHANNELBUFFER pChannel
)
294 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
295 TRACE("(%p)->Connect(%p)\n",This
,pChannel
);
297 This
->pChannel
= pChannel
;
298 IRpcChannelBuffer_AddRef(pChannel
);
299 if (This
->base_proxy
) IRpcProxyBuffer_Connect( This
->base_proxy
, pChannel
);
303 static VOID WINAPI
StdProxy_Disconnect(LPRPCPROXYBUFFER iface
)
305 StdProxyImpl
*This
= impl_from_IRpcProxyBuffer(iface
);
306 TRACE("(%p)->Disconnect()\n",This
);
308 if (This
->base_proxy
) IRpcProxyBuffer_Disconnect( This
->base_proxy
);
310 IRpcChannelBuffer_Release(This
->pChannel
);
311 This
->pChannel
= NULL
;
314 static const IRpcProxyBufferVtbl StdProxy_Vtbl
=
316 StdProxy_QueryInterface
,
323 static void StdProxy_GetChannel(LPVOID iface
,
324 LPRPCCHANNELBUFFER
*ppChannel
)
326 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
327 TRACE("(%p)->GetChannel(%p) %s\n",This
,ppChannel
,This
->name
);
329 *ppChannel
= This
->pChannel
;
332 static void StdProxy_GetIID(LPVOID iface
,
335 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
336 TRACE("(%p)->GetIID(%p) %s\n",This
,ppiid
,This
->name
);
341 HRESULT WINAPI
IUnknown_QueryInterface_Proxy(LPUNKNOWN iface
,
345 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
346 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This
,debugstr_guid(riid
),ppvObj
,This
->name
);
347 return IUnknown_QueryInterface(This
->pUnkOuter
,riid
,ppvObj
);
350 ULONG WINAPI
IUnknown_AddRef_Proxy(LPUNKNOWN iface
)
352 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
353 TRACE("(%p)->AddRef() %s\n",This
,This
->name
);
354 return IUnknown_AddRef(This
->pUnkOuter
);
357 ULONG WINAPI
IUnknown_Release_Proxy(LPUNKNOWN iface
)
359 StdProxyImpl
*This
= impl_from_proxy_obj( iface
);
360 TRACE("(%p)->Release() %s\n",This
,This
->name
);
361 return IUnknown_Release(This
->pUnkOuter
);
364 /***********************************************************************
365 * NdrProxyInitialize [RPCRT4.@]
367 void WINAPI
NdrProxyInitialize(void *This
,
368 PRPC_MESSAGE pRpcMsg
,
369 PMIDL_STUB_MESSAGE pStubMsg
,
370 PMIDL_STUB_DESC pStubDescriptor
,
371 unsigned int ProcNum
)
373 TRACE("(%p,%p,%p,%p,%d)\n", This
, pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
374 NdrClientInitializeNew(pRpcMsg
, pStubMsg
, pStubDescriptor
, ProcNum
);
375 StdProxy_GetChannel(This
, &pStubMsg
->pRpcChannelBuffer
);
376 IRpcChannelBuffer_GetDestCtx(pStubMsg
->pRpcChannelBuffer
,
377 &pStubMsg
->dwDestContext
,
378 &pStubMsg
->pvDestContext
);
379 TRACE("channel=%p\n", pStubMsg
->pRpcChannelBuffer
);
382 /***********************************************************************
383 * NdrProxyGetBuffer [RPCRT4.@]
385 void WINAPI
NdrProxyGetBuffer(void *This
,
386 PMIDL_STUB_MESSAGE pStubMsg
)
389 const IID
*riid
= NULL
;
391 TRACE("(%p,%p)\n", This
, pStubMsg
);
392 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->BufferLength
;
393 pStubMsg
->dwStubPhase
= PROXY_GETBUFFER
;
394 StdProxy_GetIID(This
, &riid
);
395 hr
= IRpcChannelBuffer_GetBuffer(pStubMsg
->pRpcChannelBuffer
,
396 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
400 RpcRaiseException(hr
);
403 pStubMsg
->fBufferValid
= TRUE
;
404 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
405 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
406 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
407 pStubMsg
->dwStubPhase
= PROXY_MARSHAL
;
410 /***********************************************************************
411 * NdrProxySendReceive [RPCRT4.@]
413 void WINAPI
NdrProxySendReceive(void *This
,
414 PMIDL_STUB_MESSAGE pStubMsg
)
419 TRACE("(%p,%p)\n", This
, pStubMsg
);
421 if (!pStubMsg
->pRpcChannelBuffer
)
423 WARN("Trying to use disconnected proxy %p\n", This
);
424 RpcRaiseException(RPC_E_DISCONNECTED
);
427 pStubMsg
->dwStubPhase
= PROXY_SENDRECEIVE
;
428 /* avoid sending uninitialised parts of the buffer on the wire */
429 pStubMsg
->RpcMsg
->BufferLength
= pStubMsg
->Buffer
- (unsigned char *)pStubMsg
->RpcMsg
->Buffer
;
430 hr
= IRpcChannelBuffer_SendReceive(pStubMsg
->pRpcChannelBuffer
,
431 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
,
433 pStubMsg
->dwStubPhase
= PROXY_UNMARSHAL
;
434 pStubMsg
->BufferLength
= pStubMsg
->RpcMsg
->BufferLength
;
435 pStubMsg
->BufferStart
= pStubMsg
->RpcMsg
->Buffer
;
436 pStubMsg
->BufferEnd
= pStubMsg
->BufferStart
+ pStubMsg
->BufferLength
;
437 pStubMsg
->Buffer
= pStubMsg
->BufferStart
;
439 /* raise exception if call failed */
440 if (hr
== RPC_S_CALL_FAILED
) RpcRaiseException(*(DWORD
*)pStubMsg
->Buffer
);
441 else if (FAILED(hr
)) RpcRaiseException(hr
);
444 /***********************************************************************
445 * NdrProxyFreeBuffer [RPCRT4.@]
447 void WINAPI
NdrProxyFreeBuffer(void *This
,
448 PMIDL_STUB_MESSAGE pStubMsg
)
450 TRACE("(%p,%p)\n", This
, pStubMsg
);
452 if (pStubMsg
->fBufferValid
)
454 IRpcChannelBuffer_FreeBuffer(pStubMsg
->pRpcChannelBuffer
,
455 (RPCOLEMESSAGE
*)pStubMsg
->RpcMsg
);
456 pStubMsg
->fBufferValid
= TRUE
;
460 /***********************************************************************
461 * NdrProxyErrorHandler [RPCRT4.@]
463 HRESULT WINAPI
NdrProxyErrorHandler(DWORD dwExceptionCode
)
465 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode
);
467 if (FAILED(dwExceptionCode
))
468 return dwExceptionCode
;
470 return HRESULT_FROM_WIN32(dwExceptionCode
);
474 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo
, LPUNKNOWN pUnkOuter
, REFIID riid
,
475 LPRPCPROXYBUFFER
*ppProxy
, LPVOID
*ppv
)
477 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
478 HMODULE hUser32
= LoadLibraryA("user32");
479 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
481 FIXME("%p %p %s %p %p\n", pTypeInfo
, pUnkOuter
, debugstr_guid(riid
), ppProxy
, ppv
);
485 "The native implementation of OLEAUT32.DLL cannot be used "
486 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
487 "Wine: Unimplemented CreateProxyFromTypeInfo",
495 CreateStubFromTypeInfo(ITypeInfo
*pTypeInfo
, REFIID riid
, IUnknown
*pUnkServer
,
496 IRpcStubBuffer
**ppStub
)
498 typedef INT (WINAPI
*MessageBoxA
)(HWND
,LPCSTR
,LPCSTR
,UINT
);
499 HMODULE hUser32
= LoadLibraryA("user32");
500 MessageBoxA pMessageBoxA
= (void *)GetProcAddress(hUser32
, "MessageBoxA");
502 FIXME("%p %s %p %p\n", pTypeInfo
, debugstr_guid(riid
), pUnkServer
, ppStub
);
506 "The native implementation of OLEAUT32.DLL cannot be used "
507 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
508 "Wine: Unimplemented CreateProxyFromTypeInfo",