Port ConstantsReader

This commit is contained in:
de4dot 2012-10-31 17:09:58 +01:00
parent 6f73696cc5
commit 04e1568c61
2 changed files with 36 additions and 31 deletions

View File

@ -18,18 +18,17 @@
*/ */
using System.Collections.Generic; using System.Collections.Generic;
using Mono.Cecil; using dot10.DotNet;
using Mono.Cecil.Cil; using dot10.DotNet.Emit;
using Mono.Cecil.Metadata;
using de4dot.blocks; using de4dot.blocks;
namespace de4dot.code.deobfuscators { namespace de4dot.code.deobfuscators {
class ConstantsReader { class ConstantsReader {
protected IInstructions instructions; protected IInstructions instructions;
protected IList<VariableDefinition> locals; protected IList<Local> locals;
protected Dictionary<VariableDefinition, int> localsValuesInt32 = new Dictionary<VariableDefinition, int>(); protected Dictionary<Local, int> localsValuesInt32 = new Dictionary<Local, int>();
protected Dictionary<VariableDefinition, long> localsValuesInt64 = new Dictionary<VariableDefinition, long>(); protected Dictionary<Local, long> localsValuesInt64 = new Dictionary<Local, long>();
protected Dictionary<VariableDefinition, double> localsValuesDouble = new Dictionary<VariableDefinition, double>(); protected Dictionary<Local, double> localsValuesDouble = new Dictionary<Local, double>();
bool emulateConvInstrs; bool emulateConvInstrs;
public interface IInstructions { public interface IInstructions {
@ -53,6 +52,7 @@ namespace de4dot.code.deobfuscators {
} }
} }
#if PORT
class ListInstrs : IInstructions { class ListInstrs : IInstructions {
IList<Instr> instrs; IList<Instr> instrs;
@ -68,6 +68,7 @@ namespace de4dot.code.deobfuscators {
this.instrs = instrs; this.instrs = instrs;
} }
} }
#endif
public bool EmulateConvInstructions { public bool EmulateConvInstructions {
get { return emulateConvInstrs; } get { return emulateConvInstrs; }
@ -91,6 +92,7 @@ namespace de4dot.code.deobfuscators {
: this(new ListInstructions(instrs), emulateConvInstrs) { : this(new ListInstructions(instrs), emulateConvInstrs) {
} }
#if PORT
public ConstantsReader(IList<Instr> instrs) public ConstantsReader(IList<Instr> instrs)
: this(new ListInstrs(instrs)) { : this(new ListInstrs(instrs)) {
} }
@ -98,34 +100,37 @@ namespace de4dot.code.deobfuscators {
public ConstantsReader(IList<Instr> instrs, bool emulateConvInstrs) public ConstantsReader(IList<Instr> instrs, bool emulateConvInstrs)
: this(new ListInstrs(instrs), emulateConvInstrs) { : this(new ListInstrs(instrs), emulateConvInstrs) {
} }
#endif
public ConstantsReader(MethodDefinition method) public ConstantsReader(MethodDef method)
: this(method.Body.Instructions) { : this(method.CilBody.Instructions) {
this.locals = method.Body.Variables; this.locals = method.CilBody.LocalList;
} }
public ConstantsReader(IList<Instr> instrs, IList<VariableDefinition> locals) #if PORT
public ConstantsReader(IList<Instr> instrs, IList<Local> locals)
: this(instrs) { : this(instrs) {
this.locals = locals; this.locals = locals;
} }
#endif
public void setConstantInt32(VariableDefinition local, int value) { public void setConstantInt32(Local local, int value) {
localsValuesInt32[local] = value; localsValuesInt32[local] = value;
} }
public void setConstantInt32(VariableDefinition local, uint value) { public void setConstantInt32(Local local, uint value) {
setConstantInt32(local, (int)value); setConstantInt32(local, (int)value);
} }
public void setConstantInt64(VariableDefinition local, long value) { public void setConstantInt64(Local local, long value) {
localsValuesInt64[local] = value; localsValuesInt64[local] = value;
} }
public void setConstantInt64(VariableDefinition local, ulong value) { public void setConstantInt64(Local local, ulong value) {
setConstantInt64(local, (long)value); setConstantInt64(local, (long)value);
} }
public void setConstantDouble(VariableDefinition local, double value) { public void setConstantDouble(Local local, double value) {
localsValuesDouble[local] = value; localsValuesDouble[local] = value;
} }
@ -143,13 +148,13 @@ namespace de4dot.code.deobfuscators {
} }
public bool isLoadConstantInt32(Instruction instr) { public bool isLoadConstantInt32(Instruction instr) {
if (DotNetUtils.isLdcI4(instr)) if (instr.IsLdcI4())
return true; return true;
if (DotNetUtils.isLdloc(instr)) { if (instr.IsLdloc()) {
int tmp; int tmp;
return getLocalConstantInt32(instr, out tmp); return getLocalConstantInt32(instr, out tmp);
} }
if (DotNetUtils.isLdarg(instr)) { if (instr.IsLdarg()) {
int tmp; int tmp;
return getArgConstantInt32(instr, out tmp); return getArgConstantInt32(instr, out tmp);
} }
@ -159,11 +164,11 @@ namespace de4dot.code.deobfuscators {
public bool isLoadConstantInt64(Instruction instr) { public bool isLoadConstantInt64(Instruction instr) {
if (instr.OpCode.Code == Code.Ldc_I8) if (instr.OpCode.Code == Code.Ldc_I8)
return true; return true;
if (DotNetUtils.isLdloc(instr)) { if (instr.IsLdloc()) {
long tmp; long tmp;
return getLocalConstantInt64(instr, out tmp); return getLocalConstantInt64(instr, out tmp);
} }
if (DotNetUtils.isLdarg(instr)) { if (instr.IsLdarg()) {
long tmp; long tmp;
return getArgConstantInt64(instr, out tmp); return getArgConstantInt64(instr, out tmp);
} }
@ -173,11 +178,11 @@ namespace de4dot.code.deobfuscators {
public bool isLoadConstantDouble(Instruction instr) { public bool isLoadConstantDouble(Instruction instr) {
if (instr.OpCode.Code == Code.Ldc_R8) if (instr.OpCode.Code == Code.Ldc_R8)
return true; return true;
if (DotNetUtils.isLdloc(instr)) { if (instr.IsLdloc()) {
double tmp; double tmp;
return getLocalConstantDouble(instr, out tmp); return getLocalConstantDouble(instr, out tmp);
} }
if (DotNetUtils.isLdarg(instr)) { if (instr.IsLdarg()) {
double tmp; double tmp;
return getArgConstantDouble(instr, out tmp); return getArgConstantDouble(instr, out tmp);
} }
@ -309,7 +314,7 @@ namespace de4dot.code.deobfuscators {
case Code.Ldc_I4_7: case Code.Ldc_I4_7:
case Code.Ldc_I4_8: case Code.Ldc_I4_8:
case Code.Ldc_I4_M1: case Code.Ldc_I4_M1:
stack.Push(new ConstantInfo<int>(index, DotNetUtils.getLdcI4Value(instr))); stack.Push(new ConstantInfo<int>(index, instr.GetLdcI4Value()));
break; break;
case Code.Add: case Code.Add:
@ -698,10 +703,10 @@ done:
value = 0; value = 0;
if (locals == null) if (locals == null)
return false; return false;
var local = DotNetUtils.getLocalVar(locals, instr); var local = instr.GetLocal(locals);
if (local == null) if (local == null)
return false; return false;
if (local.VariableType.EType != ElementType.I4 && local.VariableType.EType != ElementType.U4) if (local.Type.ElementType != ElementType.I4 && local.Type.ElementType != ElementType.U4)
return false; return false;
return localsValuesInt32.TryGetValue(local, out value); return localsValuesInt32.TryGetValue(local, out value);
} }
@ -715,10 +720,10 @@ done:
value = 0; value = 0;
if (locals == null) if (locals == null)
return false; return false;
var local = DotNetUtils.getLocalVar(locals, instr); var local = instr.GetLocal(locals);
if (local == null) if (local == null)
return false; return false;
if (local.VariableType.EType != ElementType.I8 && local.VariableType.EType != ElementType.U8) if (local.Type.ElementType != ElementType.I8 && local.Type.ElementType != ElementType.U8)
return false; return false;
return localsValuesInt64.TryGetValue(local, out value); return localsValuesInt64.TryGetValue(local, out value);
} }
@ -732,10 +737,10 @@ done:
value = 0; value = 0;
if (locals == null) if (locals == null)
return false; return false;
var local = DotNetUtils.getLocalVar(locals, instr); var local = instr.GetLocal(locals);
if (local == null) if (local == null)
return false; return false;
if (local.VariableType.EType != ElementType.R8) if (local.Type.ElementType != ElementType.R8)
return false; return false;
return localsValuesDouble.TryGetValue(local, out value); return localsValuesDouble.TryGetValue(local, out value);
} }

2
dot10

@ -1 +1 @@
Subproject commit c7a83ee87964d694a1e87ed9feb6cbc054226295 Subproject commit ec48311025fd4e9a56da7881857d90eaa0e3b5ca