public MethodReference ImportMethodReference(MethodReference mr, ImportContext context) { if (mr.DeclaringType.Module == m_module) { return(mr); } ImportCache(); if (mr is MethodSpecification) { return(GetMethodSpec(mr, context)); } MethodReference meth = m_memberRefCache [mr.ToString()] as MethodReference; if (meth != null) { return(meth); } meth = new MethodReference( mr.Name, mr.HasThis, mr.ExplicitThis, mr.CallingConvention); meth.DeclaringType = ImportTypeReference(mr.DeclaringType, context); TypeReference contextType = meth.DeclaringType; while (contextType is TypeSpecification) { contextType = (contextType as TypeSpecification).ElementType; } context.GenericContext.Method = meth; context.GenericContext.Type = contextType; foreach (GenericParameter gp in mr.GenericParameters) { meth.GenericParameters.Add(GenericParameter.Clone(gp, context)); } meth.ReturnType.ReturnType = ImportTypeReference(mr.ReturnType.ReturnType, context); foreach (ParameterDefinition param in mr.Parameters) { meth.Parameters.Add(new ParameterDefinition( ImportTypeReference(param.ParameterType, context))); } m_module.MemberReferences.Add(meth); m_memberRefCache [mr.ToString()] = meth; return(meth); }
private bool DoMatchProperty(MethodReference method) { if (method.Name.StartsWith("set_")) { if (method.Parameters.Count == 1) { if (method.Parameters[0].ParameterType.FullName == "System.String") { string name = method.ToString(); foreach (string pattern in m_properties) { if (name.Match(pattern)) return true; } } } } return false; }
private void DoCheck(TypedInstruction call, MethodReference target, int nth) { int index = m_info.Tracker.GetStackIndex(call.Index, nth); if (index >= 0) { LoadString load = m_info.Instructions[index] as LoadString; if (load != null) { m_offset = call.Untyped.Offset; m_bad = target.ToString(); Log.DebugLine(this, "bad call at {0:X2}", m_offset); } } }
static string GetId(ModuleDefinition mod, MethodReference mtd) { char asmRef = (char)(mod.AssemblyReferences.IndexOf(mtd.DeclaringType.Scope as AssemblyNameReference) + 2); return "\0" + asmRef + mtd.ToString(); }
private static string GetId(ModuleDefinition mod, bool isVirt, MethodReference mtd) { string virt = isVirt ? "\r" : "\n"; char asmRef = (char)(mod.AssemblyReferences.IndexOf(mtd.DeclaringType.Scope as AssemblyNameReference) + 2); return "\0" + asmRef + virt + mtd.ToString(); }
private static string GetNameFromNewObjMethodReference(Mono.Cecil.MethodReference methodReference) { string[] array = methodReference.ToString().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); string[] array2 = array[1].Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries); return(array2[0].Trim()); }
private bool DoCallsExternal(MethodReference method, Entry entry, CallGraph graph, List<string> chain, int depth, ref string name) { if (depth > 8) // this can't be too large or the rule takes a very long time to run return false; chain.Add(method.ToString()); if (entry.ExternalCalls.ContainsKey(method)) { name = entry.ExternalCalls[method]; return true; } foreach (MethodReference candidate in graph.Calls(method)) { if (DoCallsExternal(candidate, entry, graph, chain, depth + 1, ref name)) return true; } chain.RemoveAt(chain.Count - 1); return false; }
MethodReference GetMethodReference (MethodReference source) { string name = source.ToString (); MethodReference result; if (!methodsrefs_cache.TryGetValue (name, out result)) { TypeReference tr = GetTypeReference (source.DeclaringType, false); if (source is GenericInstanceMethod) { GenericInstanceMethod mr = source as GenericInstanceMethod; mr.ElementMethod = GetMethodReference (mr.ElementMethod); VisitParameterDefinitionCollection (mr.Parameters); result = mr; } else result = GetMethodReferenceForType (tr, source); methodsrefs_cache.Add (name, result); } return result; }
public void Print(MethodReference mRef) { Console.WriteLine ("{0} {1}", mRef.DeclaringType.FullName, mRef.ToString()); }
public MethodReference ImportMethodReference(MethodReference mr, ImportContext context) { if (mr.DeclaringType.Module == m_module) return mr; ImportCache (); if (mr is MethodSpecification) return GetMethodSpec (mr, context); MethodReference meth = m_memberRefCache [mr.ToString ()] as MethodReference; if (meth != null) return meth; meth = new MethodReference ( mr.Name, mr.HasThis, mr.ExplicitThis, mr.CallingConvention); meth.DeclaringType = ImportTypeReference (mr.DeclaringType, context); TypeReference contextType = meth.DeclaringType; while (contextType is TypeSpecification) contextType = (contextType as TypeSpecification).ElementType; context.GenericContext.Method = meth; context.GenericContext.Type = contextType; foreach (GenericParameter gp in mr.GenericParameters) meth.GenericParameters.Add (GenericParameter.Clone (gp, context)); meth.ReturnType.ReturnType = ImportTypeReference (mr.ReturnType.ReturnType, context); foreach (ParameterDefinition param in mr.Parameters) meth.Parameters.Add (new ParameterDefinition ( ImportTypeReference (param.ParameterType, context))); m_module.MemberReferences.Add (meth); m_memberRefCache [mr.ToString ()] = meth; return meth; }
private BfMethod method_5(BfMethod bfMethod_0, MethodReference methodReference_0) { GenericInstanceType genericInstanceType = (GenericInstanceType)methodReference_0.DeclaringType; BfType bfType = this._cache.method_7(genericInstanceType.ElementType); HashSet<string> hashSet = new HashSet<string>(); BfMethod result; if (bfType == null) { result = null; } else { foreach (GenericParameter genericParameter in bfType.method_2().GenericParameters) { hashSet.Add(genericParameter.FullName); } MethodReference methodReference = null; List<MethodReference> list = new List<MethodReference>(); list.AddRange(bfType.method_2().Constructors.Cast<MethodReference>()); list.AddRange(bfType.method_2().Methods.Cast<MethodReference>()); using (List<MethodReference>.Enumerator enumerator2 = list.GetEnumerator()) { while (enumerator2.MoveNext()) { MethodDefinition methodDefinition = (MethodDefinition)enumerator2.Current; if (methodDefinition.Name == methodReference_0.Name && methodDefinition.Parameters.Count == methodReference_0.Parameters.Count) { bool flag = true; for (int i = 0; i < methodDefinition.Parameters.Count; i++) { ParameterDefinition parameterDefinition = methodDefinition.Parameters[i]; ParameterDefinition parameterDefinition2 = methodReference_0.Parameters[i]; if (hashSet.Contains(BfCache.smethod_3(parameterDefinition.ParameterType).FullName)) { if (!parameterDefinition2.ToString().StartsWith("A_")) { flag = false; } } else if (!(Regex.Replace(parameterDefinition2.ParameterType.FullName, "<[^>]*>", "") == Regex.Replace(parameterDefinition.ParameterType.FullName, "<[^>]*>", ""))) { flag = false; } } if (flag) { methodReference = methodDefinition; } } } } if (methodReference == null) { Logger.LogWarning("GetMethodRefFromGenericInstance", "Not good " + methodReference_0.ToString()); result = null; } else { BfMethod.smethod_0(methodReference); result = this._cache.method_5(methodReference); } } return result; }
private bool DoFoundBadSetter(CallGraph graph, MethodReference method, List<string> chain) { if (m_visited.IndexOf(method) >= 0) return false; m_visited.Add(method); bool found = false; Log.DebugLine(this, "checking {0}", method); if (m_unlocked.IndexOf(method) >= 0 && m_setters.IndexOf(method) >= 0) { Log.DebugLine(this, "it's a setter"); found = true; } else { foreach (MethodReference callee in graph.Calls(method)) { Log.Indent(); if (DoFoundBadSetter(graph, callee, chain)) { found = true; Log.Unindent(); break; } Log.Unindent(); } } if (found) chain.Insert(0, method.ToString()); return found; }
private bool DoValidMethod(MethodReference method) { string fullName = method.ToString(); if (fullName.Contains("Mono.Security.ASN1::Add")) return false; else if (fullName.Contains("System.Boolean System.Collections.Generic.HashSet`1<T>::Add")) return false; else if (fullName.Contains("System.Collections.ArrayList::Add")) return false; else if (fullName.Contains("System.Collections.IList::Add")) return false; else if (fullName.Contains("System.Collections.IList::Add")) return false; else if (fullName.Contains("System.Diagnostics.Process::Start()")) // note that people should generally use the result of the other overloads return false; else if (fullName.Contains("System.IO.Directory::CreateDirectory")) return false; else if (fullName.Contains("System.Diagnostics.TraceListenerCollection::Add")) return false; else if (fullName.Contains("System.IO.DirectoryInfo::CreateSubdirectory")) return false; else if (fullName.Contains("System.IO.FileStream::Seek")) return false; else if (fullName.Contains("System.IO.MemoryStream::Seek")) return false; else if (fullName.Contains("System.IO.Stream::Seek")) return false; else if (fullName.Contains("System.IO.UnmanagedMemoryStream::Seek")) return false; else if (fullName.Contains("System.Security.PermissionSet::AddPermission")) return false; else if (fullName.Contains("System.Security.PermissionSet::RemovePermission")) return false; else if (fullName.Contains("System.Security.PermissionSet::SetPermission")) return false; else if (fullName.Contains("System.Reflection.MethodBase::Invoke")) return false; else if (fullName.Contains("System.Runtime.InteropServices.Marshal::AddRef")) return false; else if (fullName.Contains("System.Runtime.InteropServices.Marshal::Release")) return false; else if (fullName.Contains("System.Runtime.Remoting.Lifetime.ILease::Renew")) return false; else if (fullName.Contains("System.Text.StringBuilder::Append")) return false; else if (fullName.Contains("System.Text.StringBuilder::Insert")) return false; else if (fullName.Contains("System.Text.StringBuilder::Remove")) return false; else if (fullName.Contains("System.Text.StringBuilder::Replace")) return false; else if (fullName.Contains("System.Threading.Interlocked::Add")) return false; else if (fullName.Contains("System.Threading.Interlocked::CompareExchange")) return false; else if (fullName.Contains("System.Threading.Interlocked::Decrement")) return false; else if (fullName.Contains("System.Threading.Interlocked::Exchange")) return false; else if (fullName.Contains("System.Threading.Interlocked::Increment")) return false; else if (fullName.Contains("System.Windows.Forms.ListBox/ObjectCollection::Add")) return false; else if (fullName.Contains("System.Windows.Forms.Menu/MenuItemCollection::Add")) return false; else if (fullName.Contains("System.Xml.Schema.XmlSchemaSet::Add")) return false; else if (method.Name == "BeginInvoke") { if (method.Parameters.Count > 0) if (method.Parameters[0].ParameterType.FullName == "System.Delegate") return false; else if (method.Parameters[0].ParameterType.FullName == "System.IAsyncResult") return false; } return true; }
private bool DoCallsLock(MethodReference method, FieldReference field, Entry entry, CallGraph graph, List<string> chain, int depth) { if (depth > 8) // this can't be too large or the rule takes a very long time to run return false; chain.Add(method.ToString()); if (entry.Locked.ContainsKey(method) && entry.Locked[method].IndexOf(field) >= 0) { return true; } foreach (MethodReference candidate in graph.Calls(method)) { if (DoCallsLock(candidate, field, entry, graph, chain, depth + 1)) return true; } chain.RemoveAt(chain.Count - 1); return false; }
string GetNameO(bool isVirt, MethodReference mbr) { return ObfuscationHelper.GetNewName((isVirt ? "V>." : "") + mbr.ToString()); }
private bool DoMatchMethod(MethodReference method) { string name = method.ToString(); if (!method.Name.StartsWith("set_")) { foreach (string pattern in m_methods) { if (name.Match(pattern)) return true; } } foreach (string custom in m_custom) { if (name.Contains(custom)) return true; } return false; }
private bool DoMatchCtor(MethodReference method) { string name = method.ToString(); if (name.StartsWith("System.Void System.Windows.Forms.") || name.StartsWith("System.Void Gtk.")) { if (name.Contains("::.ctor(")) return true; } foreach (string custom in m_custom) { if (name.Contains(custom)) return true; } return false; }
// Method name has to match one of the methods we're looking for // and can't already use IFormatProvider. private bool DoMatchMethod(MethodReference method) { if (DoHasProvider(method)) return false; string name = method.ToString(); foreach (string pattern in m_methods) { if (name.Contains(pattern)) return true; } return false; }
internal BfMethod method_5(MethodReference methodReference_0) { BfAssembly bfAssembly; BfMethod result; if (methodReference_0.DeclaringType.Scope is AssemblyNameReference) { bfAssembly = this.dictionary_0[((AssemblyNameReference)methodReference_0.DeclaringType.Scope).FullName]; } else { if (!(methodReference_0.DeclaringType.Scope is ModuleDefinition)) { Logger.LogWarning("GetBfMethod1", "Couldn't find assembly for method: " + methodReference_0.ToString()); result = null; return result; } bfAssembly = this.dictionary_0[((ModuleDefinition)methodReference_0.DeclaringType.Scope).Assembly.Name.FullName]; } string text = BfMethod.smethod_0(methodReference_0); BfMethod bfMethod; bfAssembly.method_5().TryGetValue(text, out bfMethod); if (bfMethod == null) { BfType bfType = this.method_7(methodReference_0.DeclaringType); List<MethodDefinition> list = new List<MethodDefinition>(); list.AddRange(bfType.method_2().Constructors.Cast<MethodDefinition>()); list.AddRange(bfType.method_2().Methods.Cast<MethodDefinition>()); foreach (MethodDefinition current in list) { if (text == BfMethod.smethod_0(current)) { bfMethod = new BfMethod(this, current, bfType); this.methodCollection_0.method_1(bfMethod); bfType.Methods.method_1(bfMethod); bfAssembly.method_5().Add(bfMethod.UniqueName, bfMethod); break; } } if (bfMethod == null) { Logger.LogWarning("GetBfMethod2", "Couldn't find: " + text); } } result = bfMethod; return result; }
private static bool DoIsLogCall(MethodReference target) { string name = target.ToString(); if (name.Contains("Smokey.Framework.Log::ErrorLine")) return true; else if (name.Contains("Smokey.Framework.Log::WarningLine")) return true; else if (name.Contains("Smokey.Framework.Log::InfoLine")) return true; else if (name.Contains("Smokey.Framework.Log::TraceLine")) return true; else if (name.Contains("Smokey.Framework.Log::DebugLine")) return true; return false; }
private static string GetCallNameFromAnalyzeMethodCalls(Mono.Cecil.MethodReference methodReference) { string[] array = methodReference.ToString().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); return(array[1].Trim()); }
string GetNameO(MethodReference mbr) { return ObfuscationHelper.GetNewName(mbr.ToString()); }
public ICirFunction processMethodReference(ICirData cirData, MethodReference methodReference, SequencePoint sequencePoint) { try { var functionSignature = CirFactoryUtils.getFunctionUniqueSignatureFromMethodReference(methodReference); var functionType = CirFactoryUtils.getTypeUniqueSignatureFromTypeReference(methodReference.DeclaringType); var cirFunction = getCirFunction(cirData, functionSignature, functionType); cirFunction.CecilSignature = methodReference.ToString(); cirFunction.ReturnType = methodReference.ReturnType.ReturnType.FullName; cirFunction.ParentClass = getCirClass(cirData, CirFactoryUtils.getTypeUniqueSignatureFromTypeReference(methodReference.DeclaringType)); cirFunction.ParentClassFullName = methodReference.DeclaringType.FullName; cirFunction.ParentClassName = methodReference.DeclaringType.Name; // cirFunction.Module = (methodReference.DeclaringType.Module != null) ? methodReference.DeclaringType.Module.Assembly.ToString() : "[NullModule]"; //cirFunction.Module = (methodReference.DeclaringType.Module != null) ? methodReference.DeclaringType.Module.Name : "[NullModule]"; cirFunction.FunctionName = methodReference.Name; cirFunction.FunctionNameAndParameters = CecilUtils.getMethodNameAndParameters(methodReference); cirFunction.ClassNameFunctionNameAndParameters = string.Format("{0}.{1}", cirFunction.ParentClassFullName, cirFunction.FunctionNameAndParameters); cirFunction.SymbolDef = Guid.NewGuid().ToString(); /*if (sequencePoint != null) { if (string.IsNullOrEmpty(cirFunction.File) == false) { } cirFunction.File = sequencePoint.Document.Url; cirFunction.FileLine = sequencePoint.StartColumn.ToString(); }*/ //methodReference.ReturnType // to implement since we need to add reference to a CirClass return cirFunction; } catch (Exception ex) { DI.log.ex(ex, "in CirFactory.processMethodReference", true); return null; } }
/// <summary> /// Elements the specified method definition. /// </summary> /// <param name="methodDefinition">The method definition.</param> public void Element (MethodReference methodDefinition) { this.PushElement ("method"); this.AddElement ("name", methodDefinition.ToString ()); }