Skip to content

Commit

Permalink
Remove assembly loading light-up from scripting (#74409)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jul 25, 2024
1 parent 409f248 commit 5998125
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 296 deletions.
127 changes: 0 additions & 127 deletions src/Scripting/Core/CoreLightup.cs

This file was deleted.

40 changes: 6 additions & 34 deletions src/Scripting/Core/Hosting/AssemblyLoader/AssemblyAndLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.Diagnostics;
using System.Reflection;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Scripting.Hosting
{
internal readonly struct AssemblyAndLocation : IEquatable<AssemblyAndLocation>
{
public Assembly Assembly { get; }
public string Location { get; }
public bool GlobalAssemblyCache { get; }

internal AssemblyAndLocation(Assembly assembly, string location, bool fromGac)
{
Debug.Assert(assembly != null && location != null);

Assembly = assembly;
Location = location;
GlobalAssemblyCache = fromGac;
}
namespace Microsoft.CodeAnalysis.Scripting.Hosting;

public bool IsDefault => Assembly == null;

public bool Equals(AssemblyAndLocation other)
=> Assembly == other.Assembly && Location == other.Location && GlobalAssemblyCache == other.GlobalAssemblyCache;

public override int GetHashCode()
=> Hash.Combine(Assembly, Hash.Combine(Location, Hash.Combine(GlobalAssemblyCache, 0)));

public override bool Equals(object obj)
=> obj is AssemblyAndLocation && Equals((AssemblyAndLocation)obj);
internal readonly record struct AssemblyAndLocation(Assembly Assembly, string Location, bool GlobalAssemblyCache)
{
public bool IsDefault => Assembly == null;

public override string ToString()
=> Assembly + " @ " + (GlobalAssemblyCache ? "<GAC>" : Location);
}
public override string ToString()
=> Assembly + " @ " + (GlobalAssemblyCache ? "<GAC>" : Location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

namespace Microsoft.CodeAnalysis.Scripting.Hosting
{
/// <summary>
Expand Down
35 changes: 7 additions & 28 deletions src/Scripting/Core/Hosting/AssemblyLoader/AssemblyLoaderImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,22 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Scripting.Hosting
{
internal abstract class AssemblyLoaderImpl : IDisposable
internal abstract class AssemblyLoaderImpl(InteractiveAssemblyLoader loader) : IDisposable
{
internal readonly InteractiveAssemblyLoader Loader;

protected AssemblyLoaderImpl(InteractiveAssemblyLoader loader)
{
Loader = loader;
}
internal readonly InteractiveAssemblyLoader Loader = loader;

public static AssemblyLoaderImpl Create(InteractiveAssemblyLoader loader)
{
if (CoreClrShim.AssemblyLoadContext.Type != null)
{
return CreateCoreImpl(loader);
}
else
{
return new DesktopAssemblyLoaderImpl(loader);
}
}

// NoInlining to avoid loading AssemblyLoadContext if not available.
[MethodImpl(MethodImplOptions.NoInlining)]
private static AssemblyLoaderImpl CreateCoreImpl(InteractiveAssemblyLoader loader)
{
return new CoreAssemblyLoaderImpl(loader);
}
#if NET
=> new CoreAssemblyLoaderImpl(loader);
#else
=> new DesktopAssemblyLoaderImpl(loader);
#endif

public abstract Assembly LoadFromStream(Stream peStream, Stream pdbStream);
public abstract AssemblyAndLocation LoadFromPath(string path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable
#if NET

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
Expand Down Expand Up @@ -34,7 +32,7 @@ public override AssemblyAndLocation LoadFromPath(string path)
// but there is no need to reuse contexts.
var assembly = new LoadContext(Loader, Path.GetDirectoryName(path)).LoadFromAssemblyPath(path);

return new AssemblyAndLocation(assembly, path, fromGac: false);
return new AssemblyAndLocation(assembly, path, GlobalAssemblyCache: false);
}

public override void Dispose()
Expand All @@ -44,15 +42,13 @@ public override void Dispose()

private sealed class LoadContext : AssemblyLoadContext
{
private readonly string _loadDirectoryOpt;
private readonly string? _loadDirectory;
private readonly InteractiveAssemblyLoader _loader;

internal LoadContext(InteractiveAssemblyLoader loader, string loadDirectoryOpt)
internal LoadContext(InteractiveAssemblyLoader loader, string? loadDirectory)
{
Debug.Assert(loader != null);

_loader = loader;
_loadDirectoryOpt = loadDirectoryOpt;
_loadDirectory = loadDirectory;

// CoreCLR resolves assemblies in steps:
//
Expand All @@ -68,10 +64,11 @@ internal LoadContext(InteractiveAssemblyLoader loader, string loadDirectoryOpt)
// This order is necessary to avoid loading assemblies twice (by the host App and by interactive loader).

Resolving += (_, assemblyName) =>
_loader.ResolveAssembly(AssemblyIdentity.FromAssemblyReference(assemblyName), _loadDirectoryOpt);
_loader.ResolveAssembly(AssemblyIdentity.FromAssemblyReference(assemblyName), _loadDirectory);
}

protected override Assembly Load(AssemblyName assemblyName) => null;
protected override Assembly? Load(AssemblyName assemblyName) => null;
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable
#if !NET

using System;
using System.IO;
Expand All @@ -13,31 +13,28 @@ namespace Microsoft.CodeAnalysis.Scripting.Hosting
{
internal sealed class DesktopAssemblyLoaderImpl : AssemblyLoaderImpl
{
private readonly Func<string, Assembly, Assembly> _assemblyResolveHandlerOpt;

public DesktopAssemblyLoaderImpl(InteractiveAssemblyLoader loader)
: base(loader)
{
_assemblyResolveHandlerOpt = loader.ResolveAssembly;
CoreLightup.Desktop.AddAssemblyResolveHandler(_assemblyResolveHandlerOpt);
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}

public override void Dispose()
{
if (_assemblyResolveHandlerOpt != null)
{
CoreLightup.Desktop.RemoveAssemblyResolveHandler(_assemblyResolveHandlerOpt);
}
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
}

private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
=> Loader.ResolveAssembly(args.Name, args.RequestingAssembly);

public override Assembly LoadFromStream(Stream peStream, Stream pdbStream)
{
byte[] peImage = new byte[peStream.Length];
var peImage = new byte[peStream.Length];
peStream.TryReadAll(peImage, 0, peImage.Length);

if (pdbStream != null)
{
byte[] pdbImage = new byte[pdbStream.Length];
var pdbImage = new byte[pdbStream.Length];
pdbStream.TryReadAll(pdbImage, 0, pdbImage.Length);

return Assembly.Load(peImage, pdbImage);
Expand All @@ -52,9 +49,8 @@ public override AssemblyAndLocation LoadFromPath(string path)
// Assembly.LoadFile(string) automatically redirects to GAC if the assembly has a strong name and there is an equivalent assembly in GAC.

var assembly = Assembly.LoadFile(path);
var location = assembly.Location;
var fromGac = CoreLightup.Desktop.IsAssemblyFromGlobalAssemblyCache(assembly);
return new AssemblyAndLocation(assembly, location, fromGac);
return new AssemblyAndLocation(assembly, assembly.Location, assembly.GlobalAssemblyCache);
}
}
}
#endif
Loading

0 comments on commit 5998125

Please sign in to comment.