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) {
if (ResolverUtils.hasThis(methodInfo.methodBase))
return arg.Index + 1;
return arg.Index;
return arg.Sequence;
}
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) {
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<ParameterDefinition> 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<ParameterDefinition> parameters, Instruction instr) {
return getParameter(parameters, getArgIndex(method, 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, Instruction instr) {
return getParameter(parameters, getArgIndex(instr));
}
public static ParameterDefinition getParameter(IList<ParameterDefinition> 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<TypeReference> methodArgs, MethodReference method, Instruction instr) {
return getArgType(methodArgs, getArgIndex(method, instr));
public static TypeReference getArgType(IList<TypeReference> methodArgs, Instruction instr) {
return getArgType(methodArgs, getArgIndex(instr));
}
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_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:

View File

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

View File

@ -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);

View File

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

View File

@ -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;

View File

@ -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<ParameterDefinition> parameters, MethodReference method, Instruction instr) {
static ParameterDefinition getParameter(IList<ParameterDefinition> 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;

View File

@ -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);