Skip to content

Commit

Permalink
MethodDef.Parameters contains the hidden 'this' param, so add some fi…
Browse files Browse the repository at this point in the history
…xes to old code
  • Loading branch information
wtfsck committed Nov 4, 2012
1 parent 6a8e8dc commit c9f1f80
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 32 deletions.
2 changes: 1 addition & 1 deletion AssemblyData/methodsrewriter/MethodsRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ void update(Block block, NewMethodInfo currentMethodInfo) {
}

static IList<TypeSig> getParameters(MethodDef method) {
var list = new List<TypeSig>(method.Parameters.Count + 1);
var list = new List<TypeSig>(method.Parameters.Count);
for (int i = 0; i < method.Parameters.Count; i++)
list.Add(method.Parameters[i].Type);
return list;
Expand Down
7 changes: 4 additions & 3 deletions de4dot.code/ObfuscatedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,15 +436,16 @@ IEnumerable<int> findMethodTokens(string methodDesc) {
if (methodName != null && methodName != method.Name)
continue;

var sig = method.MethodSig;
if (argsStrings == null) {
if (method.Parameters.Count == 0)
if (sig.Params.Count == 0)
continue;
}
else {
if (argsStrings.Length != method.Parameters.Count)
if (argsStrings.Length != sig.Params.Count)
continue;
for (int i = 0; i < argsStrings.Length; i++) {
if (argsStrings[i] != method.Parameters[i].Type.FullName)
if (argsStrings[i] != sig.Params[i].FullName)
continue;
}
}
Expand Down
2 changes: 2 additions & 0 deletions de4dot.code/deobfuscators/TypesRestorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ void deobfuscateMethod(MethodDef method) {

argInfos.Clear();
foreach (var arg in method.Parameters) {
if (arg.IsHiddenThisParameter)
continue;
if (!isUnknownType(arg))
continue;
argInfos[arg] = new TypeInfo<Parameter>(arg);
Expand Down
14 changes: 7 additions & 7 deletions de4dot.code/renamer/Renamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ void renameMethods(TypeInfo info) {
if (param.IsReturnParameter)
Log.v("RetParam: {0} => {1}", Utils.removeNewlines(paramInfo.oldName), Utils.removeNewlines(paramInfo.newName));
else
Log.v("Param ({0}/{1}): {2} => {3}", param.Index + 1, methodDef.ParamDefs.Count, Utils.removeNewlines(paramInfo.oldName), Utils.removeNewlines(paramInfo.newName));
Log.v("Param ({0}/{1}): {2} => {3}", param.ParameterDefinition.MethodSigIndex + 1, methodDef.MethodDef.MethodSig.Params.Count, Utils.removeNewlines(paramInfo.oldName), Utils.removeNewlines(paramInfo.newName));
}
}

Expand Down Expand Up @@ -600,10 +600,10 @@ MPropertyDef createPropertySetter(string name, MMethodDef propMethod) {
if (propMethod.Property != null)
return null;

var method = propMethod.MethodDef;
if (method.Parameters.Count == 0)
var sig = propMethod.MethodDef.MethodSig;
if (sig.Params.Count == 0)
return null;
var propType = method.Parameters[method.Parameters.Count - 1].Type;
var propType = sig.Params[sig.Params.Count - 1];
var propDef = createProperty(ownerType, name, propType, null, propMethod.MethodDef);
if (propDef == null)
return null;
Expand Down Expand Up @@ -912,7 +912,7 @@ void prepareRenameMemberDefinitions(MethodNameGroups groups) {

void restoreMethodArgs(MethodNameGroups groups) {
foreach (var group in groups.getAllGroups()) {
if (group.Methods[0].ParamDefs.Count == 0)
if (group.Methods[0].VisibleParameterCount == 0)
continue;

var argNames = getValidArgNames(group);
Expand Down Expand Up @@ -949,7 +949,7 @@ string[] getValidArgNames(MethodNameGroup group) {
if (overrideDef == null)
continue;
}
if (overrideDef.ParamDefs.Count != method.ParamDefs.Count)
if (overrideDef.VisibleParameterCount != method.VisibleParameterCount)
continue;
methods.Add(overrideDef);
}
Expand Down Expand Up @@ -1373,7 +1373,7 @@ enum PropertyMethodType {
static PropertyMethodType getPropertyMethodType(MMethodDef method) {
if (DotNetUtils.hasReturnValue(method.MethodDef))
return PropertyMethodType.Getter;
if (method.ParamDefs.Count > 0)
if (method.VisibleParameterCount > 0)
return PropertyMethodType.Setter;
return PropertyMethodType.Other;
}
Expand Down
23 changes: 11 additions & 12 deletions de4dot.code/renamer/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,20 +295,22 @@ public void prepareRenameMethods2() {
void prepareRenameMethodArgs(MMethodDef methodDef) {
VariableNameState newVariableNameState = null;
ParamInfo info;
if (methodDef.ParamDefs.Count > 0) {
if (methodDef.VisibleParameterCount > 0) {
if (isEventHandler(methodDef)) {
info = param(methodDef.ParamDefs[0]);
info = param(methodDef.ParamDefs[methodDef.VisibleParameterBaseIndex]);
if (!info.gotNewName())
info.newName = "sender";

info = param(methodDef.ParamDefs[1]);
info = param(methodDef.ParamDefs[methodDef.VisibleParameterBaseIndex + 1]);
if (!info.gotNewName())
info.newName = "e";
}
else {
newVariableNameState = variableNameState.cloneParamsOnly();
var checker = NameChecker;
foreach (var paramDef in methodDef.ParamDefs) {
if (paramDef.IsHiddenThisParameter)
continue;
info = param(paramDef);
if (info.gotNewName())
continue;
Expand All @@ -329,7 +331,7 @@ void prepareRenameMethodArgs(MMethodDef methodDef) {

if ((methodDef.Property != null && methodDef == methodDef.Property.SetMethod) ||
(methodDef.Event != null && (methodDef == methodDef.Event.AddMethod || methodDef == methodDef.Event.RemoveMethod))) {
if (methodDef.ParamDefs.Count > 0) {
if (methodDef.VisibleParameterCount > 0) {
var paramDef = methodDef.ParamDefs[methodDef.ParamDefs.Count - 1];
param(paramDef).newName = "value";
}
Expand Down Expand Up @@ -403,17 +405,14 @@ string getPinvokeName(MMethodDef methodDef) {
}

static bool isEventHandler(MMethodDef methodDef) {
var md = methodDef.MethodDef;
if (md.Parameters.Count != 2)
return false;
var sig = md.MethodSig;
if (sig == null)
var sig = methodDef.MethodDef.MethodSig;
if (sig == null || sig.Params.Count != 2)
return false;
if (sig.RetType.ElementType != ElementType.Void)
return false;
if (md.Parameters[0].Type.ElementType != ElementType.Object)
if (sig.Params[0].ElementType != ElementType.Object)
return false;
if (!md.Parameters[1].Type.FullName.Contains("EventArgs"))
if (!sig.Params[1].FullName.Contains("EventArgs"))
return false;
return true;
}
Expand Down Expand Up @@ -588,7 +587,7 @@ static IMethod getVbHandler(MethodDef method, out string eventName) {
return null;
if (sig.RetType.ElementType != ElementType.Void)
return null;
if (method.Parameters.Count != 1)
if (sig.Params.Count != 1)
return null;
if (method.CilBody.LocalList.Count != 1)
return null;
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 @@ -67,7 +67,7 @@ public string create(TypeSig typeRef) {

bool isGenericParam(ITypeDefOrRef tdr) {
var ts = tdr as TypeSpec;
if (ts != null)
if (ts == null)
return false;
var sig = ts.TypeSig.RemovePinnedAndModifiers();
return sig is GenericSig;
Expand Down
13 changes: 13 additions & 0 deletions de4dot.code/renamer/asmmodules/MethodDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ class MMethodDef : Ref {
IList<MGenericParamDef> genericParams;
IList<MParamDef> paramDefs = new List<MParamDef>();
MParamDef returnParamDef;
int visibleParamCount;
int visibleBaseIndex;

public MPropertyDef Property { get; set; }
public MEventDef Event { get; set; }

public int VisibleParameterCount {
get { return visibleParamCount; }
}

public int VisibleParameterBaseIndex {
get { return visibleBaseIndex; }
}

public IList<MParamDef> ParamDefs {
get { return paramDefs; }
}
Expand Down Expand Up @@ -56,8 +66,11 @@ public MethodDef MethodDef {
public MMethodDef(MethodDef methodDefinition, MTypeDef owner, int index)
: base(methodDefinition, owner, index) {
genericParams = MGenericParamDef.createGenericParamDefList(MethodDef.GenericParams);
visibleBaseIndex = methodDefinition.MethodSig.HasThis ? 1 : 0;
for (int i = 0; i < methodDefinition.Parameters.Count; i++) {
var param = methodDefinition.Parameters[i];
if (param.IsNormalMethodParameter)
visibleParamCount++;
paramDefs.Add(new MParamDef(param, i));
}
returnParamDef = new MParamDef(methodDefinition.Parameters.ReturnParameter, -1);
Expand Down
4 changes: 4 additions & 0 deletions de4dot.code/renamer/asmmodules/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ public void onTypesRenamed() {
newTypes.add(typeDef);
}
types = newTypes;

bool old = ModuleDefMD.EnableTypeDefFindCache;
ModuleDefMD.EnableTypeDefFindCache = false;
ModuleDefMD.EnableTypeDefFindCache = old;
}

static ITypeDefOrRef getNonGenericTypeReference(ITypeDefOrRef typeRef) {
Expand Down
11 changes: 7 additions & 4 deletions de4dot.code/renamer/asmmodules/Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,30 +377,33 @@ IEnumerable<Module> findModules(ITypeDefOrRef type) {
if (scope == null)
return null;

if (scope.ScopeType == ScopeType.AssemblyRef)
var scopeType = scope.ScopeType;
if (scopeType == ScopeType.AssemblyRef)
return findModules((AssemblyRef)scope);

if (scope.ScopeType == ScopeType.ModuleDef) {
if (scopeType == ScopeType.ModuleDef) {
var modules = findModules((ModuleDef)scope);
if (modules != null)
return modules;
}

if (scope.ScopeType == ScopeType.ModuleRef) {
if (scopeType == ScopeType.ModuleRef) {
var moduleRef = (ModuleRef)scope;
if (moduleRef.Name == type.OwnerModule.Name) {
var modules = findModules(type.OwnerModule);
if (modules != null)
return modules;
}
}

if (scopeType == ScopeType.ModuleRef || scopeType == ScopeType.ModuleDef) {
var asm = type.OwnerModule.Assembly;
if (asm == null)
return null;
var moduleHash = assemblyHash.lookup(asm.FullName);
if (moduleHash == null)
return null;
var module = moduleHash.lookup(moduleRef.Name.String);
var module = moduleHash.lookup(scope.ScopeName);
if (module == null)
return null;
return new List<Module> { module };
Expand Down
5 changes: 4 additions & 1 deletion de4dot.code/renamer/asmmodules/ParamDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class MParamDef {
public Parameter ParameterDefinition { get; set; }
public int Index { get; private set; }
public bool IsReturnParameter {
get { return Index < 0; }
get { return ParameterDefinition.IsReturnTypeParameter; }
}
public bool IsHiddenThisParameter {
get { return ParameterDefinition.IsHiddenThisParameter; }
}

public MParamDef(Parameter parameterDefinition, int index) {
Expand Down
4 changes: 2 additions & 2 deletions de4dot.code/renamer/asmmodules/PropertyDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public bool isVirtual() {
}

public bool isItemProperty() {
if (GetMethod != null && GetMethod.ParamDefs.Count >= 1)
if (GetMethod != null && GetMethod.VisibleParameterCount >= 1)
return true;
if (SetMethod != null && SetMethod.ParamDefs.Count >= 2)
if (SetMethod != null && SetMethod.VisibleParameterCount >= 2)
return true;
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion dot10
Submodule dot10 updated from e532b4 to ebd417

0 comments on commit c9f1f80

Please sign in to comment.