Ejemplo n.º 1
0
        public override ProcedureSignature Deserialize(SerializedSignature ss, Frame frame)
        {
            if (ss == null)
                return null;
            this.argser = new ArgumentSerializer(this, Architecture, frame, ss.Convention);
            Identifier ret = null;
            int fpuDelta = FpuStackOffset;

            FpuStackOffset = 0;
            if (ss.ReturnValue != null)
            {
                ret = argser.DeserializeReturnValue(ss.ReturnValue);
                fpuDelta += FpuStackOffset;
            }

            FpuStackOffset = 0;
            var args = new List<Identifier>();
            if (ss.Arguments != null)
            {
                for (int iArg = 0; iArg < ss.Arguments.Length; ++iArg)
                {
                    var sArg = ss.Arguments[iArg];
                    var arg = DeserializeArgument(sArg, iArg, ss.Convention);
                    args.Add(arg);
                }
                fpuDelta -= FpuStackOffset;
            }
            FpuStackOffset = fpuDelta;

            var sig = new ProcedureSignature(ret, args.ToArray());
            ApplyConvention(ss, sig);
            return sig;
        }
Ejemplo n.º 2
0
        public override ProcedureSignature Deserialize(SerializedSignature ss, Core.Frame frame)
        {
            if (ss == null)
                return null;
            var argser = new ArgumentSerializer(this, Architecture, frame, ss.Convention);
            Identifier ret = null;

            if (ss.ReturnValue != null)
            {
                ret = argser.DeserializeReturnValue(ss.ReturnValue);
            }

            var args = new List<Identifier>();
            this.gr = 0;
            if (ss.Arguments != null)
            {
                for (int iArg = 0; iArg < ss.Arguments.Length; ++iArg)
                {
                    var sArg = ss.Arguments[iArg];
                    Identifier arg = DeserializeArgument(argser, sArg);
                    args.Add(arg);
                }
            }

            var sig = new ProcedureSignature(ret, args.ToArray());
            return sig;
        }
Ejemplo n.º 3
0
		public EquivalenceClassBuilder(TypeFactory factory, TypeStore store)
		{
			this.factory = factory;
			this.store = store;
			this.signature = null;
            this.segTypevars = new Dictionary<ushort, TypeVariable>();
		}
Ejemplo n.º 4
0
        public void ApplyConvention(SerializedSignature ssig, ProcedureSignature sig)
        {
            string d = ssig.Convention;
            if (d == null || d.Length == 0)
                d = DefaultConvention;
            if (d == "stdapi" || d == "__stdcall")  //$BUGBUG: platform-dependent!
                sig.StackDelta = StackOffset;

            sig.FpuStackDelta = FpuStackOffset;
        }
Ejemplo n.º 5
0
		public void PsigValidArguments()
		{
			Identifier arg = new Identifier(Registers.eax.Name, Registers.eax.DataType, Registers.eax);
			ProcedureSignature sig = new ProcedureSignature(null, new Identifier[] { arg });
			Assert.IsTrue(sig.ParametersValid);

			sig = new ProcedureSignature(arg, null);
			Assert.IsTrue(sig.ParametersValid);

			sig = new ProcedureSignature();
			Assert.IsFalse(sig.ParametersValid);
		}
Ejemplo n.º 6
0
		public ApplicationBuilderTests()
		{
			arch = new IntelArchitecture(ProcessorMode.Protected32);
            frame = arch.CreateFrame();
			ret = frame.EnsureRegister(Registers.eax);
			arg04 = new Identifier("arg04",   PrimitiveType.Word32, new StackArgumentStorage(4, PrimitiveType.Word32));
			arg08 = new Identifier("arg08",   PrimitiveType.Word16, new StackArgumentStorage(8, PrimitiveType.Word16));
			arg0C = new Identifier("arg0C",   PrimitiveType.Byte, new StackArgumentStorage(0x0C, PrimitiveType.Byte));
			regOut = new Identifier("edxOut", PrimitiveType.Word32, new OutArgumentStorage(frame.EnsureRegister(Registers.edx)));
            sig = new ProcedureSignature(ret,
                new Identifier[] { arg04, arg08, arg0C, regOut })
                {
                };
		
        }
 public void Test()
 {
     Given_ProcedureSerializer("stdapi");
     ProcedureSignature sig = new ProcedureSignature(
         new Identifier("qax", PrimitiveType.Word32, Registers.eax),
         new Identifier[] {
             new Identifier("qbx", PrimitiveType.Word32, Registers.ebx)
         });
         
     SerializedSignature ssig = ser.Serialize(sig);
     Assert.IsNotNull(ssig.ReturnValue);
     Assert.AreEqual("qax", ssig.ReturnValue.Name);
     Register_v1 sreg = (Register_v1) ssig.ReturnValue.Kind;
     Assert.AreEqual("eax", sreg.Name);
 }
        public void PpcPs_Serialize()
        {
            Given_ProcedureSerializer();
            ProcedureSignature sig = new ProcedureSignature(
                new Identifier("qax", PrimitiveType.Word32, arch.Registers[3]),
                new Identifier[] {
                    new Identifier("qbx", PrimitiveType.Word32, arch.Registers[3])
                });

            SerializedSignature ssig = ser.Serialize(sig);
            Assert.IsNotNull(ssig.ReturnValue);
            Assert.AreEqual("qax", ssig.ReturnValue.Name);
            Register_v1 sreg = (Register_v1)ssig.ReturnValue.Kind;
            Assert.AreEqual("r3", sreg.Name);
        }
Ejemplo n.º 9
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());
		}
Ejemplo n.º 10
0
		public override void VisitApplication(Application appl)
		{
			signature = null;
			appl.Procedure.Accept(this);
			ProcedureSignature sig = signature;

			if (sig != null)
			{
				if (sig.Parameters.Length != appl.Arguments.Length)
					throw new InvalidOperationException("Parameter count must match.");
			}

			for (int i = 0; i < appl.Arguments.Length; ++i)
			{
				appl.Arguments[i].Accept(this);
				if (sig != null)
				{
					EnsureTypeVariable(sig.Parameters[i]);
					store.MergeClasses(appl.Arguments[i].TypeVariable, sig.Parameters[i].TypeVariable);
				}
			}
			EnsureTypeVariable(appl);
		}
Ejemplo n.º 11
0
		public void FrBindMixedParameters()
		{
			Frame f = new Frame(PrimitiveType.Word16);
			Identifier ax = f.EnsureRegister(Registers.ax);
			Identifier cx = f.EnsureRegister(Registers.cx);
			int stack = PrimitiveType.Word16.Size;
			Identifier arg1 = f.EnsureStackLocal(-stack, PrimitiveType.Word16);

			ProcedureSignature sig = new ProcedureSignature(
				ax,
			    cx,
			    new Identifier("arg0", PrimitiveType.Word16, new StackArgumentStorage(0, PrimitiveType.Word16)));
			
			var cs = new CallSite(stack, 0);
			ProcedureConstant fn = new ProcedureConstant(PrimitiveType.Pointer32, new PseudoProcedure("bar", sig));
			ApplicationBuilder ab = new ApplicationBuilder(arch, f, cs, fn, sig, true);
            Instruction instr = ab.CreateInstruction();
			using (FileUnitTester fut = new FileUnitTester("Core/FrBindMixedParameters.txt"))
			{
				f.Write(fut.TextWriter);
				fut.TextWriter.WriteLine(instr.ToString());
				fut.AssertFilesEqual();
			}
		}
Ejemplo n.º 12
0
		public void FrBindStackParameters()
		{
			Frame f = new Frame(PrimitiveType.Word16);
			f.ReturnAddressSize = 4;						// far call.
			int stack = 2;
			Identifier loc02 = f.EnsureStackLocal(-stack, PrimitiveType.Word16, "wLoc02");
			stack += loc02.DataType.Size;
			Identifier loc04 = f.EnsureStackLocal(-stack, PrimitiveType.Word16, "wLoc04");

			ProcedureSignature sig = new ProcedureSignature(
				null, new Identifier[] {
					new Identifier("arg0", PrimitiveType.Word16, new StackArgumentStorage(4, PrimitiveType.Word16)),
					new Identifier("arg1", PrimitiveType.Word16, new StackArgumentStorage(6, PrimitiveType.Word16)) });

			var cs = new CallSite(f.ReturnAddressSize + 2 * 4, 0);
			var fn = new ProcedureConstant(PrimitiveType.Pointer32, new PseudoProcedure("foo", sig));
			ApplicationBuilder ab = new ApplicationBuilder(arch, f, cs, fn, sig, true);
            Instruction instr = ab.CreateInstruction(); 
			using (FileUnitTester fut = new FileUnitTester("Core/FrBindStackParameters.txt"))
			{
				f.Write(fut.TextWriter);
				fut.TextWriter.WriteLine(instr.ToString());
				fut.AssertFilesEqual();
			}
		}
Ejemplo n.º 13
0
 public ExternalProcedure(string name, ProcedureSignature signature, ProcedureCharacteristics chars) : base(name)
 {
     this.Signature = signature;
     this.Characteristics = chars;
 }
Ejemplo n.º 14
0
		public ExternalProcedure(string name, ProcedureSignature signature) : base(name)
		{
			this.Signature = signature;
		}
Ejemplo n.º 15
0
        public void Bwi_CallProcedureWithSignature()
        {
            var proc2 = new Procedure("fn2000", new Frame(PrimitiveType.Pointer32));
            var sig = new ProcedureSignature(
                proc2.Frame.EnsureRegister(new RegisterStorage("r1", 1, PrimitiveType.Word32)),
                proc2.Frame.EnsureRegister(new RegisterStorage("r2", 2, PrimitiveType.Word32)),
                proc2.Frame.EnsureRegister(new RegisterStorage("r3", 3, PrimitiveType.Word32)));
            proc2.Signature = sig;
            var block2 = new Block(proc, "l00100008");
            var block3 = new Block(proc, "l00100004");
            arch.Stub(a => a.PointerType).Return(PrimitiveType.Pointer32);
            scanner.Expect(s => s.FindContainingBlock(Address.Ptr32(0x00001000))).IgnoreArguments().Return(block).Repeat.Times(2);
            scanner.Expect(s => s.FindContainingBlock(Address.Ptr32(0x00001004))).IgnoreArguments().Return(block2); // .Repeat.Times(2);
            scanner.Expect(s => s.GetImportedProcedure(null, null)).IgnoreArguments().Return(null);
            scanner.Expect(s => s.EnqueueJumpTarget(
                Arg<Address>.Is.NotNull,
                Arg<Address>.Matches(a => a.ToLinear() == 0x00100004),
                Arg<Procedure>.Is.NotNull,
                Arg<ProcessorState>.Is.NotNull))
                .Return(block3);
            scanner.Expect(s => s.ScanProcedure(
                Arg<Address>.Is.Equal(Address.Ptr32(0x2000)),
                Arg<string>.Is.Null,
                Arg<ProcessorState>.Is.NotNull)).Return(proc2);
            scanner.Expect(s => s.GetTrace(null, null, null)).IgnoreArguments().Return(trace);
            mr.ReplayAll();

            trace.Add(m => m.Call(Address.Ptr32(0x2000), 0));
            trace.Add(m => m.Return(0, 0));
            var wi = CreateWorkItem(Address.Ptr32(0x1000), new FakeProcessorState(arch));
            wi.ProcessInternal();

            mr.VerifyAll();
            var sw = new StringWriter();
            proc.Write(false, sw);
            var sExp = @"// testProc
// Return size: 0
void testProc()
testProc_entry:
l00100000:
	r1 = fn2000(r2, r3)
	goto l00100004
	// succ:  l00100004
testProc_exit:
";
            Assert.AreEqual(sExp, sw.ToString());
        }
Ejemplo n.º 16
0
 public void BwiX86_SetCallSignatureAdAddress(Address addrCallInstruction, ProcedureSignature signature)
 {
     sigs.Add(addrCallInstruction.ToLinear(), signature);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Perform any adjustments to the processor's state after returning from a procedure call with the
 /// specified signature.
 /// </summary>
 /// <param name="sigCallee">The signature of the called procedure.</param>
 public abstract void OnAfterCall(Identifier stackReg, ProcedureSignature sigCallee, ExpressionVisitor<Expression> eval);
Ejemplo n.º 18
0
		public PseudoProcedure(string name, ProcedureSignature sig) : base(name)
		{
			this.sig = sig;
		}
Ejemplo n.º 19
0
 public override void OnAfterCall(Identifier stackReg, ProcedureSignature sigCallee, ExpressionVisitor<Expression> eval)
 {
 }
Ejemplo n.º 20
0
 public override void OnAfterCall(Identifier sp, ProcedureSignature sig, ExpressionVisitor<Expression> eval)
 {
     if (sig == null)
         return;
     var spReg = (RegisterStorage)sp.Storage;
     var spVal = GetValue(spReg);
     var stackOffset = SetValue(
         spReg,
         new BinaryExpression(
             Operator.IAdd,
             spVal.DataType,
             sp,
             Constant.Create(
                 PrimitiveType.CreateWord(spReg.DataType.Size),
                 sig.StackDelta)).Accept(eval));
     if (stackOffset.IsValid)
     {
         if (stackOffset.ToInt32() > 0)
             ErrorListener("Possible stack underflow detected.");
     }
     ShrinkFpuStack(-sig.FpuStackDelta);
 }
Ejemplo n.º 21
0
 public override void OnProcedureLeft(ProcedureSignature sig)
 {
     sig.FpuStackDelta = FpuStackItems;     
 }
Ejemplo n.º 22
0
 public override void OnAfterCall(Identifier stackReg, ProcedureSignature sigCallee, ExpressionVisitor<Expression> eval)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 23
0
		public override void VisitProcedureConstant(ProcedureConstant pc)
		{
			EnsureTypeVariable(pc);
			VisitProcedure(pc.Procedure);
			if (pc.Procedure.Signature != null)
			{
				store.MergeClasses(pc.TypeVariable, pc.Procedure.Signature.TypeVariable);
				signature = pc.Procedure.Signature;
			}
		}
Ejemplo n.º 24
0
 private void RewriteFormals(ProcedureSignature sig)
 {
     if (sig.ReturnValue != null)
         sig.ReturnValue.DataType = sig.ReturnValue.TypeVariable.DataType;
     if (sig.Parameters != null)
     {
         foreach (Identifier formalArg in sig.Parameters)
         {
             if (formalArg.TypeVariable != null)
                 formalArg.DataType = formalArg.TypeVariable.DataType;
         }
     }
 }
Ejemplo n.º 25
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());
        }
Ejemplo n.º 26
0
 public override void OnProcedureLeft(ProcedureSignature procedureSignature)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 27
0
		public void AddCallSignature(Address addr, ProcedureSignature sig)
		{
			callSignatures[addr] = sig;
		}
Ejemplo n.º 28
0
 public override void OnProcedureLeft(ProcedureSignature procedureSignature)
 {
 }
Ejemplo n.º 29
0
 public abstract void OnProcedureEntered();                 // Some registers need to be updated when a procedure is entered.
 public abstract void OnProcedureLeft(ProcedureSignature procedureSignature);