Exemple #1
0
        public Helper(IModule module, PeReader.DefaultHost host, Log.Log logger)
        {
            this.host = host;
            this.logger = logger;
            this.module = module;

            // get all needed functions and namespaces
            this.systemString = UnitHelper.FindType(this.host.NameTable, this.host.LoadAssembly(this.host.CoreAssemblySymbolicIdentity), "System.String");
            this.systemIOTextWriter = UnitHelper.FindType(this.host.NameTable, this.host.LoadAssembly(this.host.CoreAssemblySymbolicIdentity), "System.IO.TextWriter");
            this.systemIOStreamWriter = UnitHelper.FindType(this.host.NameTable, this.host.LoadAssembly(this.host.CoreAssemblySymbolicIdentity), "System.IO.StreamWriter");
            this.systemInt32 = UnitHelper.FindType(this.host.NameTable, this.host.LoadAssembly(this.host.CoreAssemblySymbolicIdentity), "System.Int32");
            this.systemObject = UnitHelper.FindType(this.host.NameTable, this.host.LoadAssembly(this.host.CoreAssemblySymbolicIdentity), "System.Object");
            this.systemConsole = UnitHelper.FindType(this.host.NameTable, this.host.LoadAssembly(this.host.CoreAssemblySymbolicIdentity), "System.Console");
            this.systemRandom = UnitHelper.FindType(this.host.NameTable, this.host.LoadAssembly(this.host.CoreAssemblySymbolicIdentity), "System.Random");

            ITypeReference[] concatThreeParameterTypes = { this.host.PlatformType.SystemString, this.host.PlatformType.SystemString, this.host.PlatformType.SystemString };
            ITypeReference[] concatTwoParameterTypes = { this.host.PlatformType.SystemString, this.host.PlatformType.SystemString };
            ITypeReference[] streamWriterAppendTypes = { this.host.PlatformType.SystemString, this.host.PlatformType.SystemBoolean };

            this.stringConcatThree = TypeHelper.GetMethod(systemString, this.host.NameTable.GetNameFor("Concat"), concatThreeParameterTypes);
            this.stringConcatTwo = TypeHelper.GetMethod(systemString, this.host.NameTable.GetNameFor("Concat"), concatTwoParameterTypes);
            this.textWriterWriteLine = TypeHelper.GetMethod(systemIOTextWriter, this.host.NameTable.GetNameFor("WriteLine"), this.host.PlatformType.SystemString);
            this.streamWriterCtor = TypeHelper.GetMethod(systemIOStreamWriter, this.host.NameTable.GetNameFor(".ctor"), this.host.PlatformType.SystemString);
            this.streamWriterCtorAppend = TypeHelper.GetMethod(systemIOStreamWriter, this.host.NameTable.GetNameFor(".ctor"), streamWriterAppendTypes);
            this.textWriterWrite = TypeHelper.GetMethod(systemIOTextWriter, this.host.NameTable.GetNameFor("Write"), this.host.PlatformType.SystemString);
            this.int32ToString = TypeHelper.GetMethod(systemInt32, this.host.NameTable.GetNameFor("ToString"));
            this.textWriterClose = TypeHelper.GetMethod(systemIOTextWriter, this.host.NameTable.GetNameFor("Close"));
            this.objectGetHashCode = TypeHelper.GetMethod(systemObject, this.host.NameTable.GetNameFor("GetHashCode"));
            this.systemConsoleWriteLine = TypeHelper.GetMethod(systemConsole, host.NameTable.GetNameFor("WriteLine"), host.PlatformType.SystemString);
            this.objectCtor = TypeHelper.GetMethod(systemObject, this.host.NameTable.GetNameFor(".ctor"));
            this.systemRandomCtor = TypeHelper.GetMethod(systemRandom, this.host.NameTable.GetNameFor(".ctor"));
            this.systemRandomNext = TypeHelper.GetMethod(systemRandom, this.host.NameTable.GetNameFor("Next"), host.PlatformType.SystemInt32);
        }
 public HostEnvironment(INameTable table, IEnumerable<String> assemblyPaths, IEnumerable<String> referencedAssemblies)
     : base(table, new InternFactory(), 4, assemblyPaths, true)
 {
     _peReader = new PeReader(this);
     _assemblyPaths = assemblyPaths;
     _referencedAssemblies = referencedAssemblies;
 }
        public CfgManipulator(IModule module, PeReader.DefaultHost host, Log.Log logger, MethodCfg methodCfg) {
            this.host = host;
            this.logger = logger;
            this.module = module;

            this.methodCfg = methodCfg;
        }
        public InterfaceTransformer(IModule module, PeReader.DefaultHost host, Log.Log logger) {

            this.host = host;
            this.logger = logger;
            this.module = module;
            this.helperClass = new Helper(module, host, logger);

        }
Exemple #5
0
 internal ILMergeHost(List <string> /*?*/ libPaths)
     : base(new NameTable(), new InternFactory(), 0, libPaths, true)
 {
     this.peReader = new PeReader(this);
     if (libPaths != null)
     {
         foreach (var p in libPaths)
         {
             this.libPaths.Add(p);
         }
     }
 }
        public GraphRandomStateGenerator(IModule module, PeReader.DefaultHost host, Log.Log logger, CfgManipulator cfgManipulator, Helper helperClass, Graph.Graph graph, MethodCfg methodCfg, bool debugging) {

            this.module = module;
            this.host = host;
            this.logger = logger;
            this.cfgManipulator = cfgManipulator;            
            this.helperClass = helperClass;
            this.graph = graph;
            this.methodCfg = methodCfg;
            this.debugging = debugging;

            // add local random generator variable
            this.tempRandomLocal = new LocalDefinition();
            this.tempRandomLocal.IsReference = false;
            this.tempRandomLocal.IsPinned = false;
            this.tempRandomLocal.IsModified = false;
            this.tempRandomLocal.Type = this.helperClass.systemRandom;
            this.tempRandomLocal.MethodDefinition = methodCfg.method;
            cfgManipulator.addLocalVariable(this.tempRandomLocal);


        }
 public TestHostEnvironment()
 {
     reader = new PeReader(this);
 }
 /// <summary>
 /// Allocates a simple host environment using default settings inherited from MetadataReaderHost and that
 /// uses PeReader as its metadata reader.
 /// </summary>
 /// <param name="nameTable">
 /// A collection of IName instances that represent names that are commonly used during compilation.
 /// This is a provided as a parameter to the host environment in order to allow more than one host
 /// environment to co-exist while agreeing on how to map strings to IName instances.
 /// </param>
 public DefaultHost(INameTable nameTable)
     : base(nameTable)
 {
     this.peReader = new PeReader(this);
 }
 /// <summary>
 /// Allocates a simple host environment using default settings inherited from MetadataReaderHost and that
 /// uses PeReader as its metadata reader.
 /// </summary>
 /// <param name="nameTable">
 /// A collection of IName instances that represent names that are commonly used during compilation.
 /// This is a provided as a parameter to the host environment in order to allow more than one host
 /// environment to co-exist while agreeing on how to map strings to IName instances.
 /// </param>
 public DefaultHost(INameTable nameTable)
     : base(nameTable, new InternFactory(), 0, null, false)
 {
     this.peReader = new PeReader(this);
 }
Exemple #10
0
        static void mergeClassWithAllAncestors(NamespaceTypeDefinition mergeTargetClass, PeReader.DefaultHost host) {

            List<NamespaceTypeDefinition> ancestorClasses = new List<NamespaceTypeDefinition>();
            ancestorClasses.Add(mergeTargetClass);

            NamespaceTypeDefinition currentClass = mergeTargetClass;
            NamespaceTypeDefinition ancestorClass = null;


            while (true) {

                // get class from which was inherited
                if (currentClass.BaseClasses.Count() == 1) {                    
                    // only add base classes of type NamespaceTypeDefinition
                    if (currentClass.BaseClasses.ElementAt(0) as NamespaceTypeDefinition != null) {
                        ancestorClass = (currentClass.BaseClasses.ElementAt(0) as NamespaceTypeDefinition);
                    }
                    // ignore ancestor that are not of type NamespaceTypeDefinition
                    else {
                        break;
                    }
                }
                else {
                    throw new ArgumentException("Do not know how to handle multiple inheritance.");
                }

                // add ancestor class to list of ancestor classes
                globalLog.writeLine("Found ancestor: " + ancestorClass.ToString());
                ancestorClasses.Add(ancestorClass);

                currentClass = ancestorClass;
            }

            // mrge class with ancenstor (start with the "highest" ancestor)
            for (int i = (ancestorClasses.Count - 1); i >= 0; i--) {
                globalLog.writeLine("Merge class \"" + ancestorClasses[i].ToString() + "\" with ancestor");
                mergeClassWithAncestor(ancestorClasses[i], host);
                globalLog.writeLine("");
            }


        }
Exemple #11
0
        public CodeGenerator(IModule module, PeReader.DefaultHost host, Log.Log logger, Random prng, MethodCfg methodCfg,
            bool debugging=false, CfgManipulator manipulator=null)
        {
            this.host = host;
            this.logger = logger;
            this.module = module;
            this.prng = prng;
            this.methodCfg = methodCfg;
            this.debugging = debugging;
            this.manipulator = manipulator;

            callableMethods = retrieveCallableMethods();
        }
 /// <summary>
 /// Allocates a simple host environment using default settings inherited from MetadataReaderHost and that
 /// uses PeReader as its metadata reader.
 /// </summary>
 /// <param name="nameTable">
 /// A collection of IName instances that represent names that are commonly used during compilation.
 /// This is a provided as a parameter to the host environment in order to allow more than one host
 /// environment to co-exist while agreeing on how to map strings to IName instances.
 /// </param>
 public DefaultHost(INameTable nameTable)
   : base(nameTable) {
   this.peReader = new PeReader(this);
 }
 /// <summary>
 /// Allocates a simple host environment using default settings inherited from MetadataReaderHost and that
 /// uses PeReader as its metadata reader.
 /// </summary>
 /// <param name="nameTable">
 /// A collection of IName instances that represent names that are commonly used during compilation.
 /// This is a provided as a parameter to the host environment in order to allow more than one host
 /// environment to co-exist while agreeing on how to map strings to IName instances.
 /// </param>
 public DefaultHost(INameTable nameTable)
     : base(nameTable, new InternFactory(), 0, null, false)
 {
     this.peReader = new PeReader(this);
 }
Exemple #14
0
 public CfgBuilder(IModule module, PeReader.DefaultHost host, Log.Log logger) {
     this.host = host;
     this.logger = logger;
     this.module = module;
 }
Exemple #15
0
 internal HostEnvironment(int pointerSizeInBits)
     : base(new NameTable(), new InternFactory(), (byte)(pointerSizeInBits / 8), null, false)
 {
     Debug.Assert(pointerSizeInBits == 32 || pointerSizeInBits == 64);
       this.peReader = new PeReader(this);
       string loc = typeof(object).Assembly.Location;
       System.Reflection.AssemblyName mscorlibName = new System.Reflection.AssemblyName(typeof(object).Assembly.FullName);
       var tempMscorlibIdentity = new AssemblyIdentity(this.NameTable.GetNameFor(mscorlibName.Name), "", mscorlibName.Version, mscorlibName.GetPublicKeyToken(), loc);
       this.RegisterAsLatest(this.peReader.OpenAssembly(BinaryDocument.GetBinaryDocumentForFile(tempMscorlibIdentity.Location, this), out this.mscorlibIdentity));
       loc = typeof(Microsoft.Research.Vcc.Runtime).Assembly.Location;
       System.Reflection.AssemblyName runtimeName = new System.Reflection.AssemblyName(typeof(Microsoft.Research.Vcc.Runtime).Assembly.FullName);
       var tempVccRuntimeAssemblyIdentity = new AssemblyIdentity(this.NameTable.GetNameFor(runtimeName.Name), "", runtimeName.Version, runtimeName.GetPublicKeyToken(), loc);
       this.RegisterAsLatest(this.peReader.OpenAssembly(BinaryDocument.GetBinaryDocumentForFile(tempVccRuntimeAssemblyIdentity.Location, this), out this.vccRuntimeAssemblyIdentity));
 }
Exemple #16
0
 internal HostEnvironment()
   : base(new NameTable(), 4) {
   this.peReader = new PeReader(this);
 }
 public CcsHost()
 {
     this.peReader = new PeReader(this);
     this.pdbReaders = new Dictionary<string, PdbReader>(StringComparer.OrdinalIgnoreCase);
 }
Exemple #18
0
 public CCIEnvironment()
     : base(new NameTable(), 4)
 {
     mPeReader = new PeReader(this);
 }
 internal HostEnvironment()
 {
     this.peReader = new PeReader(this);
 }
 /// <summary>
 /// Allocates a simple host environment using default settings inherited from MetadataReaderHost and that
 /// uses PeReader as its metadata reader.
 /// </summary>
 public DefaultHost()
     : base(new NameTable(), new InternFactory(), 0, null, true)
 {
     this.peReader = new PeReader(this);
 }
    /// <summary>
    /// Searches the analysis for a method by the name of f and returns a bool depending on whether f is reachable
    /// </summary>
    static bool IsMethodReachable(PeReader.DefaultHost host, RapidTypeAnalysis analysis, string className, string methodName) {
      var methodIName = host.NameTable.GetNameFor(methodName);
      var classIName = host.NameTable.GetNameFor(className);


      // t-devinc: perhaps should change this to use TestCompilerResult.FindMethod()
      // This would allow us to be more clear about specialized vs. not
      // in our tests.

      bool found = false;
      foreach (var reachableMethod in analysis.ReachableMethods()) {

        var containingTypeReference = reachableMethod.ContainingType; //Going to fail if we have a specialized method (which we will -- will have to make sure to get the unspecialized in the RTA).

        INamedTypeReference containingNamedTypeReference;

        if (containingTypeReference is IGenericTypeInstanceReference) {
          containingNamedTypeReference = ((IGenericTypeInstanceReference)containingTypeReference).GenericType;
        }
        else {
          containingNamedTypeReference = ((INamedTypeReference)containingTypeReference);
        }

        IName reachableClassIName = containingNamedTypeReference.Name;

        if (methodIName.UniqueKey == reachableMethod.Name.UniqueKey && classIName.UniqueKey == reachableClassIName.UniqueKey) {
          found = true;
          break;
        }
      }
      return found;
    }
 /// <summary>
 /// Allocates a simple host environment using default settings inherited from MetadataReaderHost and that
 /// uses PeReader as its metadata reader.
 /// </summary>
 public DefaultHost()
   : base(new NameTable()) {
   this.peReader = new PeReader(this);
 }
Exemple #23
0
        static void mergeClassWithAncestor(NamespaceTypeDefinition mergeTargetClass, PeReader.DefaultHost host) {

            // get class from which was inherited
            ITypeDefinition ancestorClass = null;
            INamedEntity ancestorClassName = null;
            if (mergeTargetClass.BaseClasses.Count == 1) {

                // TODO
                // entferne wenn Methode umgeschrieben
                /*
                // ignore inheritance from System.Object
                if (mergeTargetClass.BaseClasses[0].ToString() != "System.Object") {
                    ancestorClass = (mergeTargetClass.BaseClasses[0] as NamespaceTypeDefinition);
                }
                // return if ancestor class equals System.Object
                else {
                    return;
                }
                */



                // check base class is of type NamespaceTypeDefinition
                if (mergeTargetClass.BaseClasses.ElementAt(0) as NamespaceTypeDefinition != null) {
                    ancestorClass = (mergeTargetClass.BaseClasses.ElementAt(0) as ITypeDefinition);
                    ancestorClassName = (mergeTargetClass.BaseClasses.ElementAt(0) as INamedEntity);
                }                
                // ignore inheritance from System.Object
                else if (mergeTargetClass.BaseClasses[0].ToString() == "System.Object") {                                       
                    return;                 
                }
                // check for needed values in base class and its resolved value (for example when base class of type NamespaceTypeReference)
                else if ((mergeTargetClass.BaseClasses.ElementAt(0).ResolvedType as ITypeDefinition != null)
                    && (mergeTargetClass.BaseClasses.ElementAt(0) as INamedEntity != null)) {
                    // TODO
                    // weiss nicht ob es Sinn macht externe Klassen wie z.B. system.ValueType
                    // in die aktuelle Klasse rein zu mergen => denn Trick mit methoden auf Virtual stellen
                    // damit JIT Compiler unsere Methoden aufruft klappt nicht in dem Fall (da wir die Methoden von System.ValueType nicht umschreiben koennen)
                    //ancestorClass = (mergeTargetClass.BaseClasses[0].ResolvedType as ITypeDefinition);
                    //ancestorClassName = (mergeTargetClass.BaseClasses[0].ResolvedType as INamedEntity);
                    return;
                }
                else {
                    throw new ArgumentException("Do not know how to handle base class.");
                }
            }
            else {
                throw new ArgumentException("Do not know how to handle multiple inheritance.");
            }


            // copy all attributes from the ancestor class to the current class
            if (ancestorClass.Fields != null) {
                globalLog.writeLine("Copying fileds");
                foreach (FieldDefinition field in ancestorClass.Fields) {
                    FieldDefinition copy = new FieldDefinition();
                    globalLog.writeLine("Copying field: " + field.Name.ToString());
                    copyField(copy, field);
                    copy.ContainingTypeDefinition = mergeTargetClass;

                    // set intern factory of the copied field to the one of the class
                    // (without it, the generated binary file will have strange results like use only the same field)
                    copy.InternFactory = mergeTargetClass.InternFactory;

                    mergeTargetClass.Fields.Add(copy);
                }
                globalLog.writeLine("");
            }

            // list of all constructors of the ancestor class
            List<MethodDefinition> copiedAncestorConstructors = new List<MethodDefinition>();            

            // copy methods from the ancestor class to the current class
            if (ancestorClass.Methods != null) {
                globalLog.writeLine("Copying methods");
                foreach (IMethodDefinition method in ancestorClass.Methods) {
                    globalLog.writeLine("Copying method: " + method.Name.ToString());

                    // check if the method is a constructor
                    if (method.IsConstructor) {
                        MethodDefinition copiedAncestorConstructor = new MethodDefinition();
                        copyMethod(copiedAncestorConstructor, method, host);

                        // TODO
                        // name muss noch geaendert werden
                        String tempName = "ctor_" + ancestorClassName.Name.ToString();
                        copiedAncestorConstructor.Name = host.NameTable.GetNameFor(tempName);

                        // constructor has the "SpecialName" (the name describes the functionality)
                        // and "RTSpecialName" (runtime should check name encoding) flag set 
                        // runtime will raise an exception if these flags are set for a copied constructor
                        // which is added as a normal function 
                        copiedAncestorConstructor.IsSpecialName = false;
                        copiedAncestorConstructor.IsRuntimeSpecial = false;




                        // TODO:
                        // vielleicht einfach von späterer Schleife machen lassen (sollte jetzt schon gehen, aber noch nicht getestet,
                        // deshalb erst ein mal drin gelassen)
                        // copy constructor and rewrite instructions to use attributes and functions
                        // inside the same class
                        var testIlGenerator = new ILGenerator(host, copiedAncestorConstructor);
                        foreach (var operation in copiedAncestorConstructor.Body.Operations) {
                            switch (operation.OperationCode) {

                                case OperationCode.Call:

                                    // HIER NOCH CHECKEN OB ANDERE FUNKTIONEN INNERHALB DES ANCESTORS AUFGERUFEN WERDEN
                                    // DIESE MUESSEN UMGELEITET WERDEN

                                    /*
                                    Microsoft.Cci.MutableCodeModel.MethodDefinition blah = null;
                                    if (operation.Value is Microsoft.Cci.MutableCodeModel.MethodDefinition) {
                                        blah = (Microsoft.Cci.MutableCodeModel.MethodDefinition)(operation.Value);
                                        NamespaceTypeDefinition test = (NamespaceTypeDefinition)blah.ContainingTypeDefinition;

                                        if (ancestorClass.Name.Equals(test.Name)) {
                                            System.Console.WriteLine("Ja");
                                        }


                                    }
                                    */

                                    testIlGenerator.Emit(operation.OperationCode, operation.Value);
                                    break;

                                case OperationCode.Stfld:

                                    FieldDefinition attribute = (FieldDefinition)operation.Value;

                                    // search for the method attribute that is used in the copied constructor
                                    // and replace it with the method attribute from the current class
                                    FieldDefinition foundField = null;
                                    foreach (FieldDefinition field in mergeTargetClass.Fields) {
                                        if (attribute.Name.Equals(field.Name)
                                            && attribute.Type.Equals(field.Type)) {
                                            foundField = field;
                                            break;
                                        }
                                    }
                                    if (foundField == null) {
                                        System.Console.WriteLine("Attribute not found");
                                        return;
                                    }

                                    testIlGenerator.Emit(operation.OperationCode, foundField);

                                    break;

                                default:
                                    testIlGenerator.Emit(operation.OperationCode, operation.Value);
                                    break;

                            }
                        }

                        // create new body
                        List<ILocalDefinition> variableListCopy = new List<ILocalDefinition>(method.Body.LocalVariables);
                        List<ITypeDefinition> privateHelperTypesListCopy = new List<ITypeDefinition>(method.Body.PrivateHelperTypes);
                        var newBody = new ILGeneratorMethodBody(testIlGenerator, method.Body.LocalsAreZeroed, method.Body.MaxStack, copiedAncestorConstructor, variableListCopy, privateHelperTypesListCopy);
                        copiedAncestorConstructor.Body = newBody;

                        // set intern factory of the copied constructor to the one of the class
                        // (without it, the generated binary file will have strange results like call always the same method)
                        copiedAncestorConstructor.InternFactory = mergeTargetClass.InternFactory;

                        // add copied constructor to the class
                        copiedAncestorConstructor.ContainingTypeDefinition = mergeTargetClass;
                        copiedAncestorConstructor.Visibility = TypeMemberVisibility.Private;
                        mergeTargetClass.Methods.Add(copiedAncestorConstructor);

                        // add copied ancestor constructor to the list of copied ancestor constructors
                        copiedAncestorConstructors.Add(copiedAncestorConstructor);

                        continue;
                    }

                    else {

                        // copy method
                        MethodDefinition copy = new MethodDefinition();
                        copyMethod(copy, method, host);
                        copy.ContainingTypeDefinition = mergeTargetClass;

                        // set intern factory of the copied method to the one of the class
                        // (without it, the generating of the binary file will have strange results)
                        copy.InternFactory = mergeTargetClass.InternFactory;

                        // add method to the class
                        if (mergeTargetClass.Methods == null) {
                            mergeTargetClass.Methods = new List<IMethodDefinition>();
                        }
                        mergeTargetClass.Methods.Add(copy);
                    }
                }
                globalLog.writeLine("");
            }


            // rewrite constructors of the class to call the copied constructor instead of
            // the constructor of the ancestor class
            // (rewriting methods is handled later)
            foreach (MethodDefinition method in mergeTargetClass.Methods) {

                // check if method is a constructor
                if (method.IsConstructor) {

                    var testIlGenerator = new ILGenerator(host, method);
                    foreach (var operation in method.Body.Operations) {

                        switch (operation.OperationCode) {

                            // only call instructions that call a constructor have to be redirected
                            case OperationCode.Call:

                                MethodDefinition calledMethod = (MethodDefinition)operation.Value;
                                if (calledMethod.IsConstructor) {

                                    // TODO hier treten noch Probleme auf, wenn System.Object ist nicht der oberste Ancestor
                                    // sondern z.B. System.ValueType
                                    // check if the called constructor is not the constructor of System.Object
                                    // and if the constructor that is called is not a constructor of THIS class
                                    NamespaceTypeDefinition calledMethodNamespace = (NamespaceTypeDefinition)calledMethod.ContainingTypeDefinition;
                                    if (calledMethodNamespace.ToString() != "System.Object"
                                        && !calledMethodNamespace.Name.Equals(mergeTargetClass.Name)) {

                                        // search for the copied constructor that has to be called
                                        // (it is searched by parameter types because they always are named the same ".ctor")
                                        MethodDefinition copiedConstructorToCall = null;
                                        foreach (MethodDefinition copiedAncestorConstructor in copiedAncestorConstructors) {

                                            // check if the count of the parameters of the called constructor and the copied constructor are the same
                                            if (copiedAncestorConstructor.ParameterCount == calledMethod.ParameterCount) {

                                                // check if the parameters have the same type in the same order
                                                bool found = true;
                                                for (int i = 0; i < calledMethod.ParameterCount; i++) {
                                                    if (calledMethod.Parameters[i].Type != copiedAncestorConstructor.Parameters[i].Type) {
                                                        found = false;
                                                        break;
                                                    }
                                                }
                                                if (found) {
                                                    copiedConstructorToCall = copiedAncestorConstructor;
                                                    break;
                                                }
                                            }
                                        }
                                        if (copiedConstructorToCall == null) {
                                            throw new ArgumentException("No copied constructor with the same parameter types in the same order.");
                                        }

                                        // call copied constructor instead of constructor of the ancestor
                                        testIlGenerator.Emit(operation.OperationCode, copiedConstructorToCall);

                                    }
                                    // just emit if the called constructor is the constructor of System.Object
                                    else {
                                        testIlGenerator.Emit(operation.OperationCode, operation.Value);
                                    }
                                }

                                // just emit operation if no constructor is called
                                // (rewriting methods is handled later)
                                else {
                                    testIlGenerator.Emit(operation.OperationCode, operation.Value);
                                }

                                break;

                            // just emit operation if none of the above cases match
                            default:
                                testIlGenerator.Emit(operation.OperationCode, operation.Value);
                                break;
                        }
                    }

                    // create new body
                    List<ILocalDefinition> variableListCopy = new List<ILocalDefinition>(method.Body.LocalVariables);
                    List<ITypeDefinition> privateHelperTypesListCopy = new List<ITypeDefinition>(method.Body.PrivateHelperTypes);
                    var newBody = new ILGeneratorMethodBody(testIlGenerator, method.Body.LocalsAreZeroed, method.Body.MaxStack, method, variableListCopy, privateHelperTypesListCopy);
                    method.Body = newBody;
                }
            }


            // rewrite all methods to use local attributes/methods instead of the ones of the ancestor
            foreach (MethodDefinition method in mergeTargetClass.Methods) {

                var testIlGenerator = new ILGenerator(host, method);
                foreach (var operation in method.Body.Operations) {
                    switch (operation.OperationCode) {

                        case OperationCode.Stfld:
                        case OperationCode.Ldfld:

                            // get namespace of attribute
                            FieldDefinition attribute = (FieldDefinition)operation.Value;
                            INamedTypeDefinition attributeNamespace = (INamedTypeDefinition)attribute.ContainingTypeDefinition;

                            // check if namespace of attribute equals the namespace of THIS class
                            // => just emit operation
                            if (mergeTargetClass.Name.Equals(attributeNamespace.Name)) {
                                testIlGenerator.Emit(operation.OperationCode, operation.Value);
                                continue;
                            }

                            // try the namespace of all ancestors of this class to see if it was copied
                            else {

                                // flag that indicates if the operation was emitted during the search loop
                                bool operationEmitted = false;

                                // search through all ancestors to find the namespace of the used attribute
                                NamespaceTypeDefinition tempAncestorClass = mergeTargetClass;
                                while (true) {

                                    // get class from which was inherited
                                    if (tempAncestorClass.BaseClasses.Count == 1) {
                                        // ignore inheritance from System.Object
                                        if (tempAncestorClass.BaseClasses[0].ToString() != "System.Object") {
                                            tempAncestorClass = (tempAncestorClass.BaseClasses[0] as NamespaceTypeDefinition);
                                        }
                                        // return if ancestor class equals System.Object
                                        else {
                                            break;
                                        }
                                    }
                                    else {
                                        throw new ArgumentException("Do not know how to handle multiple inheritance.");
                                    }

                                    // check namespace of used attribute is equal to namespace of ancestor
                                    // => change target of operation to THIS class                                        
                                    if (tempAncestorClass.Name.Equals(attributeNamespace.Name)) {

                                        // search for the method attribute that is used in the copied method
                                        // and replace it with the method attribute from the current class
                                        FieldDefinition foundField = null;
                                        foreach (FieldDefinition field in mergeTargetClass.Fields) {
                                            if (attribute.Name.Equals(field.Name)
                                                && attribute.Type.Equals(field.Type)) {
                                                foundField = field;
                                                break;
                                            }
                                        }
                                        if (foundField == null) {
                                            throw new ArgumentException("Attribute not found.");
                                        }

                                        // emit operation and set flag that it was emitted
                                        operationEmitted = true;
                                        testIlGenerator.Emit(operation.OperationCode, foundField);
                                        break;
                                    }
                                }

                                // if operation was not emitted yet => emit it
                                if (operationEmitted == false) {
                                    testIlGenerator.Emit(operation.OperationCode, operation.Value);
                                }
                            }
                            break;

                        case OperationCode.Call:

                            // just emit if value is of type method reference
                            if (operation.Value is Microsoft.Cci.MutableCodeModel.MethodReference) {
                                testIlGenerator.Emit(operation.OperationCode, operation.Value);
                                continue;
                            }

                            // get namespace of called method
                            MethodDefinition calledMethod = (MethodDefinition)operation.Value;
                            NamespaceTypeDefinition calledMethodNamespace = (NamespaceTypeDefinition)calledMethod.ContainingTypeDefinition;

                            // check if namespace of called method equals the namespace of THIS class
                            // => just emit operation
                            if (mergeTargetClass.Name.Equals(calledMethodNamespace.Name)) {
                                testIlGenerator.Emit(operation.OperationCode, operation.Value);
                                continue;
                            }

                            // try the namespace of all ancestors of this class to see if it was copied
                            else {

                                // flag that indicates if the operation was emitted during the search loop
                                bool operationEmitted = false;

                                // search through all ancestors to find the namespace of the called method
                                NamespaceTypeDefinition tempAncestorClass = mergeTargetClass;
                                while (true) {

                                    // get class from which was inherited
                                    if (tempAncestorClass.BaseClasses.Count == 1) {
                                        // ignore inheritance from System.Object
                                        if (tempAncestorClass.BaseClasses[0].ToString() != "System.Object") {
                                            tempAncestorClass = (tempAncestorClass.BaseClasses[0] as NamespaceTypeDefinition);
                                        }
                                        // return if ancestor class equals System.Object
                                        else {
                                            break;
                                        }
                                    }
                                    else {
                                        throw new ArgumentException("Do not know how to handle multiple inheritance.");
                                    }

                                    // check namespace of called method is equal to namespace of ancestor, if the same
                                    // => find copied method in THIS class
                                    // => change target of operation to method in THIS class    
                                    if (tempAncestorClass.Name.Equals(calledMethodNamespace.Name)) {

                                        // search through all methods in THISS class and search for the copied one
                                        MethodDefinition foundMethod = null;
                                        foreach (MethodDefinition newMethod in mergeTargetClass.Methods) {

                                            // skip constructors (can not be called from the ancestor)
                                            if (newMethod.IsConstructor) {
                                                continue;
                                            }

                                            if (newMethod.ParameterCount == calledMethod.ParameterCount) {

                                                // check if the parameters have the same type in the same order
                                                bool parameterCorrect = true;
                                                for (int i = 0; i < calledMethod.ParameterCount; i++) {
                                                    if (calledMethod.Parameters[i].Type != newMethod.Parameters[i].Type) {
                                                        parameterCorrect = false;
                                                        break;
                                                    }
                                                }

                                                // if the parameters are correct => check the name
                                                if (parameterCorrect) {
                                                    if (calledMethod.Name.Equals(newMethod.Name)) {

                                                        // if name is the same => found method to call
                                                        foundMethod = newMethod;
                                                        break;
                                                    }
                                                }
                                            }
                                        }

                                        // check if the method we want to call was found
                                        if (foundMethod == null) {
                                            throw new ArgumentException("Did not find a method to call.");
                                        }

                                        // emit operation and set flag that it was emitted
                                        operationEmitted = true;
                                        testIlGenerator.Emit(operation.OperationCode, foundMethod);
                                        break;

                                    }
                                }

                                // if operation was not emitted yet => emit it
                                if (operationEmitted == false) {
                                    testIlGenerator.Emit(operation.OperationCode, operation.Value);
                                }
                            }
                            break;

                        // just emit operation if none of the above cases match
                        default:
                            testIlGenerator.Emit(operation.OperationCode, operation.Value);
                            break;

                    }
                }

                // create new body
                List<ILocalDefinition> variableListCopy = new List<ILocalDefinition>(method.Body.LocalVariables);
                List<ITypeDefinition> privateHelperTypesListCopy = new List<ITypeDefinition>(method.Body.PrivateHelperTypes);
                var newBody = new ILGeneratorMethodBody(testIlGenerator, method.Body.LocalsAreZeroed, method.Body.MaxStack, method, variableListCopy, privateHelperTypesListCopy);
                method.Body = newBody;
            }
        }
 internal HostEnvironment()
   : base(new NameTable(), new InternFactory(), 0, null, false) {
   this.peReader = new PeReader(this);
 }
Exemple #25
0
        public CodeMutator(IModule module, PeReader.DefaultHost host, Log.Log logger, Random prng, MethodCfg methodCfg,
            CfgManipulator manipulator, bool debugging=false)
        {
            this.host = host;
            this.logger = logger;
            this.module = module;
            this.prng = prng;
            this.methodCfg = methodCfg;
            this.debugging = debugging;
            this.manipulator = manipulator;

            returnBlock = new BasicBlock();
            var exitBranch = new ExitBranchTarget();

            returnBlock.exitBranch = exitBranch;
            returnBlock.operations.Add(createNewOperation(OperationCode.Ret));

            methodCfg.basicBlocks.Add(returnBlock);
        }
 /// <summary>
 /// Searches the analysis for a field by the name of f and returns a bool depending on whether f is reachable
 /// </summary>
 static bool IsFieldReachable(PeReader.DefaultHost host, RapidTypeAnalysis analysis, string f) {
   var fieldName = host.NameTable.GetNameFor(f);
   bool found = false;
   foreach (var field in analysis.ReachableFields()) {
     if (fieldName.UniqueKey == field.Name.UniqueKey) {
       found = true;
       break;
     }
   }
   return found;
 }
Exemple #27
0
 public CompilerHostEnvironment()
     : base(new NameTable(), 4)
 {
     PeReader = new PeReader(this);
 }
      public TestCompilerResults(IAssembly mainAssembly, IAssembly[] libraryAssemblies, PeReader.DefaultHost host) {
        this.MainAssembly = mainAssembly;

        this.Host = host;

        this.WholeProgram = new WholeProgram(new IAssembly[] { mainAssembly }, host);
      }
 public NopTransformer(IModule module, PeReader.DefaultHost host, Log.Log logger) {
     this.host = host;
     this.logger = logger;
     this.module = module;
 }
Exemple #30
0
        static void copyMethod(MethodDefinition dest, IMethodDefinition source, PeReader.DefaultHost host) {

            // only copy body if it is not a dummy (= empty)
            if (!(source.Body is Microsoft.Cci.Dummy)) {
                // TODO
                // langsames kopieren des Bodies, schnellerer weg möglich?
                var testIlGenerator = new ILGenerator(host, dest);
                foreach (var operation in source.Body.Operations) {
                    testIlGenerator.Emit(operation.OperationCode, operation.Value);
                }
                List<ILocalDefinition> variableListCopy = new List<ILocalDefinition>(source.Body.LocalVariables);
                List<ITypeDefinition> privateHelperTypesListCopy = new List<ITypeDefinition>(source.Body.PrivateHelperTypes);
                var newBody = new ILGeneratorMethodBody(testIlGenerator, source.Body.LocalsAreZeroed, source.Body.MaxStack, dest, variableListCopy, privateHelperTypesListCopy);
                dest.Body = newBody;
            }

            dest.CallingConvention = source.CallingConvention;
            if (source.IsGeneric)
                dest.GenericParameters = new List<IGenericMethodParameter>(source.GenericParameters);
            else
                dest.GenericParameters = null;
            if (source.ParameterCount > 0)
                dest.Parameters = new List<IParameterDefinition>(source.Parameters);
            else
                dest.Parameters = null;
            if (source.IsPlatformInvoke)
                dest.PlatformInvokeData = source.PlatformInvokeData;
            else
                dest.PlatformInvokeData = Dummy.PlatformInvokeInformation;
            dest.ReturnValueAttributes = new List<ICustomAttribute>(source.ReturnValueAttributes);
            if (source.ReturnValueIsModified)
                dest.ReturnValueCustomModifiers = new List<ICustomModifier>(source.ReturnValueCustomModifiers);
            else
                dest.ReturnValueCustomModifiers = new List<ICustomModifier>(0);
            if (source.ReturnValueIsMarshalledExplicitly)
                dest.ReturnValueMarshallingInformation = source.ReturnValueMarshallingInformation;
            else
                dest.ReturnValueMarshallingInformation = Dummy.MarshallingInformation;
            if (source.HasDeclarativeSecurity && IteratorHelper.EnumerableIsNotEmpty(source.SecurityAttributes))
                dest.SecurityAttributes = new List<ISecurityAttribute>(source.SecurityAttributes);
            else
                dest.SecurityAttributes = null;
            dest.Type = source.Type;
            dest.AcceptsExtraArguments = source.AcceptsExtraArguments;
            dest.HasDeclarativeSecurity = source.HasDeclarativeSecurity;
            dest.IsAbstract = source.IsAbstract;
            dest.IsAccessCheckedOnOverride = source.IsAccessCheckedOnOverride;
            dest.IsCil = source.IsCil;
            dest.IsExternal = source.IsExternal;
            dest.IsForwardReference = source.IsForwardReference;
            dest.IsHiddenBySignature = source.IsHiddenBySignature;
            dest.IsNativeCode = source.IsNativeCode;
            dest.IsNewSlot = source.IsNewSlot;
            dest.IsNeverInlined = source.IsNeverInlined;
            dest.IsAggressivelyInlined = source.IsAggressivelyInlined;
            dest.IsNeverOptimized = source.IsNeverOptimized;
            dest.IsPlatformInvoke = source.IsPlatformInvoke;
            dest.IsRuntimeImplemented = source.IsRuntimeImplemented;
            dest.IsRuntimeInternal = source.IsRuntimeInternal;
            dest.IsRuntimeSpecial = source.IsRuntimeSpecial;
            dest.IsSealed = source.IsSealed;
            dest.IsSpecialName = source.IsSpecialName;
            dest.IsStatic = source.IsStatic;
            dest.IsSynchronized = source.IsSynchronized;
            dest.IsUnmanaged = source.IsUnmanaged;
            if (dest.IsStatic)
                dest.IsVirtual = false;
            else
                dest.IsVirtual = source.IsVirtual;
            dest.PreserveSignature = source.PreserveSignature;
            dest.RequiresSecurityObject = source.RequiresSecurityObject;
            dest.ReturnValueIsByRef = source.ReturnValueIsByRef;
            dest.ReturnValueIsMarshalledExplicitly = source.ReturnValueIsMarshalledExplicitly;
            dest.ReturnValueName = source.ReturnValueName;
            dest.Name = source.Name;
            dest.Visibility = source.Visibility;
        }
 /// <summary>
 /// Allocates a simple host environment using default settings inherited from MetadataReaderHost and that
 /// uses PeReader as its metadata reader.
 /// </summary>
 /// <param name="nameTable">
 /// A collection of IName instances that represent names that are commonly used during compilation.
 /// This is a provided as a parameter to the host environment in order to allow more than one host
 /// environment to co-exist while agreeing on how to map strings to IName instances.
 /// </param>
 /// <param name="projectToCLRTypes">True if the host should project references to certain Windows Runtime types and methods
 /// to corresponding CLR types and methods, in order to emulate the runtime behavior of the CLR.</param>
 public DefaultWindowsRuntimeHost(INameTable nameTable, bool projectToCLRTypes = true)
   : base(nameTable, new InternFactory(), 0, null, false, projectToCLRTypes) {
   this.peReader = new PeReader(this);
 }
 /// <summary>
 /// Allocates a simple host environment using default settings inherited from MetadataReaderHost and that
 /// uses PeReader as its metadata reader.
 /// </summary>
 public DefaultHost()
     : base(new NameTable(), new InternFactory(), 0, null, true)
 {
     this.peReader = new PeReader(this);
 }
Exemple #33
0
 internal SanitizerHostEnvironment(NameTable nameTable)
   : base(nameTable, new InternFactory(), 4, null, true)
 {
   this.peReader = new PeReader(this);
 }
Exemple #34
0
 public CompilerHostEnvironment()
     : base(new NameTable(), new InternFactory(), 0, null, false)
 {
     PeReader = new PeReader(this);
 }
 /// <summary>
 /// Allocates a simple host environment using default settings inherited from MetadataReaderHost and that
 /// uses PeReader as its metadata reader.
 /// </summary>
 public DefaultHost()
     : base(new NameTable())
 {
     this.peReader = new PeReader(this);
 }