Skip to content

Commit

Permalink
Fix porting mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Nov 5, 2012
1 parent ea00186 commit 6d45a34
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 22 deletions.
11 changes: 7 additions & 4 deletions AssemblyData/methodsrewriter/AssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,23 @@ void initTypes() {
}

TypeResolver getTypeResolver(ITypeDefOrRef typeRef) {
var key = typeRef.Namespace + "." + typeRef.TypeName;
if (typeRef == null)
return null;
var scopeType = typeRef.ScopeType;
var key = scopeType.Namespace + "." + scopeType.TypeName;
List<TypeResolver> list;
if (!types.TryGetValue(key, out list))
return null;

if (typeRef is TypeDef) {
if (scopeType is TypeDef) {
foreach (var resolver in list) {
if (resolver.type.MetadataToken == typeRef.MDToken.Raw)
if (resolver.type.MetadataToken == scopeType.MDToken.Raw)
return resolver;
}
}

foreach (var resolver in list) {
if (ResolverUtils.compareTypes(resolver.type, typeRef))
if (ResolverUtils.compareTypes(resolver.type, scopeType))
return resolver;
}

Expand Down
10 changes: 5 additions & 5 deletions AssemblyData/methodsrewriter/MethodsRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ void update(Block block, NewMethodInfo currentMethodInfo) {

for (int j = mparams.Count - 1; j >= 0; j--) {
var argType = mparams[j];
if (argType.ElementType == ElementType.ValueType)
block.insert(n++, Instruction.Create(OpCodes.Box, ((ValueTypeSig)argType).TypeDefOrRef));
if (argType.RemovePinnedAndModifiers().IsValueType)
block.insert(n++, Instruction.Create(OpCodes.Box, ((TypeDefOrRefSig)argType).TypeDefOrRef));
block.insert(n++, create(OpCodes.Stloc, new Operand(Operand.Type.TempObj)));
block.insert(n++, create(OpCodes.Ldloc, new Operand(Operand.Type.TempObjArray)));
block.insert(n++, Instruction.Create(OpCodes.Ldc_I4, j));
Expand All @@ -310,14 +310,14 @@ void update(Block block, NewMethodInfo currentMethodInfo) {
block.insert(n++, Instruction.Create(OpCodes.Ldc_I4, j));
block.insert(n++, Instruction.Create(OpCodes.Ldelem_Ref));
var argType = mparams[j];
if (argType.ElementType == ElementType.ValueType)
block.insert(n++, Instruction.Create(OpCodes.Unbox_Any, ((ValueTypeSig)argType).TypeDefOrRef));
if (argType.RemovePinnedAndModifiers().IsValueType)
block.insert(n++, Instruction.Create(OpCodes.Unbox_Any, ((TypeDefOrRefSig)argType).TypeDefOrRef));
else {
// Don't cast it to its correct type. This will sometimes cause
// an exception in some EF obfuscated assembly since we'll be
// trying to cast a System.Reflection.AssemblyName type to some
// other type.
// block.insert(n++, Instruction.Create(OpCodes.Castclass, argType));
// block.insert(n++, Instruction.Create(OpCodes.Castclass, argType.ToTypeDefOrRef()));
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions AssemblyData/methodsrewriter/Resolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ public static MField getField(IField fieldReference) {
public static object getRtObject(ITokenOperand memberRef) {
if (memberRef == null)
return null;
else if (memberRef is ITypeDefOrRef)
return getRtType((ITypeDefOrRef)memberRef);
else if (memberRef is IField)
return getRtField((IField)memberRef);
else if (memberRef is IMethod)
return getRtMethod((IMethod)memberRef);
var tdr = memberRef as ITypeDefOrRef;
if (tdr != null)
return getRtType(tdr);
var field = memberRef as IField;
if (field != null && field.FieldSig != null)
return getRtField(field);
var method = memberRef as IMethod;
if (method != null && method.MethodSig != null)
return getRtMethod(method);

throw new ApplicationException(string.Format("Unknown MemberReference: {0}", memberRef));
}
Expand Down Expand Up @@ -144,9 +147,7 @@ static Type resolve(IType typeRef) {
var resolvedType = resolver.resolve(scopeType);
if (resolvedType != null)
return fixType(typeRef, resolvedType);
var tdr = typeRef as ITypeDefOrRef;
uint token = tdr == null ? 0 : tdr.MDToken.Raw;
throw new ApplicationException(string.Format("Could not resolve type {0} ({1:X8}) in assembly {2}", typeRef, token, resolver));
throw new ApplicationException(string.Format("Could not resolve type {0} ({1:X8}) in assembly {2}", typeRef, typeRef.MDToken.Raw, resolver));
}

static FieldInfo resolve(IField fieldRef) {
Expand All @@ -171,7 +172,7 @@ static MethodBase resolve(IMethod methodRef) {

static Type fixType(IType typeRef, Type type) {
var sig = typeRef as TypeSig;
if (sig != null) {
if (sig == null) {
var ts = typeRef as TypeSpec;
if (ts != null)
sig = ts.TypeSig;
Expand Down
2 changes: 1 addition & 1 deletion de4dot.code/deobfuscators/TypesRestorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ public TypesRestorer(ModuleDef module)
protected override bool isValidType(IGenericParameterProvider gpp, TypeSig type) {
if (type == null)
return false;
if (type.ElementType == ElementType.ValueType)
if (type.IsValueType)
return false;
if (type.ElementType == ElementType.Object)
return false;
Expand Down
2 changes: 1 addition & 1 deletion de4dot.code/renamer/TypeNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public string create(TypeSig typeRef) {
if (typeNames.TryGetValue(typeFullName, out nc))
return nc.create();

var fullName = elementType.FullName;
var fullName = elementType == null ? typeRef.FullName : elementType.FullName;
string shortName;
var dict = prefix == "" ? fullNameToShortName : fullNameToShortNamePrefix;
if (!dict.TryGetValue(fullName, out shortName)) {
Expand Down
2 changes: 1 addition & 1 deletion dot10
Submodule dot10 updated from 14a78f to 83714e

0 comments on commit 6d45a34

Please sign in to comment.