<Compile Include="$(BclSourcesRoot)\System\Resources\ManifestBasedResourceGroveler.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\CastHelpers.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\ICastableHelpers.cs" />
- <Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\RuntimeFeature.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\RuntimeHelpers.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\ControlledExecution.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\DependentHandle.cs" />
<linker>
<assembly fullname="System.Private.CoreLib">
+ <type fullname="System.Runtime.CompilerServices.RuntimeFeature" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="true">
+ <method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="true" />
+ <method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="true" />
+ </type>
+ <type fullname="System.Runtime.CompilerServices.RuntimeFeature" feature="System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported" featurevalue="false">
+ <method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="false" />
+ <method signature="System.Boolean get_IsDynamicCodeSupported()" body="stub" value="false" />
+ </type>
<type fullname="System.StartupHookProvider" feature="System.StartupHookProvider.IsSupported" featurevalue="false">
<method signature="System.Boolean get_IsSupported()" body="stub" value="false" />
</type>
throw new InvalidOperationException();
}
+ EnsureDynamicCodeSupported();
+
_access = access;
_internalAssembly = CreateDynamicAssembly(assemblyLoadContext ?? AssemblyLoadContext.GetLoadContext(callingAssembly)!, name, access);
ArgumentNullException.ThrowIfNull(namedFields);
ArgumentNullException.ThrowIfNull(fieldValues);
+ AssemblyBuilder.EnsureDynamicCodeSupported();
+
#pragma warning disable CA2208 // Instantiate argument exceptions correctly, combination of arguments used
if (namedProperties.Length != propertyValues.Length)
throw new ArgumentException(SR.Arg_ArrayLengthsDiffer, "namedProperties, propertyValues");
if (m_module == null && mod != null)
throw new ArgumentException(SR.NotSupported_MustBeModuleBuilder);
+
+ AssemblyBuilder.EnsureDynamicCodeSupported();
}
[MemberNotNull(nameof(m_signature))]
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace System.Runtime.CompilerServices
-{
- public static partial class RuntimeFeature
- {
- public static bool IsDynamicCodeSupported => true;
- public static bool IsDynamicCodeCompiled => true;
- }
-}
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RequiredMemberAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeCompatibilityAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeFeature.cs" />
+ <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeFeature.NonNativeAot.cs" Condition="'$(FeatureNativeAot)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeWrappedException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\SkipLocalsInitAttribute.cs" />
using System.Diagnostics.CodeAnalysis;
using System.IO;
+using System.Runtime.CompilerServices;
namespace System.Reflection.Emit
{
public override Stream? GetManifestResourceStream(Type type, string name) =>
throw new NotSupportedException(SR.NotSupported_DynamicAssembly);
+
+ internal static void EnsureDynamicCodeSupported()
+ {
+ if (!RuntimeFeature.IsDynamicCodeSupported)
+ {
+ ThrowDynamicCodeNotSupported();
+ }
+ }
+
+ private static void ThrowDynamicCodeNotSupported() =>
+ throw new PlatformNotSupportedException(SR.PlatformNotSupported_ReflectionEmit);
}
}
{
ArgumentNullException.ThrowIfNull(name);
+ AssemblyBuilder.EnsureDynamicCodeSupported();
+
if (attributes != (MethodAttributes.Static | MethodAttributes.Public) || callingConvention != CallingConventions.Standard)
throw new NotSupportedException(SR.NotSupported_DynamicMethodFlags);
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System.Runtime.CompilerServices
+{
+ public static partial class RuntimeFeature
+ {
+ public static bool IsDynamicCodeSupported
+ {
+#if MONO
+ [Intrinsic] // the Mono AOT compiler will change this flag to false for FullAOT scenarios, otherwise this code is used
+#endif
+ get;
+ } = AppContext.TryGetSwitch("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported", out bool isDynamicCodeSupported) ? isDynamicCodeSupported : true;
+
+ public static bool IsDynamicCodeCompiled
+ {
+#if MONO
+ [Intrinsic] // the Mono AOT compiler and Interpreter will change this flag to false for FullAOT and interpreted scenarios, otherwise this code is used
+#endif
+ get => IsDynamicCodeSupported;
+ }
+ }
+}
using System.Collections.Generic;
using System.IO;
using System.Linq;
+using Microsoft.DotNet.RemoteExecutor;
using Xunit;
namespace System.Reflection.Emit.Tests
AssertExtensions.Throws<ArgumentException>(paramName, () => new CustomAttributeBuilder(con, new object[0], namedProperties, propertyValues, new FieldInfo[0], new object[0]));
}
+ [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+ public static void ThrowsWhenDynamicCodeNotSupported()
+ {
+ RemoteInvokeOptions options = new RemoteInvokeOptions();
+ options.RuntimeConfigurationOptions.Add("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported", false.ToString());
+
+ using RemoteInvokeHandle remoteHandle = RemoteExecutor.Invoke(static () =>
+ {
+ ConstructorInfo con = typeof(TestAttribute).GetConstructor(new Type[0]);
+ object[] constructorArgs = new object[0];
+ PropertyInfo[] namedProperties = Helpers.GetProperties(typeof(TestAttribute), nameof(TestAttribute.ObjectProperty));
+ object[] propertyValues = new object[] { new int[0, 0] };
+ FieldInfo[] namedFields = new FieldInfo[0];
+ object[] fieldValues = new object[0];
+
+ Assert.Throws<PlatformNotSupportedException>(() => new CustomAttributeBuilder(con, constructorArgs));
+ Assert.Throws<PlatformNotSupportedException>(() => new CustomAttributeBuilder(con, constructorArgs, namedFields, fieldValues));
+ Assert.Throws<PlatformNotSupportedException>(() => new CustomAttributeBuilder(con, constructorArgs, namedProperties, propertyValues));
+ Assert.Throws<PlatformNotSupportedException>(() => new CustomAttributeBuilder(con, constructorArgs, namedProperties, propertyValues, namedFields, fieldValues));
+ }, options);
+ }
+
private static Type CreateEnum(Type underlyingType, params object[] literalValues)
{
ModuleBuilder module = Helpers.DynamicModule();
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using Microsoft.DotNet.RemoteExecutor;
+using Xunit;
+
+namespace System.Reflection.Emit.Tests
+{
+ public class SignatureHelperDynamicCodeNotSupported
+ {
+ [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+ public static void ThrowsWhenDynamicCodeNotSupported()
+ {
+ RemoteInvokeOptions options = new RemoteInvokeOptions();
+ options.RuntimeConfigurationOptions.Add("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported", false.ToString());
+
+ using RemoteInvokeHandle remoteHandle = RemoteExecutor.Invoke(static () =>
+ {
+ Assert.Throws<PlatformNotSupportedException>(() => SignatureHelper.GetFieldSigHelper(null));
+ Assert.Throws<PlatformNotSupportedException>(() => SignatureHelper.GetLocalVarSigHelper());
+ Assert.Throws<PlatformNotSupportedException>(() => SignatureHelper.GetMethodSigHelper(CallingConventions.Any, typeof(int)));
+
+ // Mono always throws NotImplementedException - https://github.com/dotnet/runtime/issues/37794
+ if (!PlatformDetection.IsMonoRuntime)
+ {
+ Assert.Throws<PlatformNotSupportedException>(() => SignatureHelper.GetPropertySigHelper(null, typeof(string), new Type[] { typeof(string), typeof(int) }));
+ }
+ }, options);
+ }
+ }
+}
<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<TestRuntime>true</TestRuntime>
+ <IncludeRemoteExecutor>true</IncludeRemoteExecutor>
</PropertyGroup>
<ItemGroup>
<Compile Include="ILGenerator\DeclareLocalTests.cs" />
<Compile Include="SignatureHelper\SignatureHelperGetMethodSigHelper.cs" />
<Compile Include="SignatureHelper\SignatureHelperGetPropertySigHelper.cs" />
<Compile Include="SignatureHelper\SignatureHelperGetSignature.cs" />
+ <Compile Include="SignatureHelper\SignatureHelperDynamicCodeNotSupported.cs" />
<Compile Include="SignatureHelper\SignatureHelperToString.cs" />
<Compile Include="CustomAttributeBuilderTests.cs" />
<Compile Include="Utilities.cs" />
// The .NET Foundation licenses this file to you under the MIT license.
using System;
+using Microsoft.DotNet.RemoteExecutor;
using Xunit;
namespace System.Reflection.Emit.Tests
AssertExtensions.Throws<ArgumentException>(null, () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], owner, true));
AssertExtensions.Throws<ArgumentException>(null, () => new DynamicMethod("Method", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void), new Type[0], owner, false));
}
+
+ [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+ public static void ThrowsWhenDynamicCodeNotSupported()
+ {
+ RemoteInvokeOptions options = new RemoteInvokeOptions();
+ options.RuntimeConfigurationOptions.Add("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported", false.ToString());
+
+ using RemoteInvokeHandle remoteHandle = RemoteExecutor.Invoke(static () =>
+ {
+ Module module = typeof(TestClass).GetTypeInfo().Module;
+ string name = "Method";
+ Type returnType = typeof(void);
+ Type[] parameterTypes = null;
+ Type owner = typeof(TestClass);
+
+ Assert.Throws<PlatformNotSupportedException>(() => new DynamicMethod(name, returnType, parameterTypes));
+ Assert.Throws<PlatformNotSupportedException>(() => new DynamicMethod(name, returnType, parameterTypes, true));
+ Assert.Throws<PlatformNotSupportedException>(() => new DynamicMethod(name, returnType, parameterTypes, module));
+ Assert.Throws<PlatformNotSupportedException>(() => new DynamicMethod(name, returnType, parameterTypes, owner));
+ Assert.Throws<PlatformNotSupportedException>(() => new DynamicMethod(name, returnType, parameterTypes, module, true));
+ Assert.Throws<PlatformNotSupportedException>(() => new DynamicMethod(name, returnType, parameterTypes, owner, true));
+ Assert.Throws<PlatformNotSupportedException>(() => new DynamicMethod(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, module, true));
+ Assert.Throws<PlatformNotSupportedException>(() => new DynamicMethod(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, true));
+ }, options);
+ }
}
}
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<TestRuntime>true</TestRuntime>
+ <IncludeRemoteExecutor>true</IncludeRemoteExecutor>
</PropertyGroup>
<ItemGroup>
<Compile Include="DynamicILInfoTests.cs" />
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
+using Microsoft.DotNet.RemoteExecutor;
using Xunit;
namespace System.Reflection.Emit.Tests
Assert.Empty(internalAssemblyBuilder.Location);
}
+ [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+ public static void ThrowsWhenDynamicCodeNotSupported()
+ {
+ RemoteInvokeOptions options = new RemoteInvokeOptions();
+ options.RuntimeConfigurationOptions.Add("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported", false.ToString());
+
+ using RemoteInvokeHandle remoteHandle = RemoteExecutor.Invoke(static () =>
+ {
+ var assemblyName = new AssemblyName("TestName");
+ AssemblyBuilderAccess access = AssemblyBuilderAccess.Run;
+
+ Assert.Throws<PlatformNotSupportedException>(() => AssemblyBuilder.DefineDynamicAssembly(assemblyName, access));
+ Assert.Throws<PlatformNotSupportedException>(() => AssemblyBuilder.DefineDynamicAssembly(assemblyName, access, assemblyAttributes: null));
+ }, options);
+ }
}
}
<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<TestRuntime>true</TestRuntime>
+ <IncludeRemoteExecutor>true</IncludeRemoteExecutor>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyBuilderTests.cs" />
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
+using Microsoft.DotNet.RemoteExecutor;
using Xunit;
namespace System.Runtime.CompilerServices.Tests
{
Assert.True(RuntimeFeature.IsSupported(probedValue));
}
+
+ [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
+ [InlineData(true)]
+ [InlineData(false)]
+ public static void DynamicCode_ContextSwitch(bool isDynamicCodeSupported)
+ {
+ RemoteInvokeOptions options = new RemoteInvokeOptions();
+ options.RuntimeConfigurationOptions.Add("System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported", isDynamicCodeSupported.ToString());
+
+ // IsDynamicCodeCompiled on Mono interpreter always returns false
+ bool isDynamicCodeCompiled = PlatformDetection.IsMonoInterpreter ? false : isDynamicCodeSupported;
+
+ using RemoteInvokeHandle remoteHandle = RemoteExecutor.Invoke(static (isDynamicCodeSupportedString, isDynamicCodeCompiledString) =>
+ {
+ bool isDynamicCodeSupported = bool.Parse(isDynamicCodeSupportedString);
+ Assert.Equal(isDynamicCodeSupported, RuntimeFeature.IsDynamicCodeSupported);
+
+ bool isDynamicCodeCompiled = bool.Parse(isDynamicCodeCompiledString);
+ Assert.Equal(isDynamicCodeCompiled, RuntimeFeature.IsDynamicCodeCompiled);
+ }, isDynamicCodeSupported.ToString(), isDynamicCodeCompiled.ToString(), options);
+ }
}
}
<Compile Include="$(BclSourcesRoot)\System\Runtime\JitInfo.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\JitHelpers.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\RuntimeHelpers.Mono.cs" />
- <Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\RuntimeFeature.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\GCHandle.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\Marshal.Mono.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\InteropServices\MemoryMarshal.Mono.cs" />
[DynamicDependency(nameof(access))] // Automatically keeps all previous fields too due to StructLayout
private AssemblyBuilder(AssemblyName n, AssemblyBuilderAccess access)
{
+ EnsureDynamicCodeSupported();
+
aname = (AssemblyName)n.Clone();
if (!Enum.IsDefined(typeof(AssemblyBuilderAccess), access))
ArgumentNullException.ThrowIfNull(namedFields);
ArgumentNullException.ThrowIfNull(fieldValues);
+ AssemblyBuilder.EnsureDynamicCodeSupported();
+
if (con.GetParametersCount() != constructorArgs.Length)
throw new ArgumentException(SR.Argument_BadParameterCountsForConstructor);
if (namedProperties.Length != propertyValues.Length)
[DynamicDependency(nameof(modopts))] // Automatically keeps all previous fields too due to StructLayout
internal SignatureHelper(ModuleBuilder? module, SignatureHelperType type)
{
+ AssemblyBuilder.EnsureDynamicCodeSupported();
+
this.type = type;
this.module = module;
}
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-using System.Reflection;
-
-namespace System.Runtime.CompilerServices
-{
- public partial class RuntimeFeature
- {
- public static bool IsDynamicCodeSupported
- {
- [Intrinsic] // the JIT/AOT compiler will change this flag to false for FullAOT scenarios, otherwise true
- get => IsDynamicCodeSupported;
- }
-
- public static bool IsDynamicCodeCompiled
- {
- [Intrinsic] // the JIT/AOT compiler will change this flag to false for FullAOT scenarios, otherwise true
- get => IsDynamicCodeCompiled;
- }
- }
-}
else if (in_corlib &&
!strcmp ("System.Runtime.CompilerServices", klass_name_space) &&
!strcmp ("RuntimeFeature", klass_name)) {
- if (!strcmp (tm, "get_IsDynamicCodeSupported"))
- *op = MINT_LDC_I4_1;
- else if (!strcmp (tm, "get_IsDynamicCodeCompiled"))
+ // NOTE: on the interpreter, use the C# code in System.Private.CoreLib for IsDynamicCodeSupported
+ // and always return false for IsDynamicCodeCompiled
+ if (!strcmp (tm, "get_IsDynamicCodeCompiled"))
*op = MINT_LDC_I4_0;
} else if (in_corlib &&
(!strncmp ("System.Runtime.Intrinsics.Arm", klass_name_space, 29) ||
}
}
- // Return false for RuntimeFeature.IsDynamicCodeSupported and RuntimeFeature.IsDynamicCodeCompiled on FullAOT, otherwise true
+ // On FullAOT, return false for RuntimeFeature:
+ // - IsDynamicCodeCompiled
+ // - IsDynamicCodeSupported and no interpreter
+ // otherwise use the C# code in System.Private.CoreLib
if (in_corlib &&
+ cfg->full_aot &&
!strcmp ("System.Runtime.CompilerServices", cmethod_klass_name_space) &&
!strcmp ("RuntimeFeature", cmethod_klass_name)) {
if (!strcmp (cmethod->name, "get_IsDynamicCodeCompiled")) {
- EMIT_NEW_ICONST (cfg, ins, cfg->full_aot ? 0 : 1);
+ EMIT_NEW_ICONST (cfg, ins, 0);
ins->type = STACK_I4;
return ins;
- } else if (!strcmp (cmethod->name, "get_IsDynamicCodeSupported")) {
- EMIT_NEW_ICONST (cfg, ins, cfg->full_aot ? (cfg->interp ? 1 : 0) : 1);
+ } else if (!strcmp (cmethod->name, "get_IsDynamicCodeSupported") && !cfg->interp) {
+ EMIT_NEW_ICONST (cfg, ins, 0);
ins->type = STACK_I4;
return ins;
}
<ExcludeList Include="$(XunitTestBinBase)/reflection/Modifiers/modifiers/*">
<Issue>https://github.com/dotnet/runtimelab/issues/201</Issue>
</ExcludeList>
- <ExcludeList Include="$(XunitTestBinBase)/reflection/RefEmit/EmittingIgnoresAccessChecksToAttributeIsRespected\EmittingIgnoresAccessChecksToAttributeIsRespected/*">
- <Issue>https://github.com/dotnet/runtimelab/issues/155: Ref emit</Issue>
- </ExcludeList>
<ExcludeList Include="$(XunitTestBinBase)/reflection/SetValue/TrySetReadonlyStaticField/*">
<Issue>https://github.com/dotnet/runtimelab/issues/200</Issue>
</ExcludeList>
<ExcludeList Include = "$(XunitTestBinBase)/Interop/PInvoke/Primitives/Int/PInvokeIntTest/**">
<Issue>Needs coreclr build</Issue>
</ExcludeList>
+
+ <ExcludeList Include = "$(XunitTestBinBase)/reflection/RefEmit/EmittingIgnoresAccessChecksToAttributeIsRespected/**">
+ <Issue>Reflection.Emit is not supported on fullaot</Issue>
+ </ExcludeList>
</ItemGroup>