Skip to content

Commit df9de14

Browse files
authored
Add IsDynamicCodeSupported Feature Switch (#80246)
* Add IsDynamicCodeSupported Feature Switch This adds the ability to disable reflection emit for testing. It also allows for applications/libraries to simulate NativeAOT behavior (like switching on RuntimeFeature.IsDynamicCodeSupported) without actually publishing for NativeAOT. Fix #39806 * Add IsDynamicCodeSupported feature switch support for Mono * Remove featuredefault for IsDynamicCodeSupported so it isn't substituted during CoreLib's build. * Add Intrinsic attribute back for Mono
1 parent 1c735ed commit df9de14

File tree

26 files changed

+196
-44
lines changed

26 files changed

+196
-44
lines changed

src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@
208208
<Compile Include="$(BclSourcesRoot)\System\Resources\ManifestBasedResourceGroveler.CoreCLR.cs" />
209209
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\CastHelpers.cs" />
210210
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\ICastableHelpers.cs" />
211-
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\RuntimeFeature.CoreCLR.cs" />
212211
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\RuntimeHelpers.CoreCLR.cs" />
213212
<Compile Include="$(BclSourcesRoot)\System\Runtime\ControlledExecution.CoreCLR.cs" />
214213
<Compile Include="$(BclSourcesRoot)\System\Runtime\DependentHandle.cs" />

src/coreclr/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<linker>
22
<assembly fullname="System.Private.CoreLib">
3+
<type fullname="System.Runtime.CompilerServices.RuntimeFeature" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="true">
4+
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="true" />
5+
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="true" />
6+
</type>
7+
<type fullname="System.Runtime.CompilerServices.RuntimeFeature" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="false">
8+
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="false" />
9+
<method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="false" />
10+
</type>
311
<type fullname="System.StartupHookProvider" feature="System.StartupHookProvider.IsSupported" featurevalue="false">
412
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
513
</type>

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ internal AssemblyBuilder(AssemblyName name,
5656
throw new InvalidOperationException();
5757
}
5858

59+
EnsureDynamicCodeSupported();
60+
5961
_access = access;
6062

6163
_internalAssembly = CreateDynamicAssembly(assemblyLoadContext ?? AssemblyLoadContext.GetLoadContext(callingAssembly)!, name, access);

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/CustomAttributeBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public CustomAttributeBuilder(ConstructorInfo con, object?[] constructorArgs, Pr
5757
ArgumentNullException.ThrowIfNull(namedFields);
5858
ArgumentNullException.ThrowIfNull(fieldValues);
5959

60+
AssemblyBuilder.EnsureDynamicCodeSupported();
61+
6062
#pragma warning disable CA2208 // Instantiate argument exceptions correctly, combination of arguments used
6163
if (namedProperties.Length != propertyValues.Length)
6264
throw new ArgumentException(SR.Arg_ArrayLengthsDiffer, "namedProperties, propertyValues");

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ private void Init(Module? mod)
223223

224224
if (m_module == null && mod != null)
225225
throw new ArgumentException(SR.NotSupported_MustBeModuleBuilder);
226+
227+
AssemblyBuilder.EnsureDynamicCodeSupported();
226228
}
227229

228230
[MemberNotNull(nameof(m_signature))]

src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeFeature.CoreCLR.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@
809809
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RequiredMemberAttribute.cs" />
810810
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeCompatibilityAttribute.cs" />
811811
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeFeature.cs" />
812+
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeFeature.NonNativeAot.cs" Condition="'$(FeatureNativeAot)' != 'true'" />
812813
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeHelpers.cs" />
813814
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeWrappedException.cs" />
814815
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\SkipLocalsInitAttribute.cs" />

src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics.CodeAnalysis;
55
using System.IO;
6+
using System.Runtime.CompilerServices;
67

78
namespace System.Reflection.Emit
89
{
@@ -38,5 +39,16 @@ public override string[] GetManifestResourceNames() =>
3839

3940
public override Stream? GetManifestResourceStream(Type type, string name) =>
4041
throw new NotSupportedException(SR.NotSupported_DynamicAssembly);
42+
43+
internal static void EnsureDynamicCodeSupported()
44+
{
45+
if (!RuntimeFeature.IsDynamicCodeSupported)
46+
{
47+
ThrowDynamicCodeNotSupported();
48+
}
49+
}
50+
51+
private static void ThrowDynamicCodeNotSupported() =>
52+
throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionEmit);
4153
}
4254
}

src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ private void Init(string name,
223223
{
224224
ArgumentNullException.ThrowIfNull(name);
225225

226+
AssemblyBuilder.EnsureDynamicCodeSupported();
227+
226228
if (attributes != (MethodAttributes.Static | MethodAttributes.Public) || callingConvention != CallingConventions.Standard)
227229
throw new NotSupportedException(SR.NotSupported_DynamicMethodFlags);
228230

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.Runtime.CompilerServices
5+
{
6+
public static partial class RuntimeFeature
7+
{
8+
public static bool IsDynamicCodeSupported
9+
{
10+
#if MONO
11+
[Intrinsic] // the Mono AOT compiler will change this flag to false for FullAOT scenarios, otherwise this code is used
12+
#endif
13+
get;
14+
} = AppContext.TryGetSwitch("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported", out bool isDynamicCodeSupported) ? isDynamicCodeSupported : true;
15+
16+
public static bool IsDynamicCodeCompiled
17+
{
18+
#if MONO
19+
[Intrinsic] // the Mono AOT compiler and Interpreter will change this flag to false for FullAOT and interpreted scenarios, otherwise this code is used
20+
#endif
21+
get => IsDynamicCodeSupported;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)