summaryrefslogtreecommitdiff
path: root/src/vm/appdomainstack.h
blob: 1390364e474ec37ae2bf19c1c2b3b24c2ebff4be (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// Appdomainstack.h -
//


//


#ifndef __appdomainstack_h__
#define __appdomainstack_h__

#include "vars.hpp"
#include "util.hpp"


// Stack of AppDomains executing on the current thread. Used in security optimization to avoid stackwalks
#define ADSTACK_BLOCK_SIZE        16
#define INVALID_APPDOMAIN_ID ((DWORD)-1)
#define CURRENT_APPDOMAIN_ID ((ADID)(DWORD)0)
#define __GetADID(index)   ((index)<ADSTACK_BLOCK_SIZE?m_pStack[(index)].m_domainID:m_pExtraStack[((index)-ADSTACK_BLOCK_SIZE)].m_domainID)
#define __GetEntryPtr(index) ((index)<ADSTACK_BLOCK_SIZE?&(m_pStack[(index)]):&(m_pExtraStack[((index)-ADSTACK_BLOCK_SIZE)]))

struct AppDomainStackEntry
{
    ADID  m_domainID;
    DWORD m_dwOverridesCount;
    DWORD m_dwAsserts;
    DWORD m_dwPreviousThreadWideSpecialFlags;

    FORCEINLINE bool operator==(const AppDomainStackEntry& entry) const
    {
        return (m_domainID == entry.m_domainID &&
                m_dwOverridesCount == entry.m_dwOverridesCount &&
                m_dwAsserts == entry.m_dwAsserts);
    }
    FORCEINLINE bool operator!=(const AppDomainStackEntry& entry) const
    {
        return (m_domainID != entry.m_domainID ||
                m_dwOverridesCount != entry.m_dwOverridesCount ||
                m_dwAsserts != entry.m_dwAsserts);

    }
    BOOL IsFullyTrustedWithNoStackModifiers(void);
    BOOL IsHomogeneousWithNoStackModifiers(void);
    BOOL HasFlagsOrFullyTrustedWithNoStackModifiers(DWORD flags);
#ifndef DACCESS_COMPILE    
    void UpdateHomogeneousPLS(OBJECTREF* homogeneousPLS);
#endif
};

class AppDomainStack
{
public:
    AppDomainStack() : m_numEntries(0), m_pExtraStack(NULL), m_ExtraStackSize(0), m_dwOverridesCount(0), m_dwAsserts(0), m_dwThreadWideSpecialFlags(0xFFFFFFFF)
    {
        LIMITED_METHOD_CONTRACT;
        FillEntries(m_pStack, ADSTACK_BLOCK_SIZE);
    }

    AppDomainStack(const AppDomainStack& stack):m_numEntries(0), m_pExtraStack(NULL), m_ExtraStackSize(0), m_dwOverridesCount(0), m_dwAsserts(0)
    {
        CONTRACTL {
            THROWS;
            GC_NOTRIGGER;
            MODE_ANY;
        }
        CONTRACTL_END;

        m_dwThreadWideSpecialFlags = stack.m_dwThreadWideSpecialFlags;
        m_numEntries = stack.m_numEntries;
        m_dwOverridesCount = stack.m_dwOverridesCount;
        m_dwAsserts = stack.m_dwAsserts;
        LOG((LF_APPDOMAIN, LL_INFO100, "copy ctor: m_dwAsserts:%d stack.m_dwAsserts:%d\n",m_dwAsserts, stack.m_dwAsserts));
        memcpy(m_pStack, stack.m_pStack, sizeof( AppDomainStackEntry) * ADSTACK_BLOCK_SIZE);
        // If there is anything stored in the extra allocated space, copy that over
        if (m_numEntries > ADSTACK_BLOCK_SIZE)
        {
            // #blocks to allocate = ceil(numDomains/blocksize) - 1 = ceil ((numdomains - blocksize)/blocksize) = numdomains/blocksize
            DWORD numBlocks = m_numEntries/ADSTACK_BLOCK_SIZE; 
            m_ExtraStackSize = numBlocks*ADSTACK_BLOCK_SIZE;
            m_pExtraStack = new AppDomainStackEntry[m_ExtraStackSize];
            memcpy(m_pExtraStack, stack.m_pExtraStack, sizeof(AppDomainStackEntry)*(m_numEntries-ADSTACK_BLOCK_SIZE));
            FillEntries((m_pExtraStack+m_numEntries-ADSTACK_BLOCK_SIZE), (m_ExtraStackSize -(m_numEntries-ADSTACK_BLOCK_SIZE)));
        }
    }

    ~AppDomainStack()
    {
        CONTRACTL
        {
            MODE_ANY;
            GC_NOTRIGGER;
            NOTHROW;
        } CONTRACTL_END;
        if (m_pExtraStack != NULL)
            delete[] m_pExtraStack;
        m_pExtraStack = NULL;
        m_ExtraStackSize = 0;
    }

    bool operator!= (const AppDomainStack& stack) const
    {
        return !(*this == stack);
    }

    bool operator== (const AppDomainStack& stack) const
    {
        LIMITED_METHOD_CONTRACT;
        if (this == &stack) // degenerate case: comparing with self
            return true;
        if (this->m_numEntries != stack.m_numEntries || 
            this->m_dwAsserts != stack.m_dwAsserts || 
            this->m_dwOverridesCount != stack.m_dwOverridesCount)
            return false;
        for (unsigned i =0; i < stack.m_numEntries; i++)
        {
            if (i < ADSTACK_BLOCK_SIZE)
            {
                if (this->m_pStack[i] != stack.m_pStack[i])
                    return false;
            }
            else
            {
                if (this->m_pExtraStack[i-ADSTACK_BLOCK_SIZE] != stack.m_pExtraStack[i-ADSTACK_BLOCK_SIZE])
                    return false;
            }
        }
        return true;
    }
    inline AppDomainStack& operator =(const AppDomainStack& stack)
    {
        CONTRACTL {
            THROWS;
            GC_NOTRIGGER;
            MODE_ANY;
        }
        CONTRACTL_END;

        // Degenerate case (assigning x = x)
        if (this == &stack)
            return *this;

        m_dwThreadWideSpecialFlags = stack.m_dwThreadWideSpecialFlags;
        m_numEntries = stack.m_numEntries;
        m_dwOverridesCount = stack.m_dwOverridesCount;
        m_dwAsserts = stack.m_dwAsserts;
        LOG((LF_APPDOMAIN, LL_INFO100, "= operator : m_dwAsserts:%d stack.m_dwAsserts:%d\n",m_dwAsserts, stack.m_dwAsserts));
        memcpy(m_pStack, stack.m_pStack, sizeof( AppDomainStackEntry) * ADSTACK_BLOCK_SIZE);
        // If there is anything stored in the extra allocated space, copy that over
        if (m_numEntries > ADSTACK_BLOCK_SIZE)
        {
            // #blocks to allocate = ceil(numDomains/blocksize) - 1 = ceil ((numdomains - blocksize)/blocksize) = numdomains/blocksize
            DWORD numBlocks = m_numEntries/ADSTACK_BLOCK_SIZE; 
            if (m_ExtraStackSize < numBlocks*ADSTACK_BLOCK_SIZE)
            {
                // free ptr if it exists
                if (m_pExtraStack != NULL)
                    delete[] m_pExtraStack;
                m_pExtraStack = NULL;

                m_ExtraStackSize = numBlocks*ADSTACK_BLOCK_SIZE;
                m_pExtraStack = new AppDomainStackEntry[m_ExtraStackSize];
            }

            memset(m_pExtraStack, 0xFF, sizeof(ADID) * numBlocks);
            memcpy(m_pExtraStack, stack.m_pExtraStack, sizeof(AppDomainStackEntry)*(m_numEntries-ADSTACK_BLOCK_SIZE));
            FillEntries((m_pExtraStack+m_numEntries-ADSTACK_BLOCK_SIZE), (m_ExtraStackSize -(m_numEntries-ADSTACK_BLOCK_SIZE)));
        }
    
        return *this;
    }

    inline void PushDomain(ADID pDomain);
    inline ADID PopDomain();

    inline void InitDomainIteration(DWORD *pIndex) const;
    // Gets the next AD on the stack
    inline ADID GetNextDomainOnStack(DWORD *pIndex, DWORD *pOverrides, DWORD *pAsserts) const;
    inline AppDomainStackEntry* GetNextDomainEntryOnStack(DWORD *pIndex);
    inline AppDomainStackEntry* GetCurrentDomainEntryOnStack(DWORD pIndex);
    // Updates the asserts/overrides on the next AD on the stack
    inline void UpdateDomainOnStack(DWORD pIndex, DWORD asserts, DWORD overrides);
    inline DWORD   GetNumDomains() const;
    inline void ClearDomainStack();
    inline DWORD GetThreadWideSpecialFlag() const;
    inline DWORD IncrementOverridesCount();
    inline DWORD DecrementOverridesCount();
    inline DWORD GetOverridesCount();
    inline DWORD GetInnerAppDomainOverridesCount();
    inline DWORD IncrementAssertCount();
    inline DWORD DecrementAssertCount();
    inline DWORD GetAssertCount();
    inline DWORD GetInnerAppDomainAssertCount();
    bool IsDefaultSecurityInfo() const;
    BOOL AllDomainsHomogeneousWithNoStackModifiers();

private:
    inline void AddMoreDomains(void);
    inline AppDomainStackEntry* ReadTopOfStack();
    void UpdateStackFromEntries();
    static void FillEntries(AppDomainStackEntry ptr[], DWORD size)
    {
        CONTRACTL 
        {
            MODE_ANY;
            GC_NOTRIGGER;
            NOTHROW;
        }CONTRACTL_END;
        _ASSERTE(ptr != NULL);
        DWORD i;
        const AppDomainStackEntry tmp_entry = {ADID(INVALID_APPDOMAIN_ID), 0, 0};
        for(i=0;i<size;i++)
            ptr[i]=tmp_entry;
    }

#ifdef _DEBUG
    inline void LogADStackUpdate(void);
    void CheckOverridesAssertCounts(); // Debug only code to check that assert count/overrides count are always in sync across adstack
#endif

    DWORD       m_numEntries;
    AppDomainStackEntry m_pStack[ADSTACK_BLOCK_SIZE];
    AppDomainStackEntry *m_pExtraStack;
    DWORD       m_ExtraStackSize;
    DWORD m_dwOverridesCount; // across all entries
    DWORD m_dwAsserts; // across all entries
    DWORD       m_dwThreadWideSpecialFlags; // this flag records the last evaluated thread wide security state
};
#endif