Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
Respect IsDynamicCodeSupported in more places in Linq.Expressions#88539
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7f9228e
Respect IsDynamicCodeSupported in more places in Linq.Expressions
eerhardt 4643eae
Work around DynamicDelegateAugments.CreateObjectArrayDelegate not exi…
eerhardt b57a897
Adjust CompileWorksWhenDynamicCodeNotSupported to test in more scenar…
eerhardt 514bd95
Address PR feedback
eerhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
37 changes: 32 additions & 5 deletions
37 src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/DelegateHelpers.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -4,14 +4,15 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Reflection; | ||
| using System.Reflection.Emit; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Text; | ||
| using System.Threading; | ||
| namespace System.Dynamic.Utils | ||
| { | ||
| internal static class DelegateHelpers | ||
| { | ||
| // This can be flipped to true using feature switches at publishing time | ||
| // This can be flipped to false using feature switches at publishing time | ||
| internal static bool CanEmitObjectArrayDelegate => true; | ||
| // Separate class so that the it can be trimmed away and doesn't get conflated | ||
| @@ -21,14 +22,23 @@ private static class DynamicDelegateLightup | ||
| public static Func<Type, Func<object?[], object?>, Delegate> CreateObjectArrayDelegate { get; } | ||
| = CreateObjectArrayDelegateInternal(); | ||
| [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", | ||
| Justification = "Works around https://github.com/dotnet/linker/issues/2392")] | ||
| private static Func<Type, Func<object?[], object?>, Delegate> CreateObjectArrayDelegateInternal() | ||
| => Type.GetType("Internal.Runtime.Augments.DynamicDelegateAugments")! | ||
| .GetMethod("CreateObjectArrayDelegate")! | ||
| .CreateDelegate<Func<Type, Func<object?[], object?>, Delegate>>(); | ||
| } | ||
| private static class ForceAllowDynamicCodeLightup | ||
| { | ||
| public static Func<IDisposable>? ForceAllowDynamicCodeDelegate { get; } | ||
| = ForceAllowDynamicCodeDelegateInternal(); | ||
| private static Func<IDisposable>? ForceAllowDynamicCodeDelegateInternal() | ||
| => typeof(AssemblyBuilder) | ||
| .GetMethod("ForceAllowDynamicCode", BindingFlags.NonPublic | BindingFlags.Static) | ||
| ?.CreateDelegate<Func<IDisposable>>(); | ||
| } | ||
| internal static Delegate CreateObjectArrayDelegate(Type delegateType, Func<object?[], object?> handler) | ||
| { | ||
| if (CanEmitObjectArrayDelegate) | ||
| @@ -186,6 +196,23 @@ private static Delegate CreateObjectArrayDelegateRefEmit(Type delegateType, Func | ||
| if (thunkMethod == null) | ||
| { | ||
| static IDisposable? CreateForceAllowDynamicCodeScope() | ||
| { | ||
| if (!RuntimeFeature.IsDynamicCodeSupported) | ||
| { | ||
| // Force 'new DynamicMethod' to not throw even though RuntimeFeature.IsDynamicCodeSupported is false. | ||
| // If we are running on a runtime that supports dynamic code, even though the feature switch is off, | ||
| // for example when running on CoreClr with PublishAot=true, this will allow IL to be emitted. | ||
| // If we are running on a runtime that really doesn't support dynamic code, like NativeAOT, | ||
| // CanEmitObjectArrayDelegate will be flipped to 'false', and this method won't be invoked. | ||
| return ForceAllowDynamicCodeLightup.ForceAllowDynamicCodeDelegate?.Invoke(); | ||
| } | ||
| return null; | ||
| } | ||
| using IDisposable? forceAllowDynamicCodeScope = CreateForceAllowDynamicCodeScope(); | ||
eerhardt marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| int thunkIndex = Interlocked.Increment(ref s_ThunksCreated); | ||
| Type[] paramTypes = new Type[parameters.Length + 1]; | ||
| paramTypes[0] = typeof(Func<object[], object>); | ||
| @@ -270,8 +297,8 @@ private static Delegate CreateObjectArrayDelegateRefEmit(Type delegateType, Func | ||
| ilgen.BeginFinallyBlock(); | ||
| for (int i = 0; i < parameters.Length; i++) | ||
| { | ||
| if (parameters[i].ParameterType.IsByRef) | ||
| { | ||
| if (parameters[i].ParameterType.IsByRef) | ||
| { | ||
| Type byrefToType = parameters[i].ParameterType.GetElementType()!; | ||
| // update parameter | ||
2 changes: 1 addition & 1 deletion
2 ...raries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/CallInstruction.cs
This file contains hidden or 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
52 changes: 44 additions & 8 deletions
52 src/libraries/System.Linq.Expressions/tests/CompilerTests.cs
This file contains hidden or 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
4 changes: 4 additions & 0 deletions
4 src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.LibraryBuild.xml
This file contains hidden or 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
33 changes: 32 additions & 1 deletion
33 src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to change this into
If it remains a constant value it will still cause issues for iOS-like platforms as described in: #87924
My last comment: #87924 (comment) breaks down the benefits of having these conditional variables as feature switches.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that would be a good enhancement to make in a future PR. It isn't necessary to fix the issue this PR is fixing - #81803.