Use ParameterDefinition.Sequence

This commit is contained in:
de4dot 2012-01-21 20:31:47 +01:00
parent 3c85b3f964
commit 2dadd773ec
9 changed files with 37 additions and 49 deletions

View File

@ -257,9 +257,7 @@ namespace AssemblyData.methodsrewriter {
} }
int getArgIndex(ParameterDefinition arg) { int getArgIndex(ParameterDefinition arg) {
if (ResolverUtils.hasThis(methodInfo.methodBase)) return arg.Sequence;
return arg.Index + 1;
return arg.Index;
} }
int getLocalIndex(VariableDefinition local) { int getLocalIndex(VariableDefinition local) {

View File

@ -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) { switch (instr.OpCode.Code) {
case Code.Ldarg_0: return 0; case Code.Ldarg_0: return 0;
case Code.Ldarg_1: return 1; case Code.Ldarg_1: return 1;
@ -818,22 +818,16 @@ namespace de4dot.blocks {
case Code.Ldarga_S: case Code.Ldarga_S:
case Code.Ldarg: case Code.Ldarg:
case Code.Ldarg_S: case Code.Ldarg_S:
return getArgIndex(method, instr.Operand as ParameterDefinition); return getArgIndex(instr.Operand as ParameterDefinition);
} }
return -1; return -1;
} }
public static int getArgIndex(MethodReference method, ParameterDefinition arg) { public static int getArgIndex(ParameterDefinition arg) {
return getArgIndex(method.HasImplicitThis, arg);
}
public static int getArgIndex(bool implicitThis, ParameterDefinition arg) {
if (arg == null) if (arg == null)
return -1; return -1;
if (implicitThis) return arg.Sequence;
return arg.Index + 1;
return arg.Index;
} }
public static List<ParameterDefinition> getParameters(MethodReference method) { public static List<ParameterDefinition> getParameters(MethodReference method) {
@ -851,15 +845,11 @@ namespace de4dot.blocks {
} }
public static ParameterDefinition getParameter(MethodReference method, Instruction instr) { 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<ParameterDefinition> parameters, Instruction instr) { public static ParameterDefinition getParameter(IList<ParameterDefinition> parameters, Instruction instr) {
return getParameter(parameters, getArgIndex(method, instr)); return getParameter(parameters, getArgIndex(instr));
}
public static ParameterDefinition getParameter(IList<ParameterDefinition> parameters, MethodReference method, Instruction instr) {
return getParameter(parameters, getArgIndex(method, instr));
} }
public static ParameterDefinition getParameter(IList<ParameterDefinition> parameters, int index) { public static ParameterDefinition getParameter(IList<ParameterDefinition> parameters, int index) {
@ -878,11 +868,11 @@ namespace de4dot.blocks {
} }
public static TypeReference getArgType(MethodReference method, Instruction instr) { public static TypeReference getArgType(MethodReference method, Instruction instr) {
return getArgType(getArgs(method), method, instr); return getArgType(getArgs(method), instr);
} }
public static TypeReference getArgType(IList<TypeReference> methodArgs, MethodReference method, Instruction instr) { public static TypeReference getArgType(IList<TypeReference> methodArgs, Instruction instr) {
return getArgType(methodArgs, getArgIndex(method, instr)); return getArgType(methodArgs, getArgIndex(instr));
} }
public static TypeReference getArgType(IList<TypeReference> methodArgs, int index) { public static TypeReference getArgType(IList<TypeReference> methodArgs, int index) {

View File

@ -51,7 +51,7 @@ namespace de4dot.blocks.cflow {
case Code.Ldarg_2: case Code.Ldarg_2:
case Code.Ldarg_3: case Code.Ldarg_3:
case Code.Ldarg_S: 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; break;
case Code.Ldloc: case Code.Ldloc:

View File

@ -166,7 +166,7 @@ namespace de4dot.blocks.cflow {
} }
int index(ParameterDefinition arg) { int index(ParameterDefinition arg) {
return arg.Index + argBase; return arg.Sequence;
} }
public Value getArg(ParameterDefinition arg) { public Value getArg(ParameterDefinition arg) {

View File

@ -82,7 +82,7 @@ namespace de4dot.blocks.cflow {
if (!isLdarg) if (!isLdarg)
break; break;
if (DotNetUtils.getArgIndex(methodToInline, instr) != loadIndex) if (DotNetUtils.getArgIndex(instr) != loadIndex)
return false; return false;
loadIndex++; loadIndex++;
instr = DotNetUtils.getInstruction(methodToInline.Body.Instructions, ref instrIndex); instr = DotNetUtils.getInstruction(methodToInline.Body.Instructions, ref instrIndex);

View File

@ -748,7 +748,7 @@ namespace de4dot.code {
var s = instr.GetOperandString(); var s = instr.GetOperandString();
if (s != "") if (s != "")
return s; return s;
return string.Format("<arg_{0}>", DotNetUtils.getArgIndex(method, arg)); return string.Format("<arg_{0}>", DotNetUtils.getArgIndex(arg));
} }
else else
return instr.GetOperandString(); return instr.GetOperandString();

View File

@ -113,7 +113,7 @@ namespace de4dot.code.deobfuscators {
case Code.Ldarg_3: case Code.Ldarg_3:
case Code.Ldarga: case Code.Ldarga:
case Code.Ldarga_S: case Code.Ldarga_S:
if (DotNetUtils.getArgIndex(method, instr) != loadIndex) if (DotNetUtils.getArgIndex(instr) != loadIndex)
return false; return false;
loadIndex++; loadIndex++;
continue; continue;

View File

@ -197,7 +197,7 @@ namespace de4dot.code.deobfuscators {
foreach (var info in argInfos.Values) { foreach (var info in argInfos.Values) {
if (info.updateNewType(module)) { 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; info.arg.ParameterType = info.newType;
changed = true; 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;
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 { class PushedArgs {
@ -316,7 +316,7 @@ namespace de4dot.code.deobfuscators {
case Code.Ldarg_1: case Code.Ldarg_1:
case Code.Ldarg_2: case Code.Ldarg_2:
case Code.Ldarg_3: case Code.Ldarg_3:
addMethodArgType(getParameter(methodParams, method, ldInstr), DotNetUtils.getParameter(calledMethodParams, calledMethodParamIndex)); addMethodArgType(getParameter(methodParams, ldInstr), DotNetUtils.getParameter(calledMethodParams, calledMethodParamIndex));
break; break;
default: default:
@ -329,7 +329,7 @@ namespace de4dot.code.deobfuscators {
pushedArgs = getPushedArgInstructions(instructions, i); pushedArgs = getPushedArgInstructions(instructions, i);
if (pushedArgs.NumValidArgs < 1) if (pushedArgs.NumValidArgs < 1)
break; break;
addMethodArgType(getParameter(methodParams, method, pushedArgs.getEnd(0)), instr.Operand as TypeReference); addMethodArgType(getParameter(methodParams, pushedArgs.getEnd(0)), instr.Operand as TypeReference);
break; break;
case Code.Stloc: case Code.Stloc:
@ -341,23 +341,23 @@ namespace de4dot.code.deobfuscators {
pushedArgs = getPushedArgInstructions(instructions, i); pushedArgs = getPushedArgInstructions(instructions, i);
if (pushedArgs.NumValidArgs < 1) if (pushedArgs.NumValidArgs < 1)
break; 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; break;
case Code.Stsfld: case Code.Stsfld:
pushedArgs = getPushedArgInstructions(instructions, i); pushedArgs = getPushedArgInstructions(instructions, i);
if (pushedArgs.NumValidArgs < 1) if (pushedArgs.NumValidArgs < 1)
break; break;
addMethodArgType(getParameter(methodParams, method, pushedArgs.getEnd(0)), instr.Operand as FieldReference); addMethodArgType(getParameter(methodParams, pushedArgs.getEnd(0)), instr.Operand as FieldReference);
break; break;
case Code.Stfld: case Code.Stfld:
pushedArgs = getPushedArgInstructions(instructions, i); pushedArgs = getPushedArgInstructions(instructions, i);
if (pushedArgs.NumValidArgs >= 1) { if (pushedArgs.NumValidArgs >= 1) {
var field = instr.Operand as FieldReference; 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) if (pushedArgs.NumValidArgs >= 2 && field != null)
addMethodArgType(getParameter(methodParams, method, pushedArgs.getEnd(1)), field.DeclaringType); addMethodArgType(getParameter(methodParams, pushedArgs.getEnd(1)), field.DeclaringType);
} }
break; break;
@ -366,7 +366,7 @@ namespace de4dot.code.deobfuscators {
pushedArgs = getPushedArgInstructions(instructions, i); pushedArgs = getPushedArgInstructions(instructions, i);
if (pushedArgs.NumValidArgs < 1) if (pushedArgs.NumValidArgs < 1)
break; break;
addMethodArgType(getParameter(methodParams, method, pushedArgs.getEnd(0)), instr.Operand as FieldReference); addMethodArgType(getParameter(methodParams, pushedArgs.getEnd(0)), instr.Operand as FieldReference);
break; break;
//TODO: For better results, these should be checked: //TODO: For better results, these should be checked:
@ -427,7 +427,7 @@ namespace de4dot.code.deobfuscators {
} }
} }
static ParameterDefinition getParameter(IList<ParameterDefinition> parameters, MethodReference method, Instruction instr) { static ParameterDefinition getParameter(IList<ParameterDefinition> parameters, Instruction instr) {
switch (instr.OpCode.Code) { switch (instr.OpCode.Code) {
case Code.Ldarg: case Code.Ldarg:
case Code.Ldarg_S: case Code.Ldarg_S:
@ -435,7 +435,7 @@ namespace de4dot.code.deobfuscators {
case Code.Ldarg_1: case Code.Ldarg_1:
case Code.Ldarg_2: case Code.Ldarg_2:
case Code.Ldarg_3: case Code.Ldarg_3:
return DotNetUtils.getParameter(parameters, method, instr); return DotNetUtils.getParameter(parameters, instr);
default: default:
return null; return null;

View File

@ -504,7 +504,7 @@ namespace de4dot.code.renamer {
var instructions = method.Body.Instructions; var instructions = method.Body.Instructions;
int index = 0; int index = 0;
var ldarg0 = DotNetUtils.getInstruction(instructions, ref index); var ldarg0 = DotNetUtils.getInstruction(instructions, ref index);
if (ldarg0 == null || DotNetUtils.getArgIndex(method, ldarg0) != 0) if (ldarg0 == null || DotNetUtils.getArgIndex(ldarg0) != 0)
return null; return null;
var ldfld = DotNetUtils.getInstruction(instructions, ref index); var ldfld = DotNetUtils.getInstruction(instructions, ref index);
if (ldfld == null || ldfld.OpCode.Code != Code.Ldfld) if (ldfld == null || ldfld.OpCode.Code != Code.Ldfld)
@ -662,7 +662,7 @@ namespace de4dot.code.renamer {
// newobj event_handler_ctor // newobj event_handler_ctor
// callvirt add_SomeEvent // callvirt add_SomeEvent
if (DotNetUtils.getArgIndex(methodDef.MethodDefinition, instructions[i]) != 0) if (DotNetUtils.getArgIndex(instructions[i]) != 0)
continue; continue;
int index = i + 1; int index = i + 1;
@ -676,13 +676,13 @@ namespace de4dot.code.renamer {
if (fieldDef == null) if (fieldDef == null)
continue; continue;
if (DotNetUtils.getArgIndex(methodDef.MethodDefinition, instructions[index++]) != 0) if (DotNetUtils.getArgIndex(instructions[index++]) != 0)
continue; continue;
MethodReference methodRef; MethodReference methodRef;
var instr = instructions[index + 1]; var instr = instructions[index + 1];
if (instr.OpCode.Code == Code.Ldvirtftn) { if (instr.OpCode.Code == Code.Ldvirtftn) {
if (!isThisOrDup(methodDef.MethodDefinition, instructions[index++])) if (!isThisOrDup(instructions[index++]))
continue; continue;
var ldvirtftn = instructions[index++]; var ldvirtftn = instructions[index++];
methodRef = ldvirtftn.Operand as MethodReference; methodRef = ldvirtftn.Operand as MethodReference;
@ -741,18 +741,18 @@ namespace de4dot.code.renamer {
// newobj event handler ctor // newobj event handler ctor
// call add_Xyz // call add_Xyz
if (DotNetUtils.getArgIndex(method, instructions[i]) != 0) if (DotNetUtils.getArgIndex(instructions[i]) != 0)
continue; continue;
int index = i + 1; int index = i + 1;
if (!isThisOrDup(method, instructions[index++])) if (!isThisOrDup(instructions[index++]))
continue; continue;
MethodReference handler; MethodReference handler;
if (instructions[index].OpCode.Code == Code.Ldftn) { if (instructions[index].OpCode.Code == Code.Ldftn) {
handler = instructions[index++].Operand as MethodReference; handler = instructions[index++].Operand as MethodReference;
} }
else { else {
if (!isThisOrDup(method, instructions[index++])) if (!isThisOrDup(instructions[index++]))
continue; continue;
var instr = instructions[index++]; var instr = instructions[index++];
if (instr.OpCode.Code != Code.Ldvirtftn) if (instr.OpCode.Code != Code.Ldvirtftn)
@ -789,8 +789,8 @@ namespace de4dot.code.renamer {
} }
} }
static bool isThisOrDup(MethodReference method, Instruction instr) { static bool isThisOrDup(Instruction instr) {
return DotNetUtils.getArgIndex(method, instr) == 0 || instr.OpCode.Code == Code.Dup; return DotNetUtils.getArgIndex(instr) == 0 || instr.OpCode.Code == Code.Dup;
} }
static bool isEventHandlerCtor(MethodReference method) { static bool isEventHandlerCtor(MethodReference method) {
@ -830,7 +830,7 @@ namespace de4dot.code.renamer {
if (className == null) if (className == null)
continue; continue;
if (DotNetUtils.getArgIndex(methodDef.MethodDefinition, instructions[i - 2]) != 0) if (DotNetUtils.getArgIndex(instructions[i - 2]) != 0)
continue; continue;
findInitializeComponentMethod(type, methodDef); findInitializeComponentMethod(type, methodDef);