diff --git a/AssemblyData/methodsrewriter/CodeGenerator.cs b/AssemblyData/methodsrewriter/CodeGenerator.cs index c2c8c3f4..fb15a971 100644 --- a/AssemblyData/methodsrewriter/CodeGenerator.cs +++ b/AssemblyData/methodsrewriter/CodeGenerator.cs @@ -257,9 +257,7 @@ namespace AssemblyData.methodsrewriter { } int getArgIndex(ParameterDefinition arg) { - if (ResolverUtils.hasThis(methodInfo.methodBase)) - return arg.Index + 1; - return arg.Index; + return arg.Sequence; } int getLocalIndex(VariableDefinition local) { diff --git a/blocks/DotNetUtils.cs b/blocks/DotNetUtils.cs index af579499..4cdfcc01 100644 --- a/blocks/DotNetUtils.cs +++ b/blocks/DotNetUtils.cs @@ -807,7 +807,7 @@ namespace de4dot.blocks { } } - public static int getArgIndex(MethodReference method, Instruction instr) { + public static int getArgIndex(Instruction instr) { switch (instr.OpCode.Code) { case Code.Ldarg_0: return 0; case Code.Ldarg_1: return 1; @@ -818,22 +818,16 @@ namespace de4dot.blocks { case Code.Ldarga_S: case Code.Ldarg: case Code.Ldarg_S: - return getArgIndex(method, instr.Operand as ParameterDefinition); + return getArgIndex(instr.Operand as ParameterDefinition); } return -1; } - public static int getArgIndex(MethodReference method, ParameterDefinition arg) { - return getArgIndex(method.HasImplicitThis, arg); - } - - public static int getArgIndex(bool implicitThis, ParameterDefinition arg) { + public static int getArgIndex(ParameterDefinition arg) { if (arg == null) return -1; - if (implicitThis) - return arg.Index + 1; - return arg.Index; + return arg.Sequence; } public static List getParameters(MethodReference method) { @@ -851,15 +845,11 @@ namespace de4dot.blocks { } public static ParameterDefinition getParameter(MethodReference method, Instruction instr) { - return getParameter(getParameters(method), method, instr); + return getParameter(getParameters(method), instr); } - public static ParameterDefinition getParameter(MethodReference method, IList parameters, Instruction instr) { - return getParameter(parameters, getArgIndex(method, instr)); - } - - public static ParameterDefinition getParameter(IList parameters, MethodReference method, Instruction instr) { - return getParameter(parameters, getArgIndex(method, instr)); + public static ParameterDefinition getParameter(IList parameters, Instruction instr) { + return getParameter(parameters, getArgIndex(instr)); } public static ParameterDefinition getParameter(IList parameters, int index) { @@ -878,11 +868,11 @@ namespace de4dot.blocks { } public static TypeReference getArgType(MethodReference method, Instruction instr) { - return getArgType(getArgs(method), method, instr); + return getArgType(getArgs(method), instr); } - public static TypeReference getArgType(IList methodArgs, MethodReference method, Instruction instr) { - return getArgType(methodArgs, getArgIndex(method, instr)); + public static TypeReference getArgType(IList methodArgs, Instruction instr) { + return getArgType(methodArgs, getArgIndex(instr)); } public static TypeReference getArgType(IList methodArgs, int index) { diff --git a/blocks/cflow/ConstantsFolder.cs b/blocks/cflow/ConstantsFolder.cs index 458c0fd2..0a567297 100644 --- a/blocks/cflow/ConstantsFolder.cs +++ b/blocks/cflow/ConstantsFolder.cs @@ -51,7 +51,7 @@ namespace de4dot.blocks.cflow { case Code.Ldarg_2: case Code.Ldarg_3: case Code.Ldarg_S: - changed |= fixLoadInstruction(block, i, instructionEmulator.getArg(DotNetUtils.getParameter(args, blocks.Method, instr.Instruction))); + changed |= fixLoadInstruction(block, i, instructionEmulator.getArg(DotNetUtils.getParameter(args, instr.Instruction))); break; case Code.Ldloc: diff --git a/blocks/cflow/InstructionEmulator.cs b/blocks/cflow/InstructionEmulator.cs index b2f7cca0..d6991bec 100644 --- a/blocks/cflow/InstructionEmulator.cs +++ b/blocks/cflow/InstructionEmulator.cs @@ -166,7 +166,7 @@ namespace de4dot.blocks.cflow { } int index(ParameterDefinition arg) { - return arg.Index + argBase; + return arg.Sequence; } public Value getArg(ParameterDefinition arg) { diff --git a/blocks/cflow/MethodCallInlinerBase.cs b/blocks/cflow/MethodCallInlinerBase.cs index 9af60d0b..e07c2cb6 100644 --- a/blocks/cflow/MethodCallInlinerBase.cs +++ b/blocks/cflow/MethodCallInlinerBase.cs @@ -82,7 +82,7 @@ namespace de4dot.blocks.cflow { if (!isLdarg) break; - if (DotNetUtils.getArgIndex(methodToInline, instr) != loadIndex) + if (DotNetUtils.getArgIndex(instr) != loadIndex) return false; loadIndex++; instr = DotNetUtils.getInstruction(methodToInline.Body.Instructions, ref instrIndex); diff --git a/de4dot.code/ObfuscatedFile.cs b/de4dot.code/ObfuscatedFile.cs index f16970b3..4212c792 100644 --- a/de4dot.code/ObfuscatedFile.cs +++ b/de4dot.code/ObfuscatedFile.cs @@ -748,7 +748,7 @@ namespace de4dot.code { var s = instr.GetOperandString(); if (s != "") return s; - return string.Format("", DotNetUtils.getArgIndex(method, arg)); + return string.Format("", DotNetUtils.getArgIndex(arg)); } else return instr.GetOperandString(); diff --git a/de4dot.code/deobfuscators/InlinedMethodsFinder.cs b/de4dot.code/deobfuscators/InlinedMethodsFinder.cs index 74791d6b..c8f6fb49 100644 --- a/de4dot.code/deobfuscators/InlinedMethodsFinder.cs +++ b/de4dot.code/deobfuscators/InlinedMethodsFinder.cs @@ -113,7 +113,7 @@ namespace de4dot.code.deobfuscators { case Code.Ldarg_3: case Code.Ldarga: case Code.Ldarga_S: - if (DotNetUtils.getArgIndex(method, instr) != loadIndex) + if (DotNetUtils.getArgIndex(instr) != loadIndex) return false; loadIndex++; continue; diff --git a/de4dot.code/deobfuscators/TypesRestorer.cs b/de4dot.code/deobfuscators/TypesRestorer.cs index b79a2416..4049fac3 100644 --- a/de4dot.code/deobfuscators/TypesRestorer.cs +++ b/de4dot.code/deobfuscators/TypesRestorer.cs @@ -197,7 +197,7 @@ namespace de4dot.code.deobfuscators { foreach (var info in argInfos.Values) { if (info.updateNewType(module)) { - getUpdatedMethod(method).newArgTypes[DotNetUtils.getArgIndex(method, info.arg)] = info.newType; + getUpdatedMethod(method).newArgTypes[DotNetUtils.getArgIndex(info.arg)] = info.newType; info.arg.ParameterType = info.newType; changed = true; } @@ -210,7 +210,7 @@ namespace de4dot.code.deobfuscators { if (a.arg.Method.MetadataToken.ToInt32() < b.arg.Method.MetadataToken.ToInt32()) return -1; if (a.arg.Method.MetadataToken.ToInt32() > b.arg.Method.MetadataToken.ToInt32()) return 1; - return Utils.compareInt32(a.arg.Index, b.arg.Index); + return Utils.compareInt32(a.arg.Sequence, b.arg.Sequence); } class PushedArgs { @@ -316,7 +316,7 @@ namespace de4dot.code.deobfuscators { case Code.Ldarg_1: case Code.Ldarg_2: case Code.Ldarg_3: - addMethodArgType(getParameter(methodParams, method, ldInstr), DotNetUtils.getParameter(calledMethodParams, calledMethodParamIndex)); + addMethodArgType(getParameter(methodParams, ldInstr), DotNetUtils.getParameter(calledMethodParams, calledMethodParamIndex)); break; default: @@ -329,7 +329,7 @@ namespace de4dot.code.deobfuscators { pushedArgs = getPushedArgInstructions(instructions, i); if (pushedArgs.NumValidArgs < 1) break; - addMethodArgType(getParameter(methodParams, method, pushedArgs.getEnd(0)), instr.Operand as TypeReference); + addMethodArgType(getParameter(methodParams, pushedArgs.getEnd(0)), instr.Operand as TypeReference); break; case Code.Stloc: @@ -341,23 +341,23 @@ namespace de4dot.code.deobfuscators { pushedArgs = getPushedArgInstructions(instructions, i); if (pushedArgs.NumValidArgs < 1) break; - addMethodArgType(getParameter(methodParams, method, pushedArgs.getEnd(0)), DotNetUtils.getLocalVar(method.Body.Variables, instr)); + addMethodArgType(getParameter(methodParams, pushedArgs.getEnd(0)), DotNetUtils.getLocalVar(method.Body.Variables, instr)); break; case Code.Stsfld: pushedArgs = getPushedArgInstructions(instructions, i); if (pushedArgs.NumValidArgs < 1) break; - addMethodArgType(getParameter(methodParams, method, pushedArgs.getEnd(0)), instr.Operand as FieldReference); + addMethodArgType(getParameter(methodParams, pushedArgs.getEnd(0)), instr.Operand as FieldReference); break; case Code.Stfld: pushedArgs = getPushedArgInstructions(instructions, i); if (pushedArgs.NumValidArgs >= 1) { var field = instr.Operand as FieldReference; - addMethodArgType(getParameter(methodParams, method, pushedArgs.getEnd(0)), field); + addMethodArgType(getParameter(methodParams, pushedArgs.getEnd(0)), field); if (pushedArgs.NumValidArgs >= 2 && field != null) - addMethodArgType(getParameter(methodParams, method, pushedArgs.getEnd(1)), field.DeclaringType); + addMethodArgType(getParameter(methodParams, pushedArgs.getEnd(1)), field.DeclaringType); } break; @@ -366,7 +366,7 @@ namespace de4dot.code.deobfuscators { pushedArgs = getPushedArgInstructions(instructions, i); if (pushedArgs.NumValidArgs < 1) break; - addMethodArgType(getParameter(methodParams, method, pushedArgs.getEnd(0)), instr.Operand as FieldReference); + addMethodArgType(getParameter(methodParams, pushedArgs.getEnd(0)), instr.Operand as FieldReference); break; //TODO: For better results, these should be checked: @@ -427,7 +427,7 @@ namespace de4dot.code.deobfuscators { } } - static ParameterDefinition getParameter(IList parameters, MethodReference method, Instruction instr) { + static ParameterDefinition getParameter(IList parameters, Instruction instr) { switch (instr.OpCode.Code) { case Code.Ldarg: case Code.Ldarg_S: @@ -435,7 +435,7 @@ namespace de4dot.code.deobfuscators { case Code.Ldarg_1: case Code.Ldarg_2: case Code.Ldarg_3: - return DotNetUtils.getParameter(parameters, method, instr); + return DotNetUtils.getParameter(parameters, instr); default: return null; diff --git a/de4dot.code/renamer/TypeInfo.cs b/de4dot.code/renamer/TypeInfo.cs index e2fab96a..b961e6c8 100644 --- a/de4dot.code/renamer/TypeInfo.cs +++ b/de4dot.code/renamer/TypeInfo.cs @@ -504,7 +504,7 @@ namespace de4dot.code.renamer { var instructions = method.Body.Instructions; int index = 0; var ldarg0 = DotNetUtils.getInstruction(instructions, ref index); - if (ldarg0 == null || DotNetUtils.getArgIndex(method, ldarg0) != 0) + if (ldarg0 == null || DotNetUtils.getArgIndex(ldarg0) != 0) return null; var ldfld = DotNetUtils.getInstruction(instructions, ref index); if (ldfld == null || ldfld.OpCode.Code != Code.Ldfld) @@ -662,7 +662,7 @@ namespace de4dot.code.renamer { // newobj event_handler_ctor // callvirt add_SomeEvent - if (DotNetUtils.getArgIndex(methodDef.MethodDefinition, instructions[i]) != 0) + if (DotNetUtils.getArgIndex(instructions[i]) != 0) continue; int index = i + 1; @@ -676,13 +676,13 @@ namespace de4dot.code.renamer { if (fieldDef == null) continue; - if (DotNetUtils.getArgIndex(methodDef.MethodDefinition, instructions[index++]) != 0) + if (DotNetUtils.getArgIndex(instructions[index++]) != 0) continue; MethodReference methodRef; var instr = instructions[index + 1]; if (instr.OpCode.Code == Code.Ldvirtftn) { - if (!isThisOrDup(methodDef.MethodDefinition, instructions[index++])) + if (!isThisOrDup(instructions[index++])) continue; var ldvirtftn = instructions[index++]; methodRef = ldvirtftn.Operand as MethodReference; @@ -741,18 +741,18 @@ namespace de4dot.code.renamer { // newobj event handler ctor // call add_Xyz - if (DotNetUtils.getArgIndex(method, instructions[i]) != 0) + if (DotNetUtils.getArgIndex(instructions[i]) != 0) continue; int index = i + 1; - if (!isThisOrDup(method, instructions[index++])) + if (!isThisOrDup(instructions[index++])) continue; MethodReference handler; if (instructions[index].OpCode.Code == Code.Ldftn) { handler = instructions[index++].Operand as MethodReference; } else { - if (!isThisOrDup(method, instructions[index++])) + if (!isThisOrDup(instructions[index++])) continue; var instr = instructions[index++]; if (instr.OpCode.Code != Code.Ldvirtftn) @@ -789,8 +789,8 @@ namespace de4dot.code.renamer { } } - static bool isThisOrDup(MethodReference method, Instruction instr) { - return DotNetUtils.getArgIndex(method, instr) == 0 || instr.OpCode.Code == Code.Dup; + static bool isThisOrDup(Instruction instr) { + return DotNetUtils.getArgIndex(instr) == 0 || instr.OpCode.Code == Code.Dup; } static bool isEventHandlerCtor(MethodReference method) { @@ -830,7 +830,7 @@ namespace de4dot.code.renamer { if (className == null) continue; - if (DotNetUtils.getArgIndex(methodDef.MethodDefinition, instructions[i - 2]) != 0) + if (DotNetUtils.getArgIndex(instructions[i - 2]) != 0) continue; findInitializeComponentMethod(type, methodDef);