Пример #1
0
 public void ExtpBind()
 {
     var sig = new ProcedureSignature(
         new Identifier(Registers.ax.Name, PrimitiveType.Word16, Registers.ax),
         new Identifier [] {
             new Identifier(Registers.bx.Name, PrimitiveType.Word16, Registers.bx),
             new Identifier(Registers.cl.Name, PrimitiveType.Byte, Registers.cl) } );
     var ep = new ExternalProcedure("foo", sig);
     Assert.AreEqual("Register word16 foo(Register word16 bx, Register byte cl)", ep.ToString());
     var fn = new ProcedureConstant(PrimitiveType.Pointer32, ep);
     var arch = new FakeArchitecture();
     var frame = arch.CreateFrame();
     var ab = new ApplicationBuilder(new FakeArchitecture(), frame, new CallSite(0, 0), fn, ep.Signature, false);
     var instr = ab.CreateInstruction();
     Assert.AreEqual("ax = foo(bx, cl)", instr.ToString());
 }
Пример #2
0
 public override ExternalProcedure ResolveImportedProcedure(
     IImportResolver resolver, 
     Platform platform, 
     AddressContext ctx)
 {
     var ep = resolver.ResolveProcedure(ModuleName, ImportName, platform);
     if (ep != null)
         return ep;
     // Can we guess at the signature?
     var sig = platform.SignatureFromName(ImportName);
     if (sig != null)
     {
         ep = new ExternalProcedure(ImportName, sig);   //$BUGBUG: mangled name!
     }
     else
     {
         ctx.Warn("Unable to resolve imported reference {0}.", this);
         return new ExternalProcedure(this.ToString(), null);
     }
     return ep;
 }
Пример #3
0
        public Expression ResolveImport(string moduleName, int ordinal, IPlatform platform)
        {
            foreach (var program in project.Programs)
            {
                if (!program.EnvironmentMetadata.Modules.TryGetValue(moduleName, out var mod))
                {
                    continue;
                }

                if (mod.ServicesByOrdinal.TryGetValue(ordinal, out var svc))
                {
                    EnsureSignature(program, svc);
                    var ep = new ExternalProcedure(svc.Name, svc.Signature, svc.Characteristics);
                    return(new ProcedureConstant(platform.PointerType, ep));
                }

                if (mod.GlobalsByOrdinal.TryGetValue(ordinal, out var sym))
                {
                    return(CreateReferenceToImport(sym));
                }
            }
            return(platform.ResolveImportByOrdinal(moduleName, ordinal));
        }
Пример #4
0
 private ProcedureBase GivenFunction(string name, RegisterStorage ret, params object [] args)
 {
     var frame = pb.Program.Architecture.CreateFrame();
     Identifier idRet = null;
     if (ret != null)
     {
         idRet = frame.EnsureRegister(ret);
     }
     List<Identifier> parameters = new List<Identifier>();
     foreach (int offset in args)
     {
         parameters.Add(frame.EnsureStackArgument(offset, pb.Program.Architecture.WordWidth));
     }
     var proc = new ExternalProcedure(name, new ProcedureSignature(
         idRet, 
         parameters.ToArray()));
     return proc;
 }
Пример #5
0
 private void When_Lookup_Procedure(string moduleName, string procName)
 {
     this.extProc = this.win32.LookupProcedureByName(moduleName, procName);
 }
Пример #6
0
        public void Scanner_ImportedProcedure()
        {
            program = new Program();
            program.ImportReferences.Add(
                Address.Ptr32(0x2000),
                new NamedImportReference(
                    Address.Ptr32(0x2000),
                    "module",
                    "grox"));
            Given_Trace(new RtlTrace(0x2000) {
                    m => m.SideEffect(m.Word32(0x1234))
            });
            var groxSig = CreateSignature("ax", "bx");
            var grox = new ExternalProcedure("grox", groxSig);

            importResolver.Stub(i => i.ResolveProcedure(
                Arg<string>.Is.Equal("module"),
                Arg<string>.Is.Equal("grox"),
                Arg<IPlatform>.Is.NotNull)).Return(grox);
            mr.ReplayAll();

            var scan = CreateScanner(program, 0x1000, 0x2000);
            var proc = scan.ScanProcedure(Address.Ptr32(0x2000), "fn000020", arch.CreateProcessorState());
            Assert.AreEqual("grox", proc.Name);
            Assert.AreEqual("ax", proc.Signature.ReturnValue.Name);
            Assert.AreEqual("bx", proc.Signature.Parameters[0].Name);
        }
Пример #7
0
 public void ExdApplication()
 {
     var sig = FunctionType.Action(new[] { Id("r", PrimitiveType.Real32) });
     var ep = new ExternalProcedure("test", sig);
     RunTest(
         Test(m.Fn(ep, m.Load(PrimitiveType.Word32, m.Word32(0x0300400))), VoidType.Instance));
 }
Пример #8
0
 public void TerDeclaration()
 {
     ProgramBuilder pm = new ProgramBuilder();
     pm.Add("proc1", m =>
     {
         var ax = m.Reg16("ax", 0);
         var rand = new ExternalProcedure(
             "rand",
             new FunctionType(
                 new Identifier("ax", PrimitiveType.Int16, ax.Storage),
                 new Identifier[0]));
         m.Declare(ax, m.Fn(rand));
         m.Store(m.Word16(0x1300), ax);
         m.Return();
     });
     RunTest(pm, "Typing/TerDeclaration.txt");
 }
Пример #9
0
 private ExternalProcedure CreateExternalProcedure(string name, Identifier ret, params Identifier[] parameters)
 {
     var ep = new ExternalProcedure(name, new FunctionType(ret, parameters));
     ep.Signature.ReturnAddressOnStack = 4;
     return ep;
 }
Пример #10
0
        public void TrcoCallFunctionWithArraySize()
        {
            var m = new ProcedureBuilder();
            var sig = new ProcedureSignature(null, 
                m.Frame.EnsureStackArgument(0, PrimitiveType.Word32));
            var ex = new ExternalProcedure("malloc", sig, new ProcedureCharacteristics
            {
                Allocator = true,
                ArraySize = new ArraySizeCharacteristic
                {
                    Argument = "r",
                    Factors = new ArraySizeFactor[] 
                    {
                        new ArraySizeFactor { Constant = "1" }
                    }
                }
            });

            Identifier eax = m.Local32("eax");
            var call = m.Assign(eax, m.Fn(new ProcedureConstant(PrimitiveType.Word32, ex), m.Word32(3)));

            coll = CreateCollector();
            call.Accept(eqb);
            call.Accept(coll);
            StringWriter sw = new StringWriter();
            handler.Traits.Write(sw);
            string sExp =
                "T_1 (in malloc : word32)" + nl +
                "\ttrait_func(T_4 -> T_5)" + nl +
                "T_3 (in dwArg00 : word32)" + nl +
                "\ttrait_primitive(word32)" + nl +
                "T_4 (in 0x00000003 : word32)" + nl +
                "\ttrait_primitive(word32)" + nl +
                "\ttrait_equal(T_3)" + nl +
                "T_5 (in malloc(0x00000003) : word32)" + nl +
                "\ttrait_primitive(word32)"; 
            Console.WriteLine(sw.ToString());
            Assert.AreEqual(sExp, sw.ToString());
        }
Пример #11
0
        public void Bwi_CallingAllocaWithNonConstant()
        {
            scanner = mr.StrictMock<IScanner>();
            arch = new IntelArchitecture(ProcessorMode.Protected32);
            program.Platform = new DefaultPlatform(null, arch);

            var sig = CreateSignature(Registers.esp, Registers.eax);
            var alloca = new ExternalProcedure("alloca", sig, new ProcedureCharacteristics
            {
                IsAlloca = true
            });

            trace.Add(m => m.Call(Address.Ptr32(0x2000), 4));

            using (mr.Record())
            {
                scanner.Stub(x => x.FindContainingBlock(
                    Arg<Address>.Is.Anything)).Return(block);
                scanner.Expect(x => x.GetImportedProcedure(
                    Arg<Address>.Is.Equal(Address.Ptr32(0x2000u)),
                    Arg<Address>.Is.NotNull)).Return(alloca);
                scanner.Stub(x => x.GetTrace(null, null, null)).IgnoreArguments().Return(trace);
                    
            }
            var wi = CreateWorkItem(Address.Ptr32(0x1000), new FakeProcessorState(arch));
            wi.ProcessInternal();
            mr.VerifyAll();
            Assert.AreEqual(1, block.Statements.Count);
            Assert.AreEqual("esp = alloca(eax)", block.Statements.Last.ToString());
        }
Пример #12
0
        public void Bwi_CallingAllocaWithConstant()
        {
            scanner = mr.StrictMock<IScanner>();
            prog.Architecture = new IntelArchitecture(ProcessorMode.Protected32);

            var sig = CreateSignature(Registers.esp, Registers.eax);
            var alloca = new ExternalProcedure("alloca", sig);
            alloca.Characteristics = new ProcedureCharacteristics
            {
                IsAlloca = true
            };

            using (mr.Record())
            {
                scanner.Stub(x => x.FindContainingBlock(
                    Arg<Address>.Is.Anything)).Return(block);
                scanner.Expect(x => x.GetImportedProcedure(
                    Arg<Address>.Matches(a => a.ToLinear() == 0x2000u),
                    Arg<Address>.Is.NotNull)).Return(alloca);
                scanner.Stub(x => x.GetTrace(null, null, null)).IgnoreArguments().Return(trace);
            }
            trace.Add(m => m.Call(Address.Ptr32(0x2000), 4));
            var state = new FakeProcessorState(prog.Architecture);
            state.SetRegister(Registers.eax, Constant.Word32(0x0400));
            var wi = CreateWorkItem(Address.Ptr32(0x1000), state);
            wi.ProcessInternal();

            mr.VerifyAll();
            Assert.AreEqual(1, block.Statements.Count);
            Assert.AreEqual("esp = esp - 0x00000400", block.Statements.Last.ToString());
        }
Пример #13
0
 private bool TryGetNoDecompiledProcedure(Address addr, out ExternalProcedure ep)
 {
     Procedure_v1 sProc;
     if (!TryGetNoDecompiledParsedProcedure(addr, out sProc))
     {
         ep = null;
         return false;
     }
     var ser = program.CreateProcedureSerializer();
     var sig = ser.Deserialize(
         sProc.Signature,
         program.Architecture.CreateFrame());
     ep = new ExternalProcedure(sProc.Name, sig);
     return true;
 }
Пример #14
0
 public override ExternalProcedure LookupProcedureByName(string moduleName, string procName)
 {
     //$REVIEW: looks a lot like Win32library, perhaps push to parent class?
     EnsureTypeLibraries(PlatformIdentifier);
     var sig = Metadata.Lookup(procName);
     if (sig == null)
         return null;
     var proc = new ExternalProcedure(procName, sig);
     var characteristics = CharacteristicsLibs.Select(cl => cl.Lookup(procName))
         .Where(c => c != null)
         .FirstOrDefault();
     if (characteristics != null)
         proc.Characteristics = characteristics;
     return proc;
 }
Пример #15
0
        private bool TryGetNoDecompiledProcedure(Address addr, out ExternalProcedure ep)
        {
            Procedure_v1 sProc;
            if (!program.User.Procedures.TryGetValue(addr, out sProc) ||
                sProc.Decompile)
            {
                ep = null;
                return false;
            }

            FunctionType sig = null;
            if (!string.IsNullOrEmpty(sProc.CSignature))
            {
                var usb = new UserSignatureBuilder(program);
                var procDecl = usb.ParseFunctionDeclaration(sProc.CSignature);
                if (procDecl != null)
                {
                    var ser = program.CreateProcedureSerializer();
                    sig = ser.Deserialize(
                        procDecl.Signature,
                        program.Architecture.CreateFrame());
                }
            }
            else
            {
                Warn(addr, "The user-defined procedure at address {0} did not have a signature.", addr); 
            }

            ep = new ExternalProcedure(sProc.Name, sig);
            return true;
        }
Пример #16
0
 public void Vafs_X86Sprintf()
 {
     Given_VaScanner(x86);
     Given_StackString(8, "%c");
     Assert.IsTrue(vafs.TryScan(addrInstr, x86SprintfSig, printfChr));
     var ep = new ExternalProcedure("sprintf", x86SprintfSig);
     var pc = new ProcedureConstant(new CodeType(), ep);
     var instr = vafs.BuildInstruction(pc, new CallSite(4, 0));
     Assert.AreEqual(
         "sprintf(Mem0[esp:(ptr char)], Mem0[esp + 4:(ptr char)], " +
                 "Mem0[esp + 8:char])",
         instr.ToString());
     var appl = (Application)((SideEffect)instr).Expression;
     var sig = ((ProcedureConstant)appl.Procedure).Procedure.Signature;
     Assert.AreEqual(
         "(fn void ((ptr char), (ptr char), char))",
         sig.ToString());
 }