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