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
41 #include "wine/debug.h"
42 #include "compobj_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(ole);
46 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub);
47 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid);
49 /* creates a new stub manager and adds it into the apartment. caller must
50 * release stub manager when it is no longer required. the apartment and
51 * external refs together take one implicit ref */
52 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object)
54 struct stub_manager *sm;
58 sm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct stub_manager));
61 list_init(&sm->ifstubs);
63 InitializeCriticalSection(&sm->lock);
64 DEBUG_SET_CRITSEC_NAME(&sm->lock, "stub_manager");
66 IUnknown_AddRef(object);
70 /* start off with 2 references because the stub is in the apartment
71 * and the caller will also hold a reference */
74 /* yes, that's right, this starts at zero. that's zero EXTERNAL
75 * refs, ie nobody has unmarshalled anything yet. we can't have
76 * negative refs because the stub manager cannot be explicitly
77 * killed, it has to die by somebody unmarshalling then releasing
78 * the marshalled ifptr.
82 EnterCriticalSection(&apt->cs);
83 sm->oid = apt->oidc++;
84 list_add_head(&apt->stubmgrs, &sm->entry);
85 LeaveCriticalSection(&apt->cs);
87 TRACE("Created new stub manager (oid=%s) at %p for object with IUnknown %p\n", wine_dbgstr_longlong(sm->oid), sm, object);
92 /* caller must remove stub manager from apartment prior to calling this function */
93 static void stub_manager_delete(struct stub_manager *m)
97 TRACE("destroying %p (oid=%s)\n", m, wine_dbgstr_longlong(m->oid));
99 /* release every ifstub */
100 while ((cursor = list_head(&m->ifstubs)))
102 struct ifstub *ifstub = LIST_ENTRY(cursor, struct ifstub, entry);
103 stub_manager_delete_ifstub(m, ifstub);
106 IUnknown_Release(m->object);
108 DEBUG_CLEAR_CRITSEC_NAME(&m->lock);
109 DeleteCriticalSection(&m->lock);
111 HeapFree(GetProcessHeap(), 0, m);
114 /* gets the stub manager associated with an object - caller must have
115 * a reference to the apartment while a reference to the stub manager is held.
116 * it must also call release on the stub manager when it is no longer needed */
117 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object)
119 struct stub_manager *result = NULL;
122 EnterCriticalSection(&apt->cs);
123 LIST_FOR_EACH( cursor, &apt->stubmgrs )
125 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
127 if (m->object == object)
130 stub_manager_int_addref(result);
134 LeaveCriticalSection(&apt->cs);
137 TRACE("found %p for object %p\n", result, object);
139 TRACE("not found for object %p\n", object);
144 /* removes the apartment reference to an object, destroying it when no other
145 * threads have a reference to it */
146 void apartment_disconnectobject(struct apartment *apt, void *object)
149 struct stub_manager *stubmgr;
151 EnterCriticalSection(&apt->cs);
152 LIST_FOR_EACH_ENTRY( stubmgr, &apt->stubmgrs, struct stub_manager, entry )
154 if (stubmgr->object == object)
157 stub_manager_int_release(stubmgr);
161 LeaveCriticalSection(&apt->cs);
164 TRACE("disconnect object %p\n", object);
166 WARN("couldn't find object %p\n", object);
169 /* gets the stub manager associated with an object id - caller must have
170 * a reference to the apartment while a reference to the stub manager is held.
171 * it must also call release on the stub manager when it is no longer needed */
172 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid)
174 struct stub_manager *result = NULL;
177 EnterCriticalSection(&apt->cs);
178 LIST_FOR_EACH( cursor, &apt->stubmgrs )
180 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
185 stub_manager_int_addref(result);
189 LeaveCriticalSection(&apt->cs);
192 TRACE("found %p for oid %s\n", result, wine_dbgstr_longlong(oid));
194 TRACE("not found for oid %s\n", wine_dbgstr_longlong(oid));
199 /* increments the internal refcount */
200 ULONG stub_manager_int_addref(struct stub_manager *This)
204 EnterCriticalSection(&This->apt->cs);
206 LeaveCriticalSection(&This->apt->cs);
208 TRACE("before %d\n", refs - 1);
213 /* decrements the internal refcount */
214 ULONG stub_manager_int_release(struct stub_manager *This)
217 APARTMENT *apt = This->apt;
219 EnterCriticalSection(&apt->cs);
222 TRACE("after %d\n", refs);
224 /* remove from apartment so no other thread can access it... */
226 list_remove(&This->entry);
228 LeaveCriticalSection(&apt->cs);
230 /* ... so now we can delete it without being inside the apartment critsec */
232 stub_manager_delete(This);
237 /* add some external references (ie from a client that unmarshaled an ifptr) */
238 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs)
242 EnterCriticalSection(&m->lock);
244 /* make sure we don't overflow extrefs */
245 refs = min(refs, (ULONG_MAX-1 - m->extrefs));
246 rc = (m->extrefs += refs);
248 LeaveCriticalSection(&m->lock);
250 TRACE("added %u refs to %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
255 /* remove some external references */
256 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL last_unlock_releases)
260 EnterCriticalSection(&m->lock);
262 /* make sure we don't underflow extrefs */
263 refs = min(refs, m->extrefs);
264 rc = (m->extrefs -= refs);
266 LeaveCriticalSection(&m->lock);
268 TRACE("removed %u refs from %p (oid %s), rc is now %u\n", refs, m, wine_dbgstr_longlong(m->oid), rc);
270 if (rc == 0 && last_unlock_releases)
271 stub_manager_int_release(m);
276 static struct ifstub *stub_manager_ipid_to_ifstub(struct stub_manager *m, const IPID *ipid)
279 struct ifstub *result = NULL;
281 EnterCriticalSection(&m->lock);
282 LIST_FOR_EACH( cursor, &m->ifstubs )
284 struct ifstub *ifstub = LIST_ENTRY( cursor, struct ifstub, entry );
286 if (IsEqualGUID(ipid, &ifstub->ipid))
292 LeaveCriticalSection(&m->lock);
297 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags)
299 struct ifstub *result = NULL;
300 struct ifstub *ifstub;
302 EnterCriticalSection(&m->lock);
303 LIST_FOR_EACH_ENTRY( ifstub, &m->ifstubs, struct ifstub, entry )
305 if (IsEqualIID(iid, &ifstub->iid) && (ifstub->flags == flags))
311 LeaveCriticalSection(&m->lock);
316 /* gets the stub manager associated with an ipid - caller must have
317 * a reference to the apartment while a reference to the stub manager is held.
318 * it must also call release on the stub manager when it is no longer needed */
319 static struct stub_manager *get_stub_manager_from_ipid(APARTMENT *apt, const IPID *ipid)
321 struct stub_manager *result = NULL;
324 EnterCriticalSection(&apt->cs);
325 LIST_FOR_EACH( cursor, &apt->stubmgrs )
327 struct stub_manager *m = LIST_ENTRY( cursor, struct stub_manager, entry );
329 if (stub_manager_ipid_to_ifstub(m, ipid))
332 stub_manager_int_addref(result);
336 LeaveCriticalSection(&apt->cs);
339 TRACE("found %p for ipid %s\n", result, debugstr_guid(ipid));
341 ERR("not found for ipid %s\n", debugstr_guid(ipid));
346 HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret)
348 /* FIXME: hack for IRemUnknown */
349 if (ipid->Data2 == 0xffff)
350 *stub_apt = apartment_findfromoxid(*(OXID *)ipid->Data4, TRUE);
352 *stub_apt = apartment_findfromtid(ipid->Data2);
355 TRACE("Couldn't find apartment corresponding to TID 0x%04x\n", ipid->Data2);
356 return RPC_E_INVALID_OBJECT;
358 *stubmgr_ret = get_stub_manager_from_ipid(*stub_apt, ipid);
361 apartment_release(*stub_apt);
363 return RPC_E_INVALID_OBJECT;
368 /* gets the apartment, stub and channel of an object. the caller must
369 * release the references to all objects if the function returned success,
370 * otherwise no references are returned. */
371 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt,
372 IRpcStubBuffer **stub, IRpcChannelBuffer **chan)
374 struct stub_manager *stubmgr;
375 struct ifstub *ifstub;
379 hr = ipid_to_stub_manager(ipid, &apt, &stubmgr);
380 if (hr != S_OK) return RPC_E_DISCONNECTED;
382 ifstub = stub_manager_ipid_to_ifstub(stubmgr, ipid);
385 *stub = ifstub->stubbuffer;
386 IRpcStubBuffer_AddRef(*stub);
387 *chan = ifstub->chan;
388 IRpcChannelBuffer_AddRef(*chan);
391 stub_manager_int_release(stubmgr);
396 stub_manager_int_release(stubmgr);
397 apartment_release(apt);
398 return RPC_E_DISCONNECTED;
402 /* generates an ipid in the following format (similar to native version):
403 * Data1 = apartment-local ipid counter
404 * Data2 = apartment creator thread ID
406 * Data4 = random value
408 static inline HRESULT generate_ipid(struct stub_manager *m, IPID *ipid)
411 hr = UuidCreate(ipid);
414 ERR("couldn't create IPID for stub manager %p\n", m);
419 ipid->Data1 = InterlockedIncrement(&m->apt->ipidc);
420 ipid->Data2 = (USHORT)m->apt->tid;
421 ipid->Data3 = (USHORT)GetCurrentProcessId();
425 /* registers a new interface stub COM object with the stub manager and returns registration record */
426 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, MSHLFLAGS flags)
431 TRACE("oid=%s, stubbuffer=%p, iptr=%p, iid=%s\n",
432 wine_dbgstr_longlong(m->oid), sb, iptr, debugstr_guid(iid));
434 stub = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct ifstub));
435 if (!stub) return NULL;
437 hr = RPC_CreateServerChannel(&stub->chan);
440 HeapFree(GetProcessHeap(), 0, stub);
444 stub->stubbuffer = sb;
445 if (sb) IRpcStubBuffer_AddRef(sb);
447 IUnknown_AddRef(iptr);
453 * FIXME: this is a hack for marshalling IRemUnknown. In real
454 * DCOM, the IPID of the IRemUnknown interface is generated like
455 * any other and passed to the OXID resolver which then returns it
456 * when queried. We don't have an OXID resolver yet so instead we
457 * use a magic IPID reserved for IRemUnknown.
459 if (IsEqualIID(iid, &IID_IRemUnknown))
461 stub->ipid.Data1 = 0xffffffff;
462 stub->ipid.Data2 = 0xffff;
463 stub->ipid.Data3 = 0xffff;
464 assert(sizeof(stub->ipid.Data4) == sizeof(m->apt->oxid));
465 memcpy(&stub->ipid.Data4, &m->apt->oxid, sizeof(OXID));
468 generate_ipid(m, &stub->ipid);
470 EnterCriticalSection(&m->lock);
471 list_add_head(&m->ifstubs, &stub->entry);
472 /* every normal marshal is counted so we don't allow more than we should */
473 if (flags & MSHLFLAGS_NORMAL) m->norm_refs++;
474 LeaveCriticalSection(&m->lock);
476 TRACE("ifstub %p created with ipid %s\n", stub, debugstr_guid(&stub->ipid));
481 static void stub_manager_delete_ifstub(struct stub_manager *m, struct ifstub *ifstub)
483 TRACE("m=%p, m->oid=%s, ipid=%s\n", m, wine_dbgstr_longlong(m->oid), debugstr_guid(&ifstub->ipid));
485 list_remove(&ifstub->entry);
487 RPC_UnregisterInterface(&ifstub->iid);
489 if (ifstub->stubbuffer) IUnknown_Release(ifstub->stubbuffer);
490 IUnknown_Release(ifstub->iface);
491 IRpcChannelBuffer_Release(ifstub->chan);
493 HeapFree(GetProcessHeap(), 0, ifstub);
496 /* returns TRUE if it is possible to unmarshal, FALSE otherwise. */
497 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid)
500 struct ifstub *ifstub;
502 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
504 ERR("attempted unmarshal of unknown IPID %s\n", debugstr_guid(ipid));
508 EnterCriticalSection(&m->lock);
510 /* track normal marshals so we can enforce rules whilst in-process */
511 if (ifstub->flags & MSHLFLAGS_NORMAL)
517 ERR("attempted invalid normal unmarshal, norm_refs is zero\n");
522 LeaveCriticalSection(&m->lock);
527 /* handles refcounting for CoReleaseMarshalData */
528 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid)
530 struct ifstub *ifstub;
532 if (!(ifstub = stub_manager_ipid_to_ifstub(m, ipid)))
535 if (ifstub->flags & MSHLFLAGS_TABLEWEAK)
537 else if (ifstub->flags & MSHLFLAGS_TABLESTRONG)
540 stub_manager_ext_release(m, refs, TRUE);
543 /* is an ifstub table marshaled? */
544 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid)
546 struct ifstub *ifstub = stub_manager_ipid_to_ifstub(m, ipid);
550 return ifstub->flags & (MSHLFLAGS_TABLESTRONG | MSHLFLAGS_TABLEWEAK);
554 /*****************************************************************************
556 * IRemUnknown implementation
559 * Note: this object is not related to the lifetime of a stub_manager, but it
560 * interacts with stub managers.
563 typedef struct rem_unknown
565 const IRemUnknownVtbl *lpVtbl;
569 static const IRemUnknownVtbl RemUnknown_Vtbl;
572 /* construct an IRemUnknown object with one outstanding reference */
573 static HRESULT RemUnknown_Construct(IRemUnknown **ppRemUnknown)
575 RemUnknown *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
577 if (!This) return E_OUTOFMEMORY;
579 This->lpVtbl = &RemUnknown_Vtbl;
582 *ppRemUnknown = (IRemUnknown *)This;
586 static HRESULT WINAPI RemUnknown_QueryInterface(IRemUnknown *iface, REFIID riid, void **ppv)
588 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
590 if (IsEqualIID(riid, &IID_IUnknown) ||
591 IsEqualIID(riid, &IID_IRemUnknown))
593 *ppv = (LPVOID)iface;
594 IRemUnknown_AddRef(iface);
598 FIXME("No interface for iid %s\n", debugstr_guid(riid));
601 return E_NOINTERFACE;
604 static ULONG WINAPI RemUnknown_AddRef(IRemUnknown *iface)
607 RemUnknown *This = (RemUnknown *)iface;
609 refs = InterlockedIncrement(&This->refs);
611 TRACE("%p before: %d\n", iface, refs-1);
615 static ULONG WINAPI RemUnknown_Release(IRemUnknown *iface)
618 RemUnknown *This = (RemUnknown *)iface;
620 refs = InterlockedDecrement(&This->refs);
622 HeapFree(GetProcessHeap(), 0, This);
624 TRACE("%p after: %d\n", iface, refs);
628 static HRESULT WINAPI RemUnknown_RemQueryInterface(IRemUnknown *iface,
629 REFIPID ripid, ULONG cRefs, USHORT cIids, IID *iids /* [size_is(cIids)] */,
630 REMQIRESULT **ppQIResults /* [size_is(,cIids)] */)
634 USHORT successful_qis = 0;
636 struct stub_manager *stubmgr;
638 TRACE("(%p)->(%s, %d, %d, %p, %p)\n", iface, debugstr_guid(ripid), cRefs, cIids, iids, ppQIResults);
640 hr = ipid_to_stub_manager(ripid, &apt, &stubmgr);
641 if (hr != S_OK) return hr;
643 *ppQIResults = CoTaskMemAlloc(sizeof(REMQIRESULT) * cIids);
645 for (i = 0; i < cIids; i++)
647 HRESULT hrobj = marshal_object(apt, &(*ppQIResults)[i].std, &iids[i],
648 stubmgr->object, MSHLFLAGS_NORMAL);
651 (*ppQIResults)[i].hResult = hrobj;
654 stub_manager_int_release(stubmgr);
655 apartment_release(apt);
657 if (successful_qis == cIids)
658 return S_OK; /* we got all requested interfaces */
659 else if (successful_qis == 0)
660 return E_NOINTERFACE; /* we didn't get any interfaces */
662 return S_FALSE; /* we got some interfaces */
665 static HRESULT WINAPI RemUnknown_RemAddRef(IRemUnknown *iface,
666 USHORT cInterfaceRefs,
667 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */,
668 HRESULT *pResults /* [size_is(cInterfaceRefs)] */)
673 TRACE("(%p)->(%d, %p, %p)\n", iface, cInterfaceRefs, InterfaceRefs, pResults);
675 for (i = 0; i < cInterfaceRefs; i++)
678 struct stub_manager *stubmgr;
680 pResults[i] = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
681 if (pResults[i] != S_OK)
687 stub_manager_ext_addref(stubmgr, InterfaceRefs[i].cPublicRefs);
688 if (InterfaceRefs[i].cPrivateRefs)
689 FIXME("Adding %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
691 stub_manager_int_release(stubmgr);
692 apartment_release(apt);
698 static HRESULT WINAPI RemUnknown_RemRelease(IRemUnknown *iface,
699 USHORT cInterfaceRefs,
700 REMINTERFACEREF* InterfaceRefs /* [size_is(cInterfaceRefs)] */)
705 TRACE("(%p)->(%d, %p)\n", iface, cInterfaceRefs, InterfaceRefs);
707 for (i = 0; i < cInterfaceRefs; i++)
710 struct stub_manager *stubmgr;
712 hr = ipid_to_stub_manager(&InterfaceRefs[i].ipid, &apt, &stubmgr);
716 /* FIXME: we should undo any changes already made in this function */
720 stub_manager_ext_release(stubmgr, InterfaceRefs[i].cPublicRefs, TRUE);
721 if (InterfaceRefs[i].cPrivateRefs)
722 FIXME("Releasing %ld refs securely not implemented\n", InterfaceRefs[i].cPrivateRefs);
724 stub_manager_int_release(stubmgr);
725 apartment_release(apt);
731 static const IRemUnknownVtbl RemUnknown_Vtbl =
733 RemUnknown_QueryInterface,
736 RemUnknown_RemQueryInterface,
737 RemUnknown_RemAddRef,
738 RemUnknown_RemRelease
741 /* starts the IRemUnknown listener for the current apartment */
742 HRESULT start_apartment_remote_unknown(void)
744 IRemUnknown *pRemUnknown;
746 APARTMENT *apt = COM_CurrentApt();
748 EnterCriticalSection(&apt->cs);
749 if (!apt->remunk_exported)
751 /* create the IRemUnknown object */
752 hr = RemUnknown_Construct(&pRemUnknown);
755 STDOBJREF stdobjref; /* dummy - not used */
756 /* register it with the stub manager */
757 hr = marshal_object(apt, &stdobjref, &IID_IRemUnknown, (IUnknown *)pRemUnknown, MSHLFLAGS_NORMAL);
758 /* release our reference to the object as the stub manager will manage the life cycle for us */
759 IRemUnknown_Release(pRemUnknown);
761 apt->remunk_exported = TRUE;
764 LeaveCriticalSection(&apt->cs);