-
Notifications
You must be signed in to change notification settings - Fork 4k
/
DesktopAssemblyIdentityComparer.cs
304 lines (267 loc) · 12.6 KB
/
DesktopAssemblyIdentityComparer.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
303
304
// 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.
using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Microsoft.CodeAnalysis
{
public sealed partial class DesktopAssemblyIdentityComparer : AssemblyIdentityComparer
{
// Portability:
// If a reference or definition identity is strong, not retargetable, and portable (i.e. one of a few names hard-coded in Fusion)
// and the portability for its PublicKeyToken is not disabled in app.config file then the identity is replaced by a different identity
// before the match is performed.
//
// Portable identities:
// - System, PKT=7cec85d7bea7798e, Version=2.0.0.0-5.9.0.0 -> System, PKT=b77a5c561934e089, Version=<current framework version>
// - System.Core, PKT=7cec85d7bea7798e, Version=2.0.0.0-5.9.0.0 -> System.Core, PKT=b77a5c561934e089, Version=<current framework version>
// - System.ComponentModel.Composition, PKT=31bf3856ad364e35, Version=2.0.0.0-5.9.0.0 -> System.ComponentModel.Composition, PKT=b77a5c561934e089, Version=<current framework version>
// - Microsoft.VisualBasic, PKT=31bf3856ad364e35, Version=2.0.0.0-5.9.0.0 -> Microsoft.VisualBasic, PKT=b03f5f7f11d50a3a, Version=10.0.0.0
//
// Retargetability:
// If the reference identity specifies property Retargetable=Yes the identity is looked up in a hard-coded Fusion table.
// The table maps it to an identity that is used for identity matching.
// Note: Retargeting may change name, version and public key token of the identity.
//
// Version unification:
// FX identities (hard-coded list in Fusion):
// FX references are unified regardless of the isUnified flags, returns EquivalentFxUnified.
// Non-FX identities:
// if (isUnified1 && version1 > version2 || isUnified2 && version1 < version2) return EquivalentUnified.
public static new DesktopAssemblyIdentityComparer Default { get; } = new DesktopAssemblyIdentityComparer(default(AssemblyPortabilityPolicy));
internal readonly AssemblyPortabilityPolicy policy;
/// <param name="policy">Assembly portability policy, usually provided through an app.config file.</param>
internal DesktopAssemblyIdentityComparer(AssemblyPortabilityPolicy policy)
{
this.policy = policy;
}
/// <summary>
/// Loads <see cref="AssemblyPortabilityPolicy"/> information from XML with app.config schema.
/// </summary>
/// <exception cref="System.Xml.XmlException">The stream doesn't contain a well formed XML.</exception>
/// <exception cref="ArgumentNullException"><paramref name="input"/> is null.</exception>
/// <remarks>
/// Tries to find supportPortability elements in the given XML:
/// <![CDATA[
/// <configuration>
/// <runtime>
/// <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
/// <supportPortability PKT="7cec85d7bea7798e" enable="false"/>
/// <supportPortability PKT="31bf3856ad364e35" enable="false"/>
/// </assemblyBinding>
/// </runtime>
/// </configuration>
/// ]]>
///
/// Keeps the stream open.
/// </remarks>
public static DesktopAssemblyIdentityComparer LoadFromXml(Stream input)
{
return new DesktopAssemblyIdentityComparer(AssemblyPortabilityPolicy.LoadFromXml(input));
}
internal AssemblyPortabilityPolicy PortabilityPolicy
{
get
{
return this.policy;
}
}
internal override bool ApplyUnificationPolicies(
ref AssemblyIdentity reference,
ref AssemblyIdentity definition,
AssemblyIdentityParts referenceParts,
out bool isDefinitionFxAssembly)
{
if (reference.ContentType == AssemblyContentType.Default &&
SimpleNameComparer.Equals(reference.Name, definition.Name) &&
SimpleNameComparer.Equals(reference.Name, "mscorlib"))
{
isDefinitionFxAssembly = true;
reference = definition;
return true;
}
if (!reference.IsRetargetable && definition.IsRetargetable)
{
// Reference is not retargetable, but definition is retargetable.
// Non-equivalent.
isDefinitionFxAssembly = false;
return false;
}
// Notes:
// an assembly might be both retargetable and portable
// in that case retargetable table acts as an override.
// Apply portability policy transforms first (e.g. rewrites references to SL assemblies to their desktop equivalents)
// If the reference is partial and is missing version or PKT it is not ported.
reference = Port(reference);
definition = Port(definition);
if (reference.IsRetargetable && !definition.IsRetargetable)
{
if (!AssemblyIdentity.IsFullName(referenceParts))
{
isDefinitionFxAssembly = false;
return false;
}
// Reference needs to be retargeted before comparison,
// unless it's optionally retargetable and we already match the PK
bool skipRetargeting = IsOptionallyRetargetableAssembly(reference) &&
AssemblyIdentity.KeysEqual(reference, definition);
if (!skipRetargeting)
{
reference = Retarget(reference);
}
}
// At this point we are in one of the following states:
//
// 1) Both ref/def are not retargetable
// 2) Both ref/def are retargetable
// 3) Ref is retargetable (and has been retargeted)
//
// We can do a straight compare of ref/def at this point using the
// regular rules
if (reference.IsRetargetable && definition.IsRetargetable)
{
isDefinitionFxAssembly = IsRetargetableAssembly(definition);
}
else
{
isDefinitionFxAssembly = IsFrameworkAssembly(definition);
}
return true;
}
/// <summary>
/// Returns true if the identity is a Framework 4.5 or lower assembly.
/// </summary>
private static bool IsFrameworkAssembly(AssemblyIdentity identity)
{
// Note:
// FrameworkAssemblyTable::IsFrameworkAssembly returns false if culture is not neutral.
// However its caller doesn't initialize the culture and hence the culture is ignored.
// PrepQueryMatchData(pName, wzName, &dwSizeName, wzVersion, &dwSizeVer, wzPublicKeyToken, &dwSizePKT, NULL, NULL, NULL);.
if (identity.ContentType != AssemblyContentType.Default)
{
return false;
}
FrameworkAssemblyDictionary.Value value;
if (!s_arFxPolicy.TryGetValue(identity.Name, out value) ||
!value.PublicKeyToken.SequenceEqual(identity.PublicKeyToken))
{
return false;
}
// build and revision numbers are ignored
uint thisVersion = ((uint)identity.Version.Major << 16) | (uint)identity.Version.Minor;
uint fxVersion = ((uint)value.Version.Major << 16) | (uint)value.Version.Minor;
return thisVersion <= fxVersion;
}
private static bool IsRetargetableAssembly(AssemblyIdentity identity)
{
bool retargetable, portable;
IsRetargetableAssembly(identity, out retargetable, out portable);
return retargetable;
}
private static bool IsOptionallyRetargetableAssembly(AssemblyIdentity identity)
{
if (!identity.IsRetargetable)
{
return false;
}
bool retargetable, portable;
IsRetargetableAssembly(identity, out retargetable, out portable);
return retargetable && portable;
}
private static bool IsTriviallyNonRetargetable(AssemblyIdentity identity)
{
// Short-circuit zero-version/non-neutral culture/weak name,
// which will never match retargeted identities.
return identity.CultureName.Length != 0
|| identity.ContentType != AssemblyContentType.Default
|| !identity.IsStrongName;
}
private static void IsRetargetableAssembly(AssemblyIdentity identity, out bool retargetable, out bool portable)
{
retargetable = portable = false;
if (IsTriviallyNonRetargetable(identity))
{
return;
}
FrameworkRetargetingDictionary.Value value;
retargetable = s_arRetargetPolicy.TryGetValue(identity, out value);
portable = value.IsPortable;
}
private static AssemblyIdentity Retarget(AssemblyIdentity identity)
{
if (IsTriviallyNonRetargetable(identity))
{
return identity;
}
FrameworkRetargetingDictionary.Value value;
if (s_arRetargetPolicy.TryGetValue(identity, out value))
{
return new AssemblyIdentity(
value.NewName ?? identity.Name,
(Version)value.NewVersion,
identity.CultureName,
value.NewPublicKeyToken,
hasPublicKey: false,
isRetargetable: identity.IsRetargetable,
contentType: AssemblyContentType.Default);
}
return identity;
}
private AssemblyIdentity Port(AssemblyIdentity identity)
{
if (identity.IsRetargetable || !identity.IsStrongName || identity.ContentType != AssemblyContentType.Default)
{
return identity;
}
Version? newVersion = null;
ImmutableArray<byte> newPublicKeyToken = default;
var version = (AssemblyVersion)identity.Version;
if (version >= new AssemblyVersion(2, 0, 0, 0) && version <= new AssemblyVersion(5, 9, 0, 0))
{
if (identity.PublicKeyToken.SequenceEqual(s_SILVERLIGHT_PLATFORM_PUBLICKEY_STR_L))
{
if (!policy.SuppressSilverlightPlatformAssembliesPortability)
{
if (SimpleNameComparer.Equals(identity.Name, "System") ||
SimpleNameComparer.Equals(identity.Name, "System.Core"))
{
newVersion = (Version)s_VER_ASSEMBLYVERSION_STR_L;
newPublicKeyToken = s_ECMA_PUBLICKEY_STR_L;
}
}
}
else if (identity.PublicKeyToken.SequenceEqual(s_SILVERLIGHT_PUBLICKEY_STR_L))
{
if (!policy.SuppressSilverlightLibraryAssembliesPortability)
{
if (SimpleNameComparer.Equals(identity.Name, "Microsoft.VisualBasic"))
{
newVersion = new Version(10, 0, 0, 0);
newPublicKeyToken = s_MICROSOFT_PUBLICKEY_STR_L;
}
if (SimpleNameComparer.Equals(identity.Name, "System.ComponentModel.Composition"))
{
newVersion = (Version)s_VER_ASSEMBLYVERSION_STR_L;
newPublicKeyToken = s_ECMA_PUBLICKEY_STR_L;
}
}
}
}
if (newVersion == null)
{
return identity;
}
return new AssemblyIdentity(
identity.Name,
newVersion,
identity.CultureName,
newPublicKeyToken,
hasPublicKey: false,
isRetargetable: identity.IsRetargetable,
contentType: AssemblyContentType.Default);
}
}
}