-
Notifications
You must be signed in to change notification settings - Fork 567
Move AddKeepAlives trimmer step to post-trimming task
#10952
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
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c33b39f
[xabt] Move `AddKeepAlives` trimmer step to standalone MSBuild task
sbomer e24ddc3
Fix missing using directive in AddKeepAlivesHelper.cs
sbomer 21ca475
Fix Windows file locking by using DirectoryAssemblyResolver
sbomer d74ce3a
[xabt] Combine post-trimming steps into single PostTrimmingPipeline task
sbomer 59e7c89
[xabt] Refactor PostTrimmingPipeline to use IAssemblyModifierPipeline…
sbomer acf4b70
Merge branch 'main' into dev/sbomer/addkeepalives-inner
sbomer e8a2b8e
Initial plan
Copilot 8215697
Address copilot reviewer comments: nullable Func, whitespace fix, upd…
Copilot e6b2e5b
Memoize corlib resolution in PostTrimmingPipeline to avoid repeated r…
Copilot cfc21e1
Merge pull request #10981 from dotnet/copilot/sub-pr-10952
sbomer c033084
Update src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/StripEm…
sbomer 3fa2b85
Restore missing 'using Xamarin.Android.Tasks' in StripEmbeddedLibrari…
sbomer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
124 changes: 124 additions & 0 deletions
124
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesHelper.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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Mono.Cecil; | ||
| using Mono.Cecil.Cil; | ||
| using Xamarin.Android.Tasks; | ||
|
|
||
| namespace MonoDroid.Tuner | ||
| { | ||
| static class AddKeepAlivesHelper | ||
| { | ||
| internal static bool AddKeepAlives (AssemblyDefinition assembly, IMetadataResolver resolver, Func<AssemblyDefinition> getCorlibAssembly, Action<string> logMessage) | ||
| { | ||
| if (!assembly.MainModule.HasTypeReference ("Java.Lang.Object")) | ||
| return false; | ||
|
|
||
| // Anything that was built against .NET for Android will have | ||
| // keep-alives already compiled in. | ||
| if (MonoAndroidHelper.IsDotNetAndroidAssembly (assembly)) | ||
| return false; | ||
|
|
||
| MethodDefinition? methodKeepAlive = null; | ||
| bool changed = false; | ||
| foreach (TypeDefinition type in assembly.MainModule.Types) | ||
| changed |= ProcessType (type, resolver, ref methodKeepAlive, getCorlibAssembly, logMessage); | ||
|
|
||
| return changed; | ||
| } | ||
|
|
||
| static bool ProcessType (TypeDefinition type, IMetadataResolver resolver, ref MethodDefinition? methodKeepAlive, Func<AssemblyDefinition> getCorlibAssembly, Action<string> logMessage) | ||
| { | ||
| bool changed = false; | ||
| if (MightNeedFix (type, resolver)) | ||
| changed |= AddKeepAlives (type, ref methodKeepAlive, getCorlibAssembly, logMessage); | ||
|
|
||
| if (type.HasNestedTypes) { | ||
| foreach (var t in type.NestedTypes) { | ||
| changed |= ProcessType (t, resolver, ref methodKeepAlive, getCorlibAssembly, logMessage); | ||
| } | ||
| } | ||
|
|
||
| return changed; | ||
| } | ||
|
|
||
| static bool MightNeedFix (TypeDefinition type, IMetadataResolver resolver) | ||
| { | ||
| return !type.IsAbstract && type.IsSubclassOf ("Java.Lang.Object", resolver); | ||
| } | ||
|
|
||
| static bool AddKeepAlives (TypeDefinition type, ref MethodDefinition? methodKeepAlive, Func<AssemblyDefinition> getCorlibAssembly, Action<string> logMessage) | ||
| { | ||
| bool changed = false; | ||
| foreach (MethodDefinition method in type.Methods) { | ||
| if (method.Parameters.Count == 0) | ||
| continue; | ||
|
|
||
| if (!method.CustomAttributes.Any (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute")) | ||
| continue; | ||
|
|
||
| var instructions = method.Body.Instructions; | ||
|
|
||
| var found = false; | ||
| for (int off = Math.Max (0, instructions.Count - 6); off < instructions.Count; off++) { | ||
| var current = instructions [off]; | ||
| if (current.OpCode == OpCodes.Call && current.Operand.ToString ().Contains ("System.GC::KeepAlive")) { | ||
| found = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (found) | ||
| continue; | ||
|
|
||
| var processor = method.Body.GetILProcessor (); | ||
| var module = method.DeclaringType.Module; | ||
| var end = instructions.Last (); | ||
| if (end.Previous.OpCode == OpCodes.Endfinally) | ||
| end = end.Previous; | ||
|
|
||
| for (int i = 0; i < method.Parameters.Count; i++) { | ||
| if (method.Parameters [i].ParameterType.IsValueType || method.Parameters [i].ParameterType.FullName == "System.String") | ||
| continue; | ||
|
|
||
| if (methodKeepAlive == null) | ||
| methodKeepAlive = GetKeepAliveMethod (getCorlibAssembly, logMessage); | ||
|
|
||
| if (methodKeepAlive == null) { | ||
| logMessage ("Unable to add KeepAlive call, did not find System.GC.KeepAlive method."); | ||
| break; | ||
| } | ||
|
|
||
| processor.InsertBefore (end, GetLoadArgumentInstruction (method.IsStatic ? i : i + 1, method.Parameters [i])); | ||
| processor.InsertBefore (end, Instruction.Create (OpCodes.Call, module.ImportReference (methodKeepAlive))); | ||
| changed = true; | ||
| } | ||
| } | ||
| return changed; | ||
| } | ||
|
|
||
| static MethodDefinition? GetKeepAliveMethod (Func<AssemblyDefinition> getCorlibAssembly, Action<string> logMessage) | ||
| { | ||
| var corlibAssembly = getCorlibAssembly (); | ||
| if (corlibAssembly == null) | ||
| return null; | ||
|
|
||
| var gcType = Extensions.GetType (corlibAssembly, "System.GC"); | ||
| if (gcType == null) | ||
| return null; | ||
|
|
||
| return Extensions.GetMethod (gcType, "KeepAlive", new string [] { "System.Object" }); | ||
| } | ||
|
|
||
| // Adapted from src/Mono.Android.Export/Mono.CodeGeneration/CodeArgumentReference.cs | ||
| static Instruction GetLoadArgumentInstruction (int argNum, ParameterDefinition parameter) | ||
| { | ||
| switch (argNum) { | ||
| case 0: return Instruction.Create (OpCodes.Ldarg_0); | ||
| case 1: return Instruction.Create (OpCodes.Ldarg_1); | ||
| case 2: return Instruction.Create (OpCodes.Ldarg_2); | ||
| case 3: return Instruction.Create (OpCodes.Ldarg_3); | ||
| default: return Instruction.Create (OpCodes.Ldarg, parameter); | ||
| } | ||
| } | ||
| } | ||
| } | ||
158 changes: 6 additions & 152 deletions
158
src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/AddKeepAlivesStep.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 |
|---|---|---|
| @@ -1,169 +1,23 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Java.Interop.Tools.Cecil; | ||
| using Mono.Cecil; | ||
| using Mono.Cecil.Cil; | ||
| using Mono.Linker; | ||
| using Mono.Linker.Steps; | ||
| using Xamarin.Android.Tasks; | ||
|
|
||
| namespace MonoDroid.Tuner | ||
| { | ||
| public class AddKeepAlivesStep : BaseStep | ||
| #if !ILLINK | ||
| , IAssemblyModifierPipelineStep | ||
| #endif // !ILLINK | ||
| public class AddKeepAlivesStep : BaseStep, IAssemblyModifierPipelineStep | ||
| { | ||
|
|
||
| protected override void ProcessAssembly (AssemblyDefinition assembly) | ||
| { | ||
| var action = Annotations.HasAction (assembly) ? Annotations.GetAction (assembly) : AssemblyAction.Skip; | ||
| if (action == AssemblyAction.Delete) | ||
| return; | ||
|
|
||
| if (AddKeepAlives (assembly)) { | ||
| if (action == AssemblyAction.Skip || action == AssemblyAction.Copy) | ||
| Annotations.SetAction (assembly, AssemblyAction.Save); | ||
| } | ||
| } | ||
|
|
||
| #if !ILLINK | ||
| public void ProcessAssembly (AssemblyDefinition assembly, StepContext context) | ||
| { | ||
| // Only run this step on user Android assemblies | ||
| if (!context.IsAndroidUserAssembly) | ||
| return; | ||
|
|
||
| context.IsAssemblyModified |= AddKeepAlives (assembly); | ||
| } | ||
| #endif // !ILLINK | ||
|
|
||
| internal bool AddKeepAlives (AssemblyDefinition assembly) | ||
| { | ||
| if (!assembly.MainModule.HasTypeReference ("Java.Lang.Object")) | ||
| return false; | ||
|
|
||
| // Anything that was built against .NET for Android will have | ||
| // keep-alives already compiled in. | ||
| if (MonoAndroidHelper.IsDotNetAndroidAssembly (assembly)) | ||
| return false; | ||
|
|
||
| bool changed = false; | ||
| foreach (TypeDefinition type in assembly.MainModule.Types) | ||
| changed |= ProcessType (type); | ||
|
|
||
| return changed; | ||
| } | ||
|
|
||
| bool ProcessType (TypeDefinition type) | ||
| { | ||
| bool changed = false; | ||
| if (MightNeedFix (type)) | ||
| changed |= AddKeepAlives (type); | ||
|
|
||
| if (type.HasNestedTypes) { | ||
| foreach (var t in type.NestedTypes) { | ||
| changed |= ProcessType (t); | ||
| } | ||
| } | ||
|
|
||
| return changed; | ||
| } | ||
|
|
||
| bool MightNeedFix (TypeDefinition type) | ||
| { | ||
| return !type.IsAbstract && type.IsSubclassOf ("Java.Lang.Object", Context); | ||
| } | ||
|
|
||
| MethodDefinition? methodKeepAlive = null; | ||
|
|
||
| bool AddKeepAlives (TypeDefinition type) | ||
| { | ||
| bool changed = false; | ||
| foreach (MethodDefinition method in type.Methods) { | ||
| if (method.Parameters.Count == 0) | ||
| continue; | ||
|
|
||
| if (!method.CustomAttributes.Any (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute")) | ||
| continue; | ||
|
|
||
| var instructions = method.Body.Instructions; | ||
|
|
||
| var found = false; | ||
| for (int off = Math.Max (0, instructions.Count - 6); off < instructions.Count; off++) { | ||
| var current = instructions [off]; | ||
| if (current.OpCode == OpCodes.Call && current.Operand.ToString ().Contains ("System.GC::KeepAlive")) { | ||
| found = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (found) | ||
| continue; | ||
|
|
||
| var processor = method.Body.GetILProcessor (); | ||
| var module = method.DeclaringType.Module; | ||
| var end = instructions.Last (); | ||
| if (end.Previous.OpCode == OpCodes.Endfinally) | ||
| end = end.Previous; | ||
|
|
||
| for (int i = 0; i < method.Parameters.Count; i++) { | ||
| if (method.Parameters [i].ParameterType.IsValueType || method.Parameters [i].ParameterType.FullName == "System.String") | ||
| continue; | ||
|
|
||
| if (methodKeepAlive == null) | ||
| methodKeepAlive = GetKeepAliveMethod (); | ||
|
|
||
| if (methodKeepAlive == null) { | ||
| LogMessage ("Unable to add KeepAlive call, did not find System.GC.KeepAlive method."); | ||
| break; | ||
| } | ||
|
|
||
| processor.InsertBefore (end, GetLoadArgumentInstruction (method.IsStatic ? i : i + 1, method.Parameters [i])); | ||
| processor.InsertBefore (end, Instruction.Create (OpCodes.Call, module.ImportReference (methodKeepAlive))); | ||
| changed = true; | ||
| } | ||
| } | ||
| return changed; | ||
| } | ||
|
|
||
| protected virtual AssemblyDefinition GetCorlibAssembly () | ||
| { | ||
| return Context.GetAssembly ("System.Private.CoreLib"); | ||
| } | ||
|
|
||
| MethodDefinition? GetKeepAliveMethod () | ||
| { | ||
| var corlibAssembly = GetCorlibAssembly (); | ||
| if (corlibAssembly == null) | ||
| return null; | ||
|
|
||
| var gcType = Extensions.GetType (corlibAssembly, "System.GC"); | ||
| if (gcType == null) | ||
| return null; | ||
|
|
||
| return Extensions.GetMethod (gcType, "KeepAlive", new string [] { "System.Object" }); | ||
| } | ||
|
|
||
| public | ||
| #if !ILLINK | ||
| override | ||
| #endif | ||
| void LogMessage (string message) | ||
| { | ||
| Context.LogMessage (message); | ||
| } | ||
|
|
||
| // Adapted from src/Mono.Android.Export/Mono.CodeGeneration/CodeArgumentReference.cs | ||
| static Instruction GetLoadArgumentInstruction (int argNum, ParameterDefinition parameter) | ||
| { | ||
| switch (argNum) { | ||
| case 0: return Instruction.Create (OpCodes.Ldarg_0); | ||
| case 1: return Instruction.Create (OpCodes.Ldarg_1); | ||
| case 2: return Instruction.Create (OpCodes.Ldarg_2); | ||
| case 3: return Instruction.Create (OpCodes.Ldarg_3); | ||
| default: return Instruction.Create (OpCodes.Ldarg, parameter); | ||
| } | ||
| context.IsAssemblyModified |= AddKeepAlivesHelper.AddKeepAlives ( | ||
| assembly, | ||
| Context, | ||
| () => Context.GetAssembly ("System.Private.CoreLib"), | ||
| (msg) => LogMessage (msg)); | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.