|
| 1 | +using System.Linq; |
| 2 | +using Confuser.Core; |
| 3 | +using Confuser.Core.Services; |
| 4 | +using Confuser.Renamer.References; |
| 5 | +using dnlib.DotNet; |
| 6 | +using dnlib.DotNet.Emit; |
| 7 | + |
| 8 | +namespace Confuser.Renamer.Analyzers { |
| 9 | + internal sealed class CallSiteAnalyzer : IRenamer { |
| 10 | + public void Analyze(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { |
| 11 | + if (!(def is MethodDef method) || !method.HasBody) |
| 12 | + return; |
| 13 | + |
| 14 | + var logger = context.Logger; |
| 15 | + |
| 16 | + var traceService = context.Registry.GetService<ITraceService>(); |
| 17 | + MethodTrace methodTrace = null; |
| 18 | + |
| 19 | + var instructions = method.Body.Instructions; |
| 20 | + foreach (var instruction in instructions) { |
| 21 | + if (!IsCreateCallSiteInstruction(instruction)) continue; |
| 22 | + |
| 23 | + if (methodTrace is null) |
| 24 | + methodTrace = traceService.Trace(method); |
| 25 | + |
| 26 | + // CallSite`1.Create(CallSiteBinder) |
| 27 | + int[] createArguments = methodTrace.TraceArguments(instruction); |
| 28 | + if (createArguments.Length != 1) continue; |
| 29 | + |
| 30 | + // Binder.InvokeMember(CSharpBinderFlags, string, IEnumerable<Type>, Type, IEnumerable<CSharpArgumentInfo>) |
| 31 | + var binderInstruction = instructions[createArguments[0]]; |
| 32 | + if (IsBinderInvokeMember(binderInstruction)) { |
| 33 | + HandleBinderInvokeMember(context, method, methodTrace, binderInstruction); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static void HandleBinderInvokeMember(ConfuserContext context, MethodDef method, MethodTrace methodTrace, Instruction instruction) { |
| 39 | + var instructions = method.Body.Instructions; |
| 40 | + |
| 41 | + int[] binderArguments = methodTrace.TraceArguments(instruction); |
| 42 | + if (binderArguments.Length != 5) return; |
| 43 | + |
| 44 | + var nameInstruction = instructions[binderArguments[1]]; |
| 45 | + var contextInstruction = instructions[binderArguments[3]]; |
| 46 | + |
| 47 | + // Name instruction is expected to contain a string constant - This is the name of the invoked member |
| 48 | + if (nameInstruction.OpCode.Code != Code.Ldstr) return; |
| 49 | + string boundMemberName = nameInstruction.Operand as string; |
| 50 | + |
| 51 | + var ldContextTokenInstruction = contextInstruction; |
| 52 | + if (IsGetTypeFromHandle(contextInstruction)) { |
| 53 | + int[] getTypeFromHandleArguments = methodTrace.TraceArguments(contextInstruction); |
| 54 | + if (getTypeFromHandleArguments.Length == 1) |
| 55 | + ldContextTokenInstruction = instructions[getTypeFromHandleArguments[0]]; |
| 56 | + } |
| 57 | + |
| 58 | + if (ldContextTokenInstruction.OpCode.Code == Code.Ldtoken && |
| 59 | + ldContextTokenInstruction.Operand is ITypeDefOrRef typeDefOrRef) { |
| 60 | + // We found the load token of the context parameter. This means we know the type the member is called for. |
| 61 | + BuildMemberReferences(context, typeDefOrRef, boundMemberName, nameInstruction); |
| 62 | + } |
| 63 | + else { |
| 64 | + context.Logger.WarnFormat( |
| 65 | + "Failed to resolve type for dynamic invoke member in {0} - blocking all members with name {1} from renaming.", |
| 66 | + method, boundMemberName); |
| 67 | + |
| 68 | + // The type referenced is unknown. To be safe, all methods matching the name need to be blocked from renaming. |
| 69 | + DisableRenamingForMethods(context, boundMemberName); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static void DisableRenamingForMethods(ConfuserContext context, string methodName) { |
| 74 | + var service = context.Registry.GetService<INameService>(); |
| 75 | + |
| 76 | + var candidateMethods = context.Modules |
| 77 | + .SelectMany(m => m.FindDefinitions()) |
| 78 | + .OfType<MethodDef>() |
| 79 | + .Where(m => m.Name.Equals(methodName)); |
| 80 | + foreach (var candidateMethod in candidateMethods) |
| 81 | + service.SetCanRename(candidateMethod, false); |
| 82 | + } |
| 83 | + |
| 84 | + static void BuildMemberReferences(ConfuserContext context, ITypeDefOrRef typeDefOrRef, string boundMemberName, |
| 85 | + Instruction nameInstruction) { |
| 86 | + var service = context.Registry.GetService<INameService>(); |
| 87 | + |
| 88 | + var boundMemberTypeDef = typeDefOrRef.ResolveTypeDef(); |
| 89 | + if (boundMemberTypeDef is null) return; |
| 90 | + |
| 91 | + var currentType = boundMemberTypeDef; |
| 92 | + while (currentType != null) { |
| 93 | + foreach (var refMethod in currentType.FindMethods(boundMemberName)) { |
| 94 | + service.AddReference(refMethod, |
| 95 | + new StringMemberNameReference(nameInstruction, refMethod)); |
| 96 | + service.ReduceRenameMode(refMethod, RenameMode.Reflection); |
| 97 | + } |
| 98 | + |
| 99 | + currentType = currentType.BaseType.ResolveTypeDef(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static bool IsCreateCallSiteInstruction(Instruction instruction) { |
| 104 | + if (instruction.OpCode.Code != Code.Call) return false; |
| 105 | + if (!(instruction.Operand is IMethodDefOrRef method)) return false; |
| 106 | + |
| 107 | + return method.DeclaringType.Namespace.Equals("System.Runtime.CompilerServices") && |
| 108 | + method.DeclaringType.Name.Equals("CallSite`1") && |
| 109 | + method.Name.Equals("Create"); |
| 110 | + } |
| 111 | + |
| 112 | + private static bool IsBinderInvokeMember(Instruction instruction) { |
| 113 | + if (instruction.OpCode.Code != Code.Call) return false; |
| 114 | + if (!(instruction.Operand is IMethodDefOrRef method)) return false; |
| 115 | + |
| 116 | + return method.DeclaringType.Namespace.Equals("Microsoft.CSharp.RuntimeBinder") && |
| 117 | + method.DeclaringType.Name.Equals("Binder") && |
| 118 | + method.Name.Equals("InvokeMember"); |
| 119 | + } |
| 120 | + |
| 121 | + private static bool IsGetTypeFromHandle(Instruction instruction) { |
| 122 | + if (instruction.OpCode.Code != Code.Call) return false; |
| 123 | + if (!(instruction.Operand is IMethodDefOrRef method)) return false; |
| 124 | + |
| 125 | + return method.DeclaringType.Namespace.Equals("System") && |
| 126 | + method.DeclaringType.Name.Equals("Type") && |
| 127 | + method.Name.Equals("GetTypeFromHandle"); |
| 128 | + } |
| 129 | + |
| 130 | + public void PreRename(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { } |
| 131 | + |
| 132 | + public void PostRename(ConfuserContext context, INameService service, ProtectionParameters parameters, IDnlibDef def) { } |
| 133 | + } |
| 134 | +} |
0 commit comments