rpcrt4: Use the same allocation technique as delegated stubs for the stubless thunks.
[wine.git] / dlls / rpcrt4 / cproxy.c
blob9854e81a0edae46b8058c11ecfe559284ede1ac5
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 MIDL_STUBLESS_PROXY_INFO *stubless;
50 const IID* piid;
51 LPUNKNOWN pUnkOuter;
52 PCInterfaceName name;
53 LPPSFACTORYBUFFER pPSFactory;
54 LPRPCCHANNELBUFFER pChannel;
55 } StdProxyImpl;
57 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
59 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
61 #if defined(__i386__)
63 #include "pshpack1.h"
65 struct thunk {
66 BYTE push;
67 DWORD index;
68 BYTE jmp;
69 LONG handler;
72 #include "poppack.h"
74 extern void call_stubless_func(void);
75 __ASM_GLOBAL_FUNC(call_stubless_func,
76 "pushl %esp\n\t" /* pointer to index */
77 "call " __ASM_NAME("ObjectStubless") "\n\t"
78 "popl %edx\n\t" /* args size */
79 "movl (%esp),%ecx\n\t" /* return address */
80 "addl %edx,%esp\n\t"
81 "jmp *%ecx" );
83 HRESULT WINAPI ObjectStubless(DWORD *args)
85 DWORD index = args[0];
86 LPVOID iface = (LPVOID)args[2];
88 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
90 const MIDL_STUBLESS_PROXY_INFO *stubless = This->stubless;
91 const PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[index];
93 /* store bytes to remove from stack */
94 args[0] = *(const WORD*)(fs + 8);
95 TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface, index, args[0], args[1]);
97 return NdrClientCall2(stubless->pStubDesc, fs, args + 2);
100 #define BLOCK_SIZE 1024
101 #define MAX_BLOCKS 64 /* 64k methods should be enough for anybody */
103 static const struct thunk *method_blocks[MAX_BLOCKS];
105 static const struct thunk *allocate_block( unsigned int num )
107 unsigned int i;
108 struct thunk *prev, *block;
110 block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
111 MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
112 if (!block) return NULL;
114 for (i = 0; i < BLOCK_SIZE; i++)
116 block[i].push = 0x68; /* pushl */
117 block[i].index = BLOCK_SIZE * num + i + 3;
118 block[i].jmp = 0xe9; /* jmp */
119 block[i].handler = (char *)call_stubless_func - (char *)(&block[i].handler + 1);
121 VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, NULL );
122 prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
123 if (prev) /* someone beat us to it */
125 VirtualFree( block, 0, MEM_RELEASE );
126 block = prev;
128 return block;
131 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
133 const void **entry = (const void **)(vtbl + 1);
134 DWORD i, j;
136 for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
138 const struct thunk *block = method_blocks[i];
139 if (!block && !(block = allocate_block( i ))) return FALSE;
140 for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
141 if (*entry == (LPVOID)-1) *entry = &block[j];
143 return TRUE;
146 #else /* __i386__ */
148 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
150 ERR("stubless proxies are not supported on this architecture\n");
151 return FALSE;
154 #endif /* __i386__ */
156 HRESULT StdProxy_Construct(REFIID riid,
157 LPUNKNOWN pUnkOuter,
158 const ProxyFileInfo *ProxyInfo,
159 int Index,
160 LPPSFACTORYBUFFER pPSFactory,
161 LPRPCPROXYBUFFER *ppProxy,
162 LPVOID *ppvObj)
164 StdProxyImpl *This;
165 const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
166 PCInterfaceName name = ProxyInfo->pNamesArray[Index];
167 CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
169 TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
171 /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
172 if (ProxyInfo->TableVersion > 1) {
173 ULONG count = ProxyInfo->pStubVtblList[Index]->header.DispatchTableCount;
174 stubless = *(const void **)vtbl;
175 vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
176 TRACE("stubless %p vtbl %p: count=%d\n", stubless, vtbl->Vtbl, count );
177 fill_stubless_table( (IUnknownVtbl *)vtbl->Vtbl, count );
180 TRACE("iid=%s vtbl=%p\n", debugstr_guid(vtbl->header.piid), vtbl->Vtbl);
182 if (!IsEqualGUID(vtbl->header.piid, riid)) {
183 ERR("IID mismatch during proxy creation\n");
184 return RPC_E_UNEXPECTED;
187 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
188 if (!This) return E_OUTOFMEMORY;
190 if (!pUnkOuter) pUnkOuter = (IUnknown *)This;
191 This->lpVtbl = &StdProxy_Vtbl;
192 This->PVtbl = vtbl->Vtbl;
193 /* one reference for the proxy */
194 This->RefCount = 1;
195 This->stubless = stubless;
196 This->piid = vtbl->header.piid;
197 This->pUnkOuter = pUnkOuter;
198 This->name = name;
199 This->pPSFactory = pPSFactory;
200 This->pChannel = NULL;
201 *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
202 *ppvObj = &This->PVtbl;
203 IUnknown_AddRef((IUnknown *)*ppvObj);
204 IPSFactoryBuffer_AddRef(pPSFactory);
206 return S_OK;
209 static void StdProxy_Destruct(LPRPCPROXYBUFFER iface)
211 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
213 if (This->pChannel)
214 IRpcProxyBuffer_Disconnect(iface);
216 IPSFactoryBuffer_Release(This->pPSFactory);
217 HeapFree(GetProcessHeap(),0,This);
220 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
221 REFIID riid,
222 LPVOID *obj)
224 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
225 TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
227 if (IsEqualGUID(&IID_IUnknown,riid) ||
228 IsEqualGUID(This->piid,riid)) {
229 *obj = &This->PVtbl;
230 InterlockedIncrement(&This->RefCount);
231 return S_OK;
234 if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
235 *obj = &This->lpVtbl;
236 InterlockedIncrement(&This->RefCount);
237 return S_OK;
240 return E_NOINTERFACE;
243 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
245 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
246 TRACE("(%p)->AddRef()\n",This);
248 return InterlockedIncrement(&This->RefCount);
251 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
253 ULONG refs;
254 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
255 TRACE("(%p)->Release()\n",This);
257 refs = InterlockedDecrement(&This->RefCount);
258 if (!refs)
259 StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
260 return refs;
263 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
264 LPRPCCHANNELBUFFER pChannel)
266 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
267 TRACE("(%p)->Connect(%p)\n",This,pChannel);
269 This->pChannel = pChannel;
270 IRpcChannelBuffer_AddRef(pChannel);
271 return S_OK;
274 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
276 ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
277 TRACE("(%p)->Disconnect()\n",This);
279 IRpcChannelBuffer_Release(This->pChannel);
280 This->pChannel = NULL;
283 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
285 StdProxy_QueryInterface,
286 StdProxy_AddRef,
287 StdProxy_Release,
288 StdProxy_Connect,
289 StdProxy_Disconnect
292 static void StdProxy_GetChannel(LPVOID iface,
293 LPRPCCHANNELBUFFER *ppChannel)
295 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
296 TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
298 *ppChannel = This->pChannel;
301 static void StdProxy_GetIID(LPVOID iface,
302 const IID **ppiid)
304 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
305 TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
307 *ppiid = This->piid;
310 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
311 REFIID riid,
312 LPVOID *ppvObj)
314 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
315 TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
316 return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
319 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
321 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
322 TRACE("(%p)->AddRef() %s\n",This,This->name);
323 return IUnknown_AddRef(This->pUnkOuter);
326 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
328 ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
329 TRACE("(%p)->Release() %s\n",This,This->name);
330 return IUnknown_Release(This->pUnkOuter);
333 /***********************************************************************
334 * NdrProxyInitialize [RPCRT4.@]
336 void WINAPI NdrProxyInitialize(void *This,
337 PRPC_MESSAGE pRpcMsg,
338 PMIDL_STUB_MESSAGE pStubMsg,
339 PMIDL_STUB_DESC pStubDescriptor,
340 unsigned int ProcNum)
342 TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
343 NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
344 StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
345 IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
346 &pStubMsg->dwDestContext,
347 &pStubMsg->pvDestContext);
348 TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
351 /***********************************************************************
352 * NdrProxyGetBuffer [RPCRT4.@]
354 void WINAPI NdrProxyGetBuffer(void *This,
355 PMIDL_STUB_MESSAGE pStubMsg)
357 HRESULT hr;
358 const IID *riid = NULL;
360 TRACE("(%p,%p)\n", This, pStubMsg);
361 pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
362 pStubMsg->dwStubPhase = PROXY_GETBUFFER;
363 StdProxy_GetIID(This, &riid);
364 hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
365 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
366 riid);
367 if (FAILED(hr))
369 RpcRaiseException(hr);
370 return;
372 pStubMsg->fBufferValid = TRUE;
373 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
374 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
375 pStubMsg->Buffer = pStubMsg->BufferStart;
376 pStubMsg->dwStubPhase = PROXY_MARSHAL;
379 /***********************************************************************
380 * NdrProxySendReceive [RPCRT4.@]
382 void WINAPI NdrProxySendReceive(void *This,
383 PMIDL_STUB_MESSAGE pStubMsg)
385 ULONG Status = 0;
386 HRESULT hr;
388 TRACE("(%p,%p)\n", This, pStubMsg);
390 if (!pStubMsg->pRpcChannelBuffer)
392 WARN("Trying to use disconnected proxy %p\n", This);
393 RpcRaiseException(RPC_E_DISCONNECTED);
396 pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
397 /* avoid sending uninitialised parts of the buffer on the wire */
398 pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
399 hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
400 (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
401 &Status);
402 pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
403 pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
404 pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
405 pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
406 pStubMsg->Buffer = pStubMsg->BufferStart;
408 /* raise exception if call failed */
409 if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
410 else if (FAILED(hr)) RpcRaiseException(hr);
413 /***********************************************************************
414 * NdrProxyFreeBuffer [RPCRT4.@]
416 void WINAPI NdrProxyFreeBuffer(void *This,
417 PMIDL_STUB_MESSAGE pStubMsg)
419 TRACE("(%p,%p)\n", This, pStubMsg);
421 if (pStubMsg->fBufferValid)
423 IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
424 (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
425 pStubMsg->fBufferValid = TRUE;
429 /***********************************************************************
430 * NdrProxyErrorHandler [RPCRT4.@]
432 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
434 WARN("(0x%08x): a proxy call failed\n", dwExceptionCode);
436 if (FAILED(dwExceptionCode))
437 return dwExceptionCode;
438 else
439 return HRESULT_FROM_WIN32(dwExceptionCode);
442 HRESULT WINAPI
443 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
444 LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
446 typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
447 HMODULE hUser32 = LoadLibraryA("user32");
448 MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
450 FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
451 if (pMessageBoxA)
453 pMessageBoxA(NULL,
454 "The native implementation of OLEAUT32.DLL cannot be used "
455 "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
456 "Wine: Unimplemented CreateProxyFromTypeInfo",
457 0x10);
458 ExitProcess(1);
460 return E_NOTIMPL;