forked from dotnet/runtime
/
AnalysisBasedMetadataManager.cs
302 lines (258 loc) · 13.3 KB
/
AnalysisBasedMetadataManager.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using Internal.TypeSystem;
using ILCompiler.Metadata;
using ILCompiler.DependencyAnalysis;
using Debug = System.Diagnostics.Debug;
using EcmaModule = Internal.TypeSystem.Ecma.EcmaModule;
using CustomAttributeHandle = System.Reflection.Metadata.CustomAttributeHandle;
using ExportedTypeHandle = System.Reflection.Metadata.ExportedTypeHandle;
namespace ILCompiler
{
/// <summary>
/// A metadata manager that knows the full set of metadata ahead of time.
/// </summary>
public sealed class AnalysisBasedMetadataManager : GeneratingMetadataManager, ICompilationRootProvider
{
private readonly List<ModuleDesc> _modulesWithMetadata;
private readonly List<MetadataType> _typesWithRootedCctorContext;
private readonly List<TypeDesc> _forcedTypes;
private readonly Dictionary<TypeDesc, MetadataCategory> _reflectableTypes = new Dictionary<TypeDesc, MetadataCategory>();
private readonly Dictionary<MethodDesc, MetadataCategory> _reflectableMethods = new Dictionary<MethodDesc, MetadataCategory>();
private readonly Dictionary<FieldDesc, MetadataCategory> _reflectableFields = new Dictionary<FieldDesc, MetadataCategory>();
private readonly HashSet<ReflectableCustomAttribute> _reflectableAttributes = new HashSet<ReflectableCustomAttribute>();
public AnalysisBasedMetadataManager(CompilerTypeSystemContext typeSystemContext)
: this(typeSystemContext, new FullyBlockedMetadataBlockingPolicy(),
new FullyBlockedManifestResourceBlockingPolicy(), null, new NoStackTraceEmissionPolicy(),
new NoDynamicInvokeThunkGenerationPolicy(), Array.Empty<ModuleDesc>(), Array.Empty<TypeDesc>(),
Array.Empty<ReflectableEntity<TypeDesc>>(), Array.Empty<ReflectableEntity<MethodDesc>>(),
Array.Empty<ReflectableEntity<FieldDesc>>(), Array.Empty<ReflectableCustomAttribute>(),
Array.Empty<MetadataType>(), default)
{
}
public AnalysisBasedMetadataManager(
CompilerTypeSystemContext typeSystemContext,
MetadataBlockingPolicy blockingPolicy,
ManifestResourceBlockingPolicy resourceBlockingPolicy,
string logFile,
StackTraceEmissionPolicy stackTracePolicy,
DynamicInvokeThunkGenerationPolicy invokeThunkGenerationPolicy,
IEnumerable<ModuleDesc> modulesWithMetadata,
IEnumerable<TypeDesc> forcedTypes,
IEnumerable<ReflectableEntity<TypeDesc>> reflectableTypes,
IEnumerable<ReflectableEntity<MethodDesc>> reflectableMethods,
IEnumerable<ReflectableEntity<FieldDesc>> reflectableFields,
IEnumerable<ReflectableCustomAttribute> reflectableAttributes,
IEnumerable<MetadataType> rootedCctorContexts,
MetadataManagerOptions options)
: base(typeSystemContext, blockingPolicy, resourceBlockingPolicy, logFile, stackTracePolicy, invokeThunkGenerationPolicy, options)
{
_modulesWithMetadata = new List<ModuleDesc>(modulesWithMetadata);
_typesWithRootedCctorContext = new List<MetadataType>(rootedCctorContexts);
_forcedTypes = new List<TypeDesc>(forcedTypes);
foreach (var refType in reflectableTypes)
{
_reflectableTypes.Add(refType.Entity, refType.Category);
}
foreach (var refMethod in reflectableMethods)
{
// Asking for description or runtime mapping for a member without asking
// for the owning type would mean we can't actually satisfy the request.
Debug.Assert((refMethod.Category & MetadataCategory.Description) == 0
|| (_reflectableTypes[refMethod.Entity.OwningType] & MetadataCategory.Description) != 0);
Debug.Assert((refMethod.Category & MetadataCategory.RuntimeMapping) == 0
|| (_reflectableTypes[refMethod.Entity.OwningType] & MetadataCategory.RuntimeMapping) != 0);
_reflectableMethods.Add(refMethod.Entity, refMethod.Category);
}
foreach (var refField in reflectableFields)
{
// Asking for description or runtime mapping for a member without asking
// for the owning type would mean we can't actually satisfy the request.
Debug.Assert((refField.Category & MetadataCategory.Description) == 0
|| (_reflectableTypes[refField.Entity.OwningType] & MetadataCategory.Description) != 0);
Debug.Assert((refField.Category & MetadataCategory.RuntimeMapping) == 0
|| (_reflectableTypes[refField.Entity.OwningType] & MetadataCategory.RuntimeMapping) != 0);
_reflectableFields.Add(refField.Entity, refField.Category);
}
foreach (var refAttribute in reflectableAttributes)
{
_reflectableAttributes.Add(refAttribute);
}
#if DEBUG
HashSet<ModuleDesc> moduleHash = new HashSet<ModuleDesc>(_modulesWithMetadata);
foreach (var refType in reflectableTypes)
{
// The instantiated types need to agree on the Description bit with the definition.
// GetMetadataCategory relies on that.
Debug.Assert((GetMetadataCategory(refType.Entity.GetTypeDefinition()) & MetadataCategory.Description)
== (GetMetadataCategory(refType.Entity) & MetadataCategory.Description));
Debug.Assert(!(refType.Entity is MetadataType) || moduleHash.Contains(((MetadataType)refType.Entity).Module));
}
foreach (var refMethod in reflectableMethods)
{
// The instantiated methods need to agree on the Description bit with the definition.
// GetMetadataCategory relies on that.
Debug.Assert((GetMetadataCategory(refMethod.Entity.GetTypicalMethodDefinition()) & MetadataCategory.Description)
== (GetMetadataCategory(refMethod.Entity) & MetadataCategory.Description));
// Canonical form of the method needs to agree with the logical form
Debug.Assert(GetMetadataCategory(refMethod.Entity) == GetMetadataCategory(refMethod.Entity.GetCanonMethodTarget(CanonicalFormKind.Specific)));
}
foreach (var refField in reflectableFields)
{
// The instantiated fields need to agree on the Description bit with the definition.
// GetMetadataCategory relies on that.
Debug.Assert((GetMetadataCategory(refField.Entity.GetTypicalFieldDefinition()) & MetadataCategory.Description)
== (GetMetadataCategory(refField.Entity) & MetadataCategory.Description));
}
#endif
}
public override IEnumerable<ModuleDesc> GetCompilationModulesWithMetadata()
{
return _modulesWithMetadata;
}
protected override void ComputeMetadata(NodeFactory factory,
out byte[] metadataBlob,
out List<MetadataMapping<MetadataType>> typeMappings,
out List<MetadataMapping<MethodDesc>> methodMappings,
out List<MetadataMapping<FieldDesc>> fieldMappings,
out List<MetadataMapping<MethodDesc>> stackTraceMapping)
{
ComputeMetadata(new Policy(_blockingPolicy, this), factory,
out metadataBlob,
out typeMappings,
out methodMappings,
out fieldMappings,
out stackTraceMapping);
}
protected sealed override MetadataCategory GetMetadataCategory(MethodDesc method)
{
if (_reflectableMethods.TryGetValue(method, out MetadataCategory value))
return value;
return 0;
}
protected sealed override MetadataCategory GetMetadataCategory(TypeDesc type)
{
if (_reflectableTypes.TryGetValue(type, out MetadataCategory value))
return value;
return 0;
}
protected sealed override MetadataCategory GetMetadataCategory(FieldDesc field)
{
if (_reflectableFields.TryGetValue(field, out MetadataCategory value))
return value;
return 0;
}
protected override IEnumerable<FieldDesc> GetFieldsWithRuntimeMapping()
{
foreach (var pair in _reflectableFields)
{
if ((pair.Value & MetadataCategory.RuntimeMapping) != 0)
yield return pair.Key;
}
}
void ICompilationRootProvider.AddCompilationRoots(IRootingServiceProvider rootProvider)
{
// We go over all the types and members that need a runtime artifact present in the
// compiled executable and root it.
const string reason = "Reflection";
foreach (var type in _forcedTypes)
{
rootProvider.AddReflectionRoot(type, reason);
}
foreach (var pair in _reflectableMethods)
{
if ((pair.Value & MetadataCategory.RuntimeMapping) != 0)
{
MethodDesc method = pair.Key;
rootProvider.AddReflectionRoot(method, reason);
}
}
foreach (var pair in _reflectableFields)
{
if ((pair.Value & MetadataCategory.RuntimeMapping) != 0)
{
FieldDesc field = pair.Key;
rootProvider.AddReflectionRoot(field, reason);
}
}
foreach (var type in _typesWithRootedCctorContext)
{
rootProvider.RootNonGCStaticBaseForType(type, reason);
}
}
private struct Policy : IMetadataPolicy
{
private readonly MetadataBlockingPolicy _blockingPolicy;
private readonly AnalysisBasedMetadataManager _parent;
public Policy(MetadataBlockingPolicy blockingPolicy,
AnalysisBasedMetadataManager parent)
{
_blockingPolicy = blockingPolicy;
_parent = parent;
}
public bool GeneratesMetadata(FieldDesc fieldDef)
{
return (_parent.GetMetadataCategory(fieldDef) & MetadataCategory.Description) != 0;
}
public bool GeneratesMetadata(MethodDesc methodDef)
{
return (_parent.GetMetadataCategory(methodDef) & MetadataCategory.Description) != 0;
}
public bool GeneratesMetadata(MetadataType typeDef)
{
return (_parent.GetMetadataCategory(typeDef) & MetadataCategory.Description) != 0;
}
public bool GeneratesMetadata(EcmaModule module, CustomAttributeHandle caHandle)
{
return _parent._reflectableAttributes.Contains(new ReflectableCustomAttribute(module, caHandle));
}
public bool GeneratesMetadata(EcmaModule module, ExportedTypeHandle exportedTypeHandle)
{
// We'll possibly need to do something else here if we ever use this MetadataManager
// with compilation modes that generate multiple metadata blobs.
// (Multi-module or .NET Native style shared library.)
// We are currently missing type forwarders pointing to the other blobs.
var targetType = (MetadataType)module.GetObject(exportedTypeHandle, NotFoundBehavior.ReturnNull);
if (targetType == null)
{
// No harm in generating a forwarder that didn't resolve.
// We'll get matching behavior at runtime.
return true;
}
return GeneratesMetadata(targetType);
}
public bool IsBlocked(MetadataType typeDef)
{
return _blockingPolicy.IsBlocked(typeDef);
}
public bool IsBlocked(MethodDesc methodDef)
{
return _blockingPolicy.IsBlocked(methodDef);
}
}
}
public struct ReflectableEntity<TEntity>
{
public readonly TEntity Entity;
public readonly MetadataCategory Category;
public ReflectableEntity(TEntity entity, MetadataCategory category)
{
Entity = entity;
Category = category;
}
}
public struct ReflectableCustomAttribute : IEquatable<ReflectableCustomAttribute>
{
public readonly EcmaModule Module;
public readonly CustomAttributeHandle CustomAttributeHandle;
public ReflectableCustomAttribute(EcmaModule module, CustomAttributeHandle caHandle)
=> (Module, CustomAttributeHandle) = (module, caHandle);
public bool Equals(ReflectableCustomAttribute other)
=> other.Module == Module && other.CustomAttributeHandle == CustomAttributeHandle;
public override bool Equals(object obj)
=> obj is ReflectableCustomAttribute other && Equals(other);
public override int GetHashCode() => Module.GetHashCode() ^ CustomAttributeHandle.GetHashCode();
}
}