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);
410 if (!last_unlock_releases)
413 LeaveCriticalSection(&m->lock);
415 TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
418 stub_manager_int_release(m);
423 /* gets the stub manager associated with an ipid - caller must have
424 * a reference to the apartment while a reference to the stub manager is held.
425 * it must also call release on the stub manager when it is no longer needed */
426 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
428 struct stub_manager *result = NULL;
431 EnterCriticalSection(&apt->cs);
432 LIST_FOR_EACH( cursor, &apt->stubmgrs )
434 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
436 if (stub_manager_ipid_to_ifstub(m, ipid))
439 stub_manager_int_addref(result);
443 LeaveCriticalSection(&apt->cs);
446 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
448 ERR("not found for ipid %s\n", debugstr_guid(ipid));
453 static HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
455 /* FIXME: hack for IRemUnknown */
456 if (ipid->Data2 == 0xffff)
457 *stub_apt = apartment_findfromoxid(*(const OXID *)ipid->Data4, TRUE);
459 *stub_apt = apartment_findfromtid(ipid->Data2);
462 TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
463 return RPC_E_INVALID_OBJECT;
465 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
468 apartment_release(*stub_apt);
470 return RPC_E_INVALID_OBJECT;
475 /* gets the apartment, stub and channel of an object. the caller must
476 * release the references to all objects (except iface) if the function
477 * returned success, otherwise no references are returned. */
478 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt,
479 IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
480 IID *iid, IUnknown **iface)
482 struct stub_manager *stubmgr;
483 struct ifstub *ifstub;
487 hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
488 if (hr != S_OK) return RPC_E_DISCONNECTED;
490 ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
493 *stub = ifstub->stubbuffer;
494 IRpcStubBuffer_AddRef(*stub);
495 *chan = ifstub->chan;
496 IRpcChannelBuffer_AddRef(*chan);
499 *iface = ifstub->iface;
501 stub_manager_int_release(stubmgr);
506 stub_manager_int_release(stubmgr);
507 apartment_release(apt);
508 return RPC_E_DISCONNECTED;
512 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
513 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
516 struct ifstub *ifstub;
518 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
520 ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
524 EnterCriticalSection(&m->lock);
526 /* track normal marshals so we can enforce rules whilst in-process */
527 if (ifstub->flags & MSHLFLAGS_NORMAL)
533 ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
538 LeaveCriticalSection(&m->lock);
543 /* handles refcounting for CoReleaseMarshalData */
544 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid, BOOL tableweak)
546 struct ifstub *ifstub;
548 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
551 if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
553 else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
556 stub_manager_ext_release(m, refs, tableweak, FALSE);
559 /* is an ifstub table marshaled? */
560 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
562 struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
566 return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
570 /*****************************************************************************
572 * IRemUnknown implementation
575 * Note: this object is not related to the lifetime of a stub_manager, but it
576 * interacts with stub managers.
579 typedef struct rem_unknown
581 IRemUnknown IRemUnknown_iface;
585 static const IRemUnknownVtbl RemUnknown_Vtbl;
587 static inline RemUnknown *impl_from_IRemUnknown(IRemUnknown *iface)
589 return CONTAINING_RECORD(iface, RemUnknown, IRemUnknown_iface);
593 /* construct an IRemUnknown object with one outstanding reference */
594 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
596 RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
598 if (!This) return E_OUTOFMEMORY;
600 This->IRemUnknown_iface.lpVtbl = &RemUnknown_Vtbl;
603 *ppRemUnknown = &This->IRemUnknown_iface;
607 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
609 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
611 if (IsEqualIID(riid, &IID_IUnknown) ||
612 IsEqualIID(riid, &IID_IRemUnknown))
615 IRemUnknown_AddRef(iface);
619 FIXME("No interface for iid %s\n", debugstr_guid(riid));
622 return E_NOINTERFACE;
625 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
628 RemUnknown *This = impl_from_IRemUnknown(iface);
630 refs = InterlockedIncrement(&This->refs);
632 TRACE("%p before: %d\n", iface, refs-1);
636 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
639 RemUnknown *This = impl_from_IRemUnknown(iface);
641 refs = InterlockedDecrement(&This->refs);
643 HeapFree(GetProcessHeap(), 0, This);
645 TRACE("%p after: %d\n", iface, refs);
649 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
650 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
651 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
655 USHORT successful_qis = 0;
657 struct stub_manager *stubmgr;
659 TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
661 hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
662 if (hr != S_OK) return hr;
664 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
666 for (i = 0; i < cIids; i++)
668 HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
669 stubmgr->object, MSHLFLAGS_NORMAL);
672 (*ppQIResults)[i].hResult = hrobj;
675 stub_manager_int_release(stubmgr);
676 apartment_release(apt);
678 if (successful_qis == cIids)
679 return S_OK; /* we got all requested interfaces */
680 else if (successful_qis == 0)
681 return E_NOINTERFACE; /* we didn't get any interfaces */
683 return S_FALSE; /* we got some interfaces */
686 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
687 USHORT cInterfaceRefs,
688 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
689 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
694 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
696 for (i = 0; i < cInterfaceRefs; i++)
699 struct stub_manager *stubmgr;
701 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
702 if (pResults[i] != S_OK)
708 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE);
709 if (InterfaceRefs[i].cPrivateRefs)
710 FIXME("Adding %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
712 stub_manager_int_release(stubmgr);
713 apartment_release(apt);
719 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
720 USHORT cInterfaceRefs,
721 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
726 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
728 for (i = 0; i < cInterfaceRefs; i++)
731 struct stub_manager *stubmgr;
733 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
737 /* FIXME: we should undo any changes already made in this function */
741 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE, TRUE);
742 if (InterfaceRefs[i].cPrivateRefs)
743 FIXME("Releasing %d refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
745 stub_manager_int_release(stubmgr);
746 apartment_release(apt);
752 static const IRemUnknownVtbl RemUnknown_Vtbl =
754 RemUnknown_QueryInterface,
757 RemUnknown_RemQueryInterface,
758 RemUnknown_RemAddRef,
759 RemUnknown_RemRelease
762 /* starts the IRemUnknown listener for the current apartment */
763 HRESULT start_apartment_remote_unknown(void)
765 IRemUnknown *pRemUnknown;
767 APARTMENT *apt = COM_CurrentApt();
769 EnterCriticalSection(&apt->cs);
770 if (!apt->remunk_exported)
772 /* create the IRemUnknown object */
773 hr = RemUnknown_Construct(&pRemUnknown);
776 STDOBJREF stdobjref; /* dummy - not used */
777 /* register it with the stub manager */
778 hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL|MSHLFLAGSP_REMUNKNOWN);
779 /* release our reference to the object as the stub manager will manage the life cycle for us */
780 IRemUnknown_Release(pRemUnknown);
782 apt->remunk_exported = TRUE;
785 LeaveCriticalSection(&apt->cs);