rpcrt4: Fix tests on win64.
[wine] / dlls / rpcrt4 / cproxy.c
1 /*
2  * COM proxy implementation
3  *
4  * Copyright 2001 Ove Kåven, TransGaming Technologies
5  * Copyright 2009 Alexandre Julliard
6  *
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.
11  *
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.
16  *
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
20  * 
21  * TODO: Handle non-i386 architectures
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28
29 #define COBJMACROS
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34
35 #include "objbase.h"
36 #include "rpcproxy.h"
37
38 #include "cpsf.h"
39 #include "ndr_misc.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43
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 */
46 typedef struct {
47   const IRpcProxyBufferVtbl *lpVtbl;
48   LPVOID *PVtbl;
49   LONG RefCount;
50   const IID* piid;
51   LPUNKNOWN pUnkOuter;
52   IUnknown *base_object;  /* must be at offset 0x10 from PVtbl */
53   IRpcProxyBuffer *base_proxy;
54   PCInterfaceName name;
55   LPPSFACTORYBUFFER pPSFactory;
56   LPRPCCHANNELBUFFER pChannel;
57 } StdProxyImpl;
58
59 static const IRpcProxyBufferVtbl StdProxy_Vtbl;
60
61 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
62
63 #if defined(__i386__)
64
65 #include "pshpack1.h"
66
67 struct thunk {
68   BYTE push;
69   DWORD index;
70   BYTE jmp;
71   LONG handler;
72 };
73
74 #include "poppack.h"
75
76 extern void call_stubless_func(void);
77 __ASM_GLOBAL_FUNC(call_stubless_func,
78                   "pushl %esp\n\t"  /* pointer to index */
79                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
80                   "call " __ASM_NAME("ObjectStubless") __ASM_STDCALL(4) "\n\t"
81                   "popl %edx\n\t"  /* args size */
82                   __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
83                   "movl (%esp),%ecx\n\t"  /* return address */
84                   "addl %edx,%esp\n\t"
85                   "jmp *%ecx" );
86
87 HRESULT WINAPI ObjectStubless(DWORD *args)
88 {
89     DWORD index = args[0];
90     void **iface = (void **)args[2];
91     const void **vtbl = (const void **)*iface;
92     const MIDL_STUBLESS_PROXY_INFO *stubless = *(const MIDL_STUBLESS_PROXY_INFO **)(vtbl - 2);
93     const PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[index];
94
95     /* store bytes to remove from stack */
96     args[0] = *(const WORD*)(fs + 8);
97     TRACE("(%p)->(%d)([%d bytes]) ret=%08x\n", iface, index, args[0], args[1]);
98
99     return NdrClientCall2(stubless->pStubDesc, fs, args + 2);
100 }
101
102 #define BLOCK_SIZE 1024
103 #define MAX_BLOCKS 64  /* 64k methods should be enough for anybody */
104
105 static const struct thunk *method_blocks[MAX_BLOCKS];
106
107 static const struct thunk *allocate_block( unsigned int num )
108 {
109     unsigned int i;
110     struct thunk *prev, *block;
111
112     block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
113                           MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
114     if (!block) return NULL;
115
116     for (i = 0; i < BLOCK_SIZE; i++)
117     {
118         block[i].push    = 0x68; /* pushl */
119         block[i].index   = BLOCK_SIZE * num + i + 3;
120         block[i].jmp     = 0xe9; /* jmp */
121         block[i].handler = (char *)call_stubless_func - (char *)(&block[i].handler + 1);
122     }
123     VirtualProtect( block, BLOCK_SIZE * sizeof(*block), PAGE_EXECUTE_READ, NULL );
124     prev = InterlockedCompareExchangePointer( (void **)&method_blocks[num], block, NULL );
125     if (prev) /* someone beat us to it */
126     {
127         VirtualFree( block, 0, MEM_RELEASE );
128         block = prev;
129     }
130     return block;
131 }
132
133 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
134 {
135     const void **entry = (const void **)(vtbl + 1);
136     DWORD i, j;
137
138     if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
139     {
140         FIXME( "%u methods not supported\n", num );
141         return FALSE;
142     }
143     for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
144     {
145         const struct thunk *block = method_blocks[i];
146         if (!block && !(block = allocate_block( i ))) return FALSE;
147         for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
148             if (*entry == (LPVOID)-1) *entry = &block[j];
149     }
150     return TRUE;
151 }
152
153 #else  /* __i386__ */
154
155 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
156 {
157     ERR("stubless proxies are not supported on this architecture\n");
158     return FALSE;
159 }
160
161 #endif  /* __i386__ */
162
163 HRESULT StdProxy_Construct(REFIID riid,
164                            LPUNKNOWN pUnkOuter,
165                            const ProxyFileInfo *ProxyInfo,
166                            int Index,
167                            LPPSFACTORYBUFFER pPSFactory,
168                            LPRPCPROXYBUFFER *ppProxy,
169                            LPVOID *ppvObj)
170 {
171   StdProxyImpl *This;
172   PCInterfaceName name = ProxyInfo->pNamesArray[Index];
173   CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
174
175   TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
176
177   /* TableVersion = 2 means it is the stubless version of CInterfaceProxyVtbl */
178   if (ProxyInfo->TableVersion > 1) {
179     ULONG count = ProxyInfo->pStubVtblList[Index]->header.DispatchTableCount;
180     vtbl = (CInterfaceProxyVtbl *)((const void **)vtbl + 1);
181     TRACE("stubless vtbl %p: count=%d\n", vtbl->Vtbl, count );
182     fill_stubless_table( (IUnknownVtbl *)vtbl->Vtbl, count );
183   }
184
185   if (!IsEqualGUID(vtbl->header.piid, riid)) {
186     ERR("IID mismatch during proxy creation\n");
187     return RPC_E_UNEXPECTED;
188   }
189
190   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
191   if (!This) return E_OUTOFMEMORY;
192
193   if (!pUnkOuter) pUnkOuter = (IUnknown *)This;
194   This->lpVtbl = &StdProxy_Vtbl;
195   This->PVtbl = vtbl->Vtbl;
196   /* one reference for the proxy */
197   This->RefCount = 1;
198   This->piid = vtbl->header.piid;
199   This->base_object = NULL;
200   This->base_proxy = NULL;
201   This->pUnkOuter = pUnkOuter;
202   This->name = name;
203   This->pPSFactory = pPSFactory;
204   This->pChannel = NULL;
205
206   if(ProxyInfo->pDelegatedIIDs && ProxyInfo->pDelegatedIIDs[Index])
207   {
208       HRESULT r = create_proxy( ProxyInfo->pDelegatedIIDs[Index], NULL,
209                                 &This->base_proxy, (void **)&This->base_object );
210       if (FAILED(r))
211       {
212           HeapFree( GetProcessHeap(), 0, This );
213           return r;
214       }
215   }
216
217   *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
218   *ppvObj = &This->PVtbl;
219   IUnknown_AddRef((IUnknown *)*ppvObj);
220   IPSFactoryBuffer_AddRef(pPSFactory);
221
222   TRACE( "iid=%s this %p proxy %p obj %p vtbl %p base proxy %p base obj %p\n",
223          debugstr_guid(riid), This, *ppProxy, *ppvObj, This->PVtbl, This->base_proxy, This->base_object );
224   return S_OK;
225 }
226
227 static void StdProxy_Destruct(LPRPCPROXYBUFFER iface)
228 {
229   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
230
231   if (This->pChannel)
232     IRpcProxyBuffer_Disconnect(iface);
233
234   if (This->base_object) IUnknown_Release( This->base_object );
235   if (This->base_proxy) IRpcProxyBuffer_Release( This->base_proxy );
236
237   IPSFactoryBuffer_Release(This->pPSFactory);
238   HeapFree(GetProcessHeap(),0,This);
239 }
240
241 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
242                                              REFIID riid,
243                                              LPVOID *obj)
244 {
245   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
246   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
247
248   if (IsEqualGUID(&IID_IUnknown,riid) ||
249       IsEqualGUID(This->piid,riid)) {
250     *obj = &This->PVtbl;
251     InterlockedIncrement(&This->RefCount);
252     return S_OK;
253   }
254
255   if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
256     *obj = &This->lpVtbl;
257     InterlockedIncrement(&This->RefCount);
258     return S_OK;
259   }
260
261   return E_NOINTERFACE;
262 }
263
264 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
265 {
266   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
267   TRACE("(%p)->AddRef()\n",This);
268
269   return InterlockedIncrement(&This->RefCount);
270 }
271
272 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
273 {
274   ULONG refs;
275   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
276   TRACE("(%p)->Release()\n",This);
277
278   refs = InterlockedDecrement(&This->RefCount);
279   if (!refs)
280     StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
281   return refs;
282 }
283
284 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
285                                       LPRPCCHANNELBUFFER pChannel)
286 {
287   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
288   TRACE("(%p)->Connect(%p)\n",This,pChannel);
289
290   This->pChannel = pChannel;
291   IRpcChannelBuffer_AddRef(pChannel);
292   if (This->base_proxy) IRpcProxyBuffer_Connect( This->base_proxy, pChannel );
293   return S_OK;
294 }
295
296 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
297 {
298   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
299   TRACE("(%p)->Disconnect()\n",This);
300
301   if (This->base_proxy) IRpcProxyBuffer_Disconnect( This->base_proxy );
302
303   IRpcChannelBuffer_Release(This->pChannel);
304   This->pChannel = NULL;
305 }
306
307 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
308 {
309   StdProxy_QueryInterface,
310   StdProxy_AddRef,
311   StdProxy_Release,
312   StdProxy_Connect,
313   StdProxy_Disconnect
314 };
315
316 static void StdProxy_GetChannel(LPVOID iface,
317                                    LPRPCCHANNELBUFFER *ppChannel)
318 {
319   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
320   TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
321
322   *ppChannel = This->pChannel;
323 }
324
325 static void StdProxy_GetIID(LPVOID iface,
326                                const IID **ppiid)
327 {
328   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
329   TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
330
331   *ppiid = This->piid;
332 }
333
334 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
335                                             REFIID riid,
336                                             LPVOID *ppvObj)
337 {
338   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
339   TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
340   return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
341 }
342
343 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
344 {
345   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
346   TRACE("(%p)->AddRef() %s\n",This,This->name);
347   return IUnknown_AddRef(This->pUnkOuter);
348 }
349
350 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
351 {
352   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
353   TRACE("(%p)->Release() %s\n",This,This->name);
354   return IUnknown_Release(This->pUnkOuter);
355 }
356
357 /***********************************************************************
358  *           NdrProxyInitialize [RPCRT4.@]
359  */
360 void WINAPI NdrProxyInitialize(void *This,
361                               PRPC_MESSAGE pRpcMsg,
362                               PMIDL_STUB_MESSAGE pStubMsg,
363                               PMIDL_STUB_DESC pStubDescriptor,
364                               unsigned int ProcNum)
365 {
366   TRACE("(%p,%p,%p,%p,%d)\n", This, pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
367   NdrClientInitializeNew(pRpcMsg, pStubMsg, pStubDescriptor, ProcNum);
368   StdProxy_GetChannel(This, &pStubMsg->pRpcChannelBuffer);
369   IRpcChannelBuffer_GetDestCtx(pStubMsg->pRpcChannelBuffer,
370                                &pStubMsg->dwDestContext,
371                                &pStubMsg->pvDestContext);
372   TRACE("channel=%p\n", pStubMsg->pRpcChannelBuffer);
373 }
374
375 /***********************************************************************
376  *           NdrProxyGetBuffer [RPCRT4.@]
377  */
378 void WINAPI NdrProxyGetBuffer(void *This,
379                              PMIDL_STUB_MESSAGE pStubMsg)
380 {
381   HRESULT hr;
382   const IID *riid = NULL;
383
384   TRACE("(%p,%p)\n", This, pStubMsg);
385   pStubMsg->RpcMsg->BufferLength = pStubMsg->BufferLength;
386   pStubMsg->dwStubPhase = PROXY_GETBUFFER;
387   StdProxy_GetIID(This, &riid);
388   hr = IRpcChannelBuffer_GetBuffer(pStubMsg->pRpcChannelBuffer,
389                                   (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
390                                   riid);
391   if (FAILED(hr))
392   {
393     RpcRaiseException(hr);
394     return;
395   }
396   pStubMsg->fBufferValid = TRUE;
397   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
398   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
399   pStubMsg->Buffer = pStubMsg->BufferStart;
400   pStubMsg->dwStubPhase = PROXY_MARSHAL;
401 }
402
403 /***********************************************************************
404  *           NdrProxySendReceive [RPCRT4.@]
405  */
406 void WINAPI NdrProxySendReceive(void *This,
407                                PMIDL_STUB_MESSAGE pStubMsg)
408 {
409   ULONG Status = 0;
410   HRESULT hr;
411
412   TRACE("(%p,%p)\n", This, pStubMsg);
413
414   if (!pStubMsg->pRpcChannelBuffer)
415   {
416     WARN("Trying to use disconnected proxy %p\n", This);
417     RpcRaiseException(RPC_E_DISCONNECTED);
418   }
419
420   pStubMsg->dwStubPhase = PROXY_SENDRECEIVE;
421   /* avoid sending uninitialised parts of the buffer on the wire */
422   pStubMsg->RpcMsg->BufferLength = pStubMsg->Buffer - (unsigned char *)pStubMsg->RpcMsg->Buffer;
423   hr = IRpcChannelBuffer_SendReceive(pStubMsg->pRpcChannelBuffer,
424                                     (RPCOLEMESSAGE*)pStubMsg->RpcMsg,
425                                     &Status);
426   pStubMsg->dwStubPhase = PROXY_UNMARSHAL;
427   pStubMsg->BufferLength = pStubMsg->RpcMsg->BufferLength;
428   pStubMsg->BufferStart = pStubMsg->RpcMsg->Buffer;
429   pStubMsg->BufferEnd = pStubMsg->BufferStart + pStubMsg->BufferLength;
430   pStubMsg->Buffer = pStubMsg->BufferStart;
431
432   /* raise exception if call failed */
433   if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
434   else if (FAILED(hr)) RpcRaiseException(hr);
435 }
436
437 /***********************************************************************
438  *           NdrProxyFreeBuffer [RPCRT4.@]
439  */
440 void WINAPI NdrProxyFreeBuffer(void *This,
441                               PMIDL_STUB_MESSAGE pStubMsg)
442 {
443   TRACE("(%p,%p)\n", This, pStubMsg);
444
445   if (pStubMsg->fBufferValid)
446   {
447     IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
448                                  (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
449     pStubMsg->fBufferValid = TRUE;
450   }
451 }
452
453 /***********************************************************************
454  *           NdrProxyErrorHandler [RPCRT4.@]
455  */
456 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
457 {
458   WARN("(0x%08x): a proxy call failed\n", dwExceptionCode);
459
460   if (FAILED(dwExceptionCode))
461     return dwExceptionCode;
462   else
463     return HRESULT_FROM_WIN32(dwExceptionCode);
464 }
465
466 HRESULT WINAPI
467 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
468                          LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
469 {
470     typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
471     HMODULE hUser32 = LoadLibraryA("user32");
472     MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
473
474     FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
475     if (pMessageBoxA)
476     {
477         pMessageBoxA(NULL,
478             "The native implementation of OLEAUT32.DLL cannot be used "
479             "with Wine's RPCRT4.DLL. Remove OLEAUT32.DLL and try again.\n",
480             "Wine: Unimplemented CreateProxyFromTypeInfo",
481             0x10);
482         ExitProcess(1);
483     }
484     return E_NOTIMPL;
485 }