Пример #1
0
 public void ApplyConvention(SerializedSignature ssig, FunctionType sig)
 {
     if (ssig.StackDelta != 0)
     {
         sig.StackDelta = ssig.StackDelta;
     }
     else
     {
         sig.StackDelta = Architecture.PointerType.Size;
     }
     sig.FpuStackDelta = FpuStackOffset;
     if (ssig.ReturnAddressOnStack != 0)
     {
         sig.ReturnAddressOnStack = ssig.ReturnAddressOnStack;
     }
     else
     {
         sig.ReturnAddressOnStack = Architecture.PointerType.Size;   //$BUG: x86 real mode?
     }
 }
Пример #2
0
        public virtual SerializedSignature Serialize(ProcedureSignature sig)
        {
            SerializedSignature ssig = new SerializedSignature();

            if (!sig.ParametersValid)
            {
                return(ssig);
            }
            ArgumentSerializer argSer = new ArgumentSerializer(this, Architecture, null, 0);

            ssig.ReturnValue = argSer.Serialize(sig.ReturnValue);
            ssig.Arguments   = new Argument_v1[sig.Parameters.Length];
            for (int i = 0; i < sig.Parameters.Length; ++i)
            {
                Identifier formal = sig.Parameters[i];
                ssig.Arguments[i] = argSer.Serialize(formal);
            }
            ssig.StackDelta    = sig.StackDelta;
            ssig.FpuStackDelta = sig.FpuStackDelta;
            return(ssig);
        }
Пример #3
0
        private SerializedCall_v1 SerializeUserCall(Program program, UserCallData uc)
        {
            if (uc == null || uc.Address == null)
            {
                return(null);
            }
            var procser = program.CreateProcedureSerializer();
            SerializedSignature ssig = null;

            if (uc.Signature != null)
            {
                ssig = procser.Serialize(uc.Signature);
            }
            return(new SerializedCall_v1
            {
                InstructionAddress = uc.Address.ToString(),
                Comment = uc.Comment,
                NoReturn = uc.NoReturn,
                Signature = ssig,
            });
        }
Пример #4
0
        public SerializedSignature Serialize(FunctionType sig)
        {
            SerializedSignature ssig = new SerializedSignature();

            if (!sig.ParametersValid)
            {
                ssig.ParametersValid = false;
                return(ssig);
            }
            ArgumentSerializer argSer = new ArgumentSerializer(Architecture);

            ssig.ReturnValue = argSer.Serialize(sig.ReturnValue);
            ssig.Arguments   = new Argument_v1[sig.Parameters.Length];
            for (int i = 0; i < sig.Parameters.Length; ++i)
            {
                Identifier formal = sig.Parameters[i];
                ssig.Arguments[i] = argSer.Serialize(formal);
            }
            ssig.StackDelta    = sig.StackDelta;
            ssig.FpuStackDelta = sig.FpuStackDelta;
            return(ssig);
        }
Пример #5
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);
            }
        }
Пример #6
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 abstract ProcedureSignature Deserialize(SerializedSignature ss, Frame frame);
Пример #7
0
 public SerializedCall_v1(Address addr, SerializedSignature sig)
 {
     InstructionAddress = addr.ToString();
     Signature          = sig;
 }
Пример #8
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);
            }
        }
Пример #9
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 abstract FunctionType Deserialize(SerializedSignature ss, Frame frame);