MSVCRT___RTDynamicCast: Reject a NULL cppobj.
[wine] / dlls / rpcrt4 / cproxy.c
1 /*
2  * COM proxy implementation
3  *
4  * Copyright 2001 Ove Kåven, TransGaming Technologies
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  * TODO: Handle non-i386 architectures
21  *       Get rid of #if 0'ed code.
22  */
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29
30 #include "objbase.h"
31 #include "rpcproxy.h"
32
33 #include "cpsf.h"
34 #include "ndr_misc.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ole);
38
39 struct StublessThunk;
40
41 /* I don't know what MS's std proxy structure looks like,
42    so this probably doesn't match, but that shouldn't matter */
43 typedef struct {
44   ICOM_VTABLE(IRpcProxyBuffer) *lpVtbl;
45   LPVOID *PVtbl;
46   DWORD RefCount;
47   const MIDL_STUBLESS_PROXY_INFO *stubless;
48   const IID* piid;
49   LPUNKNOWN pUnkOuter;
50   PCInterfaceName name;
51   LPPSFACTORYBUFFER pPSFactory;
52   LPRPCCHANNELBUFFER pChannel;
53   struct StublessThunk *thunks;
54 } StdProxyImpl;
55
56 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl;
57
58 /* How the Windows stubless proxy thunks work is explained at
59  * http://msdn.microsoft.com/library/en-us/dnmsj99/html/com0199.asp,
60  * but I'll use a slightly different method, to make life easier */
61
62 #if defined(__i386__)
63
64 struct StublessThunk {
65   BYTE push WINE_PACKED;
66   DWORD index WINE_PACKED;
67   BYTE call WINE_PACKED;
68   LONG handler WINE_PACKED;
69   BYTE ret WINE_PACKED;
70   WORD bytes WINE_PACKED;
71   BYTE pad[3];
72 };
73
74 /* adjust the stack size since we don't use Windows's method */
75 #define STACK_ADJUST sizeof(DWORD)
76
77 #define FILL_STUBLESS(x,idx,stk) \
78  x->push = 0x68; /* pushl [immediate] */ \
79  x->index = (idx); \
80  x->call = 0xe8; /* call [near] */ \
81  x->handler = (char*)ObjectStubless - (char*)&x->ret; \
82  x->ret = 0xc2; /* ret [immediate] */ \
83  x->bytes = stk; \
84  x->pad[0] = 0x8d; /* leal (%esi),%esi */ \
85  x->pad[1] = 0x76; \
86  x->pad[2] = 0x00;
87
88 static HRESULT WINAPI ObjectStubless(DWORD index)
89 {
90   char *args = (char*)(&index + 2);
91   LPVOID iface = *(LPVOID*)args;
92
93   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
94
95   PFORMAT_STRING fs = This->stubless->ProcFormatString + This->stubless->FormatStringOffset[index];
96   unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
97   TRACE("(%p)->(%ld)([%d bytes]) ret=%08lx\n", iface, index, bytes, *(DWORD*)(args+bytes));
98
99   return RPCRT4_NdrClientCall2(This->stubless->pStubDesc, fs, args);
100 }
101
102 #else  /* __i386__ */
103
104 /* can't do that on this arch */
105 struct StublessThunk { int dummy; };
106 #define FILL_STUBLESS(x,idx,stk) \
107  ERR("stubless proxies are not supported on this architecture\n");
108 #define STACK_ADJUST 0
109
110 #endif  /* __i386__ */
111
112 HRESULT WINAPI StdProxy_Construct(REFIID riid,
113                                  LPUNKNOWN pUnkOuter,
114                                  PCInterfaceName name,
115                                  CInterfaceProxyVtbl *vtbl,
116                                  CInterfaceStubVtbl *svtbl,
117                                  LPPSFACTORYBUFFER pPSFactory,
118                                  LPRPCPROXYBUFFER *ppProxy,
119                                  LPVOID *ppvObj)
120 {
121   StdProxyImpl *This;
122   const MIDL_STUBLESS_PROXY_INFO *stubless = NULL;
123
124   TRACE("(%p,%p,%p,%p,%p) %s\n", pUnkOuter, vtbl, pPSFactory, ppProxy, ppvObj, name);
125
126   /* I can't find any other way to detect stubless proxies than this hack */
127   if (!IsEqualGUID(vtbl->header.piid, riid)) {
128     stubless = *((const void **)vtbl)++;
129     TRACE("stubless=%p\n", stubless);
130   }
131
132   TRACE("iid=%s\n", debugstr_guid(vtbl->header.piid));
133   TRACE("vtbl=%p\n", vtbl->Vtbl);
134
135   if (!IsEqualGUID(vtbl->header.piid, riid)) {
136     ERR("IID mismatch during proxy creation\n");
137     return RPC_E_UNEXPECTED;
138   }
139
140   This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(StdProxyImpl));
141   if (!This) return E_OUTOFMEMORY;
142
143   if (stubless) {
144     unsigned i, count = svtbl->header.DispatchTableCount;
145     /* Maybe the original vtbl is just modified directly to point at
146      * ObjectStublessClientXXX thunks in real Windows, but I don't like it
147      */
148     TRACE("stubless thunks: count=%d\n", count);
149     This->thunks = HeapAlloc(GetProcessHeap(),0,sizeof(struct StublessThunk)*count);
150     This->PVtbl = HeapAlloc(GetProcessHeap(),0,sizeof(LPVOID)*count);
151     for (i=0; i<count; i++) {
152       struct StublessThunk *thunk = &This->thunks[i];
153       if (vtbl->Vtbl[i] == (LPVOID)-1) {
154         PFORMAT_STRING fs = stubless->ProcFormatString + stubless->FormatStringOffset[i];
155         unsigned bytes = *(WORD*)(fs+8) - STACK_ADJUST;
156         TRACE("method %d: stacksize=%d\n", i, bytes);
157         FILL_STUBLESS(thunk, i, bytes)
158         This->PVtbl[i] = thunk;
159       }
160       else {
161         memset(thunk, 0, sizeof(struct StublessThunk));
162         This->PVtbl[i] = vtbl->Vtbl[i];
163       }
164     }
165   }
166   else 
167     This->PVtbl = vtbl->Vtbl;
168
169   This->lpVtbl = &StdProxy_Vtbl;
170   This->RefCount = 1;
171   This->stubless = stubless;
172   This->piid = vtbl->header.piid;
173   This->pUnkOuter = pUnkOuter;
174   This->name = name;
175   This->pPSFactory = pPSFactory;
176   This->pChannel = NULL;
177   *ppProxy = (LPRPCPROXYBUFFER)&This->lpVtbl;
178   *ppvObj = &This->PVtbl;
179   IPSFactoryBuffer_AddRef(pPSFactory);
180
181   return S_OK;
182 }
183
184 static void WINAPI StdProxy_Destruct(LPRPCPROXYBUFFER iface)
185 {
186   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
187
188   IPSFactoryBuffer_Release(This->pPSFactory);
189   if (This->thunks) {
190     HeapFree(GetProcessHeap(),0,This->PVtbl);
191     HeapFree(GetProcessHeap(),0,This->thunks);
192   }
193   HeapFree(GetProcessHeap(),0,This);
194 }
195
196 static HRESULT WINAPI StdProxy_QueryInterface(LPRPCPROXYBUFFER iface,
197                                              REFIID riid,
198                                              LPVOID *obj)
199 {
200   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
201   TRACE("(%p)->QueryInterface(%s,%p)\n",This,debugstr_guid(riid),obj);
202
203   if (IsEqualGUID(&IID_IUnknown,riid) ||
204       IsEqualGUID(This->piid,riid)) {
205     *obj = &This->PVtbl;
206     This->RefCount++;
207     return S_OK;
208   }
209
210   if (IsEqualGUID(&IID_IRpcProxyBuffer,riid)) {
211     *obj = &This->lpVtbl;
212     This->RefCount++;
213     return S_OK;
214   }
215
216   return E_NOINTERFACE;
217 }
218
219 static ULONG WINAPI StdProxy_AddRef(LPRPCPROXYBUFFER iface)
220 {
221   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
222   TRACE("(%p)->AddRef()\n",This);
223
224   return ++(This->RefCount);
225 }
226
227 static ULONG WINAPI StdProxy_Release(LPRPCPROXYBUFFER iface)
228 {
229   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
230   TRACE("(%p)->Release()\n",This);
231
232   if (!--(This->RefCount)) {
233     StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
234     return 0;
235   }
236   return This->RefCount;
237 }
238
239 static HRESULT WINAPI StdProxy_Connect(LPRPCPROXYBUFFER iface,
240                                       LPRPCCHANNELBUFFER pChannel)
241 {
242   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
243   TRACE("(%p)->Connect(%p)\n",This,pChannel);
244
245   This->pChannel = pChannel;
246   return S_OK;
247 }
248
249 static VOID WINAPI StdProxy_Disconnect(LPRPCPROXYBUFFER iface)
250 {
251   ICOM_THIS_MULTI(StdProxyImpl,lpVtbl,iface);
252   TRACE("(%p)->Disconnect()\n",This);
253
254   This->pChannel = NULL;
255 }
256
257 static ICOM_VTABLE(IRpcProxyBuffer) StdProxy_Vtbl =
258 {
259   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
260   StdProxy_QueryInterface,
261   StdProxy_AddRef,
262   StdProxy_Release,
263   StdProxy_Connect,
264   StdProxy_Disconnect
265 };
266
267 HRESULT WINAPI StdProxy_GetChannel(LPVOID iface,
268                                   LPRPCCHANNELBUFFER *ppChannel)
269 {
270   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
271   TRACE("(%p)->GetChannel(%p) %s\n",This,ppChannel,This->name);
272
273   *ppChannel = This->pChannel;
274   return S_OK;
275 }
276
277 HRESULT WINAPI StdProxy_GetIID(LPVOID iface,
278                               const IID **ppiid)
279 {
280   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
281   TRACE("(%p)->GetIID(%p) %s\n",This,ppiid,This->name);
282
283   *ppiid = This->piid;
284   return S_OK;
285 }
286
287 HRESULT WINAPI IUnknown_QueryInterface_Proxy(LPUNKNOWN iface,
288                                             REFIID riid,
289                                             LPVOID *ppvObj)
290 {
291   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
292   TRACE("(%p)->QueryInterface(%s,%p) %s\n",This,debugstr_guid(riid),ppvObj,This->name);
293   return IUnknown_QueryInterface(This->pUnkOuter,riid,ppvObj);
294 }
295
296 ULONG WINAPI IUnknown_AddRef_Proxy(LPUNKNOWN iface)
297 {
298   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
299   TRACE("(%p)->AddRef() %s\n",This,This->name);
300 #if 0 /* interface refcounting */
301   return ++(This->RefCount);
302 #else /* object refcounting */
303   return IUnknown_AddRef(This->pUnkOuter);
304 #endif
305 }
306
307 ULONG WINAPI IUnknown_Release_Proxy(LPUNKNOWN iface)
308 {
309   ICOM_THIS_MULTI(StdProxyImpl,PVtbl,iface);
310   TRACE("(%p)->Release() %s\n",This,This->name);
311 #if 0 /* interface refcounting */
312   if (!--(This->RefCount)) {
313     StdProxy_Destruct((LPRPCPROXYBUFFER)&This->lpVtbl);
314     return 0;
315   }
316   return This->RefCount;
317 #else /* object refcounting */
318   return IUnknown_Release(This->pUnkOuter);
319 #endif
320 }
321