Ejemplo n.º 1
0
        private IEnumerable <DataType> ParseVarargsFormat(
            string varargsParserTypename,
            Address addrInstr,
            ProcedureCharacteristics chr,
            string format)
        {
            var svc  = services.RequireService <IPluginLoaderService>();
            var type = svc.GetType(varargsParserTypename);

            if (type == null)
            {
                throw new TypeLoadException(
                          string.Format(
                              "Unable to load {0} varargs parser.",
                              chr.VarargsParserClass));
            }
            var varargsParser = (IVarargsFormatParser)Activator.CreateInstance(
                type,
                program,
                addrInstr,
                format,
                services);

            varargsParser.Parse();
            return(varargsParser.ArgumentTypes);
        }
        public void Setup()
        {
            this.sc        = new ServiceContainer();
            this.x86       = new X86ArchitectureFlat32();
            this.ppc       = new PowerPcArchitecture32();
            this.m         = new ProcedureBuilder();
            this.printfChr = new ProcedureCharacteristics()
            {
                VarargsParserClass =
                    "Reko.Libraries.Libc.PrintfFormatParser,Reko.Libraries.Libc"
            };
            this.x86PrintfSig = new FunctionType(
                null,
                StackId(null, 4, CStringType()),
                StackId("...", 8, new UnknownType()));
            this.x86SprintfSig = new FunctionType(
                null,
                StackId(null, 4, CStringType()),
                StackId(null, 8, CStringType()),
                StackId("...", 12, new UnknownType()));
            this.ppcPrintfSig = new FunctionType(
                null,
                RegId(null, ppc, "r3", CStringType()),
                RegId("...", ppc, "r4", new UnknownType()));
            this.addrInstr = Address.Ptr32(0x123400);
            var listener = new FakeDecompilerEventListener();

            sc.AddService <DecompilerEventListener>(listener);
        }
Ejemplo n.º 3
0
        private static ProcedureCharacteristics DeserializeXml(string x)
        {
            XmlSerializer            ser = SerializedLibrary.CreateSerializer_v1(typeof(ProcedureCharacteristics));
            ProcedureCharacteristics pc  = (ProcedureCharacteristics)ser.Deserialize(new StringReader(x));

            return(pc);
        }
Ejemplo n.º 4
0
 public UserProcedure(Address address, string name)
 {
     Address         = address;
     Name            = name;
     Assume          = new List <RegisterValue_v2>();
     Characteristics = new ProcedureCharacteristics();
 }
Ejemplo n.º 5
0
            public Expression Intrinsic(string name, bool isIdempotent, ProcedureCharacteristics c, DataType returnType, params Expression[] args)
            {
                var intrinsic = EnsureIntrinsic(name, isIdempotent, returnType, args.Length);

                intrinsic.Characteristics = c;
                return(new Application(new ProcedureConstant(PrimitiveType.Ptr32, intrinsic), returnType, args));
            }
Ejemplo n.º 6
0
            public Expression PseudoProcedure(string name, ProcedureCharacteristics c, DataType returnType, params Expression[] args)
            {
                var ppp = EnsurePseudoProcedure(name, returnType, args.Length);

                ppp.Characteristics = c;
                return(new Application(new ProcedureConstant(PrimitiveType.Ptr32, ppp), returnType, args));
            }
Ejemplo n.º 7
0
 public IntrinsicBuilder(string intrinsicName, bool hasSideEffect, ProcedureCharacteristics characteristics)
 {
     this.intrinsicName   = intrinsicName;
     this.hasSideEffect   = hasSideEffect;
     this.characteristics = characteristics;
     this.parameters      = new List <Identifier>();
 }
Ejemplo n.º 8
0
        public void DynLink_MangledProcedureCharacteristics()
        {
            var chr = new ProcedureCharacteristics
            {
                VarargsParserClass = "FakeParser",
            };

            program.EnvironmentMetadata.Characteristics.Add("foo@mangled", chr);
            platform.Setup(p => p.SignatureFromName(
                               It.Is <string>(n => n == "foo@mangled")))
            .Returns(new Procedure_v1
            {
                Name      = "foo",
                Signature = new SerializedSignature(),
            });

            var eventListener = new FakeDecompilerEventListener();
            var proj          = new Project();
            var impres        = new DynamicLinker(proj, program, eventListener);
            var ep            = impres.ResolveProcedure("", "foo@mangled", platform.Object);

            var expected = @"
void foo()
VarargsParserClass: FakeParser";

            AssertExternalProcedure(expected, ep);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Bind the formal parameters of the signature to actual arguments in
        /// the frame of the calling procedure.
        /// </summary>
        /// <param name="sigCallee"></param>
        /// <param name="chr"></param>
        /// <returns></returns>
        public virtual List <Expression> BindArguments(FunctionType sigCallee, ProcedureCharacteristics chr)
        {
            var actuals = new List <Expression>();

            for (int i = 0; i < sigCallee.Parameters.Length; ++i)
            {
                var formalArg = sigCallee.Parameters[i];
                if (formalArg.Storage is OutArgumentStorage)
                {
                    var outArg = BindOutArg(formalArg);
                    actuals.Add(outArg);
                }
                else
                {
                    var actualArg = Bind(formalArg);
                    actuals.Add(actualArg);
                }
            }
            if (sigCallee.IsVariadic)
            {
                return(BindVariadicArguments(sigCallee, chr, actuals));
            }
            else
            {
                return(actuals);
            }
        }
Ejemplo n.º 10
0
        private void RewriteIll()
        {
            var c = new ProcedureCharacteristics
            {
                Terminates = true,
            };

            m.SideEffect(host.Intrinsic("__ill", false, c, VoidType.Instance));
        }
Ejemplo n.º 11
0
        private void RewriteIll()
        {
            var c = new ProcedureCharacteristics
            {
                Terminates = true,
            };

            emitter.SideEffect(host.PseudoProcedure("__ill", c, VoidType.Instance));
        }
Ejemplo n.º 12
0
        private void RewriteHalt()
        {
            var c = new ProcedureCharacteristics
            {
                Terminates = true,
            };

            m.SideEffect(host.PseudoProcedure("__halt", c, VoidType.Instance));
        }
Ejemplo n.º 13
0
            public Expression PseudoProcedure(string name, ProcedureCharacteristics c, DataType returnType, params Expression[] args)
            {
                var ppp = new PseudoProcedure(name, returnType, args.Length);

                return(new Application(
                           new ProcedureConstant(arch.PointerType, ppp),
                           returnType,
                           args));
            }
Ejemplo n.º 14
0
        private void RewriteSigrie(MipsInstruction instr)
        {
            var chr = new ProcedureCharacteristics
            {
                Terminates = true
            };

            m.SideEffect(host.PseudoProcedure("__reserved_instruction", chr,
                                              VoidType.Instance, RewriteOperand(instr.Operands[0])));
        }
Ejemplo n.º 15
0
        public Expression Intrinsic(string name, bool hasSideEffect, ProcedureCharacteristics c, DataType returnType, params Expression[] args)
        {
            var intrinsic = program.EnsureIntrinsicProcedure(name, hasSideEffect, returnType, args);

            intrinsic.Characteristics = c;
            return(new Application(
                       new ProcedureConstant(PrimitiveType.Ptr32, intrinsic),
                       returnType,
                       args));
        }
Ejemplo n.º 16
0
        private void RewriteHlt()
        {
            var c = new ProcedureCharacteristics
            {
                Terminates = true,
            };
            var intrinsic = host.Intrinsic("__hlt", false, c, VoidType.Instance);

            m.SideEffect(intrinsic, InstrClass.Terminates);
        }
Ejemplo n.º 17
0
        public Expression Intrinsic(string name, bool isIdempotent, ProcedureCharacteristics c, DataType returnType, params Expression[] args)
        {
            var intrinsic = Program.EnsureIntrinsicProcedure(name, isIdempotent, returnType, args);

            intrinsic.Characteristics = c;
            return(new Application(
                       new ProcedureConstant(Program.Architecture.PointerType, intrinsic),
                       returnType,
                       args));
        }
Ejemplo n.º 18
0
        public Expression PseudoProcedure(string name, ProcedureCharacteristics c, DataType returnType, params Expression[] args)
        {
            var ppp = Program.EnsurePseudoProcedure(name, returnType, args);

            ppp.Characteristics = c;
            return(new Application(
                       new ProcedureConstant(Program.Architecture.PointerType, ppp),
                       returnType,
                       args));
        }
Ejemplo n.º 19
0
        public void ProcChar_DeserializeIsMalloc()
        {
            string x = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
                       "<ProcedureCharacteristics>" +
                       "   <allocator>1</allocator>" +
                       "</ProcedureCharacteristics>";
            ProcedureCharacteristics pc = DeserializeXml(x);

            Assert.IsTrue(pc.Allocator);
        }
Ejemplo n.º 20
0
        private void RewriteHalt()
        {
            iclass = InstrClass.Terminates;
            var c = new ProcedureCharacteristics
            {
                Terminates = true,
            };

            m.SideEffect(host.Intrinsic("__halt", false, c, VoidType.Instance));
        }
Ejemplo n.º 21
0
        private void RewriteHlt()
        {
            rtlc = RtlClass.Terminates;
            var c = new ProcedureCharacteristics
            {
                Terminates = true,
            };
            var ppp = host.PseudoProcedure("__hlt", c, VoidType.Instance);

            m.SideEffect(ppp);
        }
Ejemplo n.º 22
0
        public Instruction BuildInstruction(
            Expression callee,
            CallSite site,
            ProcedureCharacteristics chr)
        {
            if (callee is ProcedureConstant pc)
            {
                pc.Procedure.Signature = this.expandedSig;
            }
            var ab = arch.CreateFrameApplicationBuilder(frame, site, callee);

            return(ab.CreateInstruction(this.expandedSig, chr));
        }
Ejemplo n.º 23
0
        public void Setup()
        {
            this.sc = new ServiceContainer();
            var cfg = new Mock <IConfigurationService>();
            var env = new Mock <PlatformDefinition>();

            cfg.Setup(c => c.GetEnvironment(It.IsAny <string>())).Returns(env.Object);
            env.Setup(e => e.Architectures).Returns(new List <PlatformArchitectureDefinition>());
            sc.AddService <IConfigurationService>(cfg.Object);
            sc.AddService <IPluginLoaderService>(new PluginLoaderService());
            this.win32      = new Win32Platform(sc, new X86ArchitectureFlat32(sc, "x86-protected-32", new Dictionary <string, object>()));
            this.win_x86_64 = new Win_x86_64_Platform(sc, new X86ArchitectureFlat64(sc, "x86-protected-64", new Dictionary <string, object>()));
            this.sysV_ppc   = new SysVPlatform(sc, new PowerPcBe32Architecture(sc, "ppc-be-32", new Dictionary <string, object>()));
            this.m          = new ProcedureBuilder();
            this.printfChr  = new ProcedureCharacteristics()
            {
                VarargsParserClass =
                    "Reko.Libraries.Libc.PrintfFormatParser,Reko.Libraries.Libc"
            };
            this.x86PrintfSig = new FunctionType(
                null,
                StackId(null, 4, CStringType32()))
            {
                IsVariadic = true
            };
            this.x86SprintfSig = new FunctionType(
                null,
                StackId(null, 4, CStringType32()),
                StackId(null, 8, CStringType32()))
            {
                IsVariadic = true
            };
            this.win_x86_64PrintfSig = new FunctionType(
                null,
                RegId(null, win_x86_64, "rcx", CStringType64()))
            {
                IsVariadic = true
            };
            this.ppcPrintfSig = new FunctionType(
                null,
                RegId(null, sysV_ppc, "r3", CStringType32()))
            {
                IsVariadic = true
            };
            this.addrInstr = Address.Ptr32(0x123400);
            this.listener  = new FakeDecompilerEventListener();
            sc.AddService <DecompilerEventListener>(listener);
            this.dummyPc = new ProcedureConstant(PrimitiveType.Ptr32, new ExternalProcedure("dummy", x86PrintfSig));
        }
Ejemplo n.º 24
0
        private void RewriteBinOv(Func <Expression, Expression, Expression> fn)
        {
            RewriteBin(fn);
            var dst = Rewrite(instr.op3);

            m.BranchInMiddleOfInstruction(
                m.Not(host.PseudoProcedure("OV", PrimitiveType.Bool, dst)),
                instr.Address + instr.Length,
                RtlClass.ConditionalTransfer);
            var ch = new ProcedureCharacteristics {
                Terminates = true
            };

            m.SideEffect(host.PseudoProcedure("__trap_overflow", ch, VoidType.Instance));
        }
Ejemplo n.º 25
0
        public void Vafs_NoVarargs()
        {
            Given_VaScanner(win32);
            var emptyChr = new ProcedureCharacteristics();
            var emptySig = new FunctionType();

            Assert.IsFalse(vafs.TryScan(addrInstr, dummyPc, null, null));
            Assert.IsFalse(vafs.TryScan(addrInstr, dummyPc, emptySig, null));
            Assert.IsFalse(vafs.TryScan(addrInstr, dummyPc, null, emptyChr));
            Assert.IsFalse(vafs.TryScan(addrInstr, dummyPc, emptySig, emptyChr));
            Assert.IsFalse(vafs.TryScan(addrInstr, dummyPc, x86PrintfSig, null));
            Assert.IsFalse(vafs.TryScan(addrInstr, dummyPc, x86PrintfSig, emptyChr));
            Assert.IsFalse(vafs.TryScan(addrInstr, dummyPc, null, printfChr));
            Assert.IsFalse(vafs.TryScan(addrInstr, dummyPc, emptySig, printfChr));
        }
Ejemplo n.º 26
0
        public bool TryScan(Address addrInstr, FunctionType sig, ProcedureCharacteristics chr)
        {
            if (
                sig == null || !sig.IsVarargs() ||
                chr == null || !VarargsParserSet(chr)
                )
            {
                this.sig = null;    //$out parameter
                return(false);
            }
            var format   = ReadVarargsFormat(sig);
            var argTypes = ParseVarargsFormat(addrInstr, chr, format);

            this.sig = ReplaceVarargs(sig, argTypes);
            return(true);
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Creates an instruction:
        ///     a = foo(b)
        /// or
        ///     foo(b)
        ///  depending on whether the signature returns a value or is of
        /// type 'void'
        /// </summary>
        /// <returns></returns>
        public Instruction CreateInstruction(
            FunctionType sigCallee,
            ProcedureCharacteristics chr)
        {
            if (sigCallee == null || !sigCallee.ParametersValid)
            {
                throw new InvalidOperationException("No signature available; application cannot be constructed.");
            }
            this.sigCallee = sigCallee;

            Expression expOut = null;
            DataType   dtOut  = VoidType.Instance;

            if (!sigCallee.HasVoidReturn)
            {
                expOut = BindReturnValue(sigCallee.ReturnValue);
                dtOut  = sigCallee.ReturnValue.DataType;
            }
            var        actuals = BindArguments(sigCallee, chr);
            Expression appl    = new Application(
                callee,
                dtOut,
                actuals.ToArray());

            if (expOut == null)
            {
                return(new SideEffect(appl));
            }
            else if (expOut is Identifier idOut)
            {
                int extraBits = expOut.DataType.BitSize - sigCallee.ReturnValue.DataType.BitSize;
                if (extraBits > 0)
                {
                    // The non-live bits of expOut can safely be replaced with anything,
                    // since they won't be used. Later stages of the decompilation
                    // process should remove the unused bits.
                    var dtUnused = PrimitiveType.CreateWord(extraBits);
                    var unused   = Constant.Create(dtUnused, 0);
                    appl = new MkSequence(expOut.DataType, unused, appl);
                }
                return(new Assignment(idOut, appl));
            }
            else
            {
                return(new Store(expOut, appl));
            }
        }
Ejemplo n.º 28
0
        private void RewriteBinOv(Func <Expression, Expression, Expression> fn)
        {
            RewriteBin(fn);
            var dst = Rewrite(instr.Operands[2]);

            m.BranchInMiddleOfInstruction(
                m.Not(host.Intrinsic("OV", false, PrimitiveType.Bool, dst)),
                instr.Address + instr.Length,
                InstrClass.ConditionalTransfer);
            var ch = new ProcedureCharacteristics {
                Terminates = true
            };

            m.SideEffect(
                host.Intrinsic("__trap_overflow", true, ch, VoidType.Instance),
                InstrClass.Transfer | InstrClass.Call);
        }
Ejemplo n.º 29
0
        public void Setup()
        {
            this.mr = new MockRepository();
            this.sc = new ServiceContainer();
            var cfg = mr.Stub <IConfigurationService>();
            var env = mr.Stub <OperatingEnvironment>();

            cfg.Stub(c => c.GetEnvironment("")).IgnoreArguments().Return(env);
            env.Stub(e => e.Architectures).Return(new List <IPlatformArchitectureElement>());
            cfg.Replay();
            env.Replay();
            sc.AddService <IConfigurationService>(cfg);
            this.win32      = new Win32Platform(sc, new X86ArchitectureFlat32());
            this.win_x86_64 = new Win_x86_64_Platform(sc, new X86ArchitectureFlat64());
            this.sysV_ppc   = new SysVPlatform(sc, new PowerPcBe32Architecture {
                Name = "ppc-be-32"
            });
            this.m         = new ProcedureBuilder();
            this.printfChr = new ProcedureCharacteristics()
            {
                VarargsParserClass =
                    "Reko.Libraries.Libc.PrintfFormatParser,Reko.Libraries.Libc"
            };
            this.x86PrintfSig = new FunctionType(
                null,
                StackId(null, 4, CStringType32()),
                StackId("...", 8, new UnknownType()));
            this.x86SprintfSig = new FunctionType(
                null,
                StackId(null, 4, CStringType32()),
                StackId(null, 8, CStringType32()),
                StackId("...", 12, new UnknownType()));
            this.win_x86_64PrintfSig = new FunctionType(
                null,
                RegId(null, win_x86_64, "rcx", CStringType64()),
                RegId("...", win_x86_64, "rdx", new UnknownType()));
            this.ppcPrintfSig = new FunctionType(
                null,
                RegId(null, sysV_ppc, "r3", CStringType32()),
                RegId("...", sysV_ppc, "r4", new UnknownType()));
            this.addrInstr = Address.Ptr32(0x123400);
            var listener = new FakeDecompilerEventListener();

            sc.AddService <DecompilerEventListener>(listener);
        }
Ejemplo n.º 30
0
        private void RewriteCall(
            Statement stm,
            CallInstruction ci,
            FunctionType sig,
            ProcedureCharacteristics chr)
        {
            ssam.AdjustRegisterAfterCall(
                stm,
                ci,
                this.arch.StackRegister,
                sig.StackDelta - ci.CallSite.SizeOfReturnAddressOnStack);
            var ab = new ApplicationBuilder(
                arch, ssa.Procedure.Frame, ci.CallSite,
                ci.Callee, sig, false);

            stm.Instruction = ab.CreateInstruction();
            ssaIdTransformer.Transform(stm, ci);
        }
Ejemplo n.º 31
0
 public SimulatedProc EnsureProc(
     Module module, 
     string procName,
     Action<IProcessorEmulator> emulator,
     ProcedureCharacteristics chars = null)
 {
     SimulatedProc proc;
     if (!module.Procedures.TryGetValue(procName, out proc))
     {
         var extProc = platform.LookupProcedureByName(module.Name, procName);
         proc = new SimulatedProc(procName, emulator);
         proc.Signature = extProc.Signature;
         if (chars != null)
             proc.Characteristics = chars;
         proc.uFakedAddress = ++this.uPseudoFn;
         InterceptedCalls[proc.uFakedAddress] = proc;
         module.Procedures.Add(procName, proc);
     }
     return proc;
 }
Ejemplo n.º 32
0
 public ExternalProcedure(string name, ProcedureSignature signature, ProcedureCharacteristics chars) : base(name)
 {
     this.Signature = signature;
     this.Characteristics = chars;
 }