Remove Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll (#4868)

The code in `AssemblyLoadContext.dll` doesn't need to be in a separate DLL anymore.
S.M.A.dll depends on `AssemblyLoadContext.dll`, so keeping that code out of S.M.A.dll doesn't help make S.M.A smaller size or less dependent. So the code in `AssemblyLoadContext.dll` is moved to `S.M.A.dll` and then we remove `AssemblyLoadContext.dll`.

The changes are:
- Move `CorePsAssemblyLoadContext.cs` to `src\S.M.A\CoreCLR\`
- Update `CorePsAssemblyLoadContext.cs` to get the test took moved to `Utils.InternalTestHooks` and update tests
- Update `build.psm1` and `.csproj` accrodingly
- Update `pwrshcommon.cpp` to remove `AssemblyLoadContext.dll` from the TPA list.
- `S.M.A.AssemblyExtensions` is removed as `PackageManagement` has finished their move to .NET Core 2.0. (I will work with Bryan to get the latest version uploaded to powershell-core)
This commit is contained in:
Dongbo Wang 2017-09-29 17:26:55 -07:00 committed by GitHub
parent 2639cd89ce
commit a4b8dd0a23
13 changed files with 52 additions and 213 deletions

View File

@ -11,8 +11,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.PowerShell.Comman
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.PowerShell.CoreCLR.Eventing", "src\Microsoft.PowerShell.CoreCLR.Eventing\Microsoft.PowerShell.CoreCLR.Eventing.csproj", "{981D3972-343D-4E17-935B-037E1C622771}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.PowerShell.CoreCLR.AssemblyLoadContext", "src\Microsoft.PowerShell.CoreCLR.AssemblyLoadContext\Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.csproj", "{377D8A33-22F2-43ED-9E81-FCB56874A48C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.PowerShell.ConsoleHost", "src\Microsoft.PowerShell.ConsoleHost\Microsoft.PowerShell.ConsoleHost.csproj", "{8FFE645D-F0C9-4220-9A88-83062ED211D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.PowerShell.Commands.Management", "src\Microsoft.PowerShell.Commands.Management\Microsoft.PowerShell.Commands.Management.csproj", "{FCE53A5E-5FAC-48BE-BAD8-2110040B5C2E}"
@ -71,14 +69,6 @@ Global
{981D3972-343D-4E17-935B-037E1C622771}.Linux|Any CPU.Build.0 = Linux|Any CPU
{981D3972-343D-4E17-935B-037E1C622771}.Release|Any CPU.ActiveCfg = Release|Any CPU
{981D3972-343D-4E17-935B-037E1C622771}.Release|Any CPU.Build.0 = Release|Any CPU
{377D8A33-22F2-43ED-9E81-FCB56874A48C}.CodeCoverage|Any CPU.ActiveCfg = CodeCoverage|Any CPU
{377D8A33-22F2-43ED-9E81-FCB56874A48C}.CodeCoverage|Any CPU.Build.0 = CodeCoverage|Any CPU
{377D8A33-22F2-43ED-9E81-FCB56874A48C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{377D8A33-22F2-43ED-9E81-FCB56874A48C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{377D8A33-22F2-43ED-9E81-FCB56874A48C}.Linux|Any CPU.ActiveCfg = Linux|Any CPU
{377D8A33-22F2-43ED-9E81-FCB56874A48C}.Linux|Any CPU.Build.0 = Linux|Any CPU
{377D8A33-22F2-43ED-9E81-FCB56874A48C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{377D8A33-22F2-43ED-9E81-FCB56874A48C}.Release|Any CPU.Build.0 = Release|Any CPU
{8FFE645D-F0C9-4220-9A88-83062ED211D2}.CodeCoverage|Any CPU.ActiveCfg = CodeCoverage|Any CPU
{8FFE645D-F0C9-4220-9A88-83062ED211D2}.CodeCoverage|Any CPU.Build.0 = CodeCoverage|Any CPU
{8FFE645D-F0C9-4220-9A88-83062ED211D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU

View File

@ -488,7 +488,7 @@ Fix steps:
}
# handle TypeGen
if ($TypeGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CorePsTypeCatalog.cs")) {
if ($TypeGen -or -not (Test-Path "$PSScriptRoot/src/System.Management.Automation/CoreCLR/CorePsTypeCatalog.cs")) {
log "Run TypeGen (generating CorePsTypeCatalog.cs)"
Start-TypeGen
}
@ -1529,7 +1529,6 @@ function Publish-NuGetFeed
'Microsoft.PowerShell.ConsoleHost',
'Microsoft.PowerShell.Security',
'System.Management.Automation',
'Microsoft.PowerShell.CoreCLR.AssemblyLoadContext',
'Microsoft.PowerShell.CoreCLR.Eventing',
'Microsoft.WSMan.Management',
'Microsoft.WSMan.Runtime',
@ -1635,7 +1634,7 @@ function Start-TypeGen
Push-Location "$PSScriptRoot/src/TypeCatalogGen"
try {
dotnet run ../Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CorePsTypeCatalog.cs powershell.inc
dotnet run ../System.Management.Automation/CoreCLR/CorePsTypeCatalog.cs powershell.inc
} finally {
Pop-Location
}
@ -2074,7 +2073,6 @@ function Start-CrossGen {
"Microsoft.PowerShell.Commands.Utility.dll",
"Microsoft.PowerShell.Commands.Management.dll",
"Microsoft.PowerShell.Security.dll",
"Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll",
"Microsoft.PowerShell.CoreCLR.Eventing.dll",
"Microsoft.PowerShell.ConsoleHost.dll",
"Microsoft.PowerShell.PSReadLine.dll",

View File

@ -1,3 +0,0 @@
using System.Runtime.CompilerServices;
using System.Reflection;
[assembly: InternalsVisibleTo("System.Management.Automation,PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]

View File

@ -1,76 +0,0 @@
/********************************************************************++
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
{
/// <summary>
/// Construct the strong assembly name from metadata
/// </summary>
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;
}
}
}

View File

@ -1,24 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<Import Project="..\..\PowerShell.Common.props" />
<PropertyGroup>
<Description>PowerShell Core's Microsoft.PowerShell.CoreCLR.AssemblyLoadContext project</Description>
<AssemblyName>Microsoft.PowerShell.CoreCLR.AssemblyLoadContext</AssemblyName>
</PropertyGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);CORECLR</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugType>portable</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Linux' ">
<DefineConstants>$(DefineConstants);UNIX</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'CodeCoverage' ">
<DebugType>full</DebugType>
</PropertyGroup>
</Project>

View File

@ -267,7 +267,7 @@ namespace System.Management.Automation
{
#if !UNIX
//Try loading from GAC
if(!TryFindInGAC(assemblyName, out asmFilePath))
if (!TryFindInGAC(assemblyName, out asmFilePath))
{
return null;
}
@ -295,53 +295,55 @@ namespace System.Management.Automation
// If there are multiple version of the assembly, load the latest.
private bool TryFindInGAC(AssemblyName assemblyName, out string assemblyFilePath)
{
bool assemblyFound = false;
assemblyFilePath = null;
if (Internal.InternalTestHooks.DisableGACLoading)
{
return false;
}
bool assemblyFound = false;
char dirSeparator = IO.Path.DirectorySeparatorChar;
if (PowerShellAssemblyLoadContextTestHooks.AllowGACLoading)
if (String.IsNullOrEmpty(_winDir))
{
if(String.IsNullOrEmpty(_winDir))
//cache value of '_winDir' folder in member variable.
_winDir = Environment.GetEnvironmentVariable("winDir");
}
if (String.IsNullOrEmpty(_gacPathMSIL))
{
//cache value of '_gacPathMSIL' folder in member variable.
_gacPathMSIL = $"{_winDir}{dirSeparator}Microsoft.NET{dirSeparator}assembly{dirSeparator}GAC_MSIL";
}
assemblyFound = FindInGac(_gacPathMSIL, assemblyName, out assemblyFilePath);
if (!assemblyFound)
{
string gacBitnessAwarePath = null;
if (Environment.Is64BitProcess)
{
//cache value of '_winDir' folder in member variable.
_winDir = Environment.GetEnvironmentVariable("winDir");
}
if (String.IsNullOrEmpty(_gacPathMSIL))
{
//cache value of '_gacPathMSIL' folder in member variable.
_gacPathMSIL = $"{_winDir}{dirSeparator}Microsoft.NET{dirSeparator}assembly{dirSeparator}GAC_MSIL";
}
assemblyFound = FindInGac(_gacPathMSIL, assemblyName, out assemblyFilePath);
if(!assemblyFound)
{
string gacBitnessAwarePath = null;
if(Environment.Is64BitProcess)
if (String.IsNullOrEmpty(_gacPath64))
{
if(String.IsNullOrEmpty(_gacPath64))
{
//cache value of '_gacPath64' folder in member variable.
_gacPath64 = $"{_winDir}{dirSeparator}Microsoft.NET{dirSeparator}assembly{dirSeparator}GAC_64";
}
gacBitnessAwarePath = _gacPath64;
}
else
{
if(String.IsNullOrEmpty(_gacPath32))
{
//cache value of '_gacPath32' folder in member variable.
_gacPath32 = $"{_winDir}{dirSeparator}Microsoft.NET{dirSeparator}assembly{dirSeparator}GAC_32";
}
gacBitnessAwarePath = _gacPath32;
//cache value of '_gacPath64' folder in member variable.
_gacPath64 = $"{_winDir}{dirSeparator}Microsoft.NET{dirSeparator}assembly{dirSeparator}GAC_64";
}
assemblyFound = FindInGac(gacBitnessAwarePath, assemblyName, out assemblyFilePath);
gacBitnessAwarePath = _gacPath64;
}
else
{
if (String.IsNullOrEmpty(_gacPath32))
{
//cache value of '_gacPath32' folder in member variable.
_gacPath32 = $"{_winDir}{dirSeparator}Microsoft.NET{dirSeparator}assembly{dirSeparator}GAC_32";
}
gacBitnessAwarePath = _gacPath32;
}
assemblyFound = FindInGac(gacBitnessAwarePath, assemblyName, out assemblyFilePath);
}
return assemblyFound;
@ -356,17 +358,17 @@ namespace System.Management.Automation
char dirSeparator = IO.Path.DirectorySeparatorChar;
string tempAssemblyDirPath = $"{gacRoot}{dirSeparator}{assemblyName.Name}";
if(Directory.Exists(tempAssemblyDirPath))
if (Directory.Exists(tempAssemblyDirPath))
{
//Enumerate all directories, sort by name and select the last. This selects the latest version.
var chosenVersionDirectory = Directory.GetDirectories(tempAssemblyDirPath).OrderBy(d => d).LastOrDefault();
if(!String.IsNullOrEmpty(chosenVersionDirectory))
if (!String.IsNullOrEmpty(chosenVersionDirectory))
{
//Select first or default as the directory will contain only one assembly. If nothing then default is null;
var foundAssemblyPath = Directory.GetFiles(chosenVersionDirectory, $"{assemblyName.Name}*").FirstOrDefault();
if(!String.IsNullOrEmpty(foundAssemblyPath))
if (!String.IsNullOrEmpty(foundAssemblyPath))
{
AssemblyName asmNameFound = AssemblyLoadContext.GetAssemblyName(foundAssemblyPath);
if (IsAssemblyMatching(assemblyName, asmNameFound))
@ -515,24 +517,6 @@ namespace System.Management.Automation
PowerShellAssemblyLoadContext.InitializeSingleton(basePaths);
}
}
/// <summary>
/// Test hooks for PowershellAssemblyLoadContext
/// </summary>
public static class PowerShellAssemblyLoadContextTestHooks
{
internal static bool AllowGACLoading = true;
/// <summary>This member is used for internal test purposes.</summary>
public static void SetTestHook(string property, bool value)
{
var fieldInfo = typeof(PowerShellAssemblyLoadContextTestHooks).GetField(property, BindingFlags.Static | BindingFlags.NonPublic);
if (fieldInfo != null)
{
fieldInfo.SetValue(null, value);
}
}
}
}
#endif

View File

@ -1,26 +0,0 @@
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
namespace Microsoft.PowerShell.CoreCLR
{
using System;
using System.Reflection;
/// <summary>
/// AssemblyExtensions
/// </summary>
public static class AssemblyExtensions
{
/// <summary>
/// Load an assembly given its file path.
/// </summary>
/// <param name="assemblyPath">The path of the file that contains the manifest of the assembly.</param>
/// <returns>The loaded assembly.</returns>
[ObsoleteAttribute("This method is obsolete. Call Assembly.LoadFrom instead", false)]
public static Assembly LoadFrom(string assemblyPath)
{
return Assembly.LoadFrom(assemblyPath);
}
}
}

View File

@ -7,7 +7,6 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.PowerShell.CoreCLR.AssemblyLoadContext\Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.csproj" />
<ProjectReference Include="..\Microsoft.PowerShell.CoreCLR.Eventing\Microsoft.PowerShell.CoreCLR.Eventing.csproj" />
<PackageReference Include="Microsoft.Win32.Registry.AccessControl" Version="4.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />

View File

@ -1470,6 +1470,7 @@ namespace System.Management.Automation.Internal
internal static bool IgnoreScriptBlockCache;
// Simulate 'System.Diagnostics.Stopwatch.IsHighResolution is false' to test Get-Uptime throw
internal static bool StopwatchIsNotHighResolution;
internal static bool DisableGACLoading;
/// <summary>This member is used for internal test purposes.</summary>
public static void SetTestHook(string property, bool value)

View File

@ -680,7 +680,6 @@ namespace NativeMsh
"Microsoft.PowerShell.Commands.Management",
"Microsoft.PowerShell.Commands.Utility",
"Microsoft.PowerShell.ConsoleHost",
"Microsoft.PowerShell.CoreCLR.AssemblyLoadContext",
"Microsoft.PowerShell.CoreCLR.Eventing",
"Microsoft.PowerShell.LocalAccounts",
"Microsoft.PowerShell.PSReadLine",
@ -1405,12 +1404,9 @@ namespace NativeMsh
bool listEmpty = true;
this->GetTrustedAssemblyList(hostEnvironment.GetCoreCLRDirectoryPath(), assemblyList, listEmpty);
// Fall back to attempt to load the CLR from the alternate inbox location
// or if the ALC was not located in the CoreCLR directory.
std::string assemblyListToSearch = assemblyList.str();
if (listEmpty ||
(std::string::npos == assemblyListToSearch.rfind("Microsoft.PowerShell.CoreCLR.AssemblyLoadContext")))
if (listEmpty)
{
// Fall back to attempt to load the CLR from the alternate inbox location
char coreCLRPowerShellExtInstallPath[MAX_PATH];
::ExpandEnvironmentStringsA(coreCLRPowerShellExtInstallDirectory, coreCLRPowerShellExtInstallPath, MAX_PATH);
this->GetTrustedAssemblyList(coreCLRPowerShellExtInstallPath, assemblyList, listEmpty);

View File

@ -36,7 +36,7 @@
<ItemGroup>
<PackageReference Include="PSDesiredStateConfiguration" Version="1.0.0-alpha01" />
<PackageReference Include="PowerShellHelpFiles" Version="1.0.0-alpha01" />
<PackageReference Include="psrp.windows" Version="6.0.0-beta.2" />
<PackageReference Include="psrp.windows" Version="6.0.0-*" />
</ItemGroup>
<PropertyGroup>

View File

@ -73,11 +73,11 @@ Describe "Import-Module with ScriptsToProcess" -Tags "CI" {
Describe "Import-Module for Binary Modules in GAC" -Tags 'CI' {
Context "Modules are not loaded from GAC" {
BeforeAll {
[System.Management.Automation.PowerShellAssemblyLoadContextTestHooks]::SetTestHook('AllowGACLoading', $false)
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('DisableGACLoading', $true)
}
AfterAll {
[System.Management.Automation.PowerShellAssemblyLoadContextTestHooks]::SetTestHook('AllowGACLoading', $true)
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('DisableGACLoading', $false)
}
It "Load PSScheduledJob from Windows Powershell Modules folder should fail" -Skip:(-not $IsWindows) {