Deserializes serialized arguments into Identifiers.
Example #1
0
        public override FunctionType Deserialize(SerializedSignature ss, Frame frame)
        {
            if (ss == null)
                return null;
            this.argDeser = new ArgumentDeserializer(this, Architecture, frame, 0, Architecture.WordWidth.Size);
            Identifier ret = null;
            int fpuDelta = FpuStackOffset;

            FpuStackOffset = 0;
            if (ss.ReturnValue != null)
            {
                ret = argDeser.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 FunctionType(ret, args.ToArray());
            ApplyConvention(ss, sig);
            return sig;
        }
Example #2
0
        public override FunctionType Deserialize(SerializedSignature ss, Frame frame)
        {
            if (ss == null)
                return new FunctionType();
            if (ss == null)
                return null;
            var argser = new ArgumentDeserializer(this, Architecture, frame, 0, Architecture.WordWidth.Size);
            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 FunctionType(ret, args.ToArray());
            return sig;
        }
Example #3
0
        public override FunctionType Deserialize(SerializedSignature ss, Frame frame)
        {
            int ncrn = 0;
            int nsaa = 0;
            // mem arg forb ret val

            var argser = new ArgumentDeserializer(
               this,
               Architecture,
               frame,
               Architecture.PointerType.Size,
               Architecture.WordWidth.Size);

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

            var args = new List<Identifier>();
            if (ss.Arguments != null)
            {
                foreach (var sArg in ss.Arguments)
                {
                    var dt = sArg.Type.Accept(TypeLoader);
                    var sizeInWords = (dt.Size + 3) / 4;

                    if (sizeInWords == 2 && (ncrn & 1) == 1)
                        ++ncrn;
                    Identifier arg;
                    if (sizeInWords <= 4 - ncrn)
                    {
                        if (sizeInWords == 2)
                        {
                            arg = frame.EnsureSequence(
                                Architecture.GetRegister(argRegs[ncrn]),
                                Architecture.GetRegister(argRegs[ncrn + 1]),
                                dt);
                            ncrn += 2;
                        }
                        else
                        {
                            arg = frame.EnsureRegister(
                                Architecture.GetRegister(argRegs[ncrn]));
                            ncrn += 1;
                        }
                    }
                    else
                    {
                        arg = frame.EnsureStackArgument(nsaa, dt);
                        nsaa += AlignedStackArgumentSize(dt);
                    }
                    args.Add(arg);
                }
            }
            return new FunctionType(ret, args.ToArray());
        }
Example #4
0
        private Identifier DeserializeArgument(ArgumentDeserializer argser, Argument_v1 sArg)
        {
            Identifier arg;
            if (sArg.Kind != null)
                return sArg.Kind.Deserialize(argser);

            var dtArg = sArg.Type.Accept(TypeLoader);
            var prim = dtArg as PrimitiveType;
            //if (prim != null && prim.Domain == Domain.Real)
            //{
            //    if (this.fr > 8)
            //    {
            //        arg = argser.Deserialize(sArg, new StackVariable_v1());
            //    }
            //    else
            //    {
            //        arg = argser.Deserialize(sArg, new Register_v1("f" + this.fr));
            //        ++this.fr;
            //    }
            //    return arg;
            //}
            if (dtArg.Size <= 4)
            {
                if (this.gr > 3)
                {
                    arg = argser.Deserialize(sArg, new StackVariable_v1());
                }
                else
                {
                    arg = argser.Deserialize(sArg, new Register_v1("r" + gr));
                    ++this.gr;
                }
                return arg;
            }
            if (dtArg.Size <= 8)
            {
                if (this.gr > 9)
                {
                    arg = argser.Deserialize(sArg, new StackVariable_v1());
                }
                else
                {
                    if ((gr & 1) == 0)
                        ++gr;
                    arg = argser.Deserialize(sArg, new SerializedSequence
                    {
                        Registers = new Register_v1[] { 
                            new Register_v1("r" + gr), 
                            new Register_v1("r" + (gr+1))
                        }
                    });
                    gr += 2;
                }
                return arg;
            }
            throw new NotImplementedException();
        }
Example #5
0
        public override FunctionType Deserialize(SerializedSignature ss, Frame frame)
        {
            if (ss == null)
                return null;
            this.argDeser = new ArgumentDeserializer(
                this,
                Architecture,
                frame,
                Architecture.PointerType.Size, //$BUG: x86 real mode?
                Architecture.WordWidth.Size);
            Identifier ret = null;
            int fpuDelta = FpuStackOffset;

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

            FpuStackOffset = 0;
            var args = new List<Identifier>();
            if (ss.EnclosingType != null)
            {
                var arg = DeserializeImplicitThisArgument(ss);
                args.Add(arg);
            }
            if (ss.Arguments != null)
            {
                if (ss.Convention == "pascal")
                {
                    for (int iArg = ss.Arguments.Length -1; iArg >= 0; --iArg)
                    {
                        var sArg = ss.Arguments[iArg];
                        var arg = DeserializeArgument(sArg, iArg, ss.Convention);
                        args.Add(arg);
                    }
                    args.Reverse();
                }
                else
                {
                    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 FunctionType(ret, args.ToArray());
            sig.IsInstanceMetod = ss.IsInstanceMethod;
            ApplyConvention(ss, sig);
            return sig;
        }
Example #6
0
        private Identifier DeserializeArgument(ArgumentDeserializer argser, Argument_v1 sArg)
        {
            Identifier arg;
            if (sArg.Kind != null)
                return argser.Deserialize(sArg);

            var dtArg = sArg.Type.Accept(TypeLoader);
            var prim = dtArg as PrimitiveType;
            arg = argser.Deserialize(sArg, new Register_v1("r" + gr));
            ++this.gr;
            return arg;
        }
 public void Setup()
 {
     mr = new MockRepository();
     var sc = new ServiceContainer();
     sc.AddService<IFileSystemService>(new FileSystemServiceImpl());
     arch = new X86ArchitectureReal();
     platform = new MsdosPlatform(sc, arch);
     sigser = mr.Stub<ProcedureSerializer>(
         arch,
         new TypeLibraryDeserializer(platform, true, new TypeLibrary()),
         "stdapi");
     argser = new ArgumentDeserializer(
         sigser,
         arch,
         arch.CreateFrame(),
         // It's possible that old, hand-written assembler passes
         // arguments on unaligned offsets
         13,
         4);
     mr.ReplayAll();
 }
Example #8
0
 public override Identifier Deserialize(ArgumentDeserializer sser)
 {
     return(sser.Deserialize(this));
 }
Example #9
0
 public override Identifier Deserialize(ArgumentDeserializer sser)
 {
     return(sser.VisitRegister(this));
 }
Example #10
0
		public override Identifier Deserialize(ArgumentDeserializer sser)
		{
			return sser.VisitRegister(this);
		}
Example #11
0
		public override Identifier Deserialize(ArgumentDeserializer sser)
		{
			return sser.Deserialize(this);
		}
Example #12
0
        /// <summary>
        /// Deserializes the signature <paramref name="ss"/>. Any instantiated
        /// registers or stack variables are introduced into the Frame.
        /// </summary>
        /// <param name="ss"></param>
        /// <param name="frame"></param>
        /// <returns></returns>
        public FunctionType Deserialize(SerializedSignature ss, Frame frame)
        {
            if (ss == null)
            {
                return(null);
            }
            // If there is no explict return address size,
            // use the architecture's default return address size.

            var retAddrSize = ss.ReturnAddressOnStack != 0
                ? ss.ReturnAddressOnStack
                : this.Architecture.ReturnAddressOnStack;

            if (!ss.ParametersValid)
            {
                return(new FunctionType
                {
                    StackDelta = ss.StackDelta,
                    ReturnAddressOnStack = retAddrSize,
                });
            }

            var parameters = new List <Identifier>();

            Identifier ret = null;

            if (UseUserSpecifiedStorages(ss))
            {
                this.argDeser = new ArgumentDeserializer(
                    this,
                    Architecture,
                    frame,
                    retAddrSize,
                    Architecture.WordWidth.Size);

                int fpuDelta = FpuStackOffset;
                FpuStackOffset = 0;
                if (ss.ReturnValue != null)
                {
                    ret       = DeserializeArgument(ss.ReturnValue, -1, "");
                    fpuDelta += FpuStackOffset;
                }

                FpuStackOffset = 0;
                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);
                        if (arg != null)
                        {
                            parameters.Add(arg);
                        }
                    }
                }
                fpuDelta      -= FpuStackOffset;
                FpuStackOffset = fpuDelta;
                var sig = new FunctionType(ret, parameters.ToArray())
                {
                    IsVariadic      = this.IsVariadic,
                    IsInstanceMetod = ss.IsInstanceMethod,
                };
                ApplyConvention(ss, sig);
                return(sig);
            }
            else
            {
                var dtRet = ss.ReturnValue != null && ss.ReturnValue.Type != null
                    ? ss.ReturnValue.Type.Accept(TypeLoader)
                    : null;

                var dtThis = ss.EnclosingType != null
                    ? new Pointer(ss.EnclosingType.Accept(TypeLoader), Architecture.PointerType.BitSize)
                    : null;
                var dtParameters = ss.Arguments != null
                    ? ss.Arguments
                                   .TakeWhile(p => p.Name != "...")
                                   .Select(p => p.Type.Accept(TypeLoader))
                                   .ToList()
                    : new List <DataType>();

                // A calling convention governs the storage of a the
                // parameters

                var cc = platform.GetCallingConvention(ss.Convention);
                if (cc == null)
                {
                    // It was impossible to determine a calling convention,
                    // so we don't know how to decode this signature accurately.
                    return(new FunctionType
                    {
                        StackDelta = ss.StackDelta,
                        ReturnAddressOnStack = retAddrSize,
                    });
                }
                var res = new CallingConventionEmitter();
                cc.Generate(res, dtRet, dtThis, dtParameters);
                if (res.Return != null)
                {
                    var retReg = res.Return as RegisterStorage;
                    ret = new Identifier(retReg != null ? retReg.Name : "", dtRet, res.Return);
                }
                if (res.ImplicitThis != null)
                {
                    var param = new Identifier("this", dtThis, res.ImplicitThis);
                    parameters.Add(param);
                }
                bool isVariadic = false;
                if (ss.Arguments != null)
                {
                    for (int i = 0; i < ss.Arguments.Length; ++i)
                    {
                        if (ss.Arguments[i].Name == "...")
                        {
                            isVariadic = true;
                        }
                        else
                        {
                            var name  = GenerateParameterName(ss.Arguments[i].Name, dtParameters[i], res.Parameters[i]);
                            var param = new Identifier(name, dtParameters[i], res.Parameters[i]);
                            parameters.Add(param);
                        }
                    }
                }
                var ft = new FunctionType(ret, parameters.ToArray())
                {
                    IsInstanceMetod = ss.IsInstanceMethod,
                    StackDelta      = ss.StackDelta != 0
                        ? ss.StackDelta
                        : res.StackDelta,
                    FpuStackDelta        = res.FpuStackDelta,
                    ReturnAddressOnStack = retAddrSize,
                    IsVariadic           = isVariadic,
                };
                return(ft);
            }
        }
Example #13
0
 private Identifier DeserializeArgument(ArgumentDeserializer argser, Argument_v1 sArg)
 {
     Identifier arg;
     if (sArg.Kind != null)
         return argser.Deserialize(sArg);
     var dtArg = sArg.Type.Accept(TypeLoader);
     var prim = dtArg as PrimitiveType;
     if (prim != null && prim.Domain == Domain.Real && !firstArgIntegral)
     {
         if ((ir % 2) != 0)
             ++ir;
         if (this.ir >= 4)
         {
             arg = argser.Deserialize(sArg, new StackVariable_v1());
         }
         else
         {
             if (prim.Size == 4)
             {
                 arg = argser.Deserialize(sArg, new Register_v1("f" + (this.ir + 12)));
                 this.ir += 1;
             }
             else if (prim.Size == 8)
             {
                 arg = argser.Deserialize(sArg, new SerializedSequence
                 {
                     Registers = new[] {
                         new Register_v1("f" + (this.ir + 12)),
                         new Register_v1("f" + (this.ir + 13))
                     }
                 });
                 this.ir += 2;
             }
             else
             {
                 throw new NotSupportedException(string.Format("Real type of size {0} not supported.", prim.Size));
             }
         }
         return arg;
     }
     if (ir == 0)
         firstArgIntegral = true;
     if (dtArg.Size <= 4)
     {
         if (this.ir >= 4)
         {
             arg = argser.Deserialize(sArg, new StackVariable_v1());
         }
         else
         {
             arg = argser.Deserialize(sArg, new Register_v1("r" + (ir+4)));
             ++this.ir;
         }
         return arg;
     }
     if (dtArg.Size <= 8)
     {
         if ((ir & 1) != 0)
             ++ir;
         if (this.ir >= 4)
         {
             arg = argser.Deserialize(sArg, new StackVariable_v1());
         }
         else
         {
             arg = argser.Deserialize(sArg, new SerializedSequence
             {
                 Registers = new Register_v1[] {
                     new Register_v1("r" + (ir+4)),
                     new Register_v1("r" + (ir+5))
                 }
             });
             ir += 2;
         }
         return arg;
     }
     throw new NotImplementedException();
 }
Example #14
0
        public override FunctionType Deserialize(SerializedSignature ss, Frame frame)
        {
            if (ss == null)
                return null;
            this.argDeser = new ArgumentDeserializer(
                this,
                Architecture,
                frame,
                Architecture.PointerType.Size,
                Architecture.WordWidth.Size);
            Identifier ret = null;

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

            var args = new List<Identifier>();
            int iArg = 0;
            if (ss.EnclosingType != null && ss.IsInstanceMethod)
            {
                var arg = DeserializeImplicitThisArgument(ss);
                args.Add(arg);
                ++iArg;
            }
            if (ss.Arguments != null)
            {
                for (iArg = 0; iArg < ss.Arguments.Length; ++iArg)
                {
                    var sArg = ss.Arguments[iArg];
                    var arg = DeserializeArgument(sArg, iArg, ss.Convention);
                    args.Add(arg);
                }
            }
            var sig = new FunctionType(ret, args.ToArray());
            sig.IsInstanceMetod = ss.IsInstanceMethod;
            ApplyConvention(ss, sig);
            return sig;
        }
Example #15
0
        /// <summary>
        /// Deserializes the signature <paramref name="ss"/>. Any instantiated
        /// registers or stack variables are introduced into the Frame.
        /// </summary>
        /// <param name="ss"></param>
        /// <param name="frame"></param>
        /// <returns></returns>
        public FunctionType Deserialize(SerializedSignature ss, Frame frame)
        {
            if (ss == null)
            {
                return(null);
            }
            var retAddrSize = this.Architecture.PointerType.Size;   //$TODO: deal with near/far calls in x86-realmode

            if (!ss.ParametersValid)
            {
                return(new FunctionType
                {
                    StackDelta = ss.StackDelta,
                    ReturnAddressOnStack = retAddrSize,
                });
            }

            var parameters = new List <Identifier>();

            Identifier ret = null;

            if (UseUserSpecifiedStorages(ss))
            {
                this.argDeser = new ArgumentDeserializer(
                    this,
                    Architecture,
                    frame,
                    retAddrSize,
                    Architecture.WordWidth.Size);

                int fpuDelta = FpuStackOffset;
                FpuStackOffset = 0;
                if (ss.ReturnValue != null)
                {
                    ret       = DeserializeArgument(ss.ReturnValue, -1, "");
                    fpuDelta += FpuStackOffset;
                }

                FpuStackOffset = 0;
                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);
                        parameters.Add(arg);
                    }
                }
                fpuDelta      -= FpuStackOffset;
                FpuStackOffset = fpuDelta;
                var sig = new FunctionType(ret, parameters.ToArray())
                {
                    IsInstanceMetod = ss.IsInstanceMethod,
                };
                ApplyConvention(ss, sig);
                return(sig);
            }
            else
            {
                var dtRet = ss.ReturnValue != null && ss.ReturnValue.Type != null
                    ? ss.ReturnValue.Type.Accept(TypeLoader)
                    : null;

                var dtThis = ss.EnclosingType != null
                    ? new Pointer(ss.EnclosingType.Accept(TypeLoader), Architecture.PointerType.BitSize)
                    : null;
                var dtParameters = ss.Arguments != null
                    ? ss.Arguments
                                   .TakeWhile(p => p.Name != "...")
                                   .Select(p => p.Type.Accept(TypeLoader))
                                   .ToList()
                    : new List <DataType>();

                // A calling convention governs the storage of a the
                // parameters

                var cc  = platform.GetCallingConvention(ss.Convention);
                var res = new CallingConventionEmitter();
                cc.Generate(res, dtRet, dtThis, dtParameters);
                if (res.Return != null)
                {
                    var retReg = res.Return as RegisterStorage;
                    ret = new Identifier(retReg != null ? retReg.Name : "", dtRet, res.Return);
                }
                if (res.ImplicitThis != null)
                {
                    var param = new Identifier("this", dtThis, res.ImplicitThis);
                    parameters.Add(param);
                }
                if (ss.Arguments != null)
                {
                    for (int i = 0; i < ss.Arguments.Length; ++i)
                    {
                        if (ss.Arguments[i].Name == "...")
                        {
                            var unk = new UnknownType();
                            parameters.Add(new Identifier("...", unk, new StackArgumentStorage(0, unk)));
                        }
                        else
                        {
                            var name  = GenerateParameterName(ss.Arguments[i].Name, dtParameters[i], res.Parameters[i]);
                            var param = new Identifier(name, dtParameters[i], res.Parameters[i]);
                            parameters.Add(param);
                        }
                    }
                }
                var ft = new FunctionType(ret, parameters.ToArray())
                {
                    IsInstanceMetod = ss.IsInstanceMethod,
                    StackDelta      = ss.StackDelta != 0
                        ? ss.StackDelta
                        : res.StackDelta,
                    FpuStackDelta        = res.FpuStackDelta,
                    ReturnAddressOnStack = retAddrSize,
                };
                return(ft);
            }
        }
Example #16
0
 public abstract Identifier Deserialize(ArgumentDeserializer sser);
Example #17
0
		public abstract Identifier Deserialize(ArgumentDeserializer sser);