Example #1
0
        private void Given_Scanner()
        {
            this.host = new Mock <IRewriterHost>();
            var dev = new Mock <DecompilerEventListener>();

            //host.Setup(h => h.EnsurePseudoProcedure(null, null, 0))
            //    .IgnoreArguments()
            //    .Return(new PseudoProcedure("<>", PrimitiveType.Word32, 2));
            host.Setup(h => h.PseudoProcedure(
                           It.IsAny <string>(),
                           It.IsAny <DataType>(),
                           It.IsAny <Expression[]>())).Returns((Expression)null);
            host.Setup(h => h.GetImport(
                           It.IsAny <Address>(),
                           It.IsAny <Address>())).Returns((Expression)null);
            host.Setup(h => h.GetImportedProcedure(
                           It.IsAny <IProcessorArchitecture>(),
                           It.IsAny <Address>(),
                           It.IsAny <Address>())).Returns((ExternalProcedure)null);
            var frame = program.Architecture.CreateFrame();

            this.sr = new ScanResults
            {
                Instructions    = new Dictionary <Address, RtlInstructionCluster>(),
                KnownProcedures = new HashSet <Address>(),
            };
            this.sh = new ShingledScanner(program, host.Object, frame, sr, dev.Object);
        }
Example #2
0
        private void Given_Scanner()
        {
            var host = mr.Stub <IRewriterHost>();

            host.Stub(h => h.EnsurePseudoProcedure(null, null, 0))
            .IgnoreArguments()
            .Return(new PseudoProcedure("<>", PrimitiveType.Word32, 2));
            host.Replay();
            this.sh = new ShingledScanner(program, host);
        }
Example #3
0
        private void Given_Scanner()
        {
            this.host = mr.Stub <IRewriterHost>();
            var dev = mr.Stub <DecompilerEventListener>();

            //host.Stub(h => h.EnsurePseudoProcedure(null, null, 0))
            //    .IgnoreArguments()
            //    .Return(new PseudoProcedure("<>", PrimitiveType.Word32, 2));
            host.Stub(h => h.PseudoProcedure("", VoidType.Instance, null)).IgnoreArguments().Return(null);
            host.Stub(h => h.GetImport(null, null)).IgnoreArguments().Return(null);
            host.Stub(h => h.GetImportedProcedure(null, null, null)).IgnoreArguments().Return(null);
            host.Replay();
            dev.Replay();
            var frame = program.Architecture.CreateFrame();

            this.sr = new ScanResults
            {
                Instructions    = new SortedList <Address, RtlInstructionCluster>(),
                KnownProcedures = new HashSet <Address>(),
            };
            this.sh = new ShingledScanner(program, host, frame, sr, dev);
        }
Example #4
0
        private void ScanProgram(Program program)
        {
            try
            {
                eventListener.ShowStatus("Rewriting reachable machine code.");
                scanner = CreateScanner(program);
                var tlDeser = program.CreateTypeLibraryDeserializer();
                foreach (var global in program.User.Globals)
                {
                    var addr = global.Key;
                    var dt   = global.Value.DataType.Accept(tlDeser);
                    scanner.EnqueueUserGlobalData(addr, dt);
                }
                foreach (ImageSymbol ep in program.EntryPoints.Values)
                {
                    scanner.EnqueueImageSymbol(ep, true);
                }
                foreach (Procedure_v1 up in program.User.Procedures.Values)
                {
                    scanner.EnqueueUserProcedure(up);
                }
                foreach (ImageSymbol sym in program.ImageSymbols.Values.Where(s => s.Type == SymbolType.Procedure))
                {
                    if (sym.NoDecompile)
                    {
                        program.EnsureUserProcedure(sym.Address, sym.Name, false);
                    }
                    else
                    {
                        scanner.EnqueueImageSymbol(sym, false);
                    }
                }
                scanner.ScanImage();

                if (program.User.Heuristics.Contains("HeuristicScanning"))
                {
                    //eventListener.ShowStatus("Finding machine code using heuristics.");
                    //scanner.ScanImageHeuristically();
                }
                if (program.User.Heuristics.Contains("Shingle heuristic"))
                {
                    eventListener.ShowStatus("Shingle scanning");
                    var sh    = new ShingledScanner(program, (IRewriterHost)scanner, eventListener);
                    var watch = new Stopwatch();
                    watch.Start();
                    var procs  = sh.Scan();
                    var pprocs = procs.ToList();
                    watch.Stop();
                    Debug.Print(
                        "Elapsed time: {0} msec for {1} procs",
                        watch.ElapsedMilliseconds,
                        pprocs.Count);

                    foreach (var addr in procs)
                    {
                        scanner.ScanProcedure(addr.Key, null, program.Architecture.CreateProcessorState());
                    }
                }
                eventListener.ShowStatus("Finished rewriting reachable machine code.");
            }
            finally
            {
                eventListener.ShowStatus("Writing .asm and .dis files.");
                host.WriteDisassembly(program, w => DumpAssembler(program, w));
                host.WriteIntermediateCode(program, w => EmitProgram(program, null, w));
            }
        }