rpcrt4: Retrieve the stubless information for the thunks directly from the virtual...
[wine.git] / dlls / rpcrt4 / cproxy.c
blob887de870b0734fc73c6fa3ccf7ca3db3fd9c6918
1 /*
2 * COM proxy implementation
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * TODO: Handle non-i386 architectures
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
28 #define COBJMACROS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
34 #include "objbase.h"
35 #include "rpcproxy.h"
37 #include "cpsf.h"
38 #include "ndr_misc.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 /* I don't know what MS's std proxy structure looks like,
44 so this probably doesn't match, but that shouldn't matter */
45 typedef struct {
46 const IRpcProxyBufferVtbl *lpVtbl;
47 LPVOID *PVtbl;
48 LONG RefCount;
49 const IID* piid;
50 LPUNKNOWN pUnkOuter;
51 PCInterfaceName name;
52 LPPSFACTORYBUFFER pPSFactory;
53 LPRPCCHANNELBUFFER pChannel;
54 } StdProxyImpl;
56 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
58 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
60 #if defined(__i386__)
62 #include "pshpack1.h"
64 struct thunk {
65 BYTE push;
66 DWORD index;
67 BYTE jmp;
68 LONG handler;
71 #include "poppack.h"
73 extern void call_stubless_func(void);
74 __ASM_GLOBAL_FUNC(call_stubless_func,
75 "pushl %esp\n\t" /* pointer to index */
76 "call " __ASM_NAME("ObjectStubless") "\n\t"
77 "popl %edx\n\t" /* args size */
78 "movl (%esp),%ecx\n\t" /* return address */
79 "addl %edx,%esp\n\t"
80 "jmp *%ecx" );
82 HRESULT WINAPI ObjectStubless(DWORD *args)
84 DWORD index = args[0];
85 void **iface = (void **)args[2];
86 const void **vtbl = (const void **)*iface;
87 const MIDL_STUBLESS_PROXY_INFO *stubless = *(const MIDL_STUBLESS_PROXY_INFO **)(vtbl - 2);
88 const PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[index];
90 /* store bytes to remove from stack */
91 args[0] = *(const WORD*)(fs + 8);
92 TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface, index, args[0], args[1]);
94 return NdrClientCall2(stubless->pStubDesc, fs, args + 2);
97 #define BLOCK_SIZE 1024
98 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
100 static const struct thunk *method_blocks[MAX_BLOCKS];
102 static const struct thunk *allocate_block( unsigned int num )
104 unsigned int i;
105 struct thunk *prev, *block;
107 block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
108 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
109 if (!block) return NULL;
111 for (i = 0; i < BLOCK_SIZE; i++)
113 block[i].push = 0x68; /* pushl */
114 block[i].index = BLOCK_SIZE * num + i + 3;
115 block[i].jmp = 0xe9; /* jmp */
116 block[i].handler = (char *)call_stubless_func - (char *)(&block[i].handler + 1);
118 VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, NULL );
119 prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
120 if (prev) /* someone beat us to it */
122 VirtualFree( block, 0, MEM_RELEASE );
123 block = prev;
125 return block;
128 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
130 const void **entry = (const void **)(vtbl + 1);
131 DWORD i, j;
133 for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
135 const struct thunk *block = method_blocks[i];
136 if (!block && !(block = allocate_block( i ))) return FALSE;
137 for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
138 if (*entry == (LPVOID)-1) *entry = &block[j];
140 return TRUE;
143 #else /* __i386__ */
145 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
147 ERR("stubless proxies are not supported on this architecture\n");
148 return FALSE;
151 #endif /* __i386__ */
153 HRESULT StdProxy_Construct(REFIID riid,
154 LPUNKNOWN pUnkOuter,
155 const ProxyFileInfo *ProxyInfo,
156 int Index,
157 LPPSFACTORYBUFFER pPSFactory,
158 LPRPCPROXYBUFFER *ppProxy,
159 LPVOID *ppvObj)
161 StdProxyImpl *This;
162 PCInterfaceName name = ProxyInfo->pNamesArray[Index];
163 CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
165 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
167 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
168 if (ProxyInfo->TableVersion > 1) {
169 ULONG count = ProxyInfo->pStubVtblList[Index]->header.DispatchTableCount;
170 vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
171 TRACE("stubless vtbl %p: count=%d\n", vtbl->Vtbl, count );
172 fill_stubless_table( (IUnknownVtbl *)vtbl->Vtbl, count );
175 TRACE("iid=%s vtbl=%p\n", debugstr_guid(vtbl->header.piid), vtbl->Vtbl);
177 if (!IsEqualGUID(vtbl->header.piid, riid)) {
178 ERR("IID mismatch during proxy creation\n");
179 return RPC_E_UNEXPECTED;
182 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
183 if (!This) return E_OUTOFMEMORY;
185 if (!pUnkOuter) pUnkOuter = (IUnknown *)This;
186 This->lpVtbl = &StdProxy_Vtbl;
187 This->PVtbl = vtbl->Vtbl;
188 /* one reference for the proxy */
189 This->RefCount = 1;
190 This->piid = vtbl->header.piid;
191 This->pUnkOuter = pUnkOuter;
192 This->name = name;
193 This->pPSFactory = pPSFactory;
194 This->pChannel = NULL;
195 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
196 *ppvObj = &This->PVtbl;
197 IUnknown_AddRef((IUnknown *)*ppvObj);
198 IPSFactoryBuffer_AddRef(pPSFactory);
200 return S_OK;
203 static void StdProxy_Destruct(LPRPCPROXYBUFFER iface)
205 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
207 if (This->pChannel)
208 IRpcProxyBuffer_Disconnect(iface);
210 IPSFactoryBuffer_Release(This->pPSFactory);
211 HeapFree(GetProcessHeap(),0,This);
214 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
215 REFIID riid,
216 LPVOID *obj)
218 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
219 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
221 if (IsEqualGUID(&IID_IUnknown,riid) ||
222 IsEqualGUID(This->piid,riid)) {
223 *obj = &This->PVtbl;
224 InterlockedIncrement(&This->RefCount);
225 return S_OK;
228 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
229 *obj = &This->lpVtbl;
230 InterlockedIncrement(&This->RefCount);
231 return S_OK;
234 return E_NOINTERFACE;
237 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
239 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
240 TRACE("(%p)->AddRef()\n",This);
242 return InterlockedIncrement(&This->RefCount);
245 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
247 ULONG refs;
248 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
249 TRACE("(%p)->Release()\n",This);
251 refs = InterlockedDecrement(&This->RefCount);
252 if (!refs)
253 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
254 return refs;
257 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
258 LPRPCCHANNELBUFFER pChannel)
260 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
261 TRACE("(%p)->Connect(%p)\n",This,pChannel);
263 This->pChannel = pChannel;
264 IRpcChannelBuffer_AddRef(pChannel);
265 return S_OK;
268 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
270 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
271 TRACE("(%p)->Disconnect()\n",This);
273 IRpcChannelBuffer_Release(This->pChannel);
274 This->pChannel = NULL;
277 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
279 StdProxy_QueryInterface,
280 StdProxy_AddRef,
281 StdProxy_Release,
282 StdProxy_Connect,
283 StdProxy_Disconnect
286 static void StdProxy_GetChannel(LPVOID iface,
287 LPRPCCHANNELBUFFER *ppChannel)
289 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
290 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
292 *ppChannel = This->pChannel;
295 static void StdProxy_GetIID(LPVOID iface,
296 const IID **ppiid)
298 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
299 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
301 *ppiid = This->piid;
304 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
305 REFIID riid,
306 LPVOID *ppvObj)
308 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
309 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
310 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
313 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
315 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
316 TRACE("(%p)->AddRef() %s\n",This,This->name);
317 return IUnknown_AddRef(This->pUnkOuter);
320 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
322 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
323 TRACE("(%p)->Release() %s\n",This,This->name);
324 return IUnknown_Release(This->pUnkOuter);
327 /***********************************************************************
328 * NdrProxyInitialize [RPCRT4.@]
330 void WINAPI NdrProxyInitialize(void *This,
331 PRPC_MESSAGE pRpcMsg,
332 PMIDL_STUB_MESSAGE pStubMsg,
333 PMIDL_STUB_DESC pStubDescriptor,
334 unsigned int ProcNum)
336 TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
337 NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
338 StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
339 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
340 &pStubMsg->dwDestContext,
341 &pStubMsg->pvDestContext);
342 TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
345 /***********************************************************************
346 * NdrProxyGetBuffer [RPCRT4.@]
348 void WINAPI NdrProxyGetBuffer(void *This,
349 PMIDL_STUB_MESSAGE pStubMsg)
351 HRESULT hr;
352 const IID *riid = NULL;
354 TRACE("(%p,%p)\n", This, pStubMsg);
355 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
356 pStubMsg->dwStubPhase = PROXY_GETBUFFER;
357 StdProxy_GetIID(This, &riid);
358 hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
359 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
360 riid);
361 if (FAILED(hr))
363 RpcRaiseException(hr);
364 return;
366 pStubMsg->fBufferValid = TRUE;
367 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
368 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
369 pStubMsg->Buffer = pStubMsg->BufferStart;
370 pStubMsg->dwStubPhase = PROXY_MARSHAL;
373 /***********************************************************************
374 * NdrProxySendReceive [RPCRT4.@]
376 void WINAPI NdrProxySendReceive(void *This,
377 PMIDL_STUB_MESSAGE pStubMsg)
379 ULONG Status = 0;
380 HRESULT hr;
382 TRACE("(%p,%p)\n", This, pStubMsg);
384 if (!pStubMsg->pRpcChannelBuffer)
386 WARN("Trying to use disconnected proxy %p\n", This);
387 RpcRaiseException(RPC_E_DISCONNECTED);
390 pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
391 /* avoid sending uninitialised parts of the buffer on the wire */
392 pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
393 hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
394 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
395 &Status);
396 pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
397 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
398 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
399 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
400 pStubMsg->Buffer = pStubMsg->BufferStart;
402 /* raise exception if call failed */
403 if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
404 else if (FAILED(hr)) RpcRaiseException(hr);
407 /***********************************************************************
408 * NdrProxyFreeBuffer [RPCRT4.@]
410 void WINAPI NdrProxyFreeBuffer(void *This,
411 PMIDL_STUB_MESSAGE pStubMsg)
413 TRACE("(%p,%p)\n", This, pStubMsg);
415 if (pStubMsg->fBufferValid)
417 IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
418 (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
419 pStubMsg->fBufferValid = TRUE;
423 /***********************************************************************
424 * NdrProxyErrorHandler [RPCRT4.@]
426 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
428 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode);
430 if (FAILED(dwExceptionCode))
431 return dwExceptionCode;
432 else
433 return HRESULT_FROM_WIN32(dwExceptionCode);
436 HRESULT WINAPI
437 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
438 LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
440 typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
441 HMODULE hUser32 = LoadLibraryA("user32");
442 MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
444 FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
445 if (pMessageBoxA)
447 pMessageBoxA(NULL,
448 "The native implementation of OLEAUT32.DLL cannot be used "
449 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
450 "Wine: Unimplemented CreateProxyFromTypeInfo",
451 0x10);
452 ExitProcess(1);
454 return E_NOTIMPL;