public void mapTypeInterfaces(ICirData cirData, TypeDefinition typeToMap) { var classSignature = CirFactoryUtils.getTypeUniqueSignatureFromTypeReference(typeToMap); var cirClass = getCirClass(cirData, classSignature); foreach (object interfaceType in typeToMap.Interfaces) { var type = interfaceType.GetType().FullName; TypeReference typeReference = null; switch (interfaceType.GetType().FullName) { case "Mono.Cecil.GenericInstanceType": { //var genericInstanceType = (GenericInstanceType)interfaceType; typeReference = (GenericInstanceType)interfaceType; break; } case "Mono.Cecil.TypeReference": { typeReference = (TypeReference)interfaceType; break; } case "Mono.Cecil.TypeDefinition": { typeReference = (TypeReference)interfaceType; break; } default: { DI.log.error("in mapTypeInterfaces, unsupported interface type: {0}", interfaceType.GetType().FullName); break; } } if (typeReference != null) { // typeReference = (TypeReference)interfaceType; var typeReferenceSignature = CirFactoryUtils.getTypeUniqueSignatureFromTypeReference(typeReference); var typeReferenceCirClass = getCirClass(cirData, typeReferenceSignature); cirClass.dSuperClasses.Add(typeReferenceCirClass.Signature, typeReferenceCirClass); } } if (typeToMap.BaseType != null) { var baseTypeSignature = CirFactoryUtils.getTypeUniqueSignatureFromTypeReference(typeToMap.BaseType); var baseCirClass = getCirClass(cirData, baseTypeSignature); cirClass.dSuperClasses.Add(baseCirClass.Signature, baseCirClass); } }
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); } }
public ICirFunction processMethodDefinition(ICirData cirData, MethodDefinition methodDefinition, SequencePoint sequencePoint) { try { //var functionSignature = methodDefinition.ToString(); var functionSignature = CirFactoryUtils.getFunctionUniqueSignatureFromMethodReference(methodDefinition); var functionClass = CirFactoryUtils.getTypeUniqueSignatureFromTypeReference(methodDefinition.DeclaringType); var cirFunction = getCirFunction(cirData, functionSignature, functionClass); if (false == cirFunction.HasBeenProcessedByCirFactory) { if (methodDefinition.CustomAttributes != null && methodDefinition.CustomAttributes.Count > 0) { foreach (CustomAttribute customAttribute in methodDefinition.CustomAttributes) { var constructorSignature = CirFactoryUtils.getFunctionUniqueSignatureFromMethodReference(customAttribute.Constructor); var cirAttribute = new CirAttribute(constructorSignature); foreach (var constructorParameter in customAttribute.ConstructorParameters) { var type = constructorParameter.GetType().FullName; } if (customAttribute.Fields.Count > 0 || customAttribute.Properties.Count > 0) { } // PublicDI.log.debug("Added attribute {0} to {1}", customAttribute.Constructor.Name, cirFunction.FunctionName); cirFunction.FunctionAttributes.Add(cirAttribute); } } // map the common values with MethodReference processMethodReference(cirData, methodDefinition, sequencePoint); cirFunction.HasBeenProcessedByCirFactory = true; // we need to put this in here or we will have an infinite loop on recursive functions cirFunction.HasControlFlowGraph = true; // ControlFlowGraph is use by the Viewers to determine if we have more than just a reference to this method cirFunction.ParentClass.bClassHasMethodsWithControlFlowGraphs = true; // also mark the parent class cirFunction.IsStatic = methodDefinition.IsStatic; cirFunction.IsUnmanaged = methodDefinition.IsUnmanaged; cirFunction.IsUnmanagedExport = methodDefinition.IsUnmanagedExport; cirFunction.IsVirtual = methodDefinition.IsVirtual; cirFunction.IsSetter = methodDefinition.IsSetter; cirFunction.IsGetter = methodDefinition.IsGetter; cirFunction.IsRuntime = methodDefinition.IsRuntime; cirFunction.IsPublic = methodDefinition.IsPublic; cirFunction.IsPrivate = methodDefinition.IsPrivate; cirFunction.IsPInvokeImpl = methodDefinition.IsPInvokeImpl; cirFunction.IsNative = methodDefinition.IsNative; cirFunction.IsManaged = methodDefinition.IsManaged; cirFunction.IsInternalCall = methodDefinition.IsInternalCall; cirFunction.IsIL = methodDefinition.IsIL; cirFunction.IsConstructor = methodDefinition.IsConstructor; cirFunction.IsAbstract = methodDefinition.IsAbstract; cirFunction.HasSecurity = methodDefinition.HasSecurity; cirFunction.HasBody = methodDefinition.HasBody; // try to find the location of the current method by going for the first line of the first method if (methodDefinition.HasBody) { foreach (Instruction instruction in methodDefinition.Body.Instructions) { if (instruction.SequencePoint != null) { cirFunction.File = instruction.SequencePoint.Document.Url; if (instruction.SequencePoint.StartLine == 16707566) // means there is no source code ref { cirFunction.FileLine = "0"; } else { cirFunction.FileLine = instruction.SequencePoint.StartLine.ToString(); } break; } } } // map method parameters (this could be on the MethodReference but if so we would have to check for doing it more than once: foreach (ParameterDefinition parameter in methodDefinition.Parameters) { ICirFunctionParameter functionParameter = new CirFunctionParameter { ParameterName = parameter.ToString(), ParameterType = parameter.ParameterType.FullName, Constant = (parameter.Constant != null) ? parameter.Constant.ToString() : "", HasConstant = parameter.HasConstant, HasDefault = parameter.HasDefault, Method = parameter.Method.ToString() }; cirFunction.FunctionParameters.Add(functionParameter); } // map the calls made and the IsCalledBy foreach (var methodCalled in CecilUtils.getMethodsCalledInsideMethod(methodDefinition)) { ICirFunction cirCalledFunction = processMemberReference(cirData, methodCalled.memberReference, methodCalled.sequencePoint); if (cirCalledFunction != null) { // store the fucntion called sequence cirFunction.FunctionsCalled.Add(new CirFunctionCall(cirCalledFunction, methodCalled.sequencePoint)); // store the unique list of funcions called if (false == cirFunction.FunctionsCalledUniqueList.Contains(cirCalledFunction)) { cirFunction.FunctionsCalledUniqueList.Add(cirCalledFunction); } // map the FunctionCalled and FunctionIsCalledBy var cirFunctionCall = new CirFunctionCall(cirCalledFunction, sequencePoint); //cirFunction.FunctionsCalled.Add(cirFunctionCall); cirCalledFunction.FunctionIsCalledBy.Add(cirFunctionCall); //if (false == cirCalledFunction.FunctionIsCalledBy.Contains(cirFunction)) // cirCalledFunction.FunctionIsCalledBy.Add(cirFunction); } } } // to implement if needed /* foreach (var methodOverride in methodDefinition.Overrides) * { * var name = methodOverride.GetType(); * }*/ return(cirFunction); } catch (Exception ex) { DI.log.ex(ex, "in CirFactory.processMethodDefinition", true); return(null); } }
public ICirClass processTypeDefinition(ICirData cirData, TypeDefinition typeDefinition) { try { var classSignature = CirFactoryUtils.getTypeUniqueSignatureFromTypeReference(typeDefinition); var cirClass = getCirClass(cirData, classSignature); // map attributes for this classs if (typeDefinition.CustomAttributes != null && typeDefinition.CustomAttributes.Count > 0) { foreach (CustomAttribute customAttribute in typeDefinition.CustomAttributes) { var constructorSignature = CirFactoryUtils.getFunctionUniqueSignatureFromMethodReference(customAttribute.Constructor); var cirAttribute = new CirAttribute(constructorSignature); foreach (var constructorParameter in customAttribute.ConstructorParameters) { // var type = constructorParameter.GetType().FullName; cirAttribute.Parameters.Add(constructorParameter.ToString(), constructorParameter.GetType().FullName); } if (customAttribute.Fields.Count > 0 || customAttribute.Properties.Count > 0) { } cirClass.ClassAttributes.Add(cirAttribute); } } if (false == cirClass.HasBeenProcessedByCirFactory) { cirClass.HasBeenProcessedByCirFactory = true; cirClass.Name = typeDefinition.Name; cirClass.FullName = typeDefinition.FullName; cirClass.Module = typeDefinition.Module.Name; cirClass.Namespace = cirClass.Namespace; cirClass.SymbolDef = Guid.NewGuid().ToString(); cirClass.IsAbstract = typeDefinition.IsAbstract; // cirClass.IsAnonymous = typeDefinition. cirClass.IsClass = typeDefinition.IsClass; cirClass.IsEnum = typeDefinition.IsEnum; cirClass.IsInterface = typeDefinition.IsInterface; cirClass.IsImport = typeDefinition.IsImport; cirClass.IsNotPublic = typeDefinition.IsNotPublic; cirClass.IsPublic = typeDefinition.IsPublic; cirClass.HasSecurity = typeDefinition.HasSecurity; } else { DI.log.info("This Class has already been processed, so only adding any possible extra methods: {0}", cirClass.Name); } // handle constuctors foreach (MethodDefinition methodDefinition in typeDefinition.Constructors) { ICirFunction cirFunction = processMethodDefinition(cirData, methodDefinition, null); if (false == cirClass.dFunctions.ContainsValue(cirFunction)) { cirClass.dFunctions.Add(cirFunction.FunctionSignature, cirFunction); } // try to find source code reference if (cirFunction.FunctionsCalled.Count > 0 && cirClass.FileLine == null) { if (false == string.IsNullOrEmpty(cirFunction.FunctionsCalled[0].fileName)) { cirClass.File = cirFunction.FunctionsCalled[0].fileName; cirClass.FileLine = "0"; // set it to zero and try to map it later } } } // handle methods foreach (MethodDefinition methodDefinition in typeDefinition.Methods) { ICirFunction cirFunction = processMethodDefinition(cirData, methodDefinition, null); if (false == cirClass.dFunctions.ContainsValue(cirFunction)) { cirClass.dFunctions.Add(cirFunction.FunctionSignature, cirFunction); } } return(cirClass); } catch (Exception ex) { DI.log.ex(ex, "in CirFactory.processTypeDefinition", true); return(null); } }
public static bool areEqual_MethodDefinitionAndCirFunction(MethodDefinition cecilMethodDefintion, ICirFunction cirFunction) { try { if (cecilMethodDefintion == null || cirFunction == null) { return(false); } var cirFunctionProperties = new Dictionary <string, object>(); foreach (var property in DI.reflection.getProperties(cirFunction.GetType())) { cirFunctionProperties.Add(property.Name, DI.reflection.getProperty(property.Name, cirFunction)); // DI.log.info("prop: {0} = {1}", property.Name, cirFunctionProperties[property.Name]); } var methodsCalledInsideMethod = CecilUtils.getMethodsCalledInsideMethod(cecilMethodDefintion); var moduleName = (cecilMethodDefintion.DeclaringType.Module != null) ? cecilMethodDefintion.DeclaringType.Module.Assembly.Name.ToString() : "[NullModule]"; foreach (var property in cirFunctionProperties.Keys) { switch (property) { case "FunctionSignature": if (cirFunctionProperties[property].ToString() != String.Format("{0}!{1}", moduleName, cecilMethodDefintion)) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: cirFunction[FunctionSignature]!=cecilMethodDefintion"); return(false); } break; case "IsPrivate": case "IsStatic": case "IsConstructor": case "IsUnmanaged": case "IsUnmanagedExport": case "IsVirtual": case "IsSetter": case "IsGetter": case "IsRuntime": case "IsPublic": case "IsPInvokeImpl": case "IsNative": case "IsManaged": case "IsInternalCall": case "IsIL": case "IsAbstract": case "HasSecurity": case "HasBody": if ((bool)cirFunctionProperties[property] != (bool)DI.reflection.getProperty(property, cecilMethodDefintion)) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: (bool)cirFunctionProperties[" + property + "] != (bool)DI.reflection.getProperty(" + property + ", cecilMethodDefintion)"); return(false); } break; case "ParentClass": var cirClass1 = (CirClass)cirFunctionProperties[property]; if (cirClass1.Signature != CirFactoryUtils.getTypeUniqueSignatureFromTypeReference(cecilMethodDefintion.DeclaringType)) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: cirFunction[ParentClass]!=cecilMethodDefintion == " + cecilMethodDefintion.DeclaringType.FullName); return(false); } break; case "ParentClassFullName": var parentClassFullName = (string)cirFunctionProperties[property]; if (parentClassFullName != cecilMethodDefintion.DeclaringType.FullName) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: cirFunction[ParentClassFullName]!=cecilMethodDefintion.FullName " + cecilMethodDefintion.DeclaringType.FullName); return(false); } break; case "ParentClassName": var parentClassName = (string)cirFunctionProperties[property]; if (parentClassName != cecilMethodDefintion.DeclaringType.Name) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: cirFunction[parentClassName]!=cecilMethodDefintion.FullName " + cecilMethodDefintion.DeclaringType.Name); return(false); } break; case "FunctionParameters": var cirFunctionParameters = (List <ICirFunctionParameter>)cirFunctionProperties[property]; if (cecilMethodDefintion.Parameters.Count != cirFunctionParameters.Count) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: the FunctionParameters count didn't match"); return(false); } foreach (ParameterDefinition parameter in cecilMethodDefintion.Parameters) { if (false == ViewHelpers.getCirParameterTypeStringList(cirFunctionParameters).Contains(parameter.ParameterType.FullName)) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: the FunctionParameters signatures didn't match"); return(false); } } break; case "FunctionsCalledUniqueList": var functionsCalledUniqueList = (List <ICirFunction>)cirFunctionProperties[property]; foreach (MethodCalled methodCalled in methodsCalledInsideMethod) { if (false == ViewHelpers.getCirFunctionStringList(functionsCalledUniqueList).Contains(CirFactoryUtils.getFunctionUniqueSignatureFromMethodReference(methodCalled.memberReference))) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: there was a missing function in the FunctionsCalledUniqueList"); return(false); } } break; case "FunctionsCalledSequence": var functionsCalledSequence = (List <ICirFunction>)cirFunctionProperties[property]; if (functionsCalledSequence.Count != methodsCalledInsideMethod.Count) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: functionsCalledSequence.Count != methodsCalledInsideMethod.Count"); return(false); } for (int i = 0; i < methodsCalledInsideMethod.Count; i++) { if (CirFactoryUtils.getFunctionUniqueSignatureFromMethodReference(methodsCalledInsideMethod[i].memberReference) != ViewHelpers.getCirFunctionStringList(functionsCalledSequence)[i]) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: the FunctionsCalledSequence does match"); return(false); } } break; case "ReturnType": if (cecilMethodDefintion.ReturnType.ReturnType.FullName != cirFunctionProperties[property].ToString()) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: ReturnType don't match"); return(false); } break; case "CecilSignature": if (cecilMethodDefintion.ToString() != cirFunctionProperties[property].ToString()) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: CecilSignature din't match"); return(false); } break; case "FunctionNameAndParameters": if (CecilUtils.getMethodNameAndParameters(cecilMethodDefintion) != cirFunctionProperties[property].ToString()) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: FunctionNameAndParameters din't match"); return(false); } break; case "FunctionName": if (cecilMethodDefintion.Name != cirFunctionProperties[property].ToString()) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: FunctionName din't match"); return(false); } break; case "Module": if (moduleName != cirFunctionProperties[property].ToString()) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: Module din't match"); return(false); } break; case "ClassNameFunctionNameAndParameters": var classNameFunctionNameAndParameters = cirFunctionProperties[property].ToString(); var expectedClassNameFunctionNameAndParameters = string.Format("{0}.{1}", cirFunction.ParentClassFullName, cirFunction.FunctionNameAndParameters); if (classNameFunctionNameAndParameters != expectedClassNameFunctionNameAndParameters) { DI.log.error( "in areEqual_MethodDefinitionAndCirFunction: classNameFunctionNameAndParameters din't match"); return(false); } break; // ignore these ones case "IsSource": case "IsSink": case "FunctionParameterTypes": case "lcfgBasicBlocks": case "SymbolDef": case "ReflectionSignature": case "O2MDbgSignature": case "UsedTypes": case "FunctionIsCalledBy": case "dSsaVariables": case "dVariables": case "HasControlFlowGraph": case "OnlyShowFunctionNameInToString": case "HasBeenProcessedByCirFactory": break; // case "": //break; default: DI.log.error(" property not handled: {0}", property); break; } } return(true); } catch (Exception ex) { DI.log.ex(ex, "in areEqual_MethodDefinitionAndCirFunction", true); return(false); } }