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);
45 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub);
46 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid);
48 /* creates a new stub manager and adds it into the apartment. caller must
49 * release stub manager when it is no longer required. the apartment and
50 * external refs together take one implicit ref */
51 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object)
53 struct stub_manager *sm;
57 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
60 list_init(&sm->ifstubs);
62 InitializeCriticalSection(&sm->lock);
63 DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
65 IUnknown_AddRef(object);
69 /* start off with 2 references because the stub is in the apartment
70 * and the caller will also hold a reference */
74 sm->oxid_info.dwPid = GetCurrentProcessId();
75 sm->oxid_info.dwTid = GetCurrentThreadId();
77 * FIXME: this is a hack for marshalling IRemUnknown. In real
78 * DCOM, the IPID of the IRemUnknown interface is generated like
79 * any other and passed to the OXID resolver which then returns it
80 * when queried. We don't have an OXID resolver yet so instead we
81 * use a magic IPID reserved for IRemUnknown.
83 sm->oxid_info.ipidRemUnknown.Data1 = 0xffffffff;
84 sm->oxid_info.ipidRemUnknown.Data2 = 0xffff;
85 sm->oxid_info.ipidRemUnknown.Data3 = 0xffff;
86 assert(sizeof(sm->oxid_info.ipidRemUnknown.Data4) == sizeof(apt->oxid));
87 memcpy(sm->oxid_info.ipidRemUnknown.Data4, &apt->oxid, sizeof(OXID));
88 sm->oxid_info.dwAuthnHint = RPC_C_AUTHN_LEVEL_NONE;
89 sm->oxid_info.psa = NULL /* FIXME */;
91 /* Yes, that's right, this starts at zero. that's zero EXTERNAL
92 * refs, i.e., nobody has unmarshalled anything yet. We can't have
93 * negative refs because the stub manager cannot be explicitly
94 * killed, it has to die by somebody unmarshalling then releasing
95 * the marshalled ifptr.
99 EnterCriticalSection(&apt->cs);
100 sm->oid = apt->oidc++;
101 list_add_head(&apt->stubmgrs, &sm->entry);
102 LeaveCriticalSection(&apt->cs);
104 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
109 /* caller must remove stub manager from apartment prior to calling this function */
110 static void stub_manager_delete(struct stub_manager *m)
114 TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
116 /* release every ifstub */
117 while ((cursor = list_head(&m->ifstubs)))
119 struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
120 stub_manager_delete_ifstub(m, ifstub);
123 CoTaskMemFree(m->oxid_info.psa);
124 IUnknown_Release(m->object);
126 DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
127 DeleteCriticalSection(&m->lock);
129 HeapFree(GetProcessHeap(), 0, m);
132 /* gets the stub manager associated with an object - caller must have
133 * a reference to the apartment while a reference to the stub manager is held.
134 * it must also call release on the stub manager when it is no longer needed */
135 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
137 struct stub_manager *result = NULL;
140 EnterCriticalSection(&apt->cs);
141 LIST_FOR_EACH( cursor, &apt->stubmgrs )
143 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
145 if (m->object == object)
148 stub_manager_int_addref(result);
152 LeaveCriticalSection(&apt->cs);
155 TRACE("found %p for object %p\n", result, object);
157 TRACE("not found for object %p\n", object);
162 /* removes the apartment reference to an object, destroying it when no other
163 * threads have a reference to it */
164 void apartment_disconnectobject(struct apartment *apt, void *object)
167 struct stub_manager *stubmgr;
169 EnterCriticalSection(&apt->cs);
170 LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
172 if (stubmgr->object == object)
175 stub_manager_int_release(stubmgr);
179 LeaveCriticalSection(&apt->cs);
182 TRACE("disconnect object %p\n", object);
184 WARN("couldn't find object %p\n", object);
187 /* gets the stub manager associated with an object id - caller must have
188 * a reference to the apartment while a reference to the stub manager is held.
189 * it must also call release on the stub manager when it is no longer needed */
190 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
192 struct stub_manager *result = NULL;
195 EnterCriticalSection(&apt->cs);
196 LIST_FOR_EACH( cursor, &apt->stubmgrs )
198 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
203 stub_manager_int_addref(result);
207 LeaveCriticalSection(&apt->cs);
210 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
212 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
217 /* increments the internal refcount */
218 ULONG stub_manager_int_addref(struct stub_manager *This)
222 EnterCriticalSection(&This->apt->cs);
224 LeaveCriticalSection(&This->apt->cs);
226 TRACE("before %d\n", refs - 1);
231 /* decrements the internal refcount */
232 ULONG stub_manager_int_release(struct stub_manager *This)
235 APARTMENT *apt = This->apt;
237 EnterCriticalSection(&apt->cs);
240 TRACE("after %d\n", refs);
242 /* remove from apartment so no other thread can access it... */
244 list_remove(&This->entry);
246 LeaveCriticalSection(&apt->cs);
248 /* ... so now we can delete it without being inside the apartment critsec */
250 stub_manager_delete(This);
255 /* add some external references (ie from a client that unmarshaled an ifptr) */
256 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs, BOOL tableweak)
260 EnterCriticalSection(&m->lock);
262 /* make sure we don't overflow extrefs */
263 refs = min(refs, (ULONG_MAX-1 - m->extrefs));
264 rc = (m->extrefs += refs);
269 LeaveCriticalSection(&m->lock);
271 TRACE("added %u refs to %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
276 /* remove some external references */
277 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL tableweak, BOOL last_unlock_releases)
281 EnterCriticalSection(&m->lock);
283 /* make sure we don't underflow extrefs */
284 refs = min(refs, m->extrefs);
285 rc = (m->extrefs -= refs);
290 LeaveCriticalSection(&m->lock);
292 TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
294 if (rc == 0 && last_unlock_releases)
295 stub_manager_int_release(m);
300 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
303 struct ifstub *result = NULL;
305 EnterCriticalSection(&m->lock);
306 LIST_FOR_EACH( cursor, &m->ifstubs )
308 struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
310 if (IsEqualGUID(ipid, &ifstub->ipid))
316 LeaveCriticalSection(&m->lock);
321 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
323 struct ifstub *result = NULL;
324 struct ifstub *ifstub;
326 EnterCriticalSection(&m->lock);
327 LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
329 if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
335 LeaveCriticalSection(&m->lock);
340 /* gets the stub manager associated with an ipid - caller must have
341 * a reference to the apartment while a reference to the stub manager is held.
342 * it must also call release on the stub manager when it is no longer needed */
343 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
345 struct stub_manager *result = NULL;
348 EnterCriticalSection(&apt->cs);
349 LIST_FOR_EACH( cursor, &apt->stubmgrs )
351 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
353 if (stub_manager_ipid_to_ifstub(m, ipid))
356 stub_manager_int_addref(result);
360 LeaveCriticalSection(&apt->cs);
363 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
365 ERR("not found for ipid %s\n", debugstr_guid(ipid));
370 static HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
372 /* FIXME: hack for IRemUnknown */
373 if (ipid->Data2 == 0xffff)
374 *stub_apt = apartment_findfromoxid(*(const OXID *)ipid->Data4, TRUE);
376 *stub_apt = apartment_findfromtid(ipid->Data2);
379 TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
380 return RPC_E_INVALID_OBJECT;
382 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
385 apartment_release(*stub_apt);
387 return RPC_E_INVALID_OBJECT;
392 /* gets the apartment, stub and channel of an object. the caller must
393 * release the references to all objects (except iface) if the function
394 * returned success, otherwise no references are returned. */
395 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt,
396 IRpcStubBuffer **stub, IRpcChannelBuffer **chan,
397 IID *iid, IUnknown **iface)
399 struct stub_manager *stubmgr;
400 struct ifstub *ifstub;
404 hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
405 if (hr != S_OK) return RPC_E_DISCONNECTED;
407 ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
410 *stub = ifstub->stubbuffer;
411 IRpcStubBuffer_AddRef(*stub);
412 *chan = ifstub->chan;
413 IRpcChannelBuffer_AddRef(*chan);
416 *iface = ifstub->iface;
418 stub_manager_int_release(stubmgr);
423 stub_manager_int_release(stubmgr);
424 apartment_release(apt);
425 return RPC_E_DISCONNECTED;
429 /* generates an ipid in the following format (similar to native version):
430 * Data1 = apartment-local ipid counter
431 * Data2 = apartment creator thread ID
433 * Data4 = random value
435 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
438 hr = UuidCreate(ipid);
441 ERR("couldn't create IPID for stub manager %p\n", m);
446 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
447 ipid->Data2 = (USHORT)m->apt->tid;
448 ipid->Data3 = (USHORT)GetCurrentProcessId();
452 /* registers a new interface stub COM object with the stub manager and returns registration record */
453 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, MSHLFLAGS flags)
458 TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
459 wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
461 stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
462 if (!stub) return NULL;
464 hr = RPC_CreateServerChannel(&stub->chan);
467 HeapFree(GetProcessHeap(), 0, stub);
471 stub->stubbuffer = sb;
472 if (sb) IRpcStubBuffer_AddRef(sb);
474 IUnknown_AddRef(iptr);
479 /* FIXME: find a cleaner way of identifying that we are creating an ifstub
480 * for the remunknown interface */
481 if (flags & MSHLFLAGSP_REMUNKNOWN)
482 stub->ipid = m->oxid_info.ipidRemUnknown;
484 generate_ipid(m, &stub->ipid);
486 EnterCriticalSection(&m->lock);
487 list_add_head(&m->ifstubs, &stub->entry);
488 /* every normal marshal is counted so we don't allow more than we should */
489 if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
490 LeaveCriticalSection(&m->lock);
492 TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
497 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
499 TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
501 list_remove(&ifstub->entry);
503 RPC_UnregisterInterface(&ifstub->iid);
505 if (ifstub->stubbuffer) IUnknown_Release(ifstub->stubbuffer);
506 IUnknown_Release(ifstub->iface);
507 IRpcChannelBuffer_Release(ifstub->chan);
509 HeapFree(GetProcessHeap(), 0, ifstub);
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, TRUE);
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 const IRemUnknownVtbl *lpVtbl;
585 static const IRemUnknownVtbl RemUnknown_Vtbl;
588 /* construct an IRemUnknown object with one outstanding reference */
589 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
591 RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
593 if (!This) return E_OUTOFMEMORY;
595 This->lpVtbl = &RemUnknown_Vtbl;
598 *ppRemUnknown = (IRemUnknown *)This;
602 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
604 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
606 if (IsEqualIID(riid, &IID_IUnknown) ||
607 IsEqualIID(riid, &IID_IRemUnknown))
610 IRemUnknown_AddRef(iface);
614 FIXME("No interface for iid %s\n", debugstr_guid(riid));
617 return E_NOINTERFACE;
620 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
623 RemUnknown *This = (RemUnknown *)iface;
625 refs = InterlockedIncrement(&This->refs);
627 TRACE("%p before: %d\n", iface, refs-1);
631 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
634 RemUnknown *This = (RemUnknown *)iface;
636 refs = InterlockedDecrement(&This->refs);
638 HeapFree(GetProcessHeap(), 0, This);
640 TRACE("%p after: %d\n", iface, refs);
644 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
645 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
646 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
650 USHORT successful_qis = 0;
652 struct stub_manager *stubmgr;
654 TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
656 hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
657 if (hr != S_OK) return hr;
659 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
661 for (i = 0; i < cIids; i++)
663 HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
664 stubmgr->object, MSHLFLAGS_NORMAL);
667 (*ppQIResults)[i].hResult = hrobj;
670 stub_manager_int_release(stubmgr);
671 apartment_release(apt);
673 if (successful_qis == cIids)
674 return S_OK; /* we got all requested interfaces */
675 else if (successful_qis == 0)
676 return E_NOINTERFACE; /* we didn't get any interfaces */
678 return S_FALSE; /* we got some interfaces */
681 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
682 USHORT cInterfaceRefs,
683 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
684 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
689 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
691 for (i = 0; i < cInterfaceRefs; i++)
694 struct stub_manager *stubmgr;
696 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
697 if (pResults[i] != S_OK)
703 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE);
704 if (InterfaceRefs[i].cPrivateRefs)
705 FIXME("Adding %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
707 stub_manager_int_release(stubmgr);
708 apartment_release(apt);
714 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
715 USHORT cInterfaceRefs,
716 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
721 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
723 for (i = 0; i < cInterfaceRefs; i++)
726 struct stub_manager *stubmgr;
728 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
732 /* FIXME: we should undo any changes already made in this function */
736 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, FALSE, TRUE);
737 if (InterfaceRefs[i].cPrivateRefs)
738 FIXME("Releasing %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
740 stub_manager_int_release(stubmgr);
741 apartment_release(apt);
747 static const IRemUnknownVtbl RemUnknown_Vtbl =
749 RemUnknown_QueryInterface,
752 RemUnknown_RemQueryInterface,
753 RemUnknown_RemAddRef,
754 RemUnknown_RemRelease
757 /* starts the IRemUnknown listener for the current apartment */
758 HRESULT start_apartment_remote_unknown(void)
760 IRemUnknown *pRemUnknown;
762 APARTMENT *apt = COM_CurrentApt();
764 EnterCriticalSection(&apt->cs);
765 if (!apt->remunk_exported)
767 /* create the IRemUnknown object */
768 hr = RemUnknown_Construct(&pRemUnknown);
771 STDOBJREF stdobjref; /* dummy - not used */
772 /* register it with the stub manager */
773 hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL|MSHLFLAGSP_REMUNKNOWN);
774 /* release our reference to the object as the stub manager will manage the life cycle for us */
775 IRemUnknown_Release(pRemUnknown);
777 apt->remunk_exported = TRUE;
780 LeaveCriticalSection(&apt->cs);