Remove more dead code and useless stores
This commit is contained in:
parent
9d132bfeaf
commit
2bba9e688a
|
@ -40,18 +40,23 @@ namespace de4dot.blocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
public IList<BaseBlock> getAllBaseBlocks() {
|
public IList<BaseBlock> getAllBaseBlocks() {
|
||||||
return getAllBlocks(new List<BaseBlock>());
|
return getTheBlocks(new List<BaseBlock>());
|
||||||
}
|
}
|
||||||
|
|
||||||
public IList<Block> getAllBlocks() {
|
public IList<Block> getAllBlocks() {
|
||||||
return getAllBlocks(new List<Block>());
|
return getTheBlocks(new List<Block>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public IList<Block> getAllBlocks(IList<Block> allBlocks) {
|
||||||
|
allBlocks.Clear();
|
||||||
|
return getTheBlocks(allBlocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IList<ScopeBlock> getAllScopeBlocks() {
|
public IList<ScopeBlock> getAllScopeBlocks() {
|
||||||
return getAllBlocks(new List<ScopeBlock>());
|
return getTheBlocks(new List<ScopeBlock>());
|
||||||
}
|
}
|
||||||
|
|
||||||
IList<T> getAllBlocks<T>(IList<T> list) where T : BaseBlock {
|
public IList<T> getTheBlocks<T>(IList<T> list) where T : BaseBlock {
|
||||||
addBlocks(list, this);
|
addBlocks(list, this);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
@ -319,7 +324,7 @@ namespace de4dot.blocks {
|
||||||
// Get removed blocks and make sure they're not referenced by remaining code
|
// Get removed blocks and make sure they're not referenced by remaining code
|
||||||
var removedBlocks = new List<Block>();
|
var removedBlocks = new List<Block>();
|
||||||
foreach (var handler in tryBlock.TryHandlerBlocks)
|
foreach (var handler in tryBlock.TryHandlerBlocks)
|
||||||
handler.getAllBlocks(removedBlocks);
|
handler.getTheBlocks(removedBlocks);
|
||||||
if (!verifyNoExternalRefs(removedBlocks))
|
if (!verifyNoExternalRefs(removedBlocks))
|
||||||
throw new ApplicationException("Removed blocks are referenced by remaining code");
|
throw new ApplicationException("Removed blocks are referenced by remaining code");
|
||||||
|
|
||||||
|
|
|
@ -38,10 +38,12 @@
|
||||||
<Compile Include="cflow\BlockCflowDeobfuscator.cs" />
|
<Compile Include="cflow\BlockCflowDeobfuscator.cs" />
|
||||||
<Compile Include="cflow\CflowUtils.cs" />
|
<Compile Include="cflow\CflowUtils.cs" />
|
||||||
<Compile Include="cflow\DeadCodeRemover.cs" />
|
<Compile Include="cflow\DeadCodeRemover.cs" />
|
||||||
|
<Compile Include="cflow\DeadStoreRemover.cs" />
|
||||||
<Compile Include="cflow\InstructionEmulator.cs" />
|
<Compile Include="cflow\InstructionEmulator.cs" />
|
||||||
<Compile Include="cflow\Int32Value.cs" />
|
<Compile Include="cflow\Int32Value.cs" />
|
||||||
<Compile Include="cflow\Int64Value.cs" />
|
<Compile Include="cflow\Int64Value.cs" />
|
||||||
<Compile Include="cflow\Real8Value.cs" />
|
<Compile Include="cflow\Real8Value.cs" />
|
||||||
|
<Compile Include="cflow\StLdlocFixer.cs" />
|
||||||
<Compile Include="cflow\SwitchCflowDeobfuscator.cs" />
|
<Compile Include="cflow\SwitchCflowDeobfuscator.cs" />
|
||||||
<Compile Include="cflow\Value.cs" />
|
<Compile Include="cflow\Value.cs" />
|
||||||
<Compile Include="cflow\ValueStack.cs" />
|
<Compile Include="cflow\ValueStack.cs" />
|
||||||
|
|
|
@ -39,14 +39,15 @@ namespace de4dot.blocks.cflow {
|
||||||
var allBlocks = new List<Block>();
|
var allBlocks = new List<Block>();
|
||||||
var switchCflowDeobfuscator = new SwitchCflowDeobfuscator();
|
var switchCflowDeobfuscator = new SwitchCflowDeobfuscator();
|
||||||
var deadCodeRemover = new DeadCodeRemover();
|
var deadCodeRemover = new DeadCodeRemover();
|
||||||
|
var deadStoreRemover = new DeadStoreRemover();
|
||||||
|
var stLdlocFixer = new StLdlocFixer();
|
||||||
bool changed;
|
bool changed;
|
||||||
do {
|
do {
|
||||||
changed = false;
|
changed = false;
|
||||||
removeDeadBlocks();
|
removeDeadBlocks();
|
||||||
mergeBlocks();
|
mergeBlocks();
|
||||||
|
|
||||||
allBlocks.Clear();
|
blocks.MethodBlocks.getAllBlocks(allBlocks);
|
||||||
allBlocks.AddRange(blocks.MethodBlocks.getAllBlocks());
|
|
||||||
|
|
||||||
foreach (var block in allBlocks) {
|
foreach (var block in allBlocks) {
|
||||||
var lastInstr = block.LastInstr;
|
var lastInstr = block.LastInstr;
|
||||||
|
@ -59,8 +60,16 @@ namespace de4dot.blocks.cflow {
|
||||||
switchCflowDeobfuscator.init(blocks, allBlocks);
|
switchCflowDeobfuscator.init(blocks, allBlocks);
|
||||||
changed |= switchCflowDeobfuscator.deobfuscate();
|
changed |= switchCflowDeobfuscator.deobfuscate();
|
||||||
|
|
||||||
|
deadStoreRemover.init(blocks, allBlocks);
|
||||||
|
changed |= deadStoreRemover.remove();
|
||||||
|
|
||||||
deadCodeRemover.init(allBlocks);
|
deadCodeRemover.init(allBlocks);
|
||||||
changed |= deadCodeRemover.remove();
|
changed |= deadCodeRemover.remove();
|
||||||
|
|
||||||
|
if (!changed) {
|
||||||
|
stLdlocFixer.init(allBlocks, blocks.Locals);
|
||||||
|
changed |= stLdlocFixer.fix();
|
||||||
|
}
|
||||||
} while (changed);
|
} while (changed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,8 @@ using System.Collections.Generic;
|
||||||
using Mono.Cecil.Cil;
|
using Mono.Cecil.Cil;
|
||||||
|
|
||||||
namespace de4dot.blocks.cflow {
|
namespace de4dot.blocks.cflow {
|
||||||
|
// Removes dead code that is the result of one of our optimizations, or created by the
|
||||||
|
// obfuscator.
|
||||||
class DeadCodeRemover {
|
class DeadCodeRemover {
|
||||||
List<Block> allBlocks;
|
List<Block> allBlocks;
|
||||||
List<int> allDeadInstructions = new List<int>();
|
List<int> allDeadInstructions = new List<int>();
|
||||||
|
@ -42,6 +44,7 @@ namespace de4dot.blocks.cflow {
|
||||||
bool remove(Block block) {
|
bool remove(Block block) {
|
||||||
allDeadInstructions.Clear();
|
allDeadInstructions.Clear();
|
||||||
|
|
||||||
|
bool changed = false;
|
||||||
var instructions = block.Instructions;
|
var instructions = block.Instructions;
|
||||||
for (int i = 0; i < instructions.Count; i++) {
|
for (int i = 0; i < instructions.Count; i++) {
|
||||||
var instr = instructions[i];
|
var instr = instructions[i];
|
||||||
|
@ -79,11 +82,13 @@ namespace de4dot.blocks.cflow {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (allDeadInstructions.Count == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
|
if (allDeadInstructions.Count > 0) {
|
||||||
block.remove(allDeadInstructions);
|
block.remove(allDeadInstructions);
|
||||||
return true;
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool okInstructions(Block block, IEnumerable<int> indexes) {
|
bool okInstructions(Block block, IEnumerable<int> indexes) {
|
||||||
|
|
152
blocks/cflow/DeadStoreRemover.cs
Normal file
152
blocks/cflow/DeadStoreRemover.cs
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
/*
|
||||||
|
Copyright (C) 2011 de4dot@gmail.com
|
||||||
|
|
||||||
|
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 Mono.Cecil.Cil;
|
||||||
|
|
||||||
|
namespace de4dot.blocks.cflow {
|
||||||
|
// Removes dead stores by replacing the stloc with a pop. Other optimizations will notice it's
|
||||||
|
// dead code and remove it.
|
||||||
|
// I've only seen Xenocode generate this kind of code, so the code below is a special case of
|
||||||
|
// the more general case.
|
||||||
|
class DeadStoreRemover {
|
||||||
|
Blocks blocks;
|
||||||
|
List<Block> allBlocks = new List<Block>();
|
||||||
|
List<AccessFlags> localFlags = new List<AccessFlags>();
|
||||||
|
List<bool> deadLocals = new List<bool>();
|
||||||
|
|
||||||
|
public void init(Blocks blocks, List<Block> allBlocks) {
|
||||||
|
this.blocks = blocks;
|
||||||
|
this.allBlocks = allBlocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Flags]
|
||||||
|
enum AccessFlags {
|
||||||
|
None = 0,
|
||||||
|
Read = 1,
|
||||||
|
Write = 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool remove() {
|
||||||
|
if (blocks.Locals.Count == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
localFlags.Clear();
|
||||||
|
deadLocals.Clear();
|
||||||
|
for (int i = 0; i < blocks.Locals.Count; i++) {
|
||||||
|
localFlags.Add(AccessFlags.None);
|
||||||
|
deadLocals.Add(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
findLoadStores();
|
||||||
|
|
||||||
|
bool deadStores = false;
|
||||||
|
for (int i = 0; i < blocks.Locals.Count; i++) {
|
||||||
|
var flags = localFlags[i];
|
||||||
|
if ((flags & AccessFlags.Read) == AccessFlags.None) {
|
||||||
|
deadLocals[i] = true;
|
||||||
|
deadStores = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!deadStores)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return removeDeadStores();
|
||||||
|
}
|
||||||
|
|
||||||
|
void findLoadStores() {
|
||||||
|
foreach (var block in allBlocks) {
|
||||||
|
foreach (var instr in block.Instructions) {
|
||||||
|
VariableDefinition local;
|
||||||
|
AccessFlags flags;
|
||||||
|
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:
|
||||||
|
local = Instr.getLocalVar(blocks.Locals, instr);
|
||||||
|
flags = AccessFlags.Read;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Code.Stloc:
|
||||||
|
case Code.Stloc_S:
|
||||||
|
case Code.Stloc_0:
|
||||||
|
case Code.Stloc_1:
|
||||||
|
case Code.Stloc_2:
|
||||||
|
case Code.Stloc_3:
|
||||||
|
local = Instr.getLocalVar(blocks.Locals, instr);
|
||||||
|
flags = AccessFlags.Write;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Code.Ldloca_S:
|
||||||
|
case Code.Ldloca:
|
||||||
|
local = instr.Operand as VariableDefinition;
|
||||||
|
flags = AccessFlags.Read | AccessFlags.Write;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
local = null;
|
||||||
|
flags = AccessFlags.None;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (local == null)
|
||||||
|
continue;
|
||||||
|
localFlags[local.Index] |= flags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool removeDeadStores() {
|
||||||
|
bool changed = false;
|
||||||
|
foreach (var block in allBlocks) {
|
||||||
|
var instructions = block.Instructions;
|
||||||
|
for (int i = 0; i < instructions.Count; i++) {
|
||||||
|
var instr = instructions[i];
|
||||||
|
VariableDefinition local;
|
||||||
|
switch (instr.OpCode.Code) {
|
||||||
|
case Code.Stloc:
|
||||||
|
case Code.Stloc_S:
|
||||||
|
case Code.Stloc_0:
|
||||||
|
case Code.Stloc_1:
|
||||||
|
case Code.Stloc_2:
|
||||||
|
case Code.Stloc_3:
|
||||||
|
local = Instr.getLocalVar(blocks.Locals, instr);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (local == null)
|
||||||
|
continue;
|
||||||
|
if (!deadLocals[local.Index])
|
||||||
|
continue;
|
||||||
|
instructions[i] = new Instr(Instruction.Create(OpCodes.Pop));
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
76
blocks/cflow/StLdlocFixer.cs
Normal file
76
blocks/cflow/StLdlocFixer.cs
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
Copyright (C) 2011 de4dot@gmail.com
|
||||||
|
|
||||||
|
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.Cil;
|
||||||
|
|
||||||
|
namespace de4dot.blocks.cflow {
|
||||||
|
// Replace stloc + ldloc with dup + stloc
|
||||||
|
class StLdlocFixer {
|
||||||
|
IList<VariableDefinition> locals;
|
||||||
|
List<Block> allBlocks;
|
||||||
|
|
||||||
|
public void init(List<Block> allBlocks, IList<VariableDefinition> locals) {
|
||||||
|
this.allBlocks = allBlocks;
|
||||||
|
this.locals = locals;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool fix() {
|
||||||
|
bool changed = false;
|
||||||
|
|
||||||
|
foreach (var block in allBlocks)
|
||||||
|
changed |= fix(block);
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fix(Block block) {
|
||||||
|
bool changed = false;
|
||||||
|
var instructions = block.Instructions;
|
||||||
|
for (int i = 0; i < instructions.Count; i++) {
|
||||||
|
var instr = instructions[i];
|
||||||
|
switch (instr.OpCode.Code) {
|
||||||
|
// Xenocode generates stloc + ldloc. Replace it with dup + stloc. It will eventually
|
||||||
|
// become dup + pop and be removed.
|
||||||
|
case Code.Stloc:
|
||||||
|
case Code.Stloc_S:
|
||||||
|
case Code.Stloc_0:
|
||||||
|
case Code.Stloc_1:
|
||||||
|
case Code.Stloc_2:
|
||||||
|
case Code.Stloc_3:
|
||||||
|
if (i + 1 >= instructions.Count)
|
||||||
|
break;
|
||||||
|
if (!instructions[i + 1].isLdloc())
|
||||||
|
break;
|
||||||
|
if (Instr.getLocalVar(locals, instr) != Instr.getLocalVar(locals, instructions[i + 1]))
|
||||||
|
break;
|
||||||
|
instructions[i] = new Instr(Instruction.Create(OpCodes.Dup));
|
||||||
|
instructions[i + 1] = instr;
|
||||||
|
changed = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user