summaryrefslogtreecommitdiff
path: root/src/debug/di/rsassembly.cpp
blob: e390f77bc4dbc745f51edb6971d47ec4a1a7a94a (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
// 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.
//*****************************************************************************
// File: RsAssembly.cpp
// 

//
//*****************************************************************************
#include "stdafx.h"
#include "primitives.h"
#include "safewrap.h"

#include "check.h"

#include <tlhelp32.h>
#include "wtsapi32.h"

#ifndef SM_REMOTESESSION
#define SM_REMOTESESSION 0x1000
#endif

#include "corpriv.h"
#include "../../dlls/mscorrc/resource.h"
#include <limits.h>


/* ------------------------------------------------------------------------- *
 * Assembly class
 * ------------------------------------------------------------------------- */
CordbAssembly::CordbAssembly(CordbAppDomain *       pAppDomain,
                             VMPTR_Assembly         vmAssembly,
                             VMPTR_DomainAssembly   vmDomainAssembly)

    : CordbBase(pAppDomain->GetProcess(),
                vmDomainAssembly.IsNull() ? VmPtrToCookie(vmAssembly) : VmPtrToCookie(vmDomainAssembly),
                enumCordbAssembly),
      m_vmAssembly(vmAssembly),
      m_vmDomainAssembly(vmDomainAssembly),
      m_pAppDomain(pAppDomain)
{
    _ASSERTE(!vmAssembly.IsNull());
}

/*
    A list of which resources owned by this object are accounted for.

    public:
        CordbAppDomain      *m_pAppDomain; // Assigned w/o addRef(), Deleted in ~CordbAssembly
*/

CordbAssembly::~CordbAssembly()
{
}

HRESULT CordbAssembly::QueryInterface(REFIID id, void **ppInterface)
{
    if (id == IID_ICorDebugAssembly)
        *ppInterface = static_cast<ICorDebugAssembly*>(this);
    else if (id == IID_ICorDebugAssembly2)
        *ppInterface = static_cast<ICorDebugAssembly2*>(this);
    else if (id == IID_IUnknown)
        *ppInterface = static_cast<IUnknown*>( static_cast<ICorDebugAssembly*>(this) );
    else
    {
        *ppInterface = NULL;
        return E_NOINTERFACE;
    }

    ExternalAddRef();
    return S_OK;
}

// Neutered by AppDomain
void CordbAssembly::Neuter()
{
    m_pAppDomain = NULL;
    CordbBase::Neuter();
}


#ifdef _DEBUG
//---------------------------------------------------------------------------------------
// Callback helper for code:CordbAssembly::DbgAssertAssemblyDeleted
// 
// Arguments
//    vmDomainAssembly - domain file in the enumeration
//    pUserData - pointer to the CordbAssembly that we just got an exit event for.
//    

// static
void CordbAssembly::DbgAssertAssemblyDeletedCallback(VMPTR_DomainAssembly vmDomainAssembly, void * pUserData)
{
    CordbAssembly * pThis = reinterpret_cast<CordbAssembly * >(pUserData);
    INTERNAL_DAC_CALLBACK(pThis->GetProcess());

    VMPTR_DomainAssembly vmAssemblyDeleted = pThis->m_vmDomainAssembly;
    
    CONSISTENCY_CHECK_MSGF((vmAssemblyDeleted != vmDomainAssembly), 
        ("An Assembly Unload event was sent, but the assembly still shows up in the enumeration.\n vmAssemblyDeleted=%p\n", 
        VmPtrToCookie(vmAssemblyDeleted)));        
}

//---------------------------------------------------------------------------------------
// Assert that a assembly is no longer discoverable via enumeration.
//
// Notes:
//   See code:IDacDbiInterface#Enumeration for rules that we're asserting.
//   This is a debug only method. It's conceptually similar to 
//   code:CordbProcess::DbgAssertAppDomainDeleted. 
//
void CordbAssembly::DbgAssertAssemblyDeleted()
{
    GetProcess()->GetDAC()->EnumerateAssembliesInAppDomain(
        GetAppDomain()->GetADToken(),
        CordbAssembly::DbgAssertAssemblyDeletedCallback,
        this);
}
#endif // _DEBUG

/*
 * GetProcess returns the process containing the assembly
 */
HRESULT CordbAssembly::GetProcess(ICorDebugProcess **ppProcess)
{
    PUBLIC_API_ENTRY(this);
    FAIL_IF_NEUTERED(this);
    VALIDATE_POINTER_TO_OBJECT(ppProcess, ICorDebugProcess **);

    return (m_pAppDomain->GetProcess (ppProcess));
}

//
// Returns the AppDomain that this assembly belongs to. 
//
// Arguments:
//    ppAppDomain - a non-NULL pointer to store the AppDomain in.
//
// Return Value:
//    S_OK
//
// Notes:
//   On the debugger right-side we currently consider every assembly to belong
//   to a single AppDomain, and create multiple CordbAssembly instances (one
//   per AppDomain) to represent domain-neutral assemblies.
//
HRESULT CordbAssembly::GetAppDomain(ICorDebugAppDomain **ppAppDomain)
{
    PUBLIC_API_ENTRY(this);
    FAIL_IF_NEUTERED(this);
    VALIDATE_POINTER_TO_OBJECT(ppAppDomain, ICorDebugAppDomain **);

    _ASSERTE(m_pAppDomain != NULL);

    *ppAppDomain = static_cast<ICorDebugAppDomain *> (m_pAppDomain);
    m_pAppDomain->ExternalAddRef();

    return S_OK;
}



/*
 * EnumerateModules enumerates all modules in the assembly
 */
HRESULT CordbAssembly::EnumerateModules(ICorDebugModuleEnum **ppModules)
{
    HRESULT hr = S_OK;
    PUBLIC_API_BEGIN(this);
    {
        ValidateOrThrow(ppModules);
        *ppModules = NULL;

        m_pAppDomain->PrepopulateModules();

        RSInitHolder<CordbEnumFilter> pModEnum(
            new CordbEnumFilter(GetProcess(), GetProcess()->GetContinueNeuterList()));
        
        RSInitHolder<CordbHashTableEnum> pEnum;
        
        CordbHashTableEnum::BuildOrThrow(
            this, 
            NULL,  // ownership
            &m_pAppDomain->m_modules,
            IID_ICorDebugModuleEnum,
            pEnum.GetAddr());
        
        // this will build up an auxillary list. Don't need pEnum after this.
        hr = pModEnum->Init(pEnum, this);
        IfFailThrow(hr);    

        pModEnum.TransferOwnershipExternal(ppModules);
       
    } 
    PUBLIC_API_END(hr);

    return hr;
}


/*
 * GetCodeBase returns the code base used to load the assembly
 */
HRESULT CordbAssembly::GetCodeBase(ULONG32 cchName,
                    ULONG32 *pcchName,
                    __out_ecount_part_opt(cchName, *pcchName) WCHAR szName[])
{
    PUBLIC_API_ENTRY(this);
    FAIL_IF_NEUTERED(this);
    VALIDATE_POINTER_TO_OBJECT_ARRAY(szName, WCHAR, cchName, true, true);
    VALIDATE_POINTER_TO_OBJECT_OR_NULL(pcchName, ULONG32 *);

    return E_NOTIMPL;
}

//
// Gets the filename of the assembly
//
// Arguments:
//   cchName - number of characters available in szName, or 0 to query length
//   pcchName - optional pointer to store the real length of the filename
//   szName - buffer in which to copy the filename, or NULL if cchName is 0.
//
// Return value:
//   S_OK on success (even if there is no filename).
//   An error code if the filename could not be read for the assembly.  This should
//   not happen unless the target is corrupt.
//
// Notes:
//   In-memory assemblies do not have a filename.  In that case, for compatibility
//   this returns success and the string "<unknown>".  We may want to change this
//   behavior in the future.
//
HRESULT CordbAssembly::GetName(ULONG32 cchName,
                               ULONG32 *pcchName,
                               __out_ecount_part_opt(cchName, *pcchName) WCHAR szName[])
{
    PUBLIC_API_ENTRY(this);
    FAIL_IF_NEUTERED(this);
    VALIDATE_POINTER_TO_OBJECT_ARRAY_OR_NULL(szName, WCHAR, cchName, true, true);
    VALIDATE_POINTER_TO_OBJECT_OR_NULL(pcchName, ULONG32 *);

    HRESULT hr = S_OK;
    EX_TRY
    {
        // Lazily initialize our cache of the assembly filename.  
        // Note that if this fails, we'll try again next time this is called.
        // This can be convenient for transient errors and debugging purposes, but could cause a 
        // performance problem if failure was common (it should not be).
        if (!m_strAssemblyFileName.IsSet())
        {
            IDacDbiInterface * pDac = m_pProcess->GetDAC(); // throws 
            BOOL fNonEmpty = pDac->GetAssemblyPath(m_vmAssembly, &m_strAssemblyFileName); // throws
            _ASSERTE(m_strAssemblyFileName.IsSet());
                   

            if (!fNonEmpty)
            {
                // File name is empty (eg. for an in-memory assembly)
                _ASSERTE(m_strAssemblyFileName.IsEmpty());

                // Construct a fake name
                // This seems unwise - the assembly doesn't have a filename, we should probably just return
                // an empty string and S_FALSE.  This is a common case (in-memory assemblies), I don't see any reason to
                // fake up a filename to pretend that it has a disk location when it doesn't.
                // But I don't want to break tests at the moment that expect this.
                // Note that all assemblies have a simple metadata name - perhaps we should have an additional API for that.
                m_strAssemblyFileName.AssignCopy(W("<unknown>"));
            }
        }

        // We should now have a non-empty string
        _ASSERTE(m_strAssemblyFileName.IsSet());
        _ASSERTE(!m_strAssemblyFileName.IsEmpty());

        // Copy it out to our caller
    }
    EX_CATCH_HRESULT(hr);
    if (FAILED(hr)) 
    {
        return hr;
    }
    return CopyOutString(m_strAssemblyFileName, cchName, pcchName, szName);
}

HRESULT CordbAssembly::IsFullyTrusted( BOOL *pbFullyTrusted )
{
    PUBLIC_API_ENTRY(this);
    FAIL_IF_NEUTERED(this);
    ATT_REQUIRE_STOPPED_MAY_FAIL(GetProcess());
    VALIDATE_POINTER_TO_OBJECT(pbFullyTrusted, BOOL*);
    
    if (m_vmDomainAssembly.IsNull())
        return E_UNEXPECTED;

    // Check for cached result 
    if( m_foptIsFullTrust.HasValue() )
    {
        *pbFullyTrusted = m_foptIsFullTrust.GetValue();
        return S_OK;
    }

    HRESULT hr = S_OK;
    EX_TRY
    {

        CordbProcess * pProcess = m_pAppDomain->GetProcess();
        IDacDbiInterface * pDac = pProcess->GetDAC();

        BOOL fIsFullTrust = pDac->IsAssemblyFullyTrusted(m_vmDomainAssembly);
        
        // Once the trust level of an assembly is known, it cannot change.
        m_foptIsFullTrust = fIsFullTrust;

        *pbFullyTrusted = fIsFullTrust;    
    }
    EX_CATCH_HRESULT(hr);
    return hr;        
}