Exemplo n.º 1
0
        public void Transform()
        {
            var ppr = new PtrPrimitiveReplacer(factory, store, program, eventListener);

            ppr.ReplaceAll();
            var cpa = new ConstantPointerAnalysis(factory, store, program);

            cpa.FollowConstantPointers();
            int iteration = 0;

            do
            {
                if (eventListener.IsCanceled())
                {
                    return;
                }
                ++iteration;
                if (iteration > 50)
                {
                    eventListener.Warn(
                        string.Format("Type transformer has looped {0} times, quitting prematurely.", iteration));
                    return;
                }
                Changed = false;
                visitedTypes.Clear();
                foreach (TypeVariable tv in store.TypeVariables)
                {
                    if (eventListener.IsCanceled())
                    {
                        return;
                    }
                    EquivalenceClass eq = tv.Class;
                    if (eq.DataType != null)
                    {
                        DateTime start = DateTime.Now;
                        eq.DataType = eq.DataType.Accept(this);
                        DateTime end = DateTime.Now;
                        if (eq.DataType is UnionType ut)
                        {
                            //trace.Verbose("= TT: took {2,4} msec to simplify {0} ({1})", tv.DataType, eq.DataType, (end - start).Milliseconds);
                        }
                    }
                    if (tv.DataType != null)
                    {
                        tv.DataType = tv.DataType.Accept(this);
                    }
                    // Debug.Print("Transformed {0}:{1}", tv, tv.Class.DataType);
                }
                if (ppr.ReplaceAll())
                {
                    Changed = true;
                }
                if (NestedComplexTypeExtractor.ReplaceAll(factory, store))
                {
                    Changed = true;
                }
            } while (Changed);
        }
Exemplo n.º 2
0
        public void Transform()
        {
            var ppr = new PtrPrimitiveReplacer(factory, store, program);

            ppr.ReplaceAll(eventListener);
            var cpa = new ConstantPointerAnalysis(factory, store, program);

            cpa.FollowConstantPointers();
            int iteration = 0;

            do
            {
                if (eventListener.IsCanceled())
                {
                    return;
                }
                ++iteration;
                if (iteration > 50)
                {
                    eventListener.Warn(new NullCodeLocation(""),
                                       string.Format("Type transformer has looped {0} times, quitting prematurely.", iteration));
                    return;
                }
                Changed           = false;
                this.visitedTypes = new HashSet <DataType>();
                foreach (TypeVariable tv in store.TypeVariables)
                {
                    if (eventListener.IsCanceled())
                    {
                        return;
                    }
                    tvCur = tv;
                    EquivalenceClass eq = tv.Class;
                    if (eq.DataType != null)
                    {
                        eq.DataType = eq.DataType.Accept(this);
                    }
                    if (tv.DataType != null)
                    {
                        tv.DataType = tv.DataType.Accept(this);
                    }
                    // Debug.Print("Transformed {0}:{1}", tv, tv.Class.DataType);
                }
                if (ppr.ReplaceAll(eventListener))
                {
                    Changed = true;
                }
                if (NestedComplexTypeExtractor.ReplaceAll(factory, store))
                {
                    Changed = true;
                }
            } while (Changed);
        }
Exemplo n.º 3
0
		/// <summary>
		/// Performs type analysis and rewrites program based on the inferred
        /// information.
		/// </summary>
		/// <remarks>
		/// For instance, all MemoryAccesses will be converted to structure
        /// field accesses or array accesses as appropriate.
		/// </remarks>
		/// <param name="program"></param>
		public void RewriteProgram(Program program)
		{
            factory = program.TypeFactory;
            store = program.TypeStore;

            aen = new ExpressionNormalizer(program.Platform.PointerType);
            eqb = new EquivalenceClassBuilder(factory, store, eventListener);
            tyco = new TypeCollector(
                program.TypeFactory, program.TypeStore, program,
                eventListener);
            //dpa = new DerivedPointerAnalysis(factory, store, program.Architecture);
            tvr = new TypeVariableReplacer(store);
            trans = new TypeTransformer(factory, store,program, eventListener);
            ctn = new ComplexTypeNamer();
            ter = new TypedExpressionRewriter(program, eventListener);

            // RestrictProcedures(program, 0, 60, true); // Re-enable this for debugging
			aen.Transform(program);
            eqb.Build(program);

            tyco.CollectTypes();
            store.BuildEquivalenceClassDataTypes(factory);
            //dpa.FollowConstantPointers(prog);
            tvr.ReplaceTypeVariables();

            eventListener.ShowStatus("Transforming datatypes.");
			var ppr = new PtrPrimitiveReplacer(factory, store, program);
			ppr.ReplaceAll(eventListener);

			trans.Transform();
			ctn.RenameAllTypes(store);
			ter.RewriteProgram(program);
		}
Exemplo n.º 4
0
        /// <summary>
        /// Performs type analysis and rewrites program based on the inferred information.
        /// </summary>
        /// <remarks>
        /// For instance, all MemoryAccesses will be converted to structure field
        /// accesses or array accesses as appropriate.
        /// </remarks>
        /// <param name="program"></param>
        public void RewriteProgram(Program program)
        {
            factory = program.TypeFactory;
            store   = program.TypeStore;

            aen = new ExpressionNormalizer(program.Platform.PointerType);
            eqb = new EquivalenceClassBuilder(factory, store);
#if OLD
            dtb  = new DataTypeBuilder(factory, store, program.Platform);
            trco = new TraitCollector(factory, store, dtb, program);
#else
            tyco = new TypeCollector(program.TypeFactory, program.TypeStore, program);
#endif
            //dpa = new DerivedPointerAnalysis(factory, store, program.Architecture);
            tvr   = new TypeVariableReplacer(store);
            trans = new TypeTransformer(factory, store, program, eventListener);
            ctn   = new ComplexTypeNamer();
            ter   = new TypedExpressionRewriter(program);

            // RestrictProcedures(program, 0, 1, true); // Re-enable this for debugging
            eventListener.ShowStatus("Gathering primitive datatypes from instructions.");
            aen.Transform(program);
            eqb.Build(program);
#if OLD
            eventListener.ShowStatus("Collecting datatype usage traits.");
            trco.CollectProgramTraits(program);
            eventListener.ShowStatus("Building equivalence classes.");
            dtb.BuildEquivalenceClassDataTypes();
#else
            eventListener.ShowStatus("Collecting data types");
            tyco.CollectTypes();
            store.BuildEquivalenceClassDataTypes(factory);
#endif
            //dpa.FollowConstantPointers(prog);
            tvr.ReplaceTypeVariables();

            eventListener.ShowStatus("Transforming datatypes.");
            var ppr = new PtrPrimitiveReplacer(factory, store, program);
            ppr.ReplaceAll();

            trans.Transform();
            ctn.RenameAllTypes(store);
            store.Dump();
            eventListener.ShowStatus("Rewriting expressions.");
            ter.RewriteProgram(program);
        }
Exemplo n.º 5
0
        private void RunTest(Program program, string outputFile)
        {
            EquivalenceClassBuilder eqb = new EquivalenceClassBuilder(factory, store);
            DataTypeBuilder dtb = new DataTypeBuilder(factory, store, program.Platform);
            eqb.Build(program);
            TraitCollector trco = new TraitCollector(factory, store, dtb, program);
            trco.CollectProgramTraits(program);
            dtb.BuildEquivalenceClassDataTypes();
            var tv = new TypeVariableReplacer(store);
            tv.ReplaceTypeVariables();
            store.CopyClassDataTypesToTypeVariables();
            var ppr = new PtrPrimitiveReplacer(factory, store, program);
            ppr.ReplaceAll(new FakeDecompilerEventListener());

            var cpa = new ConstantPointerAnalysis(factory, store, program);
            cpa.FollowConstantPointers();

            Verify(null, outputFile);
        }
Exemplo n.º 6
0
        protected override void RunTest(Program prog, string outputFilename)
		{
			TypeFactory factory = new TypeFactory();
			store = new TypeStore();
			EquivalenceClassBuilder eqb = new EquivalenceClassBuilder(factory, store);
			eqb.Build(prog);
			DataTypeBuilder dtb = new DataTypeBuilder(factory, store, prog.Platform);
			TraitCollector trco = new TraitCollector(factory, store, dtb, prog);
			trco.CollectProgramTraits(prog);
			dtb.BuildEquivalenceClassDataTypes();

			store.CopyClassDataTypesToTypeVariables();
			TypeVariableReplacer tvr = new TypeVariableReplacer(store);
			tvr.ReplaceTypeVariables();

			PtrPrimitiveReplacer ppr = new PtrPrimitiveReplacer(factory, store, prog);
			ppr.ReplaceAll(new FakeDecompilerEventListener());

			Verify(prog, outputFilename);
		}
Exemplo n.º 7
0
        /// <summary>
        /// Performs type analysis and rewrites program based on the inferred
        /// information.
        /// </summary>
        /// <remarks>
        /// For instance, all MemoryAccesses will be converted to structure
        /// field accesses or array accesses as appropriate.
        /// </remarks>
        /// <param name="program"></param>
        public void RewriteProgram(Program program)
        {
            factory = program.TypeFactory;
            store   = program.TypeStore;
            var timer = new Stopwatch();

            aen  = new ExpressionNormalizer(program.Platform.PointerType);
            eqb  = new EquivalenceClassBuilder(factory, store, eventListener);
            tyco = new TypeCollector(
                program.TypeFactory, program.TypeStore, program,
                eventListener);
            //dpa = new DerivedPointerAnalysis(factory, store, program.Architecture);
            tvr   = new TypeVariableReplacer(store);
            trans = new TypeTransformer(factory, store, program, eventListener);
            ctn   = new ComplexTypeNamer();
            ter   = new TypedExpressionRewriter(program, eventListener);

            RestrictProcedures(program, 0, 0, false);
            Time("Normalizing expressions", () => aen.Transform(program));
            Time("Building equivalence classes", () => eqb.Build(program));
            Time("Collecting data types", tyco.CollectTypes);
            Time("Build eq. class data types", () => store.BuildEquivalenceClassDataTypes(factory));
            //dpa.FollowConstantPointers(program);
            Time("Replacing type variables", tvr.ReplaceTypeVariables);

            eventListener.ShowStatus("Transforming datatypes.");
            Time("Replace primitive types", () =>
            {
                var ppr = new PtrPrimitiveReplacer(factory, store, program, eventListener);
                ppr.ReplaceAll();
            });

            Time("Transforming data types", trans.Transform);
            Time("Renaming data types", () => ctn.RenameAllTypes(store));
            Time("Rewriting program with type information", () => ter.RewriteProgram(program));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Performs type analysis and rewrites program based on the inferred information.
        /// </summary>
        /// <remarks>
        /// For instance, all MemoryAccesses will be converted to structure field
        /// accesses or array accesses as appropriate.
        /// </remarks>
        /// <param name="program"></param>
        public void RewriteProgram(Program program)
        {
            factory = program.TypeFactory;
            store = program.TypeStore;

            aen = new ExpressionNormalizer(program.Platform.PointerType);
            eqb = new EquivalenceClassBuilder(factory, store);
            dtb = new DataTypeBuilder(factory, store, program.Platform);
            trco = new TraitCollector(factory, store, dtb, program);
            //dpa = new DerivedPointerAnalysis(factory, store, program.Architecture);
            tvr = new TypeVariableReplacer(store);
            trans = new TypeTransformer(factory, store,program, eventListener);
            ctn = new ComplexTypeNamer();
            ter = new TypedExpressionRewriter(program);

            // RestrictProcedures(program, 0, 1, true); // Re-enable this for debugging
            eventListener.ShowStatus("Gathering primitive datatypes from instructions.");
            aen.Transform(program);
            eqb.Build(program);
            eventListener.ShowStatus("Collecting datatype usage traits.");
            trco.CollectProgramTraits(program);
            eventListener.ShowStatus("Building equivalence classes.");
            dtb.BuildEquivalenceClassDataTypes();
            //dpa.FollowConstantPointers(prog);
            tvr.ReplaceTypeVariables();

            eventListener.ShowStatus("Transforming datatypes.");
            var ppr = new PtrPrimitiveReplacer(factory, store, program);
            ppr.ReplaceAll();

            trans.Transform();
            ctn.RenameAllTypes(store);
            store.Dump();
            eventListener.ShowStatus("Rewriting expressions.");
            ter.RewriteProgram(program);
        }
Exemplo n.º 9
0
        public void PprReplaceInts()
        {
            var arch = new FakeArchitecture();
            var program = new Program { Architecture = arch, Platform = new DefaultPlatform(null, arch) };

            TypeFactory factory = new TypeFactory();
            store = new TypeStore();
            TypeVariable tv1 = store.CreateTypeVariable(factory);
            TypeVariable tv2 = store.CreateTypeVariable(factory);
            Assert.IsNotNull(tv1.Class, "Expected store.EnsureTypeVariable to create equivalence class");
            Assert.IsNotNull(tv2.Class, "Expected store.EnsureTypeVariable to create equivalence class");
            tv1.Class.DataType = PrimitiveType.Word32;
            tv2.Class.DataType = PrimitiveType.Word16;
            program.Globals.TypeVariable = store.CreateTypeVariable(factory);
            program.Globals.DataType = factory.CreateStructureType();

            TypeVariable tv3 = store.CreateTypeVariable(factory);
            Assert.IsNotNull(tv3.Class, "Expected store.EnsureTypeVariable to create equivalence class");

            StructureType mem = factory.CreateStructureType(null, 0);
            mem.Fields.Add(0, tv1);
            mem.Fields.Add(4, tv2);
            tv3.Class.DataType = factory.CreatePointer(mem, 4);

            store.CopyClassDataTypesToTypeVariables();
            TypeVariableReplacer tvr = new TypeVariableReplacer(store);
            tvr.ReplaceTypeVariables();

            var ppr = new PtrPrimitiveReplacer(factory, store, program);

            ppr.ReplaceAll();

            Verify(null, "Typing/PprReplaceInts.txt");
        }