-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Discover assemblies loaded by 'Assembly.Load(byte[])' and 'Assembly.LoadFile' #12203
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| bin/ | ||
| obj/ | ||
| .ionide/ | ||
| project.lock.json | ||
| *-tests.xml | ||
| /debug/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,9 +58,36 @@ internal static IEnumerable<Assembly> GetAssemblies(TypeResolutionState typeReso | |
| /// </param> | ||
| internal static IEnumerable<Assembly> GetAssemblies(string namespaceQualifiedTypeName = null) | ||
| { | ||
| return PSAssemblyLoadContext.GetAssembly(namespaceQualifiedTypeName) ?? | ||
| AssemblyLoadContext.Default.Assemblies.Where(a => | ||
| !a.FullName.StartsWith(TypeDefiner.DynamicClassAssemblyFullNamePrefix, StringComparison.Ordinal)); | ||
| return PSAssemblyLoadContext.GetAssembly(namespaceQualifiedTypeName) ?? GetPSVisibleAssemblies(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Return assemblies from the default load context and the 'individual' load contexts. | ||
| /// The 'individual' load contexts are the ones holding assemblies loaded via 'Assembly.Load(byte[])' and 'Assembly.LoadFile'. | ||
| /// Assemblies loaded in any custom load contexts are not consider visible to PowerShell to avoid type identity issues. | ||
| /// </summary> | ||
| private static IEnumerable<Assembly> GetPSVisibleAssemblies() | ||
| { | ||
| const string IndividualAssemblyLoadContext = "System.Runtime.Loader.IndividualAssemblyLoadContext"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My concern is that
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any one can create a ALC instance with the name "Assembly.Load(byte[], ...)" or "Assembly.LoadFile(". |
||
|
|
||
| foreach (Assembly assembly in AssemblyLoadContext.Default.Assemblies) | ||
| { | ||
| if (!assembly.FullName.StartsWith(TypeDefiner.DynamicClassAssemblyFullNamePrefix, StringComparison.Ordinal)) | ||
| { | ||
| yield return assembly; | ||
| } | ||
| } | ||
|
|
||
| foreach (AssemblyLoadContext context in AssemblyLoadContext.All) | ||
| { | ||
| if (IndividualAssemblyLoadContext.Equals(context.GetType().FullName, StringComparison.Ordinal)) | ||
iSazonov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| foreach (Assembly assembly in context.Assemblies) | ||
| { | ||
| yield return assembly; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| Describe "Assembly loaded in IndividualAssemblyLoadContext should be visible to PowerShell" -Tags "CI" { | ||
| BeforeAll { | ||
| $code1 = @' | ||
| namespace LoadBytes { | ||
| public class MyLoadBytesTest { | ||
| public static string GetName() { return "MyLoadBytesTest"; } | ||
| } | ||
| } | ||
| '@ | ||
| $code2 = @' | ||
| namespace LoadFile { | ||
| public class MyLoadFileTest { | ||
| public static string GetName() { return "MyLoadFileTest"; } | ||
| } | ||
| } | ||
| '@ | ||
|
|
||
| $tempFolderPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), "IndividualALCTest") | ||
| New-Item $tempFolderPath -ItemType Directory -Force > $null | ||
| $loadBytesFile = [System.IO.Path]::Combine($tempFolderPath, "MyLoadBytesTest.dll") | ||
| $loadFileFile = [System.IO.Path]::Combine($tempFolderPath, "MyLoadFileTest.dll") | ||
|
|
||
| if (-not (Test-Path $loadBytesFile)) { | ||
| Add-Type -TypeDefinition $code1 -OutputAssembly $loadBytesFile | ||
| } | ||
|
|
||
| if (-not (Test-Path $loadFileFile)) { | ||
| Add-Type -TypeDefinition $code2 -OutputAssembly $loadFileFile | ||
| } | ||
| } | ||
|
|
||
| It "Assembly loaded via 'Assembly.Load(byte[])' should be discoverable" { | ||
| $bytes = [System.IO.File]::ReadAllBytes($loadBytesFile) | ||
| [System.Reflection.Assembly]::Load($bytes) > $null | ||
|
|
||
| [LoadBytes.MyLoadBytesTest]::GetName() | Should -BeExactly "MyLoadBytesTest" | ||
| } | ||
|
|
||
| It "Assembly loaded via 'Assembly.LoadFile' should be discoverable" { | ||
| [System.Reflection.Assembly]::LoadFile($loadFileFile) > $null | ||
|
|
||
| [LoadFile.MyLoadFileTest]::GetName() | Should -BeExactly "MyLoadFileTest" | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.