diff --git a/blocks/MemberDefDict.cs b/blocks/MemberDefDict.cs index 5f6d8059..5fbb8725 100644 --- a/blocks/MemberDefDict.cs +++ b/blocks/MemberDefDict.cs @@ -123,7 +123,7 @@ namespace de4dot.blocks { return new ScopeAndTokenKey(fieldDef); } - protected abstract IFieldReferenceKey getReferenceKey(IField fieldRef); + internal abstract IFieldReferenceKey getReferenceKey(IField fieldRef); public TValue find(IField fieldRef) { TValue value; @@ -160,7 +160,7 @@ namespace de4dot.blocks { // Order: public, family, assembly, private static int[] accessibilityOrder = new int[8] { - 60, // CompilerControlled + 60, // PrivateScope 50, // Private 40, // FamANDAssem 30, // Assembly @@ -182,13 +182,13 @@ namespace de4dot.blocks { } public class FieldDefinitionDict : FieldDefinitionDictBase { - protected override IFieldReferenceKey getReferenceKey(IField fieldRef) { + internal override IFieldReferenceKey getReferenceKey(IField fieldRef) { return new FieldReferenceKey(fieldRef); } } public class FieldDefinitionAndDeclaringTypeDict : FieldDefinitionDictBase { - protected override IFieldReferenceKey getReferenceKey(IField fieldRef) { + internal override IFieldReferenceKey getReferenceKey(IField fieldRef) { return new FieldReferenceAndDeclaringTypeKey(fieldRef); } } @@ -215,7 +215,7 @@ namespace de4dot.blocks { return new ScopeAndTokenKey(methodDef); } - protected abstract IMethodReferenceKey getReferenceKey(IMethod methodRef); + internal abstract IMethodReferenceKey getReferenceKey(IMethod methodRef); public TValue find(IMethod methodRef) { TValue value; @@ -252,7 +252,7 @@ namespace de4dot.blocks { // Order: public, family, assembly, private static int[] accessibilityOrder = new int[8] { - 60, // CompilerControlled + 60, // PrivateScope 50, // Private 40, // FamANDAssem 30, // Assembly @@ -274,13 +274,13 @@ namespace de4dot.blocks { } public class MethodDefinitionDict : MethodDefinitionDictBase { - protected override IMethodReferenceKey getReferenceKey(IMethod methodRef) { + internal override IMethodReferenceKey getReferenceKey(IMethod methodRef) { return new MethodReferenceKey(methodRef); } } public class MethodDefinitionAndDeclaringTypeDict : MethodDefinitionDictBase { - protected override IMethodReferenceKey getReferenceKey(IMethod methodRef) { + internal override IMethodReferenceKey getReferenceKey(IMethod methodRef) { return new MethodReferenceAndDeclaringTypeKey(methodRef); } } @@ -306,7 +306,7 @@ namespace de4dot.blocks { return new ScopeAndTokenKey(eventReference); } - protected abstract IEventReferenceKey getReferenceKey(EventDef eventRef); + internal abstract IEventReferenceKey getReferenceKey(EventDef eventRef); public TValue find(EventDef eventRef) { TValue value; @@ -340,13 +340,13 @@ namespace de4dot.blocks { } public class EventDefinitionDict : EventDefinitionDictBase { - protected override IEventReferenceKey getReferenceKey(EventDef eventRef) { + internal override IEventReferenceKey getReferenceKey(EventDef eventRef) { return new EventReferenceKey(eventRef); } } public class EventDefinitionAndDeclaringTypeDict : EventDefinitionDictBase { - protected override IEventReferenceKey getReferenceKey(EventDef eventRef) { + internal override IEventReferenceKey getReferenceKey(EventDef eventRef) { return new EventReferenceAndDeclaringTypeKey(eventRef); } } @@ -372,7 +372,7 @@ namespace de4dot.blocks { return new ScopeAndTokenKey(propertyReference); } - protected abstract IPropertyReferenceKey getReferenceKey(PropertyDef propertyReference); + internal abstract IPropertyReferenceKey getReferenceKey(PropertyDef propertyReference); public TValue find(PropertyDef propRef) { TValue value; @@ -406,18 +406,18 @@ namespace de4dot.blocks { } public class PropertyDefinitionDict : PropertyDefinitionDictBase { - protected override IPropertyReferenceKey getReferenceKey(PropertyDef propRef) { + internal override IPropertyReferenceKey getReferenceKey(PropertyDef propRef) { return new PropertyReferenceKey(propRef); } } public class PropertyDefinitionAndDeclaringTypeDict : PropertyDefinitionDictBase { - protected override IPropertyReferenceKey getReferenceKey(PropertyDef propRef) { + internal override IPropertyReferenceKey getReferenceKey(PropertyDef propRef) { return new PropertyReferenceAndDeclaringTypeKey(propRef); } } - public class ScopeAndTokenKey { + sealed class ScopeAndTokenKey { readonly IScope scope; readonly uint token; @@ -502,23 +502,23 @@ namespace de4dot.blocks { } } - public interface IFieldReferenceKey { + interface IFieldReferenceKey { IField FieldReference { get; } } - public interface IMethodReferenceKey { + interface IMethodReferenceKey { IMethod MethodReference { get; } } - public interface IEventReferenceKey { + interface IEventReferenceKey { EventDef EventDef { get; } } - public interface IPropertyReferenceKey { + interface IPropertyReferenceKey { PropertyDef PropertyDef { get; } } - public class FieldReferenceKey : IFieldReferenceKey { + sealed class FieldReferenceKey : IFieldReferenceKey { readonly IField fieldRef; public IField FieldReference { @@ -545,7 +545,7 @@ namespace de4dot.blocks { } } - public class MethodReferenceKey : IMethodReferenceKey { + sealed class MethodReferenceKey : IMethodReferenceKey { readonly IMethod methodRef; public IMethod MethodReference { @@ -572,7 +572,7 @@ namespace de4dot.blocks { } } - public class FieldReferenceAndDeclaringTypeKey : IFieldReferenceKey { + sealed class FieldReferenceAndDeclaringTypeKey : IFieldReferenceKey { readonly IField fieldRef; public IField FieldReference { @@ -599,7 +599,7 @@ namespace de4dot.blocks { } } - public class MethodReferenceAndDeclaringTypeKey : IMethodReferenceKey { + sealed class MethodReferenceAndDeclaringTypeKey : IMethodReferenceKey { readonly IMethod methodRef; public IMethod MethodReference { @@ -626,7 +626,7 @@ namespace de4dot.blocks { } } - public class EventReferenceKey : IEventReferenceKey { + sealed class EventReferenceKey : IEventReferenceKey { readonly EventDef eventRef; public EventDef EventDef { @@ -653,7 +653,7 @@ namespace de4dot.blocks { } } - public class EventReferenceAndDeclaringTypeKey : IEventReferenceKey { + sealed class EventReferenceAndDeclaringTypeKey : IEventReferenceKey { readonly EventDef eventRef; public EventDef EventDef { @@ -680,7 +680,7 @@ namespace de4dot.blocks { } } - public class PropertyReferenceKey : IPropertyReferenceKey { + sealed class PropertyReferenceKey : IPropertyReferenceKey { readonly PropertyDef propRef; public PropertyDef PropertyDef { @@ -707,7 +707,7 @@ namespace de4dot.blocks { } } - public class PropertyReferenceAndDeclaringTypeKey : IPropertyReferenceKey { + sealed class PropertyReferenceAndDeclaringTypeKey : IPropertyReferenceKey { readonly PropertyDef propRef; public PropertyDef PropertyDef { diff --git a/de4dot.code/deobfuscators/TypesRestorer.cs b/de4dot.code/deobfuscators/TypesRestorer.cs index 51ddcdb8..428bc28f 100644 --- a/de4dot.code/deobfuscators/TypesRestorer.cs +++ b/de4dot.code/deobfuscators/TypesRestorer.cs @@ -32,7 +32,7 @@ namespace de4dot.code.deobfuscators { List allMethods; Dictionary> argInfos = new Dictionary>(); TypeInfo methodReturnInfo; - Dictionary> fieldWrites = new Dictionary>(); + Dictionary> fieldWrites = new Dictionary>(FieldEqualityComparer.CompareDeclaringTypes); Dictionary updatedMethods = new Dictionary(); Dictionary updatedFields = new Dictionary(); @@ -164,8 +164,7 @@ namespace de4dot.code.deobfuscators { if (!isUnknownType(field)) continue; - var key = new FieldReferenceAndDeclaringTypeKey(field); - fieldWrites[key] = new TypeInfo(field); + fieldWrites[field] = new TypeInfo(field); } } } @@ -487,7 +486,7 @@ namespace de4dot.code.deobfuscators { field = instr.Operand as IField; if (field == null) continue; - if (!fieldWrites.TryGetValue(new FieldReferenceAndDeclaringTypeKey(field), out info)) + if (!fieldWrites.TryGetValue(field, out info)) continue; bool wasNewobj; fieldType = getLoadedType(info.arg.DeclaringType, method, instructions, i, out wasNewobj); @@ -516,7 +515,7 @@ namespace de4dot.code.deobfuscators { field = pushInstr.Operand as IField; if (field == null) continue; - if (!fieldWrites.TryGetValue(new FieldReferenceAndDeclaringTypeKey(field), out info)) + if (!fieldWrites.TryGetValue(field, out info)) continue; fieldType = calledMethodArgs[calledMethodArgs.Count - 1 - j]; if (!isValidType(info.arg.DeclaringType, fieldType)) @@ -542,7 +541,7 @@ namespace de4dot.code.deobfuscators { } } foreach (var field in removeThese) - fieldWrites.Remove(new FieldReferenceAndDeclaringTypeKey(field)); + fieldWrites.Remove(field); return changed; } diff --git a/de4dot.code/renamer/asmmodules/TypeDef.cs b/de4dot.code/renamer/asmmodules/TypeDef.cs index def42bb8..90022474 100644 --- a/de4dot.code/renamer/asmmodules/TypeDef.cs +++ b/de4dot.code/renamer/asmmodules/TypeDef.cs @@ -24,16 +24,34 @@ using de4dot.blocks; namespace de4dot.code.renamer.asmmodules { //TODO: - class TypeReferenceInstance { - public static TypeRef make(ITypeDefOrRef type, GenericInstSig git) { - return null; + class TypeRefInstantiator { + public static ITypeDefOrRef Create(ITypeDefOrRef type, GenericInstSig git) { + if (git == null) + return type; + return Create(type, git.GenericArguments); + } + + public static ITypeDefOrRef Create(ITypeDefOrRef type, IList genericArgs) { + if (genericArgs == null || genericArgs.Count == 0) + return type; + + return type;//TODO: } } //TODO: - class MethodReferenceInstance { - public static MemberRef make(IMethod method, GenericInstSig git) { - return null; + class MethodRefInstantiator { + public static IMethod Create(IMethod method, GenericInstSig git) { + if (git == null) + return method; + return Create(method, git.GenericArguments); + } + + public static IMethod Create(IMethod method, IList 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) { - this.typeReference = TypeReferenceInstance.make(other.typeReference, git); + this.typeReference = TypeRefInstantiator.Create(other.typeReference, git); this.typeDef = other.typeDef; } @@ -107,7 +125,7 @@ namespace de4dot.code.renamer.asmmodules { public void initializeFrom(MethodInstances other, GenericInstSig git) { foreach (var list in other.methodInstances.Values) { 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)); } } @@ -527,7 +545,7 @@ namespace de4dot.code.renamer.asmmodules { //--- virtual functions. // Done. See initializeAllInterfaces(). - var methodsDict = new Dictionary(); + var methodsDict = new Dictionary(MethodEqualityComparer.DontCompareDeclaringTypes); //--- * If this class explicitly specifies that it implements the interface (i.e., the //--- 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()) { if (!method.isPublic() || !method.isVirtual() || !method.isNewSlot()) continue; - methodsDict[new MethodReferenceKey(method.MethodDef)] = method; + methodsDict[method.MethodDef] = method; } foreach (var ifaceInfo in interfaces) { @@ -550,10 +568,9 @@ namespace de4dot.code.renamer.asmmodules { var ifaceMethod = methodInst.origMethodDef; if (!ifaceMethod.isVirtual()) continue; - var ifaceMethodReference = MethodReferenceInstance.make(methodInst.methodReference, ifaceInfo.typeReference.ToGenericInstSig()); + var ifaceMethodReference = MethodRefInstantiator.Create(methodInst.methodReference, ifaceInfo.typeReference.ToGenericInstSig()); MMethodDef classMethod; - var key = new MethodReferenceKey(ifaceMethodReference); - if (!methodsDict.TryGetValue(key, out classMethod)) + if (!methodsDict.TryGetValue(ifaceMethodReference, out classMethod)) continue; 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. if (!classMethod.origMethodDef.isPublic()) continue; - methodsDict[new MethodReferenceKey(classMethod.methodReference)] = classMethod.origMethodDef; + methodsDict[classMethod.methodReference] = classMethod.origMethodDef; break; } } @@ -585,10 +602,9 @@ namespace de4dot.code.renamer.asmmodules { var ifaceMethod = methodsList[0].origMethodDef; if (!ifaceMethod.isVirtual()) continue; - var ifaceMethodRef = MethodReferenceInstance.make(ifaceMethod.MethodDef, ifaceInfo.typeReference.ToGenericInstSig()); + var ifaceMethodRef = MethodRefInstantiator.Create(ifaceMethod.MethodDef, ifaceInfo.typeReference.ToGenericInstSig()); MMethodDef classMethod; - var key = new MethodReferenceKey(ifaceMethodRef); - if (!methodsDict.TryGetValue(key, out classMethod)) + if (!methodsDict.TryGetValue(ifaceMethodRef, out classMethod)) continue; interfaceMethodInfos.addMethodIfEmpty(ifaceInfo, ifaceMethod, classMethod); }