-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathcomcache.cpp
1425 lines (1197 loc) · 39.6 KB
/
comcache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "common.h"
#include <crtwrap.h>
#include "comcache.h"
#include "runtimecallablewrapper.h"
#include "mtx.h"
#include "contxt.h"
#include "ctxtcall.h"
#include "win32threadpool.h"
#ifdef FEATURE_COMINTEROP_APARTMENT_SUPPORT
#include "olecontexthelpers.h"
#endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT
//================================================================
// Guid definitions.
const GUID IID_IEnterActivityWithNoLock = { 0xd7174f82, 0x36b8, 0x4aa8, { 0x80, 0x0a, 0xe9, 0x63, 0xab, 0x2d, 0xfa, 0xb9 } };
const GUID IID_IFuncEvalAbort = { 0xde6844f6, 0x95ac, 0x4e83, { 0x90, 0x8d, 0x9b, 0x1b, 0xea, 0x2f, 0xe0, 0x8c } };
// sanity check., to find stress bug #82137
VOID IUnkEntry::CheckValidIUnkEntry()
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
if (IsDisconnected())
{
COMPlusThrow(kInvalidComObjectException, IDS_EE_COM_OBJECT_NO_LONGER_HAS_WRAPPER);
}
}
// Version that returns an HR instead of throwing.
HRESULT IUnkEntry::HRCheckValidIUnkEntry()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
if (IsDisconnected())
{
return COR_E_INVALIDCOMOBJECT;
}
return S_OK;
}
// Returns IErrorInfo corresponding to the exception injected by the debugger to abort a func eval,
// or NULL if there is no such exception.
static IErrorInfo *CheckForFuncEvalAbortNoThrow(HRESULT hr)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
// the managed exception thrown by the debugger is translated to EXCEPTION_COMPLUS by COM
if (hr == EXCEPTION_COMPLUS)
{
GCX_PREEMP();
// we recognize the ones thrown by the debugger by QI'ing the IErrorInfo for a special IID
ReleaseHolder<IErrorInfo> pErrorInfo;
if (SafeGetErrorInfo(&pErrorInfo) == S_OK)
{
ReleaseHolder<IUnknown> pUnk;
if (SafeQueryInterface(pErrorInfo, IID_IFuncEvalAbort, &pUnk) == S_OK)
{
// QI succeeded, this is a func eval abort
return pErrorInfo.Extract();
}
else
{
// QI failed, put the IErrorInfo back
SetErrorInfo(0, pErrorInfo);
}
}
}
return NULL;
}
// Rethrows the exception injected by the debugger to abort a func eval, or does nothing if there is no such exception.
static void CheckForFuncEvalAbort(HRESULT hr)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
IErrorInfo *pErrorInfo = CheckForFuncEvalAbortNoThrow(hr);
if (pErrorInfo != NULL)
{
// COMPlusThrowHR internally releases the pErrorInfo
COMPlusThrowHR(hr, pErrorInfo);
}
}
//+-------------------------------------------------------------------------
//
// Function: STDAPI_(LPSTREAM) CreateMemStm(DWORD cb, BYTE** ppBuf))
// Create a stream in the memory
//
STDAPI_(LPSTREAM) CreateMemStm(DWORD cb, BYTE** ppBuf)
{
CONTRACT(LPSTREAM)
{
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
INJECT_FAULT(CONTRACT_RETURN NULL);
PRECONDITION(CheckPointer(ppBuf, NULL_OK));
PRECONDITION(CheckPointer(ppBuf, NULL_OK));
POSTCONDITION(CheckPointer(RETVAL, NULL_OK));
}
CONTRACT_END;
LPSTREAM pstm = NULL;
BYTE* pMem = new(nothrow) BYTE[cb];
if (pMem)
{
HRESULT hr = CInMemoryStream::CreateStreamOnMemory(pMem, cb, &pstm, TRUE);
_ASSERTE(hr == S_OK || pstm == NULL);
}
if(ppBuf)
*ppBuf = pMem;
RETURN pstm;
}
//=====================================================================
// HRESULT wCoMarshalInterThreadInterfaceInStream
HRESULT wCoMarshalInterThreadInterfaceInStream(REFIID riid,
LPUNKNOWN pUnk,
LPSTREAM *ppStm)
{
#ifdef PLATFORM_CE
return E_NOTIMPL;
#endif // !PLATFORM_CE
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_PREEMPTIVE;
PRECONDITION(CheckPointer(pUnk));
PRECONDITION(CheckPointer(ppStm));
}
CONTRACTL_END;
HRESULT hr;
LPSTREAM pStm = NULL;
DWORD mshlFlgs = MSHLFLAGS_NORMAL;
ULONG lSize;
hr = CoGetMarshalSizeMax(&lSize, IID_IUnknown, pUnk, MSHCTX_INPROC, NULL, mshlFlgs);
if (hr == S_OK)
{
// Create a stream
pStm = CreateMemStm(lSize, NULL);
if (pStm != NULL)
{
// Marshal the interface into the stream TABLE STRONG
hr = CoMarshalInterface(pStm, riid, pUnk, MSHCTX_INPROC, NULL, mshlFlgs);
}
else
{
hr = E_OUTOFMEMORY;
}
}
if (SUCCEEDED(hr))
{
// Reset the stream to the beginning
LARGE_INTEGER li;
LISet32(li, 0);
ULARGE_INTEGER li2;
pStm->Seek(li, STREAM_SEEK_SET, &li2);
// Set the return value
*ppStm = pStm;
}
else
{
// Cleanup if failure
SafeReleasePreemp(pStm);
*ppStm = NULL;
}
// Return the result
return hr;
}
//================================================================
// Struct passed in to DoCallback.
struct CtxEntryEnterContextCallbackData
{
PFNCTXCALLBACK m_pUserCallbackFunc;
LPVOID m_pUserData;
LPVOID m_pCtxCookie;
HRESULT m_UserCallbackHR;
};
//================================================================
// Static members.
CtxEntryCache* CtxEntryCache::s_pCtxEntryCache = NULL;
CtxEntryCache::CtxEntryCache()
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
m_Lock.Init(LOCK_COMCTXENTRYCACHE);
LockOwner lock = {&m_Lock, IsOwnerOfSpinLock};
}
CtxEntryCache::~CtxEntryCache()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
for (SHash<CtxEntryCacheTraits>::Iterator it = m_CtxEntryHash.Begin(); it != m_CtxEntryHash.End(); it++)
{
CtxEntry *pCtxEntry = (CtxEntry *)*it;
_ASSERTE(pCtxEntry);
LPVOID CtxCookie = pCtxEntry->GetCtxCookie();
m_CtxEntryHash.Remove(CtxCookie);
LOG((LF_INTEROP, LL_INFO100, "Leaked CtxEntry %8.8x with CtxCookie %8.8x, ref count %d\n", pCtxEntry, pCtxEntry->GetCtxCookie(), pCtxEntry->m_dwRefCount));
pCtxEntry->m_dwRefCount = 0;
delete pCtxEntry;
}
}
void CtxEntryCache::Init()
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
// This should never be called more than once.
PRECONDITION(NULL == s_pCtxEntryCache);
}
CONTRACTL_END;
// Allocate the one and only instance of the context entry cache.
s_pCtxEntryCache = new CtxEntryCache();
}
CtxEntryCache* CtxEntryCache::GetCtxEntryCache()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
PRECONDITION(CheckPointer(s_pCtxEntryCache));
}
CONTRACTL_END;
return s_pCtxEntryCache;
}
CtxEntry* CtxEntryCache::CreateCtxEntry(LPVOID pCtxCookie, Thread * pSTAThread)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
CtxEntry * pCtxEntry = NULL;
// If we don't already have a context entry for the context cookie,
// we need to create one.
NewHolder<CtxEntry> pNewCtxEntry = new CtxEntry(pCtxCookie, pSTAThread);
// tiggers GC, can't happen when we hold spin lock
pNewCtxEntry->Init();
{
TAKE_SPINLOCK_AND_DONOT_TRIGGER_GC(&m_Lock);
// double check for race
pCtxEntry = m_CtxEntryHash.Lookup(pCtxCookie);
if (pCtxEntry == NULL)
{
// We successfully allocated and initialized the entry.
m_CtxEntryHash.Add(pNewCtxEntry);
pCtxEntry = pNewCtxEntry.Extract();
}
// We must have an entry now; we need to addref it before
// we leave the lock.
pCtxEntry->AddRef ();
}
return pCtxEntry;
}
CtxEntry* CtxEntryCache::FindCtxEntry(LPVOID pCtxCookie, Thread *pThread)
{
CtxEntry *pCtxEntry = NULL;
Thread *pSTAThread = NULL;
CONTRACT (CtxEntry*)
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
INJECT_FAULT(COMPlusThrowOM());
PRECONDITION(CheckPointer(pCtxCookie));
POSTCONDITION(CheckPointer(RETVAL));
POSTCONDITION(pCtxCookie == pCtxEntry->GetCtxCookie());
POSTCONDITION(pSTAThread == pCtxEntry->GetSTAThread());
}
CONTRACT_END;
// Find our STA (if any)
if (pThread->GetApartment() == Thread::AS_InSTA)
{
// We are in an STA thread. But we may be in a NA context, so do an extra
// check for that case.
BOOL fNAContext;
// try the simple cache on Thread first
if (pCtxCookie != pThread->GetLastSTACtxCookie(&fNAContext))
{
APTTYPE type;
fNAContext = (SUCCEEDED(GetCurrentApartmentTypeNT5((IObjectContext *)pCtxCookie, &type)) && type == APTTYPE_NA);
pThread->SetLastSTACtxCookie(pCtxCookie, fNAContext);
}
if (!fNAContext)
pSTAThread = pThread;
}
ASSERT (GetThreadNULLOk ());
BOOL bFound = FALSE;
ACQUIRE_SPINLOCK_NO_HOLDER(&m_Lock);
{
// Try to find a context entry for the context cookie.
pCtxEntry = m_CtxEntryHash.Lookup(pCtxCookie);
if (pCtxEntry)
{
// We must have an entry now; we need to addref it before
// we leave the lock.
pCtxEntry->AddRef ();
bFound = TRUE;
}
}
RELEASE_SPINLOCK_NO_HOLDER(&m_Lock);
if (!bFound)
{
pCtxEntry = CreateCtxEntry(pCtxCookie, pSTAThread);
}
// Returned the found or allocated entry.
RETURN pCtxEntry;
}
void CtxEntryCache::TryDeleteCtxEntry(LPVOID pCtxCookie)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(CheckPointer(pCtxCookie));
}
CONTRACTL_END;
BOOL bDelete = FALSE;
CtxEntry *pCtxEntry = NULL;
{
TAKE_SPINLOCK_AND_DONOT_TRIGGER_GC(&m_Lock);
// Try to find a context entry for the context cookie.
pCtxEntry = m_CtxEntryHash.Lookup(pCtxCookie);
if (pCtxEntry)
{
// If the ref count of the context entry is still 0, then we can
// remove the ctx entry and delete it.
if (pCtxEntry->m_dwRefCount == 0)
{
// First remove the context entry from the list.
m_CtxEntryHash.Remove(pCtxCookie);
// We need to unlock the context entry cache before we delete the
// context entry since this can cause release to be called on
// an IP which can cause us to re-enter the runtime thus causing a
// deadlock.
// We can now safely delete the context entry.
bDelete = TRUE;
}
}
}
if (bDelete)
{
delete pCtxEntry;
}
}
//================================================================
// Get the RCW associated with this IUnkEntry
// We assert inside Init that this IUnkEntry is indeed within a RCW
RCW *IUnkEntry::GetRCW()
{
LIMITED_METHOD_CONTRACT;
return (RCW *) (((LPBYTE) this) - offsetof(RCW, m_UnkEntry));
}
//================================================================
// Initialize the entry
void IUnkEntry::Init(
IUnknown *pUnk,
BOOL bIsFreeThreaded,
Thread *pThread
DEBUGARG(RCW *pRCW)
)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_PREEMPTIVE;
PRECONDITION(CheckPointer(pUnk));
INDEBUG(PRECONDITION(CheckPointer(pRCW));)
}
CONTRACTL_END;
// Make sure this IUnkEntry is part of a RCW so that we can get back to the RCW through offset
// if we have to
_ASSERTE(((LPBYTE)pRCW) + offsetof(RCW, m_UnkEntry) == (LPBYTE) this);
// Find our context cookie
LPVOID pCtxCookie = GetCurrentCtxCookie();
_ASSERTE(pCtxCookie);
// Set up IUnkEntry's state.
if (bIsFreeThreaded)
m_pCtxEntry = NULL;
else
m_pCtxEntry = CtxEntryCache::GetCtxEntryCache()->FindCtxEntry(pCtxCookie, pThread);
m_pUnknown = pUnk;
m_pCtxCookie = pCtxCookie;
m_pStream = NULL;
// Sanity check this IUnkEntry.
CheckValidIUnkEntry();
}
//================================================================
// Release the interface pointer held by the IUnkEntry.
VOID IUnkEntry::ReleaseInterface(RCW *pRCW)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_PREEMPTIVE;
}
CONTRACTL_END;
if (g_fProcessDetach)
{
// The Release call is unsafe if the process is going away (calls into
// DLLs we don't know are even mapped).
LogInteropLeak(this);
}
else
{
// now release the IUnknown that we hold
if ((m_pUnknown != 0) && (m_pUnknown != (IUnknown *)0xBADF00D))
{
ULONG cbRef = SafeReleasePreemp(m_pUnknown, pRCW);
LogInteropRelease(m_pUnknown, cbRef, "IUnkEntry::Free: Releasing the held ref");
}
// mark the entry as dead
m_pUnknown = (IUnknown *)0xBADF00D;
}
}
//================================================================
// Free the IUnknown entry. ReleaseInterface must have been called.
VOID IUnkEntry::Free()
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_PREEMPTIVE;
PRECONDITION(g_fProcessDetach || m_pUnknown == (IUnknown *)0xBADF00D);
}
CONTRACTL_END;
// Log the de-allocation of the IUnknown entry.
LOG((LF_INTEROP, LL_INFO10000, "IUnkEntry::Free called for context 0x%08X, to release entry with m_pUnknown %p, on thread %p\n", m_pCtxCookie, m_pUnknown, GetThreadNULLOk()));
if (g_fProcessDetach)
{
IStream *pOldStream = m_pStream;
if (InterlockedExchangeT(&m_pStream, NULL) == pOldStream)
SafeReleasePreemp(pOldStream);
}
else
{
IStream *pStream = m_pStream;
m_pStream = NULL;
// This will release the stream, object in the stream and the memory on which the stream was created
if (pStream)
SafeReleaseStream(pStream);
}
// Release the ref count we have on the CtxEntry.
CtxEntry *pEntry = GetCtxEntry();
if (pEntry)
{
pEntry->Release();
m_pCtxEntry = NULL;
}
}
//================================================================
// Get IUnknown for the current context from IUnkEntry
IUnknown* IUnkEntry::GetIUnknownForCurrContext(bool fNoAddRef)
{
CONTRACT (IUnknown*)
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
POSTCONDITION(CheckPointer(RETVAL, (fNoAddRef ? NULL_OK : NULL_NOT_OK)));
}
CONTRACT_END;
IUnknown* pUnk = NULL;
LPVOID pCtxCookie = GetCurrentCtxCookie();
_ASSERTE(pCtxCookie);
CheckValidIUnkEntry();
if (m_pCtxCookie == pCtxCookie || IsFreeThreaded())
{
pUnk = GetRawIUnknown_NoAddRef();
if (!fNoAddRef)
{
RCW_VTABLEPTR(GetRCW());
ULONG cbRef = SafeAddRef(pUnk);
LogInteropAddRef(pUnk, cbRef, "IUnkEntry::GetIUnknownForCurrContext: Addref pUnk, passing ref to caller");
}
}
if (pUnk == NULL && !fNoAddRef)
pUnk = UnmarshalIUnknownForCurrContext();
RETURN pUnk;
}
//================================================================
// Unmarshal IUnknown for the current context from IUnkEntry
IUnknown* IUnkEntry::UnmarshalIUnknownForCurrContext()
{
CONTRACT (IUnknown*)
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(!IsFreeThreaded());
POSTCONDITION(CheckPointer(RETVAL));
}
CONTRACT_END;
HRESULT hrCDH = S_OK;
IUnknown* pUnk = NULL;
BOOL fRetry = TRUE;
BOOL fUnmarshalFailed = FALSE;
BOOL fCallHelper = FALSE;
CheckValidIUnkEntry();
_ASSERTE(GetCtxEntry() != NULL);
if(IsMarshalingInhibited() && (m_pCtxCookie != GetCurrentCtxCookie()))
{
// We want to use an interface in a different context but it can't be marshalled.
LOG((LF_INTEROP, LL_INFO100, "IUnkEntry::GetIUnknownForCurrContext failed as the COM object has inhibited marshaling"));
COMPlusThrow(kInvalidCastException, IDS_EE_CANNOTCAST_NOMARSHAL);
}
// Make sure we are in preemptive GC mode before we call out to COM.
GCX_PREEMP();
// Need to synchronize
while (fRetry)
{
// Marshal the interface to the stream if it hasn't been done yet.
if (m_pStream == NULL)
{
// If context transition failed, we'll return a failure HRESULT
// Otherwise, we return S_OK but m_pStream will stay being NULL
hrCDH = MarshalIUnknownToStreamCallback(this);
CheckForFuncEvalAbort(hrCDH);
}
if (TryUpdateEntry())
{
// If the interface is not marshalable or if we failed to
// enter the context, then we don't have any choice but to
// use the raw IP.
if (m_pStream == NULL)
{
// We retrieved an IP so stop retrying.
fRetry = FALSE;
// Give out this IUnknown we are holding
pUnk = GetRawIUnknown_NoAddRef();
RCW_VTABLEPTR(GetRCW());
ULONG cbRef = SafeAddRefPreemp(pUnk);
LogInteropAddRef(pUnk, cbRef, "UnmarshalIUnknownForCurrContext handing out raw IUnknown");
}
else
{
// we got control for this entry
// GetInterface for the current context
HRESULT hr;
hr = CoUnmarshalInterface(m_pStream, IID_IUnknown, (void **)&pUnk);
// If the objref in the stream times out, we need to go an marshal into the
// stream once again.
if (FAILED(hr))
{
_ASSERTE(m_pStream);
CheckForFuncEvalAbort(hr);
// This should release the stream, object in the stream and the memory on which the stream was created
SafeReleaseStream(m_pStream);
m_pStream = NULL;
// If unmarshal failed twice, then bail out.
if (fUnmarshalFailed)
{
fRetry = FALSE;
// Handing out m_pUnknown in this case would be incorrect. We should fix other places that are doing the same thing in Dev10
// To minimize code changes, throwing E_NOINTERFACE instead
COMPlusThrowHR(E_NOINTERFACE);
}
// Remember we failed to unmarshal.
fUnmarshalFailed = TRUE;
}
else
{
// Reset the stream to the beginning
LARGE_INTEGER li;
LISet32(li, 0);
ULARGE_INTEGER li2;
m_pStream->Seek(li, STREAM_SEEK_SET, &li2);
// Marshal the interface into the stream with appropriate flags
hr = CoMarshalInterface(m_pStream,
IID_IUnknown, pUnk, MSHCTX_INPROC, NULL, MSHLFLAGS_NORMAL);
if (FAILED(hr))
{
CheckForFuncEvalAbort(hr);
// The proxy is no longer valid. This sometimes manifests itself by
// a failure during re-marshaling it to the stream. When this happens,
// we need to release the pUnk we extracted and the stream and try to
// re-create the stream. We don't want to release the stream data since
// we already extracted the proxy from the stream and released it.
RCW_VTABLEPTR(GetRCW());
SafeReleasePreemp(pUnk);
SafeReleasePreemp(m_pStream);
m_pStream = NULL;
}
else
{
// Reset the stream to the beginning
LISet32(li, 0);
m_pStream->Seek(li, STREAM_SEEK_SET, &li2);
// We managed to unmarshal the IP from the stream, stop retrying.
fRetry = FALSE;
}
}
}
// Done with the entry.
EndUpdateEntry();
}
else
{
//================================================================
// We can potentially collide with the COM+ activity lock so spawn off
// another call that does its stream marshalling on the stack without
// the need to do locking.
fCallHelper = TRUE;
fRetry = FALSE;
}
}
if (fCallHelper)
{
// If we hit a collision earlier, spawn off helper that repeats this operation without locking.
pUnk = UnmarshalIUnknownForCurrContextHelper();
}
RETURN pUnk;
}
//================================================================
// Release the stream. This will force UnmarshalIUnknownForCurrContext to transition
// into the context that owns the IP and re-marshal it to the stream.
void IUnkEntry::ReleaseStream()
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
// This should release the stream, object in the stream and the memory on which the stream was created
if (m_pStream)
{
SafeReleaseStream(m_pStream);
m_pStream = NULL;
}
}
// Indicates if the COM component being wrapped by the IUnkEntry aggregates the FTM
bool IUnkEntry::IsFreeThreaded()
{
LIMITED_METHOD_CONTRACT;
return GetRCW()->IsFreeThreaded();
}
// Indicates if the COM component being wrapped by the IUnkEntry implements INoMashal.
bool IUnkEntry::IsMarshalingInhibited()
{
LIMITED_METHOD_CONTRACT;
return GetRCW()->IsMarshalingInhibited();
}
// Helper function to marshal the IUnknown pointer to the stream.
static HRESULT MarshalIUnknownToStreamHelper(IUnknown * pUnknown, IStream ** ppStream)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
IStream *pStream = NULL;
GCX_PREEMP();
// ensure we register this cookie
HRESULT hr = wCoMarshalInterThreadInterfaceInStream(IID_IUnknown, pUnknown, &pStream);
if ((hr == REGDB_E_IIDNOTREG) ||
(hr == E_FAIL) ||
(hr == E_NOINTERFACE) ||
(hr == E_INVALIDARG) ||
(hr == E_UNEXPECTED))
{
// Interface is not marshallable.
pStream = NULL;
hr = S_OK;
}
*ppStream = pStream;
return hr;
}
//================================================================
struct StreamMarshalData
{
IUnkEntry * m_pUnkEntry;
IStream * m_pStream;
};
// Fix for if the lock is held
HRESULT IUnkEntry::MarshalIUnknownToStreamCallback2(LPVOID pData)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(CheckPointer(pData));
PRECONDITION(g_fProcessDetach == FALSE);
}
CONTRACTL_END;
HRESULT hr = S_OK;
StreamMarshalData *psmd = reinterpret_cast<StreamMarshalData *>(pData);
// This should never be called during process detach.
hr = psmd->m_pUnkEntry->HRCheckValidIUnkEntry();
if (hr != S_OK)
{
// Interface not marshallable
// We'll know marshaling failed because m_pStream == NULL
return S_OK;
}
LPVOID pCurrentCtxCookie = GetCurrentCtxCookie();
_ASSERTE(pCurrentCtxCookie);
if (pCurrentCtxCookie == psmd->m_pUnkEntry->m_pCtxCookie)
{
// We are in the right context marshal the IUnknown to the
// stream directly.
hr = MarshalIUnknownToStreamHelper(psmd->m_pUnkEntry->m_pUnknown, &psmd->m_pStream);
}
else
{
// Transition into the context to marshal the IUnknown to
// the stream.
_ASSERTE(psmd->m_pUnkEntry->GetCtxEntry() != NULL);
hr = psmd->m_pUnkEntry->GetCtxEntry()->EnterContext(MarshalIUnknownToStreamCallback2, psmd);
}
return hr;
}
//================================================================
// Unmarshal IUnknown for the current context if the lock is held
IUnknown* IUnkEntry::UnmarshalIUnknownForCurrContextHelper()
{
CONTRACT (IUnknown*)
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(!IsFreeThreaded());
POSTCONDITION(CheckPointer(RETVAL));
}
CONTRACT_END;
HRESULT hrCDH = S_OK;
IUnknown * pUnk = NULL;
SafeComHolder<IStream> spStream;
CheckValidIUnkEntry();
// Make sure we are in preemptive GC mode before we call out to COM.
GCX_PREEMP();
// Marshal the interface to the stream. Any call to this function
// would be from another apartment so marshalling is needed.
StreamMarshalData smd = {this, NULL};
// If context transition failed, we'll return a failure HRESULT
// Otherwise, we return S_OK but m_pStream will stay being NULL
hrCDH = MarshalIUnknownToStreamCallback2(&smd);
spStream = smd.m_pStream;
smd.m_pStream = NULL;
CheckForFuncEvalAbort(hrCDH);
// If the interface is not marshalable or if we failed to
// enter the context, then we don't have any choice but to
// use the raw IP.
if (spStream == NULL)
{
// Give out this IUnknown we are holding
pUnk = GetRawIUnknown_NoAddRef();
RCW_VTABLEPTR(GetRCW());
ULONG cbRef = SafeAddRefPreemp(pUnk);
LogInteropAddRef(pUnk, cbRef, "UnmarshalIUnknownForCurrContext handing out raw IUnknown");
}
else
{
// we got control for this entry
// GetInterface for the current context
HRESULT hr;
hr = CoUnmarshalInterface(spStream, IID_IUnknown, reinterpret_cast<void**>(&pUnk));
spStream.Release();
if (FAILED(hr))
{
CheckForFuncEvalAbort(hr);
// Give out this IUnknown we are holding
pUnk = GetRawIUnknown_NoAddRef();
RCW_VTABLEPTR(GetRCW());
ULONG cbRef = SafeAddRefPreemp(pUnk);
LogInteropAddRef(pUnk, cbRef, "UnmarshalIUnknownForCurrContext handing out raw IUnknown");
}
}
RETURN pUnk;
}
//================================================================
// Callback called to marshal the IUnknown into a stream lazily.
HRESULT IUnkEntry::MarshalIUnknownToStreamCallback(LPVOID pData)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(CheckPointer(pData));
PRECONDITION(g_fProcessDetach == FALSE);
}
CONTRACTL_END;
HRESULT hr = S_OK;
IUnkEntry *pUnkEntry = (IUnkEntry *)pData;
// This should never be called during process detach.
hr = pUnkEntry->HRCheckValidIUnkEntry();
if (hr != S_OK)
{
// Interface not marshallable
// We'll know marshaling failed because m_pStream == NULL
return S_OK;
}
LPVOID pCurrentCtxCookie = GetCurrentCtxCookie();
_ASSERTE(pCurrentCtxCookie);
if (pCurrentCtxCookie == pUnkEntry->m_pCtxCookie)
{
// We are in the right context marshal the IUnknown to the
// stream directly.
hr = pUnkEntry->MarshalIUnknownToStream();
}
else
{