8000 Refactor PowerShell Core to use the default CoreCLR loader instead (#… · PowerShell/PowerShell@8ae6c5b · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ae6c5b

Browse files
authored
Refactor PowerShell Core to use the default CoreCLR loader instead (#3903)
Remove the code that spins up our own assembly load context. Keep the code that registers our `Resolve` method to the default loader's `Resolving` event, so that we can continue to do special assembly resolution as needed (such as the GAC probing logic needed for consuming FullCLR PS modules). Essentially, the assembly `Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll` is not needed anymore, the remaining code should be moved to S.M.A.dll. However, that will break DSC and other native hosts that are hosting powershell. So this assembly is kept for now.
1 parent 7354853 commit 8ae6c5b

File tree

32 files changed

+176
-848
lines changed
  • hostifaces
  • parser
  • remoting/fanin
  • help
  • minishell/api
  • resources
  • security
  • singleshell/config
  • utils
  • TypeCatalogGen
  • powershell
  • 32 files changed

    +176
    -848
    lines changed

    src/Microsoft.PowerShell.Commands.Management/cimSupport/cmdletization/cim/cimConverter.cs

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -80,7 +80,7 @@ internal void Copy(string source, int offset)
    8080

    8181
    internal void Copy(SecureString source, int offset)
    8282
    {
    83-
    IntPtr plainTextString = ClrFacade.SecureStringToCoTaskMemUnicode(source);
    83+
    IntPtr plainTextString = Marshal.SecureStringToCoTaskMemUnicode(source);
    8484
    try
    8585
    {
    8686
    unsafe

    src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs

    Lines changed: 9 additions & 14 deletions
    Original file line numberDiff line numberDiff line change
    @@ -641,7 +641,7 @@ protected override void ProcessRecord()
    641641
    {
    642642
    //assigning to tempmodule to rethrow for exceptions on 64 bit machines
    643643
    tempmodule = pmodule;
    644-
    WriteObject(ClrFacade.GetProcessModuleFileVersionInfo(pmodule), true);
    644+
    WriteObject(pmodule.FileVersionInfo, true);
    645645
    }
    646646
    }
    647647
    catch (InvalidOperationException exception)
    @@ -658,7 +658,7 @@ protected override void ProcessRecord()
    658658
    {
    659659
    if (exception.HResult == 299)
    660660
    {
    661-
    WriteObject(ClrFacade.GetProcessModuleFileVersionInfo(tempmodule), true);
    661+
    WriteObject(tempmodule.FileVersionInfo, true);
    662662
    }
    663663
    else
    664664
    {
    @@ -710,7 +710,7 @@ protected override void ProcessRecord()
    710710
    //if fileversion of each process is to be displayed
    711711
    try
    712712
    {
    713-
    WriteObject(ClrFacade.GetProcessModuleFileVersionInfo(PsUtils.GetMainModule(process)), true);
    713+
    WriteObject(PsUtils.GetMainModule(process).FileVersionInfo, true);
    714714
    }
    715715
    catch (InvalidOperationException exception)
    716716
    {
    @@ -726,7 +726,7 @@ protected override void ProcessRecord()
    726726
    {
    727727
    if (exception.HResult == 299)
    728728
    {
    729-
    WriteObject(ClrFacade.GetProcessModuleFileVersionInfo(PsUtils.GetMainModule(process)), true);
    729+
    WriteObject(PsUtils.GetMainModule(process).FileVersionInfo, true);
    730730
    }
    731731
    else
    732732
    {
    @@ -1966,7 +1966,7 @@ protected override void BeginProcessing()
    19661966
    //UseNewEnvironment
    19671967
    if (_UseNewEnvironment)
    19681968
    {
    1969-
    ClrFacade.GetProcessEnvironment(startInfo).Clear();
    1969+
    startInfo.EnvironmentVariables.Clear();
    19701970
    LoadEnvironmentVariable(startInfo, Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine));
    19711971
    LoadEnvironmentVariable(startInfo, Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User));
    19721972
    }
    @@ -2173,7 +2173,7 @@ private string ResolveFilePath(string path)
    21732173

    21742174
    private void LoadEnvironmentVariable(ProcessStartInfo startinfo, IDictionary EnvironmentVariables)
    21752175
    {
    2176-
    var processEnvironment = ClrFacade.GetProcessEnvironment(startinfo);
    2176+
    var processEnvironment = startinfo.EnvironmentVariables;
    21772177
    foreach (DictionaryEntry entry in EnvironmentVariables)
    21782178
    {
    21792179
    if (processEnvironment.ContainsKey(entry.Key.ToString()))
    @@ -2373,12 +2373,7 @@ private static StringBuilder BuildCommandLine(string executableFileName, string
    23732373
    return builder;
    23742374
    }
    23752375

    2376-
    private static byte[] ConvertEnvVarsToByteArray(
    2377-
    #if CORECLR
    2378-
    IDictionary<string, string> sd)
    2379-
    #else
    2380-
    StringDictionary sd)
    2381-
    #endif
    2376+
    private static byte[] ConvertEnvVarsToByteArray(StringDictionary sd)
    23822377
    {
    23832378
    string[] array = new string[sd.Count];
    23842379
    byte[] bytes = null;
    @@ -2497,7 +2492,7 @@ private Process StartWithCreateProcess(ProcessStartInfo startinfo)
    24972492
    creationFlags |= 0x00000004;
    24982493

    24992494
    IntPtr AddressOfEnvironmentBlock = IntPtr.Zero;
    2500-
    var environmentVars = ClrFacade.GetProcessEnvironment(startinfo);
    2495+
    var environmentVars = startinfo.EnvironmentVariables;
    25012496
    if (environmentVars != null)
    25022497
    {
    25032498
    if (this.UseNewEnvironment)
    @@ -2647,7 +2642,7 @@ internal ProcessCollection()
    26472642
    internal bool AssignProcessToJobObject(Process process)
    26482643
    {
    26492644
    // Add the process to the job object
    2650-
    bool result = NativeMethods.AssignProcessToJobObject(_jobObjectHandle, ClrFacade.GetRawProcessHandle(process));
    2645+
    bool result = NativeMethods.AssignProcessToJobObject(_jobObjectHandle, process.Handle);
    26512646
    return result;
    26522647
    }
    26532648

    src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -2054,7 +2054,7 @@ protected override void BeginProcessing()
    20542054
    if (null != Credential)
    20552055
    {
    20562056
    username = Credential.UserName;
    2057-
    password = ClrFacade.SecureStringToCoTaskMemUnicode(Credential.Password);
    2057+
    password = Marshal.SecureStringToCoTaskMemUnicode(Credential.Password);
    20582058
    }
    20592059

    20602060
    // Create the service

    src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs

    Lines changed: 4 additions & 14 deletions
    Original file line numberDiff line numberDiff line change
    @@ -964,7 +964,7 @@ private void LoadAssemblies(IEnumerable<string> assemblies)
    964964
    Assembly assembly = LoadAssemblyHelper(assemblyName);
    965965
    if (assembly == null)
    966966
    {
    967-
    assembly = LoadFrom(ResolveAssemblyName(assemblyName, false));
    967+
    assembly = Assembly.LoadFrom(ResolveAssemblyName(assemblyName, false));
    968968
    }
    969969

    970970
    if (passThru)
    @@ -1276,7 +1276,7 @@ private void CompileSourceToAssembly(string source)
    12761276
    {
    12771277
    ms.Flush();
    12781278
    ms.Seek(0, SeekOrigin.Begin);
    1279-
    Assembly assembly = LoadFrom(ms);
    1279+
    Assembly assembly = Assembly.Load(ms.ToArray());
    12801280
    CheckTypesForDuplicates(assembly);
    12811281
    if (passThru)
    12821282
    {
    @@ -1292,7 +1292,7 @@ private void CompileSourceToAssembly(string source)
    12921292
    {
    12931293
    if (passThru)
    12941294
    {
    1295-
    Assembly assembly = LoadFrom(outputAssembly);
    1295+
    Assembly assembly = Assembly.LoadFrom(outputAssembly);
    12961296
    CheckTypesForDuplicates(assembly);
    12971297
    WriteTypes(assembly);
    12981298
    }
    @@ -1318,16 +1318,6 @@ private AddTypeCompilerError[] GetErrors(ImmutableArray<Diagnostic> diagnostics)
    13181318
    ErrorNumber = d.Id
    13191319
    }).ToArray();
    13201320
    }
    1321-
    1322-
    private Assembly LoadFrom(Stream assembly)
    1323-
    {
    1324-
    return ClrFacade.LoadFrom(assembly);
    1325-
    }
    1326-
    1327-
    private Assembly LoadFrom(string path)
    1328-
    {
    1329-
    return ClrFacade.LoadFrom(path);
    1330-
    }
    13311321
    }
    13321322

    13331323

    @@ -1955,7 +1945,7 @@ private void LoadAssemblyFromPathOrName(List<Type> generatedTypes)
    19551945
    {
    19561946
    foreach (string path in paths)
    19571947
    {
    1958-
    generatedTypes.AddRange(ClrFacade.LoadFrom(path).GetTypes());
    1948+
    generatedTypes.AddRange(Assembly.LoadFrom(path).GetTypes());
    19591949
    }
    19601950
    }
    19611951
    // Load the assembly by name

    src/Microsoft.PowerShell.Commands.Utility/commands/utility/Import-LocalizedData.cs

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -285,7 +285,7 @@ private string GetFilePath()
    285285
    {
    286286
    try
    287287
    {
    288-
    culture = ClrFacade.GetCultureInfo(_uiculture);
    288+
    culture = CultureInfo.GetCultureInfo(_uiculture);
    289289
    }
    290290
    catch (ArgumentException)
    291291
    {

    src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs

    Lines changed: 2 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1079,8 +1079,8 @@ internal ConsoleHost(RunspaceConfiguration configuration)
    10791079
    }
    10801080
    #endif
    10811081

    1082-
    ClrFacade.SetCurrentThreadUiCulture(this.CurrentUICulture);
    1083-
    ClrFacade.SetCurrentThreadCulture(this.CurrentCulture);
    1082+
    Thread.CurrentThread.CurrentUICulture = this.CurrentUICulture;
    1083+
    Thread.CurrentThread.CurrentCulture = this.CurrentCulture;
    10841084
    // BUG: 610329. Tell PowerShell engine to apply console
    10851085
    // related properties while launching Pipeline Execution
    10861086
    // Thread.

    src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs

    Lines changed: 3 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -9,6 +9,7 @@
    99
    using System.Management.Automation.Runspaces;
    1010
    using System.Management.Automation.Tracing;
    1111
    using System.Globalization;
    12+
    using System.Threading;
    1213

    1314
    #if CORECLR
    1415
    using System.Runtime.InteropServices;
    @@ -65,8 +66,8 @@ public int Start(string consoleFilePath, string[] args)
    6566
    // The currentUICulture returned NativeCultureResolver supports this non
    6667
    // traditional fallback on Vista. So it is important to set currentUICulture
    6768
    // in the beginning before we do anything.
    68-
    ClrFacade.SetCurrentThreadUiCulture(NativeCultureResolver.UICulture);
    69-
    ClrFacade.SetCurrentThreadCulture(NativeCultureResolver.Culture);
    69+
    Thread.CurrentThread.CurrentUICulture = NativeCultureResolver.UICulture;
    70+
    Thread.CurrentThread.CurrentCulture = NativeCultureResolver.Culture;
    7071

    7172
    RunspaceConfigForSingleShell configuration = null;
    7273
    PSConsoleLoadException warning = null;

    0 commit comments

    Comments
     (0)
    0