2011-10-24 18:45:20 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-10-24 18:45:20 +08:00
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Mono.Cecil;
|
|
|
|
|
using Mono.Cecil.Cil;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.blocks.cflow {
|
2012-01-11 11:38:02 +08:00
|
|
|
|
public class MethodCallInliner : MethodCallInlinerBase {
|
2011-12-21 03:11:05 +08:00
|
|
|
|
bool inlineInstanceMethods;
|
2011-10-24 18:45:20 +08:00
|
|
|
|
|
2012-01-11 11:38:02 +08:00
|
|
|
|
public MethodCallInliner(bool inlineInstanceMethods) {
|
2011-12-21 03:11:05 +08:00
|
|
|
|
this.inlineInstanceMethods = inlineInstanceMethods;
|
2011-10-24 18:45:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-11 11:38:02 +08:00
|
|
|
|
protected override bool deobfuscateInternal() {
|
2011-10-24 18:45:20 +08:00
|
|
|
|
bool changed = false;
|
|
|
|
|
var instructions = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instructions.Count; i++) {
|
|
|
|
|
var instr = instructions[i].Instruction;
|
|
|
|
|
if (instr.OpCode.Code == Code.Call)
|
|
|
|
|
changed |= inlineMethod(instr, i);
|
|
|
|
|
}
|
|
|
|
|
return changed;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-02 17:38:56 +08:00
|
|
|
|
protected virtual bool canInline(MethodDefinition method) {
|
2012-02-25 13:33:38 +08:00
|
|
|
|
if (method.GenericParameters.Count > 0)
|
|
|
|
|
return false;
|
2012-02-02 17:38:56 +08:00
|
|
|
|
if (MemberReferenceHelper.compareMethodReferenceAndDeclaringType(method, blocks.Method))
|
|
|
|
|
return false;
|
|
|
|
|
if (!MemberReferenceHelper.compareTypes(method.DeclaringType, blocks.Method.DeclaringType))
|
|
|
|
|
return false;
|
|
|
|
|
|
2011-12-21 03:11:05 +08:00
|
|
|
|
if (method.IsStatic)
|
|
|
|
|
return true;
|
|
|
|
|
if (method.IsVirtual)
|
|
|
|
|
return false;
|
|
|
|
|
return inlineInstanceMethods;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-24 18:45:20 +08:00
|
|
|
|
bool inlineMethod(Instruction callInstr, int instrIndex) {
|
2012-01-22 02:55:37 +08:00
|
|
|
|
var methodToInline = callInstr.Operand as MethodDefinition;
|
|
|
|
|
if (methodToInline == null)
|
2011-10-24 18:45:20 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2012-01-22 02:55:37 +08:00
|
|
|
|
if (!canInline(methodToInline))
|
2011-10-24 18:45:20 +08:00
|
|
|
|
return false;
|
2012-01-22 02:55:37 +08:00
|
|
|
|
var body = methodToInline.Body;
|
2011-10-24 18:45:20 +08:00
|
|
|
|
if (body == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
int index = 0;
|
2011-11-09 18:28:09 +08:00
|
|
|
|
var instr = DotNetUtils.getInstruction(body.Instructions, ref index);
|
2011-10-24 18:45:20 +08:00
|
|
|
|
if (instr == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
switch (instr.OpCode.Code) {
|
|
|
|
|
case Code.Ldarg:
|
|
|
|
|
case Code.Ldarg_S:
|
|
|
|
|
case Code.Ldarg_0:
|
|
|
|
|
case Code.Ldarg_1:
|
|
|
|
|
case Code.Ldarg_2:
|
|
|
|
|
case Code.Ldarg_3:
|
2011-12-28 20:28:17 +08:00
|
|
|
|
case Code.Ldarga:
|
|
|
|
|
case Code.Ldarga_S:
|
2011-10-24 18:45:20 +08:00
|
|
|
|
case Code.Call:
|
2012-01-22 02:55:37 +08:00
|
|
|
|
case Code.Callvirt:
|
2011-12-28 20:28:17 +08:00
|
|
|
|
case Code.Newobj:
|
2012-01-22 02:55:37 +08:00
|
|
|
|
return inlineOtherMethod(instrIndex, methodToInline, instr, index);
|
2011-10-24 18:45:20 +08:00
|
|
|
|
|
|
|
|
|
case Code.Ldc_I4:
|
|
|
|
|
case Code.Ldc_I4_0:
|
|
|
|
|
case Code.Ldc_I4_1:
|
|
|
|
|
case Code.Ldc_I4_2:
|
|
|
|
|
case Code.Ldc_I4_3:
|
|
|
|
|
case Code.Ldc_I4_4:
|
|
|
|
|
case Code.Ldc_I4_5:
|
|
|
|
|
case Code.Ldc_I4_6:
|
|
|
|
|
case Code.Ldc_I4_7:
|
|
|
|
|
case Code.Ldc_I4_8:
|
|
|
|
|
case Code.Ldc_I4_M1:
|
|
|
|
|
case Code.Ldc_I4_S:
|
|
|
|
|
case Code.Ldc_I8:
|
|
|
|
|
case Code.Ldc_R4:
|
|
|
|
|
case Code.Ldc_R8:
|
|
|
|
|
case Code.Ldftn:
|
|
|
|
|
case Code.Ldnull:
|
|
|
|
|
case Code.Ldstr:
|
|
|
|
|
case Code.Ldtoken:
|
|
|
|
|
case Code.Ldsfld:
|
|
|
|
|
case Code.Ldsflda:
|
2012-01-22 02:55:37 +08:00
|
|
|
|
return inlineLoadMethod(instrIndex, methodToInline, instr, index);
|
2011-10-24 18:45:20 +08:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-03 16:42:55 +08:00
|
|
|
|
protected override bool isCompatibleType(int paramIndex, TypeReference origType, TypeReference newType) {
|
2011-10-24 18:45:20 +08:00
|
|
|
|
if (MemberReferenceHelper.compareTypes(origType, newType))
|
|
|
|
|
return true;
|
|
|
|
|
if (newType.IsValueType || origType.IsValueType)
|
|
|
|
|
return false;
|
|
|
|
|
return newType.FullName == "System.Object";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|