2 * A stub manager is an object that controls interface stubs. It is
3 * identified by an OID (object identifier) and acts as the network
4 * identity of the object. There can be many stub managers in a
5 * process or apartment.
7 * Copyright 2002 Marcus Meissner
8 * Copyright 2004 Mike Hearn for CodeWeavers
9 * Copyright 2004 Robert Shearman (for CodeWeavers)
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
40 #include "wine/debug.h"
41 #include "compobj_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(ole);
46 /* generates an ipid in the following format (similar to native version):
47 * Data1 = apartment-local ipid counter
48 * Data2 = apartment creator thread ID
50 * Data4 = random value
52 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
55 hr = UuidCreate(ipid);
58 ERR("couldn't create IPID for stub manager %p\n", m);
63 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
64 ipid->Data2 = (USHORT)m->apt->tid;
65 ipid->Data3 = (USHORT)GetCurrentProcessId();
69 /* registers a new interface stub COM object with the stub manager and returns registration record */
70 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, MSHLFLAGS flags)
75 TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
76 wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
78 stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
79 if (!stub) return NULL;
81 hr = RPC_CreateServerChannel(&stub->chan);
84 HeapFree(GetProcessHeap(), 0, stub);
88 stub->stubbuffer = sb;
89 if (sb) IRpcStubBuffer_AddRef(sb);
91 IUnknown_AddRef(iptr);
96 /* FIXME: find a cleaner way of identifying that we are creating an ifstub
97 * for the remunknown interface */
98 if (flags & MSHLFLAGSP_REMUNKNOWN)
99 stub->ipid = m->oxid_info.ipidRemUnknown;
101 generate_ipid(m, &stub->ipid);
103 EnterCriticalSection(&m->lock);
104 list_add_head(&m->ifstubs, &stub->entry);
105 /* every normal marshal is counted so we don't allow more than we should */
106 if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
107 LeaveCriticalSection(&m->lock);
109 TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
114 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
116 TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
118 list_remove(&ifstub->entry);
120 RPC_UnregisterInterface(&ifstub->iid);
122 if (ifstub->stubbuffer) IUnknown_Release(ifstub->stubbuffer);
123 IUnknown_Release(ifstub->iface);
124 IRpcChannelBuffer_Release(ifstub->chan);
126 HeapFree(GetProcessHeap(), 0, ifstub);
129 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
132 struct ifstub *result = NULL;
134 EnterCriticalSection(&m->lock);
135 LIST_FOR_EACH( cursor, &m->ifstubs )
137 struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
139 if (IsEqualGUID(ipid, &ifstub->ipid))
145 LeaveCriticalSection(&m->lock);
150 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
152 struct ifstub *result = NULL;
153 struct ifstub *ifstub;
155 EnterCriticalSection(&m->lock);
156 LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
158 if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
164 LeaveCriticalSection(&m->lock);
169 /* creates a new stub manager and adds it into the apartment. caller must
170 * release stub manager when it is no longer required. the apartment and
171 * external refs together take one implicit ref */
172 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object)
174 struct stub_manager *sm;
178 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
179 if (!sm) return NULL;
181 list_init(&sm->ifstubs);
183 InitializeCriticalSection(&sm->lock);
184 DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
186 IUnknown_AddRef(object);
190 /* start off with 2 references because the stub is in the apartment
191 * and the caller will also hold a reference */
195 sm->oxid_info.dwPid = GetCurrentProcessId();
196 sm->oxid_info.dwTid = GetCurrentThreadId();
198 * FIXME: this is a hack for marshalling IRemUnknown. In real
199 * DCOM, the IPID of the IRemUnknown interface is generated like
200 * any other and passed to the OXID resolver which then returns it
201 * when queried. We don't have an OXID resolver yet so instead we
202 * use a magic IPID reserved for IRemUnknown.
204 sm->oxid_info.ipidRemUnknown.Data1 = 0xffffffff;
205 sm->oxid_info.ipidRemUnknown.Data2 = 0xffff;
206 sm->oxid_info.ipidRemUnknown.Data3 = 0xffff;
207 assert(sizeof(sm->oxid_info.ipidRemUnknown.Data4) == sizeof(apt->oxid));
208 memcpy(sm->oxid_info.ipidRemUnknown.Data4, &apt->oxid, sizeof(OXID));
209 sm->oxid_info.dwAuthnHint = RPC_C_AUTHN_LEVEL_NONE;
210 sm->oxid_info.psa = NULL /* FIXME */;
212 /* Yes, that's right, this starts at zero. that's zero EXTERNAL
213 * refs, i.e., nobody has unmarshalled anything yet. We can't have
214 * negative refs because the stub manager cannot be explicitly
215 * killed, it has to die by somebody unmarshalling then releasing
216 * the marshalled ifptr.
220 EnterCriticalSection(&apt->cs);
221 sm->oid = apt->oidc++;
222 list_add_head(&apt->stubmgrs, &sm->entry);
223 LeaveCriticalSection(&apt->cs);
225 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
230 /* caller must remove stub manager from apartment prior to calling this function */
231 static void stub_manager_delete(struct stub_manager *m)
235 TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
237 /* release every ifstub */
238 while ((cursor = list_head(&m->ifstubs)))
240 struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
241 stub_manager_delete_ifstub(m, ifstub);
244 CoTaskMemFree(m->oxid_info.psa);
245 IUnknown_Release(m->object);
247 DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
248 DeleteCriticalSection(&m->lock);
250 HeapFree(GetProcessHeap(), 0, m);
253 /* increments the internal refcount */
254 static ULONG stub_manager_int_addref(struct stub_manager *This)
258 EnterCriticalSection(&This->apt->cs);
260 LeaveCriticalSection(&This->apt->cs);
262 TRACE("before %d\n", refs - 1);
267 /* decrements the internal refcount */
268 ULONG stub_manager_int_release(struct stub_manager *This)
271 APARTMENT *apt = This->apt;
273 EnterCriticalSection(&apt->cs);
276 TRACE("after %d\n", refs);
278 /* remove from apartment so no other thread can access it... */
280 list_remove(&This->entry);
282 LeaveCriticalSection(&apt->cs);
284 /* ... so now we can delete it without being inside the apartment critsec */
286 stub_manager_delete(This);
291 /* gets the stub manager associated with an object - caller must have
292 * a reference to the apartment while a reference to the stub manager is held.
293 * it must also call release on the stub manager when it is no longer needed */
294 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
296 struct stub_manager *result = NULL;
299 EnterCriticalSection(&apt->cs);
300 LIST_FOR_EACH( cursor, &apt->stubmgrs )
302 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
304 if (m->object == object)
307 stub_manager_int_addref(result);
311 LeaveCriticalSection(&apt->cs);
314 TRACE("found %p for object %p\n", result, object);
316 TRACE("not found for object %p\n", object);
321 /* removes the apartment reference to an object, destroying it when no other
322 * threads have a reference to it */
323 void apartment_disconnectobject(struct apartment *apt, void *object)
326 struct stub_manager *stubmgr;
328 EnterCriticalSection(&apt->cs);
329 LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
331 if (stubmgr->object == object)
334 stub_manager_int_release(stubmgr);
338 LeaveCriticalSection(&apt->cs);
341 TRACE("disconnect object %p\n", object);
343 WARN("couldn't find object %p\n", object);
346 /* gets the stub manager associated with an object id - caller must have
347 * a reference to the apartment while a reference to the stub manager is held.
348 * it must also call release on the stub manager when it is no longer needed */
349 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
351 struct stub_manager *result = NULL;
354 EnterCriticalSection(&apt->cs);
355 LIST_FOR_EACH( cursor, &apt->stubmgrs )
357 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
362 stub_manager_int_addref(result);
366 LeaveCriticalSection(&apt->cs);
369 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
371 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
376 /* add some external references (ie from a client that unmarshaled an ifptr) */
377 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs, BOOL tableweak)
381 EnterCriticalSection(&m->lock);
383 /* make sure we don't overflow extrefs */
384 refs = min(refs, (ULONG_MAX-1 - m->extrefs));
385 rc = (m->extrefs += refs);
390 LeaveCriticalSection(&m->lock);
392 TRACE("added %u refs to %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
397 /* remove some external references */
398 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL tableweak, BOOL last_unlock_releases)
402 EnterCriticalSection(&m->lock);
404 /* make sure we don't underflow extrefs */
405 refs = min(refs, m->extrefs);
406 rc = (m->extrefs -= refs);
411 LeaveCriticalSection(&m->lock);
413 TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
415 if (rc == 0 && last_unlock_releases)
416 stub_manager_int_release(m);
421 /* gets the stub manager associated with an ipid - caller must have
422 * a reference to the apartment while a reference to the stub manager is held.
423 * it must also call release on the stub manager when it is no longer needed */
424 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
426 struct stub_manager *result = NULL;
429 EnterCriticalSection(&apt->cs);
430 LIST_FOR_EACH( cursor, &apt->stubmgrs )
432 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
434 if (stub_manager_ipid_to_ifstub(m, ipid))
437 stub_manager_int_addref(result);
441 LeaveCriticalSection(&apt->cs);
444 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
446 ERR("not found for ipid %s\n", debugstr_guid(ipid));
451 static HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
453 /* FIXME: hack for IRemUnknown */
454 if (ipid->Data2 == 0xffff)
455 *stub_apt = apartment_findfromoxid(*(const OXID *)ipid->Data4, TRUE);
457 *stub_apt = apartment_findfromtid(ipid->Data2);
460 TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
461 return RPC_E_INVALID_OBJECT;
463 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
466 apartment_release(*stub_apt);
468 return RPC_E_INVALID_OBJECT;
473 /* gets the apartment, stub and channel of an object. the caller must
474 * release the references to all objects (except iface) if the function
475 * returned success, otherwise no references are returned. */
476 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt,
477 IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
478 IID *iid, IUnknown **iface)
480 struct stub_manager *stubmgr;
481 struct ifstub *ifstub;
485 hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
486 if (hr != S_OK) return RPC_E_DISCONNECTED;
488 ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
491 *stub = ifstub->stubbuffer;
492 IRpcStubBuffer_AddRef(*stub);
493 *chan = ifstub->chan;
494 IRpcChannelBuffer_AddRef(*chan);
497 *iface = ifstub->iface;
499 stub_manager_int_release(stubmgr);
504 stub_manager_int_release(stubmgr);
505 apartment_release(apt);
506 return RPC_E_DISCONNECTED;
510 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
511 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
514 struct ifstub *ifstub;
516 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
518 ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
522 EnterCriticalSection(&m->lock);
524 /* track normal marshals so we can enforce rules whilst in-process */
525 if (ifstub->flags & MSHLFLAGS_NORMAL)
531 ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
536 LeaveCriticalSection(&m->lock);
541 /* handles refcounting for CoReleaseMarshalData */
542 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid, BOOL tableweak)
544 struct ifstub *ifstub;
546 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
549 if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
551 else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
554 stub_manager_ext_release(m, refs, tableweak, TRUE);
557 /* is an ifstub table marshaled? */
558 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
560 struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
564 return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
568 /*****************************************************************************
570 * IRemUnknown implementation
573 * Note: this object is not related to the lifetime of a stub_manager, but it
574 * interacts with stub managers.
577 typedef struct rem_unknown
579 const IRemUnknownVtbl *lpVtbl;
583 static const IRemUnknownVtbl RemUnknown_Vtbl;
586 /* construct an IRemUnknown object with one outstanding reference */
587 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
589 RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
591 if (!This) return E_OUTOFMEMORY;
593 This->lpVtbl = &RemUnknown_Vtbl;
596 *ppRemUnknown = (IRemUnknown *)This;
600 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
602 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
604 if (IsEqualIID(riid, &IID_IUnknown) ||
605 IsEqualIID(riid, &IID_IRemUnknown))
608 IRemUnknown_AddRef(iface);
612 FIXME("No interface for iid %s\n", debugstr_guid(riid));
615 return E_NOINTERFACE;
618 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
621 RemUnknown *This = (RemUnknown *)iface;
623 refs = InterlockedIncrement(&This->refs);
625 TRACE("%p before: %d\n", iface, refs-1);
629 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
632 RemUnknown *This = (RemUnknown *)iface;
634 refs = InterlockedDecrement(&This->refs);
636 HeapFree(GetProcessHeap(), 0, This);
638 TRACE("%p after: %d\n", iface, refs);
642 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
643 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
644 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
648 USHORT successful_qis = 0;
650 struct stub_manager *stubmgr;
652 TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
654 hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
655 if (hr != S_OK) return hr;
657 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
659 for (i = 0; i < cIids; i++)
661 HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
662 stubmgr->object, MSHLFLAGS_NORMAL);
665 (*ppQIResults)[i].hResult = hrobj;
668 stub_manager_int_release(stubmgr);
669 apartment_release(apt);
671 if (successful_qis == cIids)
672 return S_OK; /* we got all requested interfaces */
673 else if (successful_qis == 0)
674 return E_NOINTERFACE; /* we didn't get any interfaces */
676 return S_FALSE; /* we got some interfaces */
679 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
680 USHORT cInterfaceRefs,
681 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
682 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
687 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
689 for (i = 0; i < cInterfaceRefs; i++)
692 struct stub_manager *stubmgr;
694 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
695 if (pResults[i] != S_OK)
701 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE);
702 if (InterfaceRefs[i].cPrivateRefs)
703 FIXME("Adding %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
705 stub_manager_int_release(stubmgr);
706 apartment_release(apt);
712 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
713 USHORT cInterfaceRefs,
714 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
719 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
721 for (i = 0; i < cInterfaceRefs; i++)
724 struct stub_manager *stubmgr;
726 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
730 /* FIXME: we should undo any changes already made in this function */
734 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE, TRUE);
735 if (InterfaceRefs[i].cPrivateRefs)
736 FIXME("Releasing %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
738 stub_manager_int_release(stubmgr);
739 apartment_release(apt);
745 static const IRemUnknownVtbl RemUnknown_Vtbl =
747 RemUnknown_QueryInterface,
750 RemUnknown_RemQueryInterface,
751 RemUnknown_RemAddRef,
752 RemUnknown_RemRelease
755 /* starts the IRemUnknown listener for the current apartment */
756 HRESULT start_apartment_remote_unknown(void)
758 IRemUnknown *pRemUnknown;
760 APARTMENT *apt = COM_CurrentApt();
762 EnterCriticalSection(&apt->cs);
763 if (!apt->remunk_exported)
765 /* create the IRemUnknown object */
766 hr = RemUnknown_Construct(&pRemUnknown);
769 STDOBJREF stdobjref; /* dummy - not used */
770 /* register it with the stub manager */
771 hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL|MSHLFLAGSP_REMUNKNOWN);
772 /* release our reference to the object as the stub manager will manage the life cycle for us */
773 IRemUnknown_Release(pRemUnknown);
775 apt->remunk_exported = TRUE;
778 LeaveCriticalSection(&apt->cs);