wined3d: Pass an IWineD3DBaseTextureImpl pointer to basetexture_get_lod().
[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   IRpcProxyBuffer IRpcProxyBuffer_iface;
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 static inline StdProxyImpl *impl_from_IRpcProxyBuffer(IRpcProxyBuffer *iface)
62 {
63     return CONTAINING_RECORD(iface, StdProxyImpl, IRpcProxyBuffer_iface);
64 }
65
66 static inline StdProxyImpl *impl_from_proxy_obj( void *iface )
67 {
68     return CONTAINING_RECORD(iface, StdProxyImpl, PVtbl);
69 }
70
71 #if defined(__i386__)
72
73 #include "pshpack1.h"
74
75 struct thunk {
76   BYTE mov_eax;
77   DWORD index;
78   BYTE jmp;
79   LONG handler;
80 };
81
82 #include "poppack.h"
83
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 */
95                   "addl %edx,%esp\n\t"
96                   "jmp *%ecx" );
97
98 HRESULT WINAPI ObjectStubless(DWORD *args)
99 {
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];
105
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]);
109
110     return NdrClientCall2(stubless->pStubDesc, fs, args + 2);
111 }
112
113 #define BLOCK_SIZE 1024
114 #define MAX_BLOCKS 64  /* 64k methods should be enough for anybody */
115
116 static const struct thunk *method_blocks[MAX_BLOCKS];
117
118 static const struct thunk *allocate_block( unsigned int num )
119 {
120     unsigned int i;
121     struct thunk *prev, *block;
122
123     block = VirtualAlloc( NULL, BLOCK_SIZE * sizeof(*block),
124                           MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
125     if (!block) return NULL;
126
127     for (i = 0; i < BLOCK_SIZE; i++)
128     {
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);
133     }
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 */
137     {
138         VirtualFree( block, 0, MEM_RELEASE );
139         block = prev;
140     }
141     return block;
142 }
143
144 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
145 {
146     const void **entry = (const void **)(vtbl + 1);
147     DWORD i, j;
148
149     if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
150     {
151         FIXME( "%u methods not supported\n", num );
152         return FALSE;
153     }
154     for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
155     {
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];
160     }
161     return TRUE;
162 }
163
164 #else  /* __i386__ */
165
166 static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
167 {
168     ERR("stubless proxies are not supported on this architecture\n");
169     return FALSE;
170 }
171
172 #endif  /* __i386__ */
173
174 HRESULT StdProxy_Construct(REFIID riid,
175                            LPUNKNOWN pUnkOuter,
176                            const ProxyFileInfo *ProxyInfo,
177                            int Index,
178                            LPPSFACTORYBUFFER pPSFactory,
179                            LPRPCPROXYBUFFER *ppProxy,
180                            LPVOID *ppvObj)
181 {
182   StdProxyImpl *This;
183   PCInterfaceName name = ProxyInfo->pNamesArray[Index];
184   CInterfaceProxyVtbl *vtbl = ProxyInfo->pProxyVtblList[Index];
185
186   TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
187
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 );
194   }
195
196   if (!IsEqualGUID(vtbl->header.piid, riid)) {
197     ERR("IID mismatch during proxy creation\n");
198     return RPC_E_UNEXPECTED;
199   }
200
201   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
202   if (!This) return E_OUTOFMEMORY;
203
204   if (!pUnkOuter) pUnkOuter = (IUnknown *)This;
205   This->IRpcProxyBuffer_iface.lpVtbl = &StdProxy_Vtbl;
206   This->PVtbl = vtbl->Vtbl;
207   /* one reference for the proxy */
208   This->RefCount = 1;
209   This->piid = vtbl->header.piid;
210   This->base_object = NULL;
211   This->base_proxy = NULL;
212   This->pUnkOuter = pUnkOuter;
213   This->name = name;
214   This->pPSFactory = pPSFactory;
215   This->pChannel = NULL;
216
217   if(ProxyInfo->pDelegatedIIDs && ProxyInfo->pDelegatedIIDs[Index])
218   {
219       HRESULT r = create_proxy( ProxyInfo->pDelegatedIIDs[Index], NULL,
220                                 &This->base_proxy, (void **)&This->base_object );
221       if (FAILED(r))
222       {
223           HeapFree( GetProcessHeap(), 0, This );
224           return r;
225       }
226   }
227
228   *ppProxy = &This->IRpcProxyBuffer_iface;
229   *ppvObj = &This->PVtbl;
230   IUnknown_AddRef((IUnknown *)*ppvObj);
231   IPSFactoryBuffer_AddRef(pPSFactory);
232
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 );
235   return S_OK;
236 }
237
238 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
239                                              REFIID riid,
240                                              LPVOID *obj)
241 {
242   StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
243   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
244
245   if (IsEqualGUID(&IID_IUnknown,riid) ||
246       IsEqualGUID(This->piid,riid)) {
247     *obj = &This->PVtbl;
248     InterlockedIncrement(&This->RefCount);
249     return S_OK;
250   }
251
252   if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
253     *obj = &This->IRpcProxyBuffer_iface;
254     InterlockedIncrement(&This->RefCount);
255     return S_OK;
256   }
257
258   return E_NOINTERFACE;
259 }
260
261 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
262 {
263   StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
264   TRACE("(%p)->AddRef()\n",This);
265
266   return InterlockedIncrement(&This->RefCount);
267 }
268
269 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
270 {
271   ULONG refs;
272   StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
273   TRACE("(%p)->Release()\n",This);
274
275   refs = InterlockedDecrement(&This->RefCount);
276   if (!refs)
277   {
278     if (This->pChannel)
279       IRpcProxyBuffer_Disconnect(&This->IRpcProxyBuffer_iface);
280
281     if (This->base_object) IUnknown_Release( This->base_object );
282     if (This->base_proxy) IRpcProxyBuffer_Release( This->base_proxy );
283
284     IPSFactoryBuffer_Release(This->pPSFactory);
285     HeapFree(GetProcessHeap(),0,This);
286   }
287
288   return refs;
289 }
290
291 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
292                                       LPRPCCHANNELBUFFER pChannel)
293 {
294   StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
295   TRACE("(%p)->Connect(%p)\n",This,pChannel);
296
297   This->pChannel = pChannel;
298   IRpcChannelBuffer_AddRef(pChannel);
299   if (This->base_proxy) IRpcProxyBuffer_Connect( This->base_proxy, pChannel );
300   return S_OK;
301 }
302
303 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
304 {
305   StdProxyImpl *This = impl_from_IRpcProxyBuffer(iface);
306   TRACE("(%p)->Disconnect()\n",This);
307
308   if (This->base_proxy) IRpcProxyBuffer_Disconnect( This->base_proxy );
309
310   IRpcChannelBuffer_Release(This->pChannel);
311   This->pChannel = NULL;
312 }
313
314 static const IRpcProxyBufferVtbl StdProxy_Vtbl =
315 {
316   StdProxy_QueryInterface,
317   StdProxy_AddRef,
318   StdProxy_Release,
319   StdProxy_Connect,
320   StdProxy_Disconnect
321 };
322
323 static void StdProxy_GetChannel(LPVOID iface,
324                                    LPRPCCHANNELBUFFER *ppChannel)
325 {
326   StdProxyImpl *This = impl_from_proxy_obj( iface );
327   TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
328
329   *ppChannel = This->pChannel;
330 }
331
332 static void StdProxy_GetIID(LPVOID iface,
333                                const IID **ppiid)
334 {
335   StdProxyImpl *This = impl_from_proxy_obj( iface );
336   TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
337
338   *ppiid = This->piid;
339 }
340
341 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
342                                             REFIID riid,
343                                             LPVOID *ppvObj)
344 {
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);
348 }
349
350 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
351 {
352   StdProxyImpl *This = impl_from_proxy_obj( iface );
353   TRACE("(%p)->AddRef() %s\n",This,This->name);
354   return IUnknown_AddRef(This->pUnkOuter);
355 }
356
357 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
358 {
359   StdProxyImpl *This = impl_from_proxy_obj( iface );
360   TRACE("(%p)->Release() %s\n",This,This->name);
361   return IUnknown_Release(This->pUnkOuter);
362 }
363
364 /***********************************************************************
365  *           NdrProxyInitialize [RPCRT4.@]
366  */
367 void WINAPI NdrProxyInitialize(void *This,
368                               PRPC_MESSAGE pRpcMsg,
369                               PMIDL_STUB_MESSAGE pStubMsg,
370                               PMIDL_STUB_DESC pStubDescriptor,
371                               unsigned int ProcNum)
372 {
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);
380 }
381
382 /***********************************************************************
383  *           NdrProxyGetBuffer [RPCRT4.@]
384  */
385 void WINAPI NdrProxyGetBuffer(void *This,
386                              PMIDL_STUB_MESSAGE pStubMsg)
387 {
388   HRESULT hr;
389   const IID *riid = NULL;
390
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,
397                                   riid);
398   if (FAILED(hr))
399   {
400     RpcRaiseException(hr);
401     return;
402   }
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;
408 }
409
410 /***********************************************************************
411  *           NdrProxySendReceive [RPCRT4.@]
412  */
413 void WINAPI NdrProxySendReceive(void *This,
414                                PMIDL_STUB_MESSAGE pStubMsg)
415 {
416   ULONG Status = 0;
417   HRESULT hr;
418
419   TRACE("(%p,%p)\n", This, pStubMsg);
420
421   if (!pStubMsg->pRpcChannelBuffer)
422   {
423     WARN("Trying to use disconnected proxy %p\n", This);
424     RpcRaiseException(RPC_E_DISCONNECTED);
425   }
426
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,
432                                     &Status);
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;
438
439   /* raise exception if call failed */
440   if (hr == RPC_S_CALL_FAILED) RpcRaiseException(*(DWORD*)pStubMsg->Buffer);
441   else if (FAILED(hr)) RpcRaiseException(hr);
442 }
443
444 /***********************************************************************
445  *           NdrProxyFreeBuffer [RPCRT4.@]
446  */
447 void WINAPI NdrProxyFreeBuffer(void *This,
448                               PMIDL_STUB_MESSAGE pStubMsg)
449 {
450   TRACE("(%p,%p)\n", This, pStubMsg);
451
452   if (pStubMsg->fBufferValid)
453   {
454     IRpcChannelBuffer_FreeBuffer(pStubMsg->pRpcChannelBuffer,
455                                  (RPCOLEMESSAGE*)pStubMsg->RpcMsg);
456     pStubMsg->fBufferValid = TRUE;
457   }
458 }
459
460 /***********************************************************************
461  *           NdrProxyErrorHandler [RPCRT4.@]
462  */
463 HRESULT WINAPI NdrProxyErrorHandler(DWORD dwExceptionCode)
464 {
465   WARN("(0x%08x): a proxy call failed\n", dwExceptionCode);
466
467   if (FAILED(dwExceptionCode))
468     return dwExceptionCode;
469   else
470     return HRESULT_FROM_WIN32(dwExceptionCode);
471 }
472
473 HRESULT WINAPI
474 CreateProxyFromTypeInfo( LPTYPEINFO pTypeInfo, LPUNKNOWN pUnkOuter, REFIID riid,
475                          LPRPCPROXYBUFFER *ppProxy, LPVOID *ppv )
476 {
477     typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
478     HMODULE hUser32 = LoadLibraryA("user32");
479     MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
480
481     FIXME("%p %p %s %p %p\n", pTypeInfo, pUnkOuter, debugstr_guid(riid), ppProxy, ppv);
482     if (pMessageBoxA)
483     {
484         pMessageBoxA(NULL,
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",
488             0x10);
489         ExitProcess(1);
490     }
491     return E_NOTIMPL;
492 }
493
494 HRESULT WINAPI
495 CreateStubFromTypeInfo(ITypeInfo *pTypeInfo, REFIID riid, IUnknown *pUnkServer,
496                        IRpcStubBuffer **ppStub )
497 {
498     typedef INT (WINAPI *MessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
499     HMODULE hUser32 = LoadLibraryA("user32");
500     MessageBoxA pMessageBoxA = (void *)GetProcAddress(hUser32, "MessageBoxA");
501
502     FIXME("%p %s %p %p\n", pTypeInfo, debugstr_guid(riid), pUnkServer, ppStub);
503     if (pMessageBoxA)
504     {
505         pMessageBoxA(NULL,
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",
509             0x10);
510         ExitProcess(1);
511     }
512     return E_NOTIMPL;
513 }