forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the metadata update related APIs to the MetadataUpdater class
The old APIs AssemblyExtensions.ApplyUpdate() and AssemblyExtensions.GetApplyUpdateCapabilities() will be removed after all the references to them have been changed to the new APIs. Add the new IsSupported API described in issue dotnet#51159. Change the tests to use the MetadataUpdater APIs.
- Loading branch information
Showing
9 changed files
with
179 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/MetadataUpdater.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace System.Reflection.Metadata | ||
{ | ||
public static class MetadataUpdater | ||
{ | ||
[DllImport(RuntimeHelpers.QCall)] | ||
private static extern unsafe void ApplyUpdate(QCallAssembly assembly, byte* metadataDelta, int metadataDeltaLength, byte* ilDelta, int ilDeltaLength, byte* pdbDelta, int pdbDeltaLength); | ||
|
||
/// <summary> | ||
/// Updates the specified assembly using the provided metadata, IL and PDB deltas. | ||
/// </summary> | ||
/// <remarks> | ||
/// Currently executing methods will continue to use the existing IL. New executions of modified methods will | ||
/// use the new IL. Different runtimes may have different limitations on what kinds of changes are supported, | ||
/// and runtimes make no guarantees as to the state of the assembly and process if the delta includes | ||
/// unsupported changes. | ||
/// </remarks> | ||
/// <param name="assembly">The assembly to update.</param> | ||
/// <param name="metadataDelta">The metadata changes to be applied.</param> | ||
/// <param name="ilDelta">The IL changes to be applied.</param> | ||
/// <param name="pdbDelta">The PDB changes to be applied.</param> | ||
/// <exception cref="ArgumentNullException">The assembly argument is null.</exception> | ||
/// <exception cref="NotSupportedException">The update could not be applied.</exception> | ||
public static void ApplyUpdate(Assembly assembly, ReadOnlySpan<byte> metadataDelta, ReadOnlySpan<byte> ilDelta, ReadOnlySpan<byte> pdbDelta) | ||
{ | ||
if (assembly == null) | ||
{ | ||
throw new ArgumentNullException(nameof(assembly)); | ||
} | ||
|
||
RuntimeAssembly? runtimeAssembly = assembly as RuntimeAssembly; | ||
if (runtimeAssembly == null) | ||
{ | ||
throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly); | ||
} | ||
|
||
unsafe | ||
{ | ||
RuntimeAssembly rtAsm = runtimeAssembly; | ||
fixed (byte* metadataDeltaPtr = metadataDelta, ilDeltaPtr = ilDelta, pdbDeltaPtr = pdbDelta) | ||
{ | ||
ApplyUpdate(new QCallAssembly(ref rtAsm), metadataDeltaPtr, metadataDelta.Length, ilDeltaPtr, ilDelta.Length, pdbDeltaPtr, pdbDelta.Length); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Returns the metadata update capabilities. | ||
/// </summary> | ||
public static string GetCapabilities() => "Baseline AddMethodToExistingType AddStaticFieldToExistingType AddInstanceFieldToExistingType NewTypeDefinition"; | ||
|
||
/// <summary> | ||
/// Returns true if the apply assembly update is enabled and available. | ||
/// </summary> | ||
public static bool IsSupported => Debugger.IsAttached || Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES") != "" || Environment.GetEnvironmentVariable("COMPlus_ForceEnc") == "1"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
src/libraries/System.Runtime.Loader/tests/AssemblyExtensionsTest.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/mono/System.Private.CoreLib/src/System/Reflection/Metadata/MetadataUpdater.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.Reflection.Metadata | ||
{ | ||
public static class MetadataUpdater | ||
{ | ||
/// <summary> | ||
/// Updates the specified assembly using the provided metadata, IL and PDB deltas. | ||
/// </summary> | ||
/// <remarks> | ||
/// Currently executing methods will continue to use the existing IL. New executions of modified methods will | ||
/// use the new IL. Different runtimes may have different limitations on what kinds of changes are supported, | ||
/// and runtimes make no guarantees as to the state of the assembly and process if the delta includes | ||
/// unsupported changes. | ||
/// </remarks> | ||
/// <param name="assembly">The assembly to update.</param> | ||
/// <param name="metadataDelta">The metadata changes to be applied.</param> | ||
/// <param name="ilDelta">The IL changes to be applied.</param> | ||
/// <param name="pdbDelta">The PDB changes to be applied.</param> | ||
/// <exception cref="ArgumentNullException">The assembly argument is null.</exception> | ||
/// <exception cref="NotSupportedException">The update could not be applied.</exception> | ||
public static void ApplyUpdate(Assembly assembly, ReadOnlySpan<byte> metadataDelta, ReadOnlySpan<byte> ilDelta, ReadOnlySpan<byte> pdbDelta) | ||
{ | ||
if (assembly is not RuntimeAssembly runtimeAssembly) | ||
{ | ||
if (assembly is null) throw new ArgumentNullException(nameof(assembly)); | ||
throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly); | ||
} | ||
|
||
// System.Private.CoreLib is not editable | ||
if (runtimeAssembly == typeof(AssemblyExtensions).Assembly) | ||
throw new InvalidOperationException (SR.InvalidOperation_AssemblyNotEditable); | ||
|
||
unsafe | ||
{ | ||
IntPtr monoAssembly = runtimeAssembly.GetUnderlyingNativeHandle (); | ||
fixed (byte* metadataDeltaPtr = metadataDelta, ilDeltaPtr = ilDelta, pdbDeltaPtr = pdbDelta) | ||
{ | ||
ApplyUpdate_internal(monoAssembly, metadataDeltaPtr, metadataDelta.Length, ilDeltaPtr, ilDelta.Length, pdbDeltaPtr, pdbDelta.Length); | ||
} | ||
} | ||
} | ||
|
||
private static Lazy<string> s_ApplyUpdateCapabilities = new Lazy<string>(() => InitializeApplyUpdateCapabilities()); | ||
|
||
public static string GetCapabilities() => s_ApplyUpdateCapabilities.Value; | ||
|
||
private static string InitializeApplyUpdateCapabilities() | ||
{ | ||
return ApplyUpdateEnabled() != 0 ? "Baseline" : string.Empty ; | ||
} | ||
|
||
public static bool IsSupported => Debugger.IsAttached || Environment.GetEnvironmentVariable("DOTNET_MODIFIABLE_ASSEMBLIES") != ""; | ||
|
||
[MethodImpl (MethodImplOptions.InternalCall)] | ||
private static extern int ApplyUpdateEnabled (); | ||
|
||
[MethodImpl (MethodImplOptions.InternalCall)] | ||
private static unsafe extern void ApplyUpdate_internal (IntPtr base_assm, byte* dmeta_bytes, int dmeta_length, byte *dil_bytes, int dil_length, byte *dpdb_bytes, int dpdb_length); | ||
} | ||
} |