|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | + |
| 4 | +using Mono.Cecil; |
| 5 | + |
| 6 | +using Mono.Linker; |
| 7 | +using Mono.Linker.Steps; |
| 8 | +using Mono.Tuner; |
| 9 | + |
| 10 | +using Xamarin.Bundler; |
| 11 | + |
| 12 | +#nullable enable |
| 13 | + |
| 14 | +namespace Xamarin.Linker.Steps { |
| 15 | + |
| 16 | + public class PreserveBlockCodeStep : AssemblyModifierStep { |
| 17 | + |
| 18 | + protected override string Name { get; } = "Preserve Block Code"; |
| 19 | + protected override int ErrorCode { get; } = 2240; |
| 20 | + |
| 21 | + protected override bool IsActiveFor (AssemblyDefinition assembly) |
| 22 | + { |
| 23 | + // We only care about assemblies that are being linked. |
| 24 | + if (Annotations.GetAction (assembly) != AssemblyAction.Link) |
| 25 | + return false; |
| 26 | + |
| 27 | + // Unless an assembly is or references our platform assembly, then it won't have anything we need to preserve |
| 28 | + if (!Configuration.Profile.IsOrReferencesProductAssembly (assembly)) |
| 29 | + return false; |
| 30 | + |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + protected override bool ProcessType (TypeDefinition type) |
| 35 | + { |
| 36 | + if (!GetMembersToPreserve (type, out var field, out var method)) |
| 37 | + return false; |
| 38 | + |
| 39 | + var modified = false; |
| 40 | + modified |= abr.AddDynamicDependencyAttributeToStaticConstructor (type, field); |
| 41 | + modified |= abr.AddDynamicDependencyAttributeToStaticConstructor (type, method); |
| 42 | + return modified; |
| 43 | + } |
| 44 | + |
| 45 | + public static bool GetMembersToPreserve (TypeDefinition type, [NotNullWhen (true)] out FieldDefinition? field, [NotNullWhen (true)] out MethodDefinition? method) |
| 46 | + { |
| 47 | + field = null; |
| 48 | + method = null; |
| 49 | + |
| 50 | + /* For the following class: |
| 51 | +
|
| 52 | + static internal class SDInnerBlock { |
| 53 | + // this field is not preserved by other means, but it must not be linked away |
| 54 | + static internal readonly DInnerBlock Handler = Invoke; |
| 55 | +
|
| 56 | + [MonoPInvokeCallback (typeof (DInnerBlock))] |
| 57 | + static internal void Invoke (IntPtr block, int magic_number) |
| 58 | + { |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + * We need to make sure the linker doesn't remove the Handler field |
| 63 | + * and the Invoke method. |
| 64 | + */ |
| 65 | + |
| 66 | + // First make sure we got the right class |
| 67 | + // The type for the field we're looking for is abstract, sealed and nested and contains exactly 1 field. |
| 68 | + if (!type.HasFields || !type.IsAbstract || !type.IsSealed || !type.IsNested) |
| 69 | + return false; |
| 70 | + if (type.Fields.Count != 1) |
| 71 | + return false; |
| 72 | + |
| 73 | + // The type is also nested inside ObjCRuntime.Trampolines class) |
| 74 | + var nestingType = type.DeclaringType; |
| 75 | + if (!nestingType.Is ("ObjCRuntime", "Trampolines")) |
| 76 | + return false; |
| 77 | + |
| 78 | + // The class has a readonly field named 'Handler' |
| 79 | + field = type.Fields [0]; |
| 80 | + if (!field.IsInitOnly) |
| 81 | + return false; |
| 82 | + if (field.Name != "Handler") |
| 83 | + return false; |
| 84 | + |
| 85 | + // The class has a parameterless 'Invoke' method with a 'MonoPInvokeCallback' attribute |
| 86 | + if (!type.HasMethods) |
| 87 | + return false; |
| 88 | + method = type.Methods.SingleOrDefault (v => { |
| 89 | + if (v.Name != "Invoke") |
| 90 | + return false; |
| 91 | + if (!v.HasParameters) |
| 92 | + return false; |
| 93 | + if (!v.HasCustomAttributes) |
| 94 | + return false; |
| 95 | + if (!v.CustomAttributes.Any (v => v.AttributeType.Name == "MonoPInvokeCallbackAttribute")) |
| 96 | + return false; |
| 97 | + return true; |
| 98 | + }); |
| 99 | + |
| 100 | + if (method is null) |
| 101 | + return false; |
| 102 | + |
| 103 | + // The type was used, so preserve the method and field |
| 104 | + return true; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments