From 90e14b402c2bbce3404aaf306ed4186763a2f2ec Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Fri, 17 Jun 2016 08:13:33 -0700 Subject: [PATCH 1/3] Update map.json with AssemblyMetadataHelper.cs --- src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/map.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/map.json b/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/map.json index b9ea94c2d5..da86400fef 100644 --- a/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/map.json +++ b/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/map.json @@ -1,3 +1,4 @@ { - "monad/src/CoreCLR/CorePsAssemblyLoadContext.cs": "CoreCLR/CorePsAssemblyLoadContext.cs" + "monad/src/CoreCLR/CorePsAssemblyLoadContext.cs": "CoreCLR/CorePsAssemblyLoadContext.cs", + "monad/src/CoreCLR/AssemblyMetadataHelper.cs": "CoreCLR/AssemblyMetadataHelper.cs" } From 67ef887900753483fad4d882ab92fb19b1bacff3 Mon Sep 17 00:00:00 2001 From: PowerShell Team Date: Fri, 17 Jun 2016 08:14:02 -0700 Subject: [PATCH 2/3] Add new file AssemblyMetadataHelper.cs from [SD:715912] commit 77eda78 --- .../CoreCLR/AssemblyMetadataHelper.cs | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/AssemblyMetadataHelper.cs diff --git a/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/AssemblyMetadataHelper.cs b/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/AssemblyMetadataHelper.cs new file mode 100644 index 0000000000..062ff320e8 --- /dev/null +++ b/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/AssemblyMetadataHelper.cs @@ -0,0 +1,76 @@ +/********************************************************************++ +Copyright (c) Microsoft Corporation. All rights reserved. +--********************************************************************/ + +using System.Globalization; +using System.Reflection; +using System.Reflection.Metadata; +using System.Security.Cryptography; + +namespace System.Management.Automation +{ + internal static class AssemblyMetadataHelper + { + /// + /// Construct the strong assembly name from metadata + /// + internal static string GetAssemblyStrongName(MetadataReader metadataReader) + { + AssemblyDefinition assemblyDefinition = metadataReader.GetAssemblyDefinition(); + string asmName = metadataReader.GetString(assemblyDefinition.Name); + string asmVersion = assemblyDefinition.Version.ToString(); + string asmCulture = metadataReader.GetString(assemblyDefinition.Culture); + asmCulture = (asmCulture == string.Empty) ? "neutral" : asmCulture; + + AssemblyHashAlgorithm hashAlgorithm = assemblyDefinition.HashAlgorithm; + BlobHandle blobHandle = assemblyDefinition.PublicKey; + BlobReader blobReader = metadataReader.GetBlobReader(blobHandle); + + string publicKeyTokenString = "null"; + // Extract public key token only if PublicKey exists in the metadata + if (blobReader.Length > 0) + { + byte[] publickey = blobReader.ReadBytes(blobReader.Length); + + HashAlgorithm hashImpl = null; + switch (hashAlgorithm) + { + case AssemblyHashAlgorithm.Sha1: + hashImpl = SHA1.Create(); + break; + case AssemblyHashAlgorithm.MD5: + hashImpl = MD5.Create(); + break; + case AssemblyHashAlgorithm.Sha256: + hashImpl = SHA256.Create(); + break; + case AssemblyHashAlgorithm.Sha384: + hashImpl = SHA384.Create(); + break; + case AssemblyHashAlgorithm.Sha512: + hashImpl = SHA512.Create(); + break; + default: + throw new NotSupportedException(); + } + + byte[] publicKeyHash = hashImpl.ComputeHash(publickey); + byte[] publicKeyTokenBytes = new byte[8]; + // Note that, the low 8 bytes of the hash of public key in reverse order is the public key tokens. + for (int i = 1; i <= 8; i++) + { + publicKeyTokenBytes[i - 1] = publicKeyHash[publicKeyHash.Length - i]; + } + + // Convert bytes to hex format strings in lower case. + publicKeyTokenString = BitConverter.ToString(publicKeyTokenBytes).Replace("-", string.Empty).ToLowerInvariant(); + } + + string strongAssemblyName = string.Format(CultureInfo.InvariantCulture, + "{0}, Version={1}, Culture={2}, PublicKeyToken={3}", + asmName, asmVersion, asmCulture, publicKeyTokenString); + + return strongAssemblyName; + } + } +} From b374e2fd06505bf69839f15d6f4e556dafd3ed1b Mon Sep 17 00:00:00 2001 From: Sergei Vorobev Date: Fri, 17 Jun 2016 11:23:29 -0700 Subject: [PATCH 3/3] Add Start-TypeGen Update Microsoft.PowerShell.CoreCLR.AssemblyLoadContext\project.json Include Crypto package for new source code --- build.psm1 | 41 +++++++++++++++++++ .../project.json | 4 +- src/TypeCatalogGen/.gitignore | 1 - 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/build.psm1 b/build.psm1 index 2a109da28e..d3518f260e 100644 --- a/build.psm1 +++ b/build.psm1 @@ -27,6 +27,7 @@ function Start-PSBuild { [switch]$Restore, [string]$Output, [switch]$ResGen, + [switch]$TypeGen, [Parameter(ParameterSetName='CoreCLR')] [switch]$Publish, @@ -194,6 +195,13 @@ function Start-PSBuild { } } + # handle TypeGen + if ($TypeGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CorePsTypeCatalog.cs")) + { + log "Run TypeGen (generating CorePsTypeCatalog.cs)" + Start-TypeGen + } + try { # Relative paths do not work well if cwd is not changed to project Push-Location $Options.Top @@ -867,6 +875,39 @@ function Send-GitDiffToSd { } } +function Start-TypeGen +{ + [CmdletBinding()] + param() + + if (!$IsWindows) + { + throw "Start-TypeGen is not supported on non-windows. Use src/TypeCatalogGen/build.sh instead" + } + + Push-Location "$PSScriptRoot/src/TypeCatalogParser" + try + { + dotnet restore -v Warning + dotnet run + } + finally + { + Pop-Location + } + + Push-Location "$PSScriptRoot/src/TypeCatalogGen" + try + { + dotnet restore -v Warning + dotnet run ../Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CorePsTypeCatalog.cs powershell.inc + } + finally + { + Pop-Location + } +} + function Start-ResGen { [CmdletBinding()] diff --git a/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/project.json b/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/project.json index beeeba76f8..b7ddc7ab42 100644 --- a/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/project.json +++ b/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/project.json @@ -26,7 +26,9 @@ }, "dependencies": { "NETStandard.Library": "1.5.0-rc2-24027", - "System.Runtime.Loader": "4.0.0-rc2-24027" + "System.Runtime.Loader": "4.0.0-rc2-24027", + "System.Reflection.Metadata": "1.1.0", + "System.Security.Cryptography.Algorithms": "4.1.0-rc2-24027" } } } diff --git a/src/TypeCatalogGen/.gitignore b/src/TypeCatalogGen/.gitignore index 172ca8f17f..d745414448 100644 --- a/src/TypeCatalogGen/.gitignore +++ b/src/TypeCatalogGen/.gitignore @@ -1,2 +1 @@ powershell.inc -CorePsTypeCatalog.cs