From d3d5f3d9c410930c9be6851563ac154731892056 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Mon, 4 Oct 2021 01:06:58 +0300 Subject: [PATCH 1/3] Remove two redundant package dependencies and update another one. They were dragging along lots of packages, cluttering the dependency graph. --- eng/Versions.props | 1 + .../ILCompiler.DependencyAnalysisFramework.csproj | 5 +---- .../ILCompiler.TypeSystem.ReadyToRun.csproj | 3 --- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index 0c5d9017d45581..c82f159232090c 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -88,6 +88,7 @@ 1.2.0-beta.304 4.5.1 4.3.0 + 5.0.0 5.0.0 4.8.2 4.5.0 diff --git a/src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/ILCompiler.DependencyAnalysisFramework.csproj b/src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/ILCompiler.DependencyAnalysisFramework.csproj index 291136d46ae916..c98ffb07608364 100644 --- a/src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/ILCompiler.DependencyAnalysisFramework.csproj +++ b/src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/ILCompiler.DependencyAnalysisFramework.csproj @@ -16,11 +16,8 @@ Debug;Release;Checked - - 4.3.0 - - 1.3.1 + $(SystemCollectionsImmutableVersion) diff --git a/src/coreclr/tools/aot/ILCompiler.TypeSystem.ReadyToRun/ILCompiler.TypeSystem.ReadyToRun.csproj b/src/coreclr/tools/aot/ILCompiler.TypeSystem.ReadyToRun/ILCompiler.TypeSystem.ReadyToRun.csproj index 82452cfa3e9c6d..86b9c06a6cf0d2 100644 --- a/src/coreclr/tools/aot/ILCompiler.TypeSystem.ReadyToRun/ILCompiler.TypeSystem.ReadyToRun.csproj +++ b/src/coreclr/tools/aot/ILCompiler.TypeSystem.ReadyToRun/ILCompiler.TypeSystem.ReadyToRun.csproj @@ -26,9 +26,6 @@ - - 4.3.0 - $(SystemReflectionMetadataVersion) From bc8a33c94c8ad797f51fa971b4706182172d5f1a Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Mon, 4 Oct 2021 01:27:56 +0300 Subject: [PATCH 2/3] Optimize crossgen's Program.IsValidPublicKey to use spans. --- src/coreclr/tools/aot/crossgen2/Program.cs | 23 +++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/coreclr/tools/aot/crossgen2/Program.cs b/src/coreclr/tools/aot/crossgen2/Program.cs index ef3eed5bb2f184..90a2d1340d1996 100644 --- a/src/coreclr/tools/aot/crossgen2/Program.cs +++ b/src/coreclr/tools/aot/crossgen2/Program.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Buffers.Binary; using System.Collections.Generic; using System.Collections.Immutable; using System.IO; @@ -696,13 +697,13 @@ private void RunSingleCompilation(Dictionary inFilePaths, Instru if (_commandLineOptions.CompositeKeyFile != null) { - ImmutableArray compositeStrongNameKey = File.ReadAllBytes(_commandLineOptions.CompositeKeyFile).ToImmutableArray(); + byte[] compositeStrongNameKey = File.ReadAllBytes(_commandLineOptions.CompositeKeyFile); if (!IsValidPublicKey(compositeStrongNameKey)) { throw new Exception(string.Format(SR.ErrorCompositeKeyFileNotPublicKey)); } - compositeImageSettings.PublicKey = compositeStrongNameKey; + compositeImageSettings.PublicKey = compositeStrongNameKey.ToImmutableArray(); } // @@ -954,7 +955,7 @@ public AlgorithmId(uint flags) } } - private static readonly ImmutableArray s_ecmaKey = ImmutableArray.Create(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0 }); + private static ReadOnlySpan s_ecmaKey => new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0 }; private const int SnPublicKeyBlobSize = 13; @@ -968,24 +969,22 @@ public AlgorithmId(uint flags) // From StrongNameInternal.cpp // Checks to see if a public key is a valid instance of a PublicKeyBlob as // defined in StongName.h - internal static bool IsValidPublicKey(ImmutableArray blob) + internal static bool IsValidPublicKey(ReadOnlySpan blob) { // The number of public key bytes must be at least large enough for the header and one byte of data. - if (blob.IsDefault || blob.Length < s_publicKeyHeaderSize + 1) + if (blob.Length < s_publicKeyHeaderSize + 1) { return false; } - var blobReader = new BinaryReader(new MemoryStream(blob.ToArray())); - // Signature algorithm ID - var sigAlgId = blobReader.ReadUInt32(); + var sigAlgId = BinaryPrimitives.ReadUInt32LittleEndian(blob); // Hash algorithm ID - var hashAlgId = blobReader.ReadUInt32(); + var hashAlgId = BinaryPrimitives.ReadUInt32LittleEndian(blob.Slice(4)); // Size of public key data in bytes, not including the header - var publicKeySize = blobReader.ReadUInt32(); + var publicKeySize = BinaryPrimitives.ReadUInt32LittleEndian(blob.Slice(8)); // publicKeySize bytes of public key data - var publicKey = blobReader.ReadByte(); + var publicKey = blob[12]; // The number of public key bytes must be the same as the size of the header plus the size of the public key data. if (blob.Length != s_publicKeyHeaderSize + publicKeySize) @@ -994,7 +993,7 @@ internal static bool IsValidPublicKey(ImmutableArray blob) } // Check for the ECMA key, which does not obey the invariants checked below. - if (System.Linq.Enumerable.SequenceEqual(blob, s_ecmaKey)) + if (blob.SequenceEqual(s_ecmaKey)) { return true; } From 6137ebf336ed0ada947eaa5dc2c9e37d9f16c871 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Tue, 26 Oct 2021 01:01:12 +0300 Subject: [PATCH 3/3] Bring BinaryReader back in IsValidPublicKey. And expedite a condition check that does not involve BinaryReader. --- src/coreclr/tools/aot/crossgen2/Program.cs | 24 ++++++++++++---------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/coreclr/tools/aot/crossgen2/Program.cs b/src/coreclr/tools/aot/crossgen2/Program.cs index 90a2d1340d1996..4df45e3065e0d3 100644 --- a/src/coreclr/tools/aot/crossgen2/Program.cs +++ b/src/coreclr/tools/aot/crossgen2/Program.cs @@ -969,7 +969,7 @@ public AlgorithmId(uint flags) // From StrongNameInternal.cpp // Checks to see if a public key is a valid instance of a PublicKeyBlob as // defined in StongName.h - internal static bool IsValidPublicKey(ReadOnlySpan blob) + internal static bool IsValidPublicKey(byte[] blob) { // The number of public key bytes must be at least large enough for the header and one byte of data. if (blob.Length < s_publicKeyHeaderSize + 1) @@ -977,14 +977,22 @@ internal static bool IsValidPublicKey(ReadOnlySpan blob) return false; } + // Check for the ECMA key, which does not obey the invariants checked below. + if (blob.AsSpan().SequenceEqual(s_ecmaKey)) + { + return true; + } + + var blobReader = new BinaryReader(new MemoryStream(blob, writable: false)); + // Signature algorithm ID - var sigAlgId = BinaryPrimitives.ReadUInt32LittleEndian(blob); + var sigAlgId = blobReader.ReadUInt32(); // Hash algorithm ID - var hashAlgId = BinaryPrimitives.ReadUInt32LittleEndian(blob.Slice(4)); + var hashAlgId = blobReader.ReadUInt32(); // Size of public key data in bytes, not including the header - var publicKeySize = BinaryPrimitives.ReadUInt32LittleEndian(blob.Slice(8)); + var publicKeySize = blobReader.ReadUInt32(); // publicKeySize bytes of public key data - var publicKey = blob[12]; + var publicKey = blobReader.ReadByte(); // The number of public key bytes must be the same as the size of the header plus the size of the public key data. if (blob.Length != s_publicKeyHeaderSize + publicKeySize) @@ -992,12 +1000,6 @@ internal static bool IsValidPublicKey(ReadOnlySpan blob) return false; } - // Check for the ECMA key, which does not obey the invariants checked below. - if (blob.SequenceEqual(s_ecmaKey)) - { - return true; - } - // The public key must be in the wincrypto PUBLICKEYBLOB format if (publicKey != PublicKeyBlobId) {