Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate consistent metadata view assembly names #333

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace Microsoft.VisualStudio.Composition
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.Composition.Reflection;

Expand Down Expand Up @@ -94,12 +96,34 @@ internal static class MetadataViewGenerator

public delegate object MetadataViewFactory(IReadOnlyDictionary<string, object?> metadata, IReadOnlyDictionary<string, object?> defaultMetadata);

private static AssemblyBuilder CreateProxyAssemblyBuilder()
private static AssemblyBuilder CreateProxyAssemblyBuilder(ImmutableHashSet<AssemblyName> assemblies)
{
var proxyAssemblyName = new AssemblyName(string.Format(CultureInfo.InvariantCulture, "MetadataViewProxies_{0}", Guid.NewGuid()));
var proxyAssemblyName = new AssemblyName(string.Format(CultureInfo.InvariantCulture, "MetadataViewProxies_{0}", GenerateGuidFromAssemblies(assemblies)));
return AssemblyBuilder.DefineDynamicAssembly(proxyAssemblyName, AssemblyBuilderAccess.Run);
}

private static Guid GenerateGuidFromAssemblies(ImmutableHashSet<AssemblyName> assemblies)
{
// To make dll load and JIT comparisons between builds easier, we generate a
// consistent name for the proxy assembly based on the input assemblies involved.
using var algorithm = SHA256.Create();

foreach (AssemblyName name in assemblies.OrderBy(a => a.FullName, StringComparer.Ordinal))
{
var bytes = Encoding.Unicode.GetBytes(name.FullName!);
algorithm.TransformBlock(bytes, 0, bytes.Length, null, 0);
}

algorithm.TransformFinalBlock(Array.Empty<byte>(), 0, 0);

// SHA256 produces 32-bytes hash but we need only 16-bytes of them to produce a GUID,
// this is not used for cryptographic purposes so we just grab the first half.
var guidBytes = new byte[16];
Array.Copy(algorithm.Hash, guidBytes, guidBytes.Length);

return new Guid(guidBytes);
}

/// <summary>
/// Gets the <see cref="ModuleBuilder"/> to use for generating a proxy for the given type.
/// </summary>
Expand All @@ -117,7 +141,7 @@ private static ModuleBuilder GetProxyModuleBuilder(TypeInfo viewType)
var skipVisibilityCheckAssemblies = SkipClrVisibilityChecks.GetSkipVisibilityChecksRequirements(viewType);
if (!TransparentProxyModuleBuilderByVisibilityCheck.TryGetValue(skipVisibilityCheckAssemblies, out ModuleBuilder? moduleBuilder))
{
var assemblyBuilder = CreateProxyAssemblyBuilder();
var assemblyBuilder = CreateProxyAssemblyBuilder(skipVisibilityCheckAssemblies);
moduleBuilder = assemblyBuilder.DefineDynamicModule("MetadataViewProxiesModule");
var skipClrVisibilityChecks = new SkipClrVisibilityChecks(assemblyBuilder, moduleBuilder);
skipClrVisibilityChecks.SkipVisibilityChecksFor(skipVisibilityCheckAssemblies);
Expand Down