Skip to content
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

Add SetEntryAssembly() API to System.Reflection #102271

Merged
merged 14 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ public void FilePathIsPopulatedCorrectly()
}

/// <summary>
/// Makes Assembly.GetEntryAssembly() return null using private reflection.
/// Makes Assembly.GetEntryAssembly() return null by passing the null literal
/// to Assembly.SetEntryAssembly().
/// </summary>
private static void MakeAssemblyGetEntryAssemblyReturnNull()
ivdiazsa marked this conversation as resolved.
Show resolved Hide resolved
{
typeof(Assembly)
.GetField("s_forceNullEntryPoint", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, true);

Assembly.SetEntryAssembly(null);
Assert.Null(Assembly.GetEntryAssembly());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,12 @@ public void EntryAssemblyName_Null_NotIncludedInTrace()
}

/// <summary>
/// Makes Assembly.GetEntryAssembly() return null using private reflection.
/// Makes Assembly.GetEntryAssembly() return null by passing the null literal
/// to Assembly.SetEntryAssembly().
/// </summary>
private static void MakeAssemblyGetEntryAssemblyReturnNull()
ivdiazsa marked this conversation as resolved.
Show resolved Hide resolved
{
typeof(Assembly)
.GetField("s_forceNullEntryPoint", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, true);

Assembly.SetEntryAssembly(null);
Assert.Null(Assembly.GetEntryAssembly());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,38 @@ public override string ToString()
return type.Module?.Assembly;
}

// internal test hook
private static bool s_forceNullEntryPoint;
private static object? s_overriddenEntryAssembly;

/// <summary>
/// Sets the application's entry assembly to the provided assembly object
/// as argument.
ivdiazsa marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="assembly">
/// Assembly object that represents the application's new entry assembly.
/// </param>
/// <remarks>
/// It is important to mention that the assembly passed to this function
/// has to be a runtime defined Assembly type object. Otherwise, an exception
/// will be thrown.
ivdiazsa marked this conversation as resolved.
Show resolved Hide resolved
/// </remarks>
public static void SetEntryAssembly(Assembly? assembly)
{
if (assembly is null)
{
s_overriddenEntryAssembly = string.Empty;
return;
}

if (assembly is not RuntimeAssembly)
throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly);

s_overriddenEntryAssembly = assembly;
ivdiazsa marked this conversation as resolved.
Show resolved Hide resolved
}

public static Assembly? GetEntryAssembly()
{
if (s_forceNullEntryPoint)
return null;
if (s_overriddenEntryAssembly is not null)
return s_overriddenEntryAssembly as Assembly;

return GetEntryAssemblyInternal();
}
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11157,6 +11157,7 @@ public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types and members the loaded assembly depends on might be removed")]
[System.ObsoleteAttribute("ReflectionOnly loading is not supported and throws PlatformNotSupportedException.", DiagnosticId="SYSLIB0018", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static System.Reflection.Assembly ReflectionOnlyLoadFrom(string assemblyFile) { throw null; }
public static void SetEntryAssembly(System.Reflection.Assembly? assembly) { throw null; }
public override string ToString() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types and members the loaded assembly depends on might be removed")]
public static System.Reflection.Assembly UnsafeLoadFrom(string assemblyFile) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Reflection.Tests;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using Microsoft.DotNet.RemoteExecutor;

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Debug Mono_MiniJIT_LibrariesTests)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Debug Mono_MiniJIT_LibrariesTests)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Debug Mono_Interpreter_LibrariesTests)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-arm64 Release NativeAOT_Libraries)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Release NativeAOT_Libraries)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-arm checked CoreCLR_ReleaseLibraries)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Debug Mono_MiniJIT_LibrariesTests)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-x64 Debug CoreCLR_Libraries)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 Debug CoreCLR_Libraries)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-x64 Debug Libraries_CheckedCoreCLR)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux_musl-arm64 checked CoreCLR_ReleaseLibraries)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Debug CoreCLR_Libraries)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-x64 checked CoreCLR_ReleaseLibraries)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build linux-arm64 Debug Libraries_CheckedCoreCLR)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build osx-x64 Debug Libraries_CheckedCoreCLR)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_EAT)

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)

Check failure on line 15 in src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs

View check run for this annotation

Azure Pipelines / runtime

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs#L15

src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs(15,24): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'RemoteExecutor' does not exist in the namespace 'Microsoft.DotNet' (are you missing an assembly reference?)
using Xunit;

[assembly:
Expand Down Expand Up @@ -181,6 +183,32 @@
Assert.True(correct, $"Unexpected assembly name {assembly}");
}

[Fact]
ivdiazsa marked this conversation as resolved.
Show resolved Hide resolved
public void SetEntryAssembly()
ivdiazsa marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.NotNull(Assembly.GetEntryAssembly());

RemoteExecutor.Invoke(() =>
{
Assembly.SetEntryAssembly(null);
Assert.Null(Assembly.GetEntryAssembly());

Assembly originalAssembly = typeof(AssemblyTests).Assembly;

Assembly.SetEntryAssembly(originalAssembly);
Assert.Equal(Assembly.GetEntryAssembly(), originalAssembly);
ivdiazsa marked this conversation as resolved.
Show resolved Hide resolved

var invalidAssembly = new PersistedAssemblyBuilder(
new AssemblyName("NotaRuntimeAssemblyTest"),
typeof(object).Assembly
);

Assert.Throws<ArgumentException>(
() => Assembly.SetEntryAssembly(invalidAssembly)
);
}).Dispose();
}
ivdiazsa marked this conversation as resolved.
Show resolved Hide resolved

[Fact]
public void GetFile()
{
Expand Down
Loading