forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembly.cpp
More file actions
296 lines (249 loc) · 7.63 KB
/
assembly.cpp
File metadata and controls
296 lines (249 loc) · 7.63 KB
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
// 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.
// ============================================================
//
// Assembly.cpp
//
//
// Implements the Assembly class
//
// ============================================================
#include "common.h"
#include "assembly.hpp"
#include "utils.hpp"
namespace BINDER_SPACE
{
namespace
{
BOOL IsPlatformArchitecture(PEKIND kArchitecture)
{
return ((kArchitecture != peMSIL) && (kArchitecture != peNone));
}
};
STDMETHODIMP Assembly::QueryInterface(REFIID riid,
void **ppv)
{
HRESULT hr = S_OK;
if (ppv == NULL)
{
hr = E_POINTER;
}
else
{
if (IsEqualIID(riid, IID_IUnknown))
{
AddRef();
*ppv = static_cast<IUnknown *>(this);
}
else
{
*ppv = NULL;
hr = E_NOINTERFACE;
}
}
return hr;
}
STDMETHODIMP_(ULONG) Assembly::AddRef()
{
return InterlockedIncrement(&m_cRef);
}
STDMETHODIMP_(ULONG) Assembly::Release()
{
ULONG ulRef = InterlockedDecrement(&m_cRef);
if (ulRef == 0)
{
delete this;
}
return ulRef;
}
Assembly::Assembly()
{
m_cRef = 1;
m_pPEImage = NULL;
m_pNativePEImage = NULL;
m_pAssemblyName = NULL;
m_pMDImport = NULL;
m_dwAssemblyFlags = FLAG_NONE;
m_pBinder = NULL;
}
Assembly::~Assembly()
{
if (m_pPEImage != NULL)
{
BinderReleasePEImage(m_pPEImage);
m_pPEImage = NULL;
}
#ifdef FEATURE_PREJIT
if (m_pNativePEImage != NULL)
{
BinderReleasePEImage(m_pNativePEImage);
m_pNativePEImage = NULL;
}
#endif
SAFE_RELEASE(m_pAssemblyName);
SAFE_RELEASE(m_pMDImport);
}
HRESULT Assembly::Init(IMDInternalImport *pIMetaDataAssemblyImport,
PEKIND PeKind,
PEImage *pPEImage,
PEImage *pNativePEImage,
SString &assemblyPath,
BOOL fIsInGAC)
{
HRESULT hr = S_OK;
ReleaseHolder<AssemblyName> pAssemblyName;
SAFE_NEW(pAssemblyName, AssemblyName);
// Get assembly name def from meta data import and store it for later refs access
IF_FAIL_GO(pAssemblyName->Init(pIMetaDataAssemblyImport, PeKind));
SetMDImport(pIMetaDataAssemblyImport);
if (!fIsInGAC)
{
GetPath().Set(assemblyPath);
}
// Safe architecture for validation
PEKIND kAssemblyArchitecture;
kAssemblyArchitecture = pAssemblyName->GetArchitecture();
SetIsInGAC(fIsInGAC);
SetPEImage(pPEImage);
SetNativePEImage(pNativePEImage);
pAssemblyName->SetIsDefinition(TRUE);
// Now take ownership of assembly names
SetAssemblyName(pAssemblyName.Extract(), FALSE /* fAddRef */);
// Finally validate architecture
if (!IsValidArchitecture(kAssemblyArchitecture))
{
// Assembly image can't be executed on this platform
IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT));
}
Exit:
return hr;
}
HRESULT Assembly::GetMVID(GUID *pMVID)
{
// Zero init the GUID incase we fail
ZeroMemory(pMVID, sizeof(GUID));
return m_pMDImport->GetScopeProps(NULL, pMVID);
}
/* static */
PEKIND Assembly::GetSystemArchitecture()
{
#if defined(_TARGET_X86_)
return peI386;
#elif defined(_TARGET_AMD64_)
return peAMD64;
#elif defined(_TARGET_ARM_)
return peARM;
#elif defined(_TARGET_ARM64_)
return peARM64;
#else
PORTABILITY_ASSERT("Assembly::GetSystemArchitecture");
#endif
}
/* static */
BOOL Assembly::IsValidArchitecture(PEKIND kArchitecture)
{
if (!IsPlatformArchitecture(kArchitecture))
return TRUE;
return (kArchitecture == GetSystemArchitecture());
}
// --------------------------------------------------------------------
// ICLRPrivAssembly methods
// --------------------------------------------------------------------
LPCWSTR Assembly::GetSimpleName()
{
AssemblyName *pAsmName = GetAssemblyName();
return (pAsmName == nullptr ? nullptr : (LPCWSTR)pAsmName->GetSimpleName());
}
HRESULT Assembly::BindAssemblyByName(IAssemblyName * pIAssemblyName, ICLRPrivAssembly ** ppAssembly)
{
return (m_pBinder == NULL) ? E_FAIL : m_pBinder->BindAssemblyByName(pIAssemblyName, ppAssembly);
}
HRESULT Assembly::GetBinderID(UINT_PTR *pBinderId)
{
return (m_pBinder == NULL) ? E_FAIL : m_pBinder->GetBinderID(pBinderId);
}
HRESULT Assembly::GetLoaderAllocator(LPVOID* pLoaderAllocator)
{
return (m_pBinder == NULL) ? E_FAIL : m_pBinder->GetLoaderAllocator(pLoaderAllocator);
}
HRESULT Assembly::GetAvailableImageTypes(
LPDWORD pdwImageTypes)
{
HRESULT hr = E_FAIL;
if(pdwImageTypes == nullptr)
return E_INVALIDARG;
*pdwImageTypes = ASSEMBLY_IMAGE_TYPE_ASSEMBLY;
return S_OK;
}
HRESULT Assembly::GetImageResource(
DWORD dwImageType,
DWORD * pdwImageType,
ICLRPrivResource ** ppIResource)
{
HRESULT hr = S_OK;
if(ppIResource == nullptr)
return E_INVALIDARG;
if ((dwImageType & ASSEMBLY_IMAGE_TYPE_ASSEMBLY) == ASSEMBLY_IMAGE_TYPE_ASSEMBLY)
{
*ppIResource = clr::SafeAddRef(&m_clrPrivRes);
if (pdwImageType != nullptr)
*pdwImageType = ASSEMBLY_IMAGE_TYPE_ASSEMBLY;
}
else
{
hr = CLR_E_BIND_IMAGE_UNAVAILABLE;
}
return hr;
}
// get parent pointer from nested type
#define GetPThis() ((BINDER_SPACE::Assembly*)(((PBYTE)this) - offsetof(BINDER_SPACE::Assembly, m_clrPrivRes)))
HRESULT Assembly::CLRPrivResourceAssembly::QueryInterface(REFIID riid, void ** ppv)
{
HRESULT hr = S_OK;
VALIDATE_ARG_RET(ppv != NULL);
if (IsEqualIID(riid, IID_IUnknown))
{
AddRef();
*ppv = this;
}
else if (IsEqualIID(riid, __uuidof(ICLRPrivResource)))
{
AddRef();
// upcasting is safe
*ppv = static_cast<ICLRPrivResource *>(this);
}
else if (IsEqualIID(riid, __uuidof(ICLRPrivResourceAssembly)))
{
AddRef();
*ppv = static_cast<ICLRPrivResourceAssembly *>(this);
}
else
{
*ppv = NULL;
hr = E_NOINTERFACE;
}
return hr;
}
ULONG Assembly::CLRPrivResourceAssembly::AddRef()
{
return GetPThis()->AddRef();
}
ULONG Assembly::CLRPrivResourceAssembly::Release()
{
return GetPThis()->Release();
}
HRESULT Assembly::CLRPrivResourceAssembly::GetResourceType(IID *pIID)
{
VALIDATE_ARG_RET(pIID != nullptr);
*pIID = __uuidof(ICLRPrivResourceAssembly);
return S_OK;
}
HRESULT Assembly::CLRPrivResourceAssembly::GetAssembly(LPVOID *ppAssembly)
{
VALIDATE_ARG_RET(ppAssembly != nullptr);
AddRef();
*ppAssembly = GetPThis();
return S_OK;
}
}