Skip to content

Commit

Permalink
If instance explicit, 'this' is 1st param
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Jan 20, 2012
1 parent 664f0f8 commit f3f8975
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 21 deletions.
4 changes: 2 additions & 2 deletions AssemblyData/methodsrewriter/MethodsRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ void update(Block block, NewMethodInfo currentMethodInfo) {
}

static List<TypeReference> getParameters(MethodDefinition method) {
int count = method.Parameters.Count + (method.HasThis ? 1 : 0);
int count = method.Parameters.Count + (method.HasImplicitThis ? 1 : 0);
var list = new List<TypeReference>(count);
if (method.HasThis)
if (method.HasImplicitThis)
list.Add(method.DeclaringType);
foreach (var argType in method.Parameters)
list.Add(argType.ParameterType);
Expand Down
15 changes: 8 additions & 7 deletions blocks/DotNetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,13 @@ static void calculateStackUsage_call(Instruction instr, out int pushes, out int
pops = 0;

var method = (IMethodSignature)instr.Operand;
bool implicitThis = method.HasThis && !method.ExplicitThis;
if (hasReturnValue(method) || (instr.OpCode.Code == Code.Newobj && method.HasThis))
pushes++;

if (method.HasParameters)
pops += method.Parameters.Count;
if (method.HasThis && instr.OpCode.Code != Code.Newobj)
if (implicitThis && instr.OpCode.Code != Code.Newobj)
pops++;
}

Expand Down Expand Up @@ -824,20 +825,20 @@ public static int getArgIndex(MethodReference method, Instruction instr) {
}

public static int getArgIndex(MethodReference method, ParameterDefinition arg) {
return getArgIndex(method.HasThis, arg);
return getArgIndex(method.HasImplicitThis, arg);
}

public static int getArgIndex(bool hasThis, ParameterDefinition arg) {
public static int getArgIndex(bool implicitThis, ParameterDefinition arg) {
if (arg == null)
return -1;
if (hasThis)
if (implicitThis)
return arg.Index + 1;
return arg.Index;
}

public static List<ParameterDefinition> getParameters(MethodReference method) {
var args = new List<ParameterDefinition>(method.Parameters.Count + 1);
if (method.HasThis)
if (method.HasImplicitThis)
args.Add(new ParameterDefinition(method.DeclaringType));
foreach (var arg in method.Parameters)
args.Add(arg);
Expand All @@ -864,7 +865,7 @@ public static ParameterDefinition getParameter(IList<ParameterDefinition> parame

public static List<TypeReference> getArgs(MethodReference method) {
var args = new List<TypeReference>(method.Parameters.Count + 1);
if (method.HasThis)
if (method.HasImplicitThis)
args.Add(method.DeclaringType);
foreach (var arg in method.Parameters)
args.Add(arg.ParameterType);
Expand All @@ -887,7 +888,7 @@ public static TypeReference getArgType(IList<TypeReference> methodArgs, int inde

public static int getArgsCount(MethodReference method) {
int count = method.Parameters.Count;
if (method.HasThis)
if (method.HasImplicitThis)
count++;
return count;
}
Expand Down
2 changes: 1 addition & 1 deletion blocks/cflow/BlockCflowDeobfuscator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BlockCflowDeobfuscator {
public void init(Blocks blocks, Block block) {
this.blocks = blocks;
this.block = block;
instructionEmulator.init(blocks.Method.HasThis, false, blocks.Method.Parameters, blocks.Locals);
instructionEmulator.init(blocks.Method.HasImplicitThis, false, blocks.Method.Parameters, blocks.Locals);
}

// Returns true if code was updated, false otherwise
Expand Down
2 changes: 1 addition & 1 deletion blocks/cflow/ConstantsFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void init(Blocks blocks, List<Block> allBlocks) {
public bool deobfuscate() {
bool changed = false;
foreach (var block in allBlocks) {
instructionEmulator.init(blocks.Method.HasThis, false, blocks.Method.Parameters, blocks.Locals);
instructionEmulator.init(blocks.Method.HasImplicitThis, false, blocks.Method.Parameters, blocks.Locals);
var instrs = block.Instructions;
for (int i = 0; i < instrs.Count; i++) {
var instr = instrs[i];
Expand Down
8 changes: 4 additions & 4 deletions blocks/cflow/InstructionEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public class InstructionEmulator {
public InstructionEmulator() {
}

public InstructionEmulator(bool hasThis, bool initLocals, IList<ParameterDefinition> parameterDefinitions, IList<VariableDefinition> variableDefinitions) {
init(hasThis, initLocals, parameterDefinitions, variableDefinitions);
public InstructionEmulator(bool implicitThis, bool initLocals, IList<ParameterDefinition> parameterDefinitions, IList<VariableDefinition> variableDefinitions) {
init(implicitThis, initLocals, parameterDefinitions, variableDefinitions);
}

public void init(bool hasThis, bool initLocals, IList<ParameterDefinition> parameterDefinitions, IList<VariableDefinition> variableDefinitions) {
public void init(bool implicitThis, bool initLocals, IList<ParameterDefinition> parameterDefinitions, IList<VariableDefinition> variableDefinitions) {
this.parameterDefinitions = parameterDefinitions;
this.variableDefinitions = variableDefinitions;
valueStack.init();

args.Clear();
argBase = 0;
if (hasThis) {
if (implicitThis) {
argBase = 1;
args.Add(new UnknownValue());
}
Expand Down
10 changes: 5 additions & 5 deletions blocks/cflow/SwitchCflowDeobfuscator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ bool deobfuscateStLdloc(IList<Block> switchTargets, Block switchFallThrough, Blo
foreach (var source in new List<Block>(block.Sources)) {
if (!isBranchBlock(source))
continue;
instructionEmulator.init(blocks.Method.HasThis, false, blocks.Method.Parameters, blocks.Locals);
instructionEmulator.init(blocks.Method.HasImplicitThis, false, blocks.Method.Parameters, blocks.Locals);
instructionEmulator.emulate(source.Instructions);

var target = getSwitchTarget(switchTargets, switchFallThrough, source, instructionEmulator.pop());
Expand All @@ -151,7 +151,7 @@ bool deobfuscateLdloc(IList<Block> switchTargets, Block switchFallThrough, Block
foreach (var source in new List<Block>(block.Sources)) {
if (!isBranchBlock(source))
continue;
instructionEmulator.init(blocks.Method.HasThis, false, blocks.Method.Parameters, blocks.Locals);
instructionEmulator.init(blocks.Method.HasImplicitThis, false, blocks.Method.Parameters, blocks.Locals);
instructionEmulator.emulate(source.Instructions);

var target = getSwitchTarget(switchTargets, switchFallThrough, source, instructionEmulator.getLocal(switchVariable));
Expand All @@ -174,7 +174,7 @@ bool deobfuscateTos(IList<Block> switchTargets, Block switchFallThrough, Block b
foreach (var source in new List<Block>(block.Sources)) {
if (!isBranchBlock(source))
continue;
instructionEmulator.init(blocks.Method.HasThis, false, blocks.Method.Parameters, blocks.Locals);
instructionEmulator.init(blocks.Method.HasImplicitThis, false, blocks.Method.Parameters, blocks.Locals);
instructionEmulator.emulate(source.Instructions);

var target = getSwitchTarget(switchTargets, switchFallThrough, source, instructionEmulator.pop());
Expand Down Expand Up @@ -245,7 +245,7 @@ bool deobfuscateType1(Block switchBlock) {
}

bool emulateGetTarget(Block switchBlock, out Block target) {
instructionEmulator.init(blocks.Method.HasThis, false, blocks.Method.Parameters, blocks.Locals);
instructionEmulator.init(blocks.Method.HasImplicitThis, false, blocks.Method.Parameters, blocks.Locals);
try {
instructionEmulator.emulate(switchBlock.Instructions, 0, switchBlock.Instructions.Count - 1);
}
Expand All @@ -259,7 +259,7 @@ bool emulateGetTarget(Block switchBlock, out Block target) {
}

bool willHaveKnownTarget(Block switchBlock, Block source) {
instructionEmulator.init(blocks.Method.HasThis, false, blocks.Method.Parameters, blocks.Locals);
instructionEmulator.init(blocks.Method.HasImplicitThis, false, blocks.Method.Parameters, blocks.Locals);
try {
instructionEmulator.emulate(source.Instructions);
instructionEmulator.emulate(switchBlock.Instructions, 0, switchBlock.Instructions.Count - 1);
Expand Down
2 changes: 1 addition & 1 deletion de4dot.code/deobfuscators/ArrayFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static int[] getInitializedInt32Array(int arraySize, MethodDefinition met
public static Value[] getInitializedArray(int arraySize, MethodDefinition method, ref int newarrIndex, Code stelemOpCode) {
var resultValueArray = new Value[arraySize];

var emulator = new InstructionEmulator(method.HasThis, false, method.Parameters, method.Body.Variables);
var emulator = new InstructionEmulator(method.HasImplicitThis, false, method.Parameters, method.Body.Variables);
var theArray = new UnknownValue();
emulator.push(theArray);

Expand Down

0 comments on commit f3f8975

Please sign in to comment.