de4dot-cex/de4dot.blocks/Blocks.cs

283 lines
8.0 KiB
C#
Raw Permalink Normal View History

2011-09-22 10:55:30 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-09-22 10:55:30 +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;
using System.Collections.Generic;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2011-09-22 10:55:30 +08:00
namespace de4dot.blocks {
2011-09-24 16:26:29 +08:00
public class Blocks {
2012-11-01 00:43:51 +08:00
MethodDef method;
IList<Local> locals;
2011-09-22 10:55:30 +08:00
MethodBlocks methodBlocks;
public MethodBlocks MethodBlocks {
get { return methodBlocks; }
}
2012-11-01 00:43:51 +08:00
public IList<Local> Locals {
2011-09-22 10:55:30 +08:00
get { return locals; }
}
2012-11-01 00:43:51 +08:00
public MethodDef Method {
2011-09-22 10:55:30 +08:00
get { return method; }
}
2012-11-01 00:43:51 +08:00
public Blocks(MethodDef method) {
2011-09-22 10:55:30 +08:00
this.method = method;
2013-01-19 20:03:57 +08:00
UpdateBlocks();
2012-02-03 11:23:08 +08:00
}
2013-01-19 20:03:57 +08:00
public void UpdateBlocks() {
var body = method.Body;
locals = body.Variables;
2013-01-19 20:03:57 +08:00
methodBlocks = new InstructionListParser(body.Instructions, body.ExceptionHandlers).Parse();
2011-09-22 10:55:30 +08:00
}
2013-01-19 20:03:57 +08:00
IEnumerable<ScopeBlock> GetAllScopeBlocks(ScopeBlock scopeBlock) {
2011-09-22 10:55:30 +08:00
var list = new List<ScopeBlock>();
list.Add(scopeBlock);
2013-01-19 20:03:57 +08:00
list.AddRange(scopeBlock.GetAllScopeBlocks());
2011-09-22 10:55:30 +08:00
return list;
}
2013-01-19 20:03:57 +08:00
public int RemoveDeadBlocks() {
return new DeadBlocksRemover(methodBlocks).Remove();
2011-09-22 10:55:30 +08:00
}
2013-01-19 20:03:57 +08:00
public void GetCode(out IList<Instruction> allInstructions, out IList<ExceptionHandler> allExceptionHandlers) {
new CodeGenerator(methodBlocks).GetCode(out allInstructions, out allExceptionHandlers);
2011-09-22 10:55:30 +08:00
}
2011-10-20 08:38:44 +08:00
struct LocalVariableInfo {
public Block block;
public int index;
public LocalVariableInfo(Block block, int index) {
this.block = block;
this.index = index;
}
}
2013-01-19 20:03:57 +08:00
public int OptimizeLocals() {
2011-10-20 08:38:44 +08:00
if (locals.Count == 0)
return 0;
2012-11-01 00:43:51 +08:00
var usedLocals = new Dictionary<Local, List<LocalVariableInfo>>();
2013-01-19 20:03:57 +08:00
foreach (var block in methodBlocks.GetAllBlocks()) {
2011-10-20 08:38:44 +08:00
for (int i = 0; i < block.Instructions.Count; i++) {
var instr = block.Instructions[i];
2012-11-01 00:43:51 +08:00
Local local;
2011-10-20 08:38:44 +08:00
switch (instr.OpCode.Code) {
case Code.Ldloc:
case Code.Ldloc_S:
case Code.Ldloc_0:
case Code.Ldloc_1:
case Code.Ldloc_2:
case Code.Ldloc_3:
case Code.Stloc:
case Code.Stloc_S:
case Code.Stloc_0:
case Code.Stloc_1:
case Code.Stloc_2:
case Code.Stloc_3:
2013-01-19 20:03:57 +08:00
local = Instr.GetLocalVar(locals, instr);
2011-10-20 08:38:44 +08:00
break;
case Code.Ldloca_S:
case Code.Ldloca:
2012-11-01 00:43:51 +08:00
local = (Local)instr.Operand;
2011-10-20 08:38:44 +08:00
break;
default:
local = null;
break;
}
if (local == null)
continue;
List<LocalVariableInfo> list;
if (!usedLocals.TryGetValue(local, out list))
usedLocals[local] = list = new List<LocalVariableInfo>();
list.Add(new LocalVariableInfo(block, i));
if (usedLocals.Count == locals.Count)
return 0;
}
}
int newIndex = -1;
2012-11-01 00:43:51 +08:00
var newLocals = new List<Local>(usedLocals.Count);
var newLocalsDict = new Dictionary<Local, bool>(usedLocals.Count);
2011-10-20 08:38:44 +08:00
foreach (var local in usedLocals.Keys) {
newIndex++;
newLocals.Add(local);
newLocalsDict[local] = true;
2011-10-20 08:38:44 +08:00
foreach (var info in usedLocals[local])
2013-01-19 20:03:57 +08:00
info.block.Instructions[info.index] = new Instr(OptimizeLocalInstr(info.block.Instructions[info.index], local, (uint)newIndex));
2011-10-20 08:38:44 +08:00
}
// We can't remove all locals. Locals that reference another assembly will
// cause the CLR to load that assembly before the method is executed if it
// hasn't been loaded yet. This is a side effect the program may depend on.
// At least one program has this dependency and will crash if we remove the
// unused local. This took a while to figure out...
var keptAssemblies = new Dictionary<string, bool>(StringComparer.Ordinal);
foreach (var local in locals) {
if (newLocalsDict.ContainsKey(local))
continue;
var defAsm = local.Type.DefinitionAssembly;
if (defAsm == null)
continue; // eg. fnptr
2012-11-17 06:50:52 +08:00
if (defAsm == method.DeclaringType.Module.Assembly)
continue; // this assembly is always loaded
if (defAsm.IsCorLib())
continue; // mscorlib is always loaded
var asmName = defAsm.FullName;
if (keptAssemblies.ContainsKey(asmName))
continue;
keptAssemblies[asmName] = true;
newLocals.Add(local);
}
2011-10-20 08:38:44 +08:00
int numRemoved = locals.Count - newLocals.Count;
locals.Clear();
foreach (var local in newLocals)
locals.Add(local);
return numRemoved;
}
2013-01-19 20:03:57 +08:00
static Instruction OptimizeLocalInstr(Instr instr, Local local, uint newIndex) {
2011-10-20 08:38:44 +08:00
switch (instr.OpCode.Code) {
case Code.Ldloc:
case Code.Ldloc_S:
case Code.Ldloc_0:
case Code.Ldloc_1:
case Code.Ldloc_2:
case Code.Ldloc_3:
if (newIndex == 0)
2012-12-16 07:03:56 +08:00
return OpCodes.Ldloc_0.ToInstruction();
2011-10-20 08:38:44 +08:00
if (newIndex == 1)
2012-12-16 07:03:56 +08:00
return OpCodes.Ldloc_1.ToInstruction();
2011-10-20 08:38:44 +08:00
if (newIndex == 2)
2012-12-16 07:03:56 +08:00
return OpCodes.Ldloc_2.ToInstruction();
2011-10-20 08:38:44 +08:00
if (newIndex == 3)
2012-12-16 07:03:56 +08:00
return OpCodes.Ldloc_3.ToInstruction();
2011-10-20 08:38:44 +08:00
if (newIndex <= 0xFF)
2012-12-16 07:03:56 +08:00
return OpCodes.Ldloc_S.ToInstruction(local);
return OpCodes.Ldloc.ToInstruction(local);
2011-10-20 08:38:44 +08:00
case Code.Stloc:
case Code.Stloc_S:
case Code.Stloc_0:
case Code.Stloc_1:
case Code.Stloc_2:
case Code.Stloc_3:
if (newIndex == 0)
2012-12-16 07:03:56 +08:00
return OpCodes.Stloc_0.ToInstruction();
2011-10-20 08:38:44 +08:00
if (newIndex == 1)
2012-12-16 07:03:56 +08:00
return OpCodes.Stloc_1.ToInstruction();
2011-10-20 08:38:44 +08:00
if (newIndex == 2)
2012-12-16 07:03:56 +08:00
return OpCodes.Stloc_2.ToInstruction();
2011-10-20 08:38:44 +08:00
if (newIndex == 3)
2012-12-16 07:03:56 +08:00
return OpCodes.Stloc_3.ToInstruction();
2011-10-20 08:38:44 +08:00
if (newIndex <= 0xFF)
2012-12-16 07:03:56 +08:00
return OpCodes.Stloc_S.ToInstruction(local);
return OpCodes.Stloc.ToInstruction(local);
2011-10-20 08:38:44 +08:00
case Code.Ldloca_S:
case Code.Ldloca:
if (newIndex <= 0xFF)
2012-12-16 07:03:56 +08:00
return OpCodes.Ldloca_S.ToInstruction(local);
return OpCodes.Ldloca.ToInstruction(local);
2011-10-20 08:38:44 +08:00
default:
throw new ApplicationException("Invalid ld/st local instruction");
}
}
2013-01-19 20:03:57 +08:00
public void RepartitionBlocks() {
MergeNopBlocks();
foreach (var scopeBlock in GetAllScopeBlocks(methodBlocks)) {
2011-10-27 21:57:33 +08:00
try {
2013-01-19 20:03:57 +08:00
scopeBlock.RepartitionBlocks();
2011-10-27 21:57:33 +08:00
}
catch (NullReferenceException) {
//TODO: Send this message to the log
2012-11-01 00:43:51 +08:00
Console.WriteLine("Null ref exception! Invalid metadata token in code? Method: {0:X8}: {1}", method.MDToken.Raw, method.FullName);
2011-10-27 21:57:33 +08:00
return;
}
}
}
2011-12-15 17:04:04 +08:00
2013-01-19 20:03:57 +08:00
void MergeNopBlocks() {
var allBlocks = methodBlocks.GetAllBlocks();
2011-12-15 17:04:04 +08:00
var nopBlocks = new Dictionary<Block, bool>();
foreach (var nopBlock in allBlocks) {
2013-01-19 20:03:57 +08:00
if (nopBlock.IsNopBlock())
2011-12-15 17:04:04 +08:00
nopBlocks[nopBlock] = true;
}
if (nopBlocks.Count == 0)
return;
for (int i = 0; i < 10; i++) {
2013-04-30 18:15:07 +08:00
bool modified = false;
2011-12-15 17:04:04 +08:00
foreach (var block in allBlocks) {
Block nopBlockTarget;
2013-01-19 20:03:57 +08:00
nopBlockTarget = GetNopBlockTarget(nopBlocks, block, block.FallThrough);
2011-12-15 17:04:04 +08:00
if (nopBlockTarget != null) {
2013-01-19 20:03:57 +08:00
block.SetNewFallThrough(nopBlockTarget);
2013-04-30 18:15:07 +08:00
modified = true;
2011-12-15 17:04:04 +08:00
}
if (block.Targets != null) {
for (int targetIndex = 0; targetIndex < block.Targets.Count; targetIndex++) {
2013-01-19 20:03:57 +08:00
nopBlockTarget = GetNopBlockTarget(nopBlocks, block, block.Targets[targetIndex]);
2011-12-15 17:04:04 +08:00
if (nopBlockTarget == null)
continue;
2013-01-19 20:03:57 +08:00
block.SetNewTarget(targetIndex, nopBlockTarget);
2013-04-30 18:15:07 +08:00
modified = true;
2011-12-15 17:04:04 +08:00
}
}
}
2013-04-30 18:15:07 +08:00
if (!modified)
2011-12-15 17:04:04 +08:00
break;
}
2011-12-15 23:17:04 +08:00
foreach (var nopBlock in nopBlocks.Keys)
2013-01-19 20:03:57 +08:00
nopBlock.Parent.RemoveDeadBlock(nopBlock);
2011-12-15 17:04:04 +08:00
}
2013-01-19 20:03:57 +08:00
static Block GetNopBlockTarget(Dictionary<Block, bool> nopBlocks, Block source, Block nopBlock) {
2011-12-15 17:04:04 +08:00
if (nopBlock == null || !nopBlocks.ContainsKey(nopBlock) || source == nopBlock.FallThrough)
return null;
2011-12-15 23:17:04 +08:00
if (nopBlock.Parent.BaseBlocks[0] == nopBlock)
2011-12-15 17:04:04 +08:00
return null;
var target = nopBlock.FallThrough;
if (nopBlock.Parent != target.Parent)
return null;
return target;
}
2011-09-22 10:55:30 +08:00
}
}