Port some code

This commit is contained in:
de4dot 2012-11-02 22:53:24 +01:00
parent 89cd55a071
commit 9b6c698dc1
3 changed files with 65 additions and 50 deletions

View File

@ -123,7 +123,7 @@ namespace de4dot.blocks {
return new ScopeAndTokenKey(fieldDef); return new ScopeAndTokenKey(fieldDef);
} }
protected abstract IFieldReferenceKey getReferenceKey(IField fieldRef); internal abstract IFieldReferenceKey getReferenceKey(IField fieldRef);
public TValue find(IField fieldRef) { public TValue find(IField fieldRef) {
TValue value; TValue value;
@ -160,7 +160,7 @@ namespace de4dot.blocks {
// Order: public, family, assembly, private // Order: public, family, assembly, private
static int[] accessibilityOrder = new int[8] { static int[] accessibilityOrder = new int[8] {
60, // CompilerControlled 60, // PrivateScope
50, // Private 50, // Private
40, // FamANDAssem 40, // FamANDAssem
30, // Assembly 30, // Assembly
@ -182,13 +182,13 @@ namespace de4dot.blocks {
} }
public class FieldDefinitionDict<TValue> : FieldDefinitionDictBase<TValue> { public class FieldDefinitionDict<TValue> : FieldDefinitionDictBase<TValue> {
protected override IFieldReferenceKey getReferenceKey(IField fieldRef) { internal override IFieldReferenceKey getReferenceKey(IField fieldRef) {
return new FieldReferenceKey(fieldRef); return new FieldReferenceKey(fieldRef);
} }
} }
public class FieldDefinitionAndDeclaringTypeDict<TValue> : FieldDefinitionDictBase<TValue> { public class FieldDefinitionAndDeclaringTypeDict<TValue> : FieldDefinitionDictBase<TValue> {
protected override IFieldReferenceKey getReferenceKey(IField fieldRef) { internal override IFieldReferenceKey getReferenceKey(IField fieldRef) {
return new FieldReferenceAndDeclaringTypeKey(fieldRef); return new FieldReferenceAndDeclaringTypeKey(fieldRef);
} }
} }
@ -215,7 +215,7 @@ namespace de4dot.blocks {
return new ScopeAndTokenKey(methodDef); return new ScopeAndTokenKey(methodDef);
} }
protected abstract IMethodReferenceKey getReferenceKey(IMethod methodRef); internal abstract IMethodReferenceKey getReferenceKey(IMethod methodRef);
public TValue find(IMethod methodRef) { public TValue find(IMethod methodRef) {
TValue value; TValue value;
@ -252,7 +252,7 @@ namespace de4dot.blocks {
// Order: public, family, assembly, private // Order: public, family, assembly, private
static int[] accessibilityOrder = new int[8] { static int[] accessibilityOrder = new int[8] {
60, // CompilerControlled 60, // PrivateScope
50, // Private 50, // Private
40, // FamANDAssem 40, // FamANDAssem
30, // Assembly 30, // Assembly
@ -274,13 +274,13 @@ namespace de4dot.blocks {
} }
public class MethodDefinitionDict<TValue> : MethodDefinitionDictBase<TValue> { public class MethodDefinitionDict<TValue> : MethodDefinitionDictBase<TValue> {
protected override IMethodReferenceKey getReferenceKey(IMethod methodRef) { internal override IMethodReferenceKey getReferenceKey(IMethod methodRef) {
return new MethodReferenceKey(methodRef); return new MethodReferenceKey(methodRef);
} }
} }
public class MethodDefinitionAndDeclaringTypeDict<TValue> : MethodDefinitionDictBase<TValue> { public class MethodDefinitionAndDeclaringTypeDict<TValue> : MethodDefinitionDictBase<TValue> {
protected override IMethodReferenceKey getReferenceKey(IMethod methodRef) { internal override IMethodReferenceKey getReferenceKey(IMethod methodRef) {
return new MethodReferenceAndDeclaringTypeKey(methodRef); return new MethodReferenceAndDeclaringTypeKey(methodRef);
} }
} }
@ -306,7 +306,7 @@ namespace de4dot.blocks {
return new ScopeAndTokenKey(eventReference); return new ScopeAndTokenKey(eventReference);
} }
protected abstract IEventReferenceKey getReferenceKey(EventDef eventRef); internal abstract IEventReferenceKey getReferenceKey(EventDef eventRef);
public TValue find(EventDef eventRef) { public TValue find(EventDef eventRef) {
TValue value; TValue value;
@ -340,13 +340,13 @@ namespace de4dot.blocks {
} }
public class EventDefinitionDict<TValue> : EventDefinitionDictBase<TValue> { public class EventDefinitionDict<TValue> : EventDefinitionDictBase<TValue> {
protected override IEventReferenceKey getReferenceKey(EventDef eventRef) { internal override IEventReferenceKey getReferenceKey(EventDef eventRef) {
return new EventReferenceKey(eventRef); return new EventReferenceKey(eventRef);
} }
} }
public class EventDefinitionAndDeclaringTypeDict<TValue> : EventDefinitionDictBase<TValue> { public class EventDefinitionAndDeclaringTypeDict<TValue> : EventDefinitionDictBase<TValue> {
protected override IEventReferenceKey getReferenceKey(EventDef eventRef) { internal override IEventReferenceKey getReferenceKey(EventDef eventRef) {
return new EventReferenceAndDeclaringTypeKey(eventRef); return new EventReferenceAndDeclaringTypeKey(eventRef);
} }
} }
@ -372,7 +372,7 @@ namespace de4dot.blocks {
return new ScopeAndTokenKey(propertyReference); return new ScopeAndTokenKey(propertyReference);
} }
protected abstract IPropertyReferenceKey getReferenceKey(PropertyDef propertyReference); internal abstract IPropertyReferenceKey getReferenceKey(PropertyDef propertyReference);
public TValue find(PropertyDef propRef) { public TValue find(PropertyDef propRef) {
TValue value; TValue value;
@ -406,18 +406,18 @@ namespace de4dot.blocks {
} }
public class PropertyDefinitionDict<TValue> : PropertyDefinitionDictBase<TValue> { public class PropertyDefinitionDict<TValue> : PropertyDefinitionDictBase<TValue> {
protected override IPropertyReferenceKey getReferenceKey(PropertyDef propRef) { internal override IPropertyReferenceKey getReferenceKey(PropertyDef propRef) {
return new PropertyReferenceKey(propRef); return new PropertyReferenceKey(propRef);
} }
} }
public class PropertyDefinitionAndDeclaringTypeDict<TValue> : PropertyDefinitionDictBase<TValue> { public class PropertyDefinitionAndDeclaringTypeDict<TValue> : PropertyDefinitionDictBase<TValue> {
protected override IPropertyReferenceKey getReferenceKey(PropertyDef propRef) { internal override IPropertyReferenceKey getReferenceKey(PropertyDef propRef) {
return new PropertyReferenceAndDeclaringTypeKey(propRef); return new PropertyReferenceAndDeclaringTypeKey(propRef);
} }
} }
public class ScopeAndTokenKey { sealed class ScopeAndTokenKey {
readonly IScope scope; readonly IScope scope;
readonly uint token; readonly uint token;
@ -502,23 +502,23 @@ namespace de4dot.blocks {
} }
} }
public interface IFieldReferenceKey { interface IFieldReferenceKey {
IField FieldReference { get; } IField FieldReference { get; }
} }
public interface IMethodReferenceKey { interface IMethodReferenceKey {
IMethod MethodReference { get; } IMethod MethodReference { get; }
} }
public interface IEventReferenceKey { interface IEventReferenceKey {
EventDef EventDef { get; } EventDef EventDef { get; }
} }
public interface IPropertyReferenceKey { interface IPropertyReferenceKey {
PropertyDef PropertyDef { get; } PropertyDef PropertyDef { get; }
} }
public class FieldReferenceKey : IFieldReferenceKey { sealed class FieldReferenceKey : IFieldReferenceKey {
readonly IField fieldRef; readonly IField fieldRef;
public IField FieldReference { public IField FieldReference {
@ -545,7 +545,7 @@ namespace de4dot.blocks {
} }
} }
public class MethodReferenceKey : IMethodReferenceKey { sealed class MethodReferenceKey : IMethodReferenceKey {
readonly IMethod methodRef; readonly IMethod methodRef;
public IMethod MethodReference { public IMethod MethodReference {
@ -572,7 +572,7 @@ namespace de4dot.blocks {
} }
} }
public class FieldReferenceAndDeclaringTypeKey : IFieldReferenceKey { sealed class FieldReferenceAndDeclaringTypeKey : IFieldReferenceKey {
readonly IField fieldRef; readonly IField fieldRef;
public IField FieldReference { public IField FieldReference {
@ -599,7 +599,7 @@ namespace de4dot.blocks {
} }
} }
public class MethodReferenceAndDeclaringTypeKey : IMethodReferenceKey { sealed class MethodReferenceAndDeclaringTypeKey : IMethodReferenceKey {
readonly IMethod methodRef; readonly IMethod methodRef;
public IMethod MethodReference { public IMethod MethodReference {
@ -626,7 +626,7 @@ namespace de4dot.blocks {
} }
} }
public class EventReferenceKey : IEventReferenceKey { sealed class EventReferenceKey : IEventReferenceKey {
readonly EventDef eventRef; readonly EventDef eventRef;
public EventDef EventDef { public EventDef EventDef {
@ -653,7 +653,7 @@ namespace de4dot.blocks {
} }
} }
public class EventReferenceAndDeclaringTypeKey : IEventReferenceKey { sealed class EventReferenceAndDeclaringTypeKey : IEventReferenceKey {
readonly EventDef eventRef; readonly EventDef eventRef;
public EventDef EventDef { public EventDef EventDef {
@ -680,7 +680,7 @@ namespace de4dot.blocks {
} }
} }
public class PropertyReferenceKey : IPropertyReferenceKey { sealed class PropertyReferenceKey : IPropertyReferenceKey {
readonly PropertyDef propRef; readonly PropertyDef propRef;
public PropertyDef PropertyDef { public PropertyDef PropertyDef {
@ -707,7 +707,7 @@ namespace de4dot.blocks {
} }
} }
public class PropertyReferenceAndDeclaringTypeKey : IPropertyReferenceKey { sealed class PropertyReferenceAndDeclaringTypeKey : IPropertyReferenceKey {
readonly PropertyDef propRef; readonly PropertyDef propRef;
public PropertyDef PropertyDef { public PropertyDef PropertyDef {

View File

@ -32,7 +32,7 @@ namespace de4dot.code.deobfuscators {
List<MethodDef> allMethods; List<MethodDef> allMethods;
Dictionary<Parameter, TypeInfo<Parameter>> argInfos = new Dictionary<Parameter, TypeInfo<Parameter>>(); Dictionary<Parameter, TypeInfo<Parameter>> argInfos = new Dictionary<Parameter, TypeInfo<Parameter>>();
TypeInfo<Parameter> methodReturnInfo; TypeInfo<Parameter> methodReturnInfo;
Dictionary<FieldReferenceAndDeclaringTypeKey, TypeInfo<FieldDef>> fieldWrites = new Dictionary<FieldReferenceAndDeclaringTypeKey, TypeInfo<FieldDef>>(); Dictionary<IField, TypeInfo<FieldDef>> fieldWrites = new Dictionary<IField, TypeInfo<FieldDef>>(FieldEqualityComparer.CompareDeclaringTypes);
Dictionary<int, UpdatedMethod> updatedMethods = new Dictionary<int, UpdatedMethod>(); Dictionary<int, UpdatedMethod> updatedMethods = new Dictionary<int, UpdatedMethod>();
Dictionary<int, UpdatedField> updatedFields = new Dictionary<int, UpdatedField>(); Dictionary<int, UpdatedField> updatedFields = new Dictionary<int, UpdatedField>();
@ -164,8 +164,7 @@ namespace de4dot.code.deobfuscators {
if (!isUnknownType(field)) if (!isUnknownType(field))
continue; continue;
var key = new FieldReferenceAndDeclaringTypeKey(field); fieldWrites[field] = new TypeInfo<FieldDef>(field);
fieldWrites[key] = new TypeInfo<FieldDef>(field);
} }
} }
} }
@ -487,7 +486,7 @@ namespace de4dot.code.deobfuscators {
field = instr.Operand as IField; field = instr.Operand as IField;
if (field == null) if (field == null)
continue; continue;
if (!fieldWrites.TryGetValue(new FieldReferenceAndDeclaringTypeKey(field), out info)) if (!fieldWrites.TryGetValue(field, out info))
continue; continue;
bool wasNewobj; bool wasNewobj;
fieldType = getLoadedType(info.arg.DeclaringType, method, instructions, i, out wasNewobj); fieldType = getLoadedType(info.arg.DeclaringType, method, instructions, i, out wasNewobj);
@ -516,7 +515,7 @@ namespace de4dot.code.deobfuscators {
field = pushInstr.Operand as IField; field = pushInstr.Operand as IField;
if (field == null) if (field == null)
continue; continue;
if (!fieldWrites.TryGetValue(new FieldReferenceAndDeclaringTypeKey(field), out info)) if (!fieldWrites.TryGetValue(field, out info))
continue; continue;
fieldType = calledMethodArgs[calledMethodArgs.Count - 1 - j]; fieldType = calledMethodArgs[calledMethodArgs.Count - 1 - j];
if (!isValidType(info.arg.DeclaringType, fieldType)) if (!isValidType(info.arg.DeclaringType, fieldType))
@ -542,7 +541,7 @@ namespace de4dot.code.deobfuscators {
} }
} }
foreach (var field in removeThese) foreach (var field in removeThese)
fieldWrites.Remove(new FieldReferenceAndDeclaringTypeKey(field)); fieldWrites.Remove(field);
return changed; return changed;
} }

View File

@ -24,16 +24,34 @@ using de4dot.blocks;
namespace de4dot.code.renamer.asmmodules { namespace de4dot.code.renamer.asmmodules {
//TODO: //TODO:
class TypeReferenceInstance { class TypeRefInstantiator {
public static TypeRef make(ITypeDefOrRef type, GenericInstSig git) { public static ITypeDefOrRef Create(ITypeDefOrRef type, GenericInstSig git) {
return null; if (git == null)
return type;
return Create(type, git.GenericArguments);
}
public static ITypeDefOrRef Create(ITypeDefOrRef type, IList<TypeSig> genericArgs) {
if (genericArgs == null || genericArgs.Count == 0)
return type;
return type;//TODO:
} }
} }
//TODO: //TODO:
class MethodReferenceInstance { class MethodRefInstantiator {
public static MemberRef make(IMethod method, GenericInstSig git) { public static IMethod Create(IMethod method, GenericInstSig git) {
return null; if (git == null)
return method;
return Create(method, git.GenericArguments);
}
public static IMethod Create(IMethod method, IList<TypeSig> genericArgs) {
if (genericArgs == null || genericArgs.Count == 0)
return method;
return method;//TODO:
} }
} }
@ -46,7 +64,7 @@ namespace de4dot.code.renamer.asmmodules {
} }
public TypeInfo(TypeInfo other, GenericInstSig git) { public TypeInfo(TypeInfo other, GenericInstSig git) {
this.typeReference = TypeReferenceInstance.make(other.typeReference, git); this.typeReference = TypeRefInstantiator.Create(other.typeReference, git);
this.typeDef = other.typeDef; this.typeDef = other.typeDef;
} }
@ -107,7 +125,7 @@ namespace de4dot.code.renamer.asmmodules {
public void initializeFrom(MethodInstances other, GenericInstSig git) { public void initializeFrom(MethodInstances other, GenericInstSig git) {
foreach (var list in other.methodInstances.Values) { foreach (var list in other.methodInstances.Values) {
foreach (var methodInst in list) { foreach (var methodInst in list) {
MemberRef newMethod = MethodReferenceInstance.make(methodInst.methodReference, git); var newMethod = MethodRefInstantiator.Create(methodInst.methodReference, git);
add(new MethodInst(methodInst.origMethodDef, newMethod)); add(new MethodInst(methodInst.origMethodDef, newMethod));
} }
} }
@ -527,7 +545,7 @@ namespace de4dot.code.renamer.asmmodules {
//--- virtual functions. //--- virtual functions.
// Done. See initializeAllInterfaces(). // Done. See initializeAllInterfaces().
var methodsDict = new Dictionary<MethodReferenceKey, MMethodDef>(); var methodsDict = new Dictionary<IMethod, MMethodDef>(MethodEqualityComparer.DontCompareDeclaringTypes);
//--- * If this class explicitly specifies that it implements the interface (i.e., the //--- * If this class explicitly specifies that it implements the interface (i.e., the
//--- interfaces that appear in this class InterfaceImpl table, §22.23) //--- interfaces that appear in this class InterfaceImpl table, §22.23)
@ -539,7 +557,7 @@ namespace de4dot.code.renamer.asmmodules {
foreach (var method in methods.getValues()) { foreach (var method in methods.getValues()) {
if (!method.isPublic() || !method.isVirtual() || !method.isNewSlot()) if (!method.isPublic() || !method.isVirtual() || !method.isNewSlot())
continue; continue;
methodsDict[new MethodReferenceKey(method.MethodDef)] = method; methodsDict[method.MethodDef] = method;
} }
foreach (var ifaceInfo in interfaces) { foreach (var ifaceInfo in interfaces) {
@ -550,10 +568,9 @@ namespace de4dot.code.renamer.asmmodules {
var ifaceMethod = methodInst.origMethodDef; var ifaceMethod = methodInst.origMethodDef;
if (!ifaceMethod.isVirtual()) if (!ifaceMethod.isVirtual())
continue; continue;
var ifaceMethodReference = MethodReferenceInstance.make(methodInst.methodReference, ifaceInfo.typeReference.ToGenericInstSig()); var ifaceMethodReference = MethodRefInstantiator.Create(methodInst.methodReference, ifaceInfo.typeReference.ToGenericInstSig());
MMethodDef classMethod; MMethodDef classMethod;
var key = new MethodReferenceKey(ifaceMethodReference); if (!methodsDict.TryGetValue(ifaceMethodReference, out classMethod))
if (!methodsDict.TryGetValue(key, out classMethod))
continue; continue;
interfaceMethodInfos.addMethod(ifaceInfo, ifaceMethod, classMethod); interfaceMethodInfos.addMethod(ifaceInfo, ifaceMethod, classMethod);
} }
@ -574,7 +591,7 @@ namespace de4dot.code.renamer.asmmodules {
// We should allow newslot methods, despite what the official doc says. // We should allow newslot methods, despite what the official doc says.
if (!classMethod.origMethodDef.isPublic()) if (!classMethod.origMethodDef.isPublic())
continue; continue;
methodsDict[new MethodReferenceKey(classMethod.methodReference)] = classMethod.origMethodDef; methodsDict[classMethod.methodReference] = classMethod.origMethodDef;
break; break;
} }
} }
@ -585,10 +602,9 @@ namespace de4dot.code.renamer.asmmodules {
var ifaceMethod = methodsList[0].origMethodDef; var ifaceMethod = methodsList[0].origMethodDef;
if (!ifaceMethod.isVirtual()) if (!ifaceMethod.isVirtual())
continue; continue;
var ifaceMethodRef = MethodReferenceInstance.make(ifaceMethod.MethodDef, ifaceInfo.typeReference.ToGenericInstSig()); var ifaceMethodRef = MethodRefInstantiator.Create(ifaceMethod.MethodDef, ifaceInfo.typeReference.ToGenericInstSig());
MMethodDef classMethod; MMethodDef classMethod;
var key = new MethodReferenceKey(ifaceMethodRef); if (!methodsDict.TryGetValue(ifaceMethodRef, out classMethod))
if (!methodsDict.TryGetValue(key, out classMethod))
continue; continue;
interfaceMethodInfos.addMethodIfEmpty(ifaceInfo, ifaceMethod, classMethod); interfaceMethodInfos.addMethodIfEmpty(ifaceInfo, ifaceMethod, classMethod);
} }