summaryrefslogtreecommitdiff
path: root/src/vm/stackcompressor.cpp
blob: 97181bb43c043715c495faf9a0a37f0c24f62b31 (plain)
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// 


// 


#include "common.h"

#ifdef FEATURE_COMPRESSEDSTACK

#include "stackcompressor.h"
#include "securitystackwalk.h"
#include "appdomainstack.inl"
#include "comdelegate.h"

//-----------------------------------------------------------
// Stack walk callback data structure for stack compress.
//-----------------------------------------------------------
typedef struct _StackCompressData
{
    void*    compressedStack;
    StackCrawlMark *    stackMark;
    DWORD               dwFlags;
    Assembly *          prevAssembly; // Previously checked assembly.
    AppDomain *         prevAppDomain;
    Frame* pCtxTxFrame;
} StackCompressData;


void TurnSecurityStackWalkProgressOn( Thread* pThread ) 
{ 
    WRAPPER_NO_CONTRACT;
    pThread->SetSecurityStackwalkInProgress( TRUE ); 
}
void TurnSecurityStackWalkProgressOff( Thread* pThread ) 
{ 
    WRAPPER_NO_CONTRACT;
    pThread->SetSecurityStackwalkInProgress( FALSE ); 
}
typedef Holder< Thread*, TurnSecurityStackWalkProgressOn, TurnSecurityStackWalkProgressOff > StackWalkProgressEnableHolder;



DWORD StackCompressor::GetCSInnerAppDomainOverridesCount(COMPRESSEDSTACKREF csRef)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;
    //csRef can be NULL - that implies that we set the CS, then crossed an AD. So we would already have counted the overrides when we hit the
    // ctxTxFrame. Nothing to do here
    if (csRef != NULL)
    {
        NewCompressedStack* cs = (NewCompressedStack*)csRef->GetUnmanagedCompressedStack();
        if (cs != NULL)
            return cs->GetInnerAppDomainOverridesCount();
    }
    return 0;
}
DWORD StackCompressor::GetCSInnerAppDomainAssertCount(COMPRESSEDSTACKREF csRef)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        SO_TOLERANT;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;
    //csRef can be NULL - that implies that we set the CS, then crossed an AD. So we would already have counted the overrides when we hit the
    // ctxTxFrame. Nothing to do here
    if (csRef != NULL)
    {
        NewCompressedStack* cs = (NewCompressedStack*)csRef->GetUnmanagedCompressedStack();
        if (cs != NULL)
            return cs->GetInnerAppDomainAssertCount();
    }
    return 0;
}

void* StackCompressor::SetAppDomainStack(Thread* pThread, void* curr)
{
    CONTRACTL
    {
        MODE_ANY;
        GC_NOTRIGGER;
        THROWS;
    } CONTRACTL_END;

    NewCompressedStack* unmanagedCompressedStack = (NewCompressedStack *)curr;

    AppDomainStack* pRetADStack = NULL;
       
    if (unmanagedCompressedStack != NULL)
    {
        pRetADStack = new AppDomainStack(pThread->GetAppDomainStack());
        pThread->SetAppDomainStack(unmanagedCompressedStack->GetAppDomainStack() );
    }
    else
    {
        if (!pThread->IsDefaultSecurityInfo())    /* Do nothing for the single domain/FT/no overrides case */
        {
            pRetADStack = new AppDomainStack(pThread->GetAppDomainStack());            
            pThread->ResetSecurityInfo(); 
        }
    }
    return (void*)pRetADStack;
}

void StackCompressor::RestoreAppDomainStack(Thread* pThread, void* appDomainStack)
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(appDomainStack != NULL);
    AppDomainStack* pADStack = (AppDomainStack*)appDomainStack;
    pThread->SetAppDomainStack(*pADStack);
    delete pADStack;
}

void StackCompressor::Destroy(void *stack)
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE(stack != NULL && "Don't pass NULL");
    NewCompressedStack* ncs = (NewCompressedStack*)stack;
    ncs->Destroy();
}


/* Forward declarations of the new CS stackwalking implementation */
static void NCS_GetCompressedStackWorker(Thread *t, void *pData);
static StackWalkAction NCS_CompressStackCB(CrawlFrame* pCf, void *pData);

OBJECTREF StackCompressor::GetCompressedStack( StackCrawlMark* stackMark, BOOL fWalkStack )
{
   
    CONTRACTL {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        SO_INTOLERANT;// Not an entry point
    } CONTRACTL_END;

    // Get the current thread that we're to walk.
    Thread * t = GetThread();    

    NewCompressedStackHolder csHolder(new NewCompressedStack());

        
    if (fWalkStack)
    {
        //
        // Initialize the callback data on the stack...        
        //

        StackCompressData walkData;
        
        walkData.dwFlags = 0;
        walkData.prevAssembly = NULL;
        walkData.prevAppDomain = NULL;
        walkData.stackMark = stackMark;
        walkData.pCtxTxFrame = NULL;


        walkData.compressedStack = (void*)csHolder.GetValue();
        NCS_GetCompressedStackWorker(t, &walkData);
    }

    struct _gc {
        SAFEHANDLE pSafeCSHandle;
    } gc;
    gc.pSafeCSHandle = NULL;
    
    GCPROTECT_BEGIN(gc);
    
    gc.pSafeCSHandle = (SAFEHANDLE) AllocateObject(MscorlibBinder::GetClass(CLASS__SAFE_CSHANDLE));
    CallDefaultConstructor(gc.pSafeCSHandle);
    gc.pSafeCSHandle->SetHandle((void*) csHolder.GetValue());
    csHolder.SuppressRelease();
    
    GCPROTECT_END();
    return (OBJECTREF) gc.pSafeCSHandle;
}

    
void NCS_GetCompressedStackWorker(Thread *t, void *pData)
{
    CONTRACTL {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    } CONTRACTL_END;

    StackCompressData *pWalkData = (StackCompressData*)pData;
    NewCompressedStack* compressedStack = (NewCompressedStack*) pWalkData->compressedStack;

    _ASSERTE( t != NULL );

    {
        StackWalkProgressEnableHolder holder( t );

        //
        // Begin the stack walk...
        //
        // LIGHTUNWIND flag: allow using stackwalk cache for security stackwalks
        // SKIPFUNCLETS flag: stop processing the stack after completing the current funclet (for instance
        // only process the catch block on x64, not the throw block)
        t->StackWalkFrames(NCS_CompressStackCB, pWalkData, SKIPFUNCLETS | LIGHTUNWIND);


        // Ignore CS (if present) when we hit a FT assert
        if (pWalkData->dwFlags & CORSEC_FT_ASSERT)
            return;
        
        // Check if there is a CS at the top of the thread
        COMPRESSEDSTACKREF csRef = (COMPRESSEDSTACKREF)t->GetCompressedStack();
        AppDomain *pAppDomain = t->GetDomain();
        Frame *pFrame = NULL;
#ifdef FEATURE_REMOTING
        if (csRef == NULL)
        {
            // There may have been an AD transition and we shd look at the CB data to see if this is the case
            if (pWalkData->pCtxTxFrame != NULL)
            {
                pFrame = pWalkData->pCtxTxFrame;
                csRef = Security::GetCSFromContextTransitionFrame(pFrame);
                _ASSERTE(csRef != NULL); //otherwise we would not have saved the frame in the CB data
                pAppDomain = pWalkData->pCtxTxFrame->GetReturnDomain();
            }
        }
#endif // FEATURE_REMOTING

        if (csRef != NULL)
        {
            

            compressedStack->ProcessCS(pAppDomain, csRef, pFrame);
        }
        else
        {
            compressedStack->ProcessAppDomainTransition(); // just to update domain overrides/assert count at the end of stackwalk
        }


    }

    return;      
}

StackWalkAction NCS_CompressStackCB(CrawlFrame* pCf, void *pData)
{
    CONTRACTL {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    } CONTRACTL_END;

    StackCompressData *pCBdata = (StackCompressData*)pData;
    NewCompressedStack* compressedStack = (NewCompressedStack*) pCBdata->compressedStack;

    // First check if the walk has skipped the required frames. The check
    // here is between the address of a local variable (the stack mark) and a
    // pointer to the EIP for a frame (which is actually the pointer to the
    // return address to the function from the previous frame). So we'll
    // actually notice which frame the stack mark was in one frame later. This
    // is fine for our purposes since we're always looking for the frame of the
    // caller of the method that actually created the stack mark. 
    _ASSERTE((pCBdata->stackMark == NULL) || (*pCBdata->stackMark == LookForMyCaller));
    if ((pCBdata->stackMark != NULL) &&
        !pCf->IsInCalleesFrames(pCBdata->stackMark))
        return SWA_CONTINUE;

    Frame *pFrame = pCf->GetFrame();

#ifdef FEATURE_REMOTING
    // Save the CtxTxFrame if this is one
    if (pCBdata->pCtxTxFrame == NULL)
    {
        if (Security::IsContextTransitionFrameWithCS(pFrame))
        {
            pCBdata->pCtxTxFrame = pFrame;
        }
    }
#endif // FEATURE_REMOTING

    //  Handle AppDomain transitions:
    AppDomain *pAppDomain = pCf->GetAppDomain();
    if (pCBdata->prevAppDomain != pAppDomain)
    {
#ifndef FEATURE_REMOTING
        BOOL bRealAppDomainTransition = (pCBdata->prevAppDomain != NULL);

        // For a "real" appdomain transition, we can stop the stackwalk since there's no managed AD transitions
        // without remoting. The "real" here denotes that this is not the first appdomain transition (from NULL to current)
        // that happens on the first crawlframe we see on a stackwalk. Also don't do the final ad transition here (that'll happen 
        // outside the callback)
        if (bRealAppDomainTransition)
        {
            return SWA_ABORT;
        }
        else
#endif // !FEATURE_REMOTING
        {
            compressedStack->ProcessAppDomainTransition();    
            pCBdata->prevAppDomain = pAppDomain;
        }
            
    }


    if (pCf->GetFunction() == NULL)
        return SWA_CONTINUE; // not a function frame, so we were just looking for CtxTransitionFrames. Resume the stackwalk...
        
    // Get the security object for this function...
    OBJECTREF* pRefSecDesc = pCf->GetAddrOfSecurityObject();

    MethodDesc * pFunc = pCf->GetFunction();

    _ASSERTE(pFunc != NULL); // we requested methods!

    Assembly * pAssem = pCf->GetAssembly();
    _ASSERTE(pAssem != NULL);
    PREFIX_ASSUME(pAssem != NULL);



    
    if (pRefSecDesc != NULL)
        SecurityDeclarative::DoDeclarativeSecurityAtStackWalk(pFunc, pAppDomain, pRefSecDesc);

    
    
    if (pFunc->GetMethodTable()->IsDelegate())
    {
        DelegateEEClass* delegateCls = (DelegateEEClass*) pFunc->GetMethodTable()->GetClass();
        if (pFunc == delegateCls->m_pBeginInvokeMethod)
        {
            // Async delegate case: we may need to insert the creator frame into the CS 
            DELEGATEREF dRef = (DELEGATEREF) ((FramedMethodFrame *)pFrame)->GetThis();
            _ASSERTE(dRef);
            if (COMDelegate::IsSecureDelegate(dRef))
            {
                if (!dRef->IsWrapperDelegate())
                {
                    MethodDesc* pCreatorMethod = (MethodDesc*) dRef->GetMethodPtrAux();
                    Assembly* pCreatorAssembly = pCreatorMethod->GetAssembly();
                    compressedStack->ProcessFrame(pAppDomain,
                                                  NULL,
                                                  NULL,
                                                  pCreatorAssembly->GetSharedSecurityDescriptor(), 
                                                  NULL) ; // ignore return value - No FSD being passed in.
                }
            }
            
        }
    }
        

    DWORD retFlags = compressedStack->ProcessFrame(pAppDomain, 
                                                   pAssem,
                                                   pFunc,
                                                   pAssem->GetSharedSecurityDescriptor(), 
                                                   (FRAMESECDESCREF *) pRefSecDesc) ;
    
    pCBdata->dwFlags |= (retFlags & CORSEC_FT_ASSERT);
    // ProcessFrame returns TRUE if we should stop stackwalking
   if (retFlags != 0 || Security::IsSpecialRunFrame(pFunc))
        return SWA_ABORT;
   
    return SWA_CONTINUE;

}
#endif // #ifdef FEATURE_COMPRESSEDSTACK