de4dot-cex/de4dot.code/deobfuscators/UnusedMethodsFinder.cs

91 lines
2.6 KiB
C#
Raw Normal View History

2011-12-21 04:47:45 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-12-21 04:47:45 +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 dnlib.DotNet;
using dnlib.DotNet.Emit;
2011-12-21 04:47:45 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators {
public class UnusedMethodsFinder {
2012-11-01 18:28:09 +08:00
ModuleDef module;
MethodCollection removedMethods;
2012-11-01 18:28:09 +08:00
Dictionary<MethodDef, bool> possiblyUnusedMethods = new Dictionary<MethodDef, bool>();
Stack<MethodDef> notUnusedStack = new Stack<MethodDef>();
2011-12-21 04:47:45 +08:00
2012-11-01 18:28:09 +08:00
public UnusedMethodsFinder(ModuleDef module, IEnumerable<MethodDef> possiblyUnusedMethods, MethodCollection removedMethods) {
2011-12-21 04:47:45 +08:00
this.module = module;
this.removedMethods = removedMethods;
2011-12-21 04:47:45 +08:00
foreach (var method in possiblyUnusedMethods) {
2013-01-19 20:03:57 +08:00
if (method != module.ManagedEntryPoint && !removedMethods.Exists(method))
2011-12-21 04:47:45 +08:00
this.possiblyUnusedMethods[method] = true;
}
}
2013-01-19 20:03:57 +08:00
public IEnumerable<MethodDef> Find() {
2011-12-21 04:47:45 +08:00
if (possiblyUnusedMethods.Count == 0)
return possiblyUnusedMethods.Keys;
foreach (var type in module.GetTypes()) {
foreach (var method in type.Methods)
2013-01-19 20:03:57 +08:00
Check(method);
2011-12-21 04:47:45 +08:00
}
while (notUnusedStack.Count > 0) {
var method = notUnusedStack.Pop();
if (!possiblyUnusedMethods.Remove(method))
continue;
2013-01-19 20:03:57 +08:00
Check(method);
2011-12-21 04:47:45 +08:00
}
return possiblyUnusedMethods.Keys;
}
2013-01-19 20:03:57 +08:00
void Check(MethodDef method) {
if (method.Body == null)
2011-12-21 04:47:45 +08:00
return;
if (possiblyUnusedMethods.ContainsKey(method))
return;
2013-01-19 20:03:57 +08:00
if (removedMethods.Exists(method))
return;
2011-12-21 04:47:45 +08:00
foreach (var instr in method.Body.Instructions) {
2011-12-21 04:47:45 +08:00
switch (instr.OpCode.Code) {
case Code.Call:
case Code.Calli:
case Code.Callvirt:
case Code.Newobj:
case Code.Ldtoken:
case Code.Ldftn:
case Code.Ldvirtftn:
2011-12-21 04:47:45 +08:00
break;
default:
continue;
}
2013-01-19 20:03:57 +08:00
var calledMethod = DotNetUtils.GetMethod2(module, instr.Operand as IMethod);
2011-12-21 04:47:45 +08:00
if (calledMethod == null)
continue;
if (possiblyUnusedMethods.ContainsKey(calledMethod))
notUnusedStack.Push(calledMethod);
}
}
}
}