Exemplo n.º 1
0
        public MachineState DoOperation(OperatorEffect operatorEffect, AbstractValue offset)
        {
            if (offset.IsPointer)
            {
                throw new ArgumentException("_offset pointer not supported.");
            }

            var newState = new MachineState(this);

            switch (operatorEffect)
            {
            case OperatorEffect.Jnz:
            {
                if (!newState.zeroFlag)
                {
                    newState.instructionPointer += offset.Value;
                }

                break;
            }

            default:
            {
                throw new ArgumentException(
                          String.Format("Unsupported OperatorEffect: {0}", operatorEffect), "operatorEffect");
            }
            }

            return(newState);
        }
Exemplo n.º 2
0
        public AbstractBuffer DoOperation(OperatorEffect operatorEffect, AbstractValue rhs)
        {
            if (null == rhs)
            {
                throw new ArgumentNullException("rhs");
            }

            var lhs = this;

            // TODO: should have a guard for if rhs isn't a pointer
            switch (operatorEffect)
            {
            case OperatorEffect.Assignment:
            {
                var result = new AbstractBuffer(lhs);

                return(result);
            }

            case OperatorEffect.Add:
            {
                var result = new AbstractBuffer(lhs);
                result.BaseIndex += rhs.Value;

                return(result);
            }

            case OperatorEffect.Sub:
            {
                var result = new AbstractBuffer(lhs);

                if (result.BaseIndex <
                    rhs.Value)
                {
                    throw new ArgumentOutOfRangeException(
                              String.Format(
                                  "Attempting to set a negative baseindex, baseindex: {0:x4}, _subValue {1:x4}",
                                  result.BaseIndex,
                                  rhs.Value
                                  )
                              );
                }

                result.BaseIndex -= rhs.Value;
                return(result);
            }

            case OperatorEffect.And:
            {
                var result = new AbstractBuffer(lhs);

                result.BaseIndex &= rhs.Value;
                return(result);
            }

            default:
                throw new ArgumentException(
                          String.Format("Unsupported OperatorEffect: {0}", operatorEffect), "operatorEffect");
            }
        }
Exemplo n.º 3
0
        public MachineState DoOperation(RegisterName lhs, OperatorEffect operatorEffect, RegisterName rhs)
        {
            var newState = new MachineState(this);
            var result   = DoOperation(Registers[lhs], operatorEffect, Registers[rhs]);

            newState.Registers[lhs] = result.Value;
            newState.ZeroFlag       = result.ZeroFlag;
            return(newState);
        }
Exemplo n.º 4
0
        private static OperationResult DoOperation(AbstractValue lhs, OperatorEffect operatorEffect, AbstractValue rhs)
        {
            if (rhs.IsPointer &&
                operatorEffect != OperatorEffect.Assignment)
            {
                throw new ArgumentException("rhs pointer only supported for OperatorEffect.Assignment.");
            }

            var           result = new OperationResult();
            AbstractValue totalValue;

            if (operatorEffect == OperatorEffect.Assignment)
            {
                var newValue = new AbstractValue(rhs);
                if (rhs.IsInitialized &&
                    rhs.IsOutOfBounds)
                {
                    newValue.IsOutOfBounds = true;
                }

                result.Value = newValue;

                return(result);
            }

            if (operatorEffect == OperatorEffect.Cmp)
            {
                result.ZeroFlag = (lhs.Value - rhs.Value) == 0;

                totalValue   = lhs;
                result.Value = totalValue;
                return(result);
            }

            if (lhs.IsPointer)
            {
                var newBuffer = lhs.PointsTo.DoOperation(operatorEffect, rhs);
                result.Value = new AbstractValue(newBuffer);
                return(result);
            }

            var total = CalculateValueFor(lhs.Value, operatorEffect, rhs.Value);

            totalValue = new AbstractValue(total);

            if (lhs.IsTainted ||
                rhs.IsTainted)
            {
                totalValue = totalValue.AddTaint();
            }

            result.Value = totalValue;
            return(result);
        }
Exemplo n.º 5
0
        public MachineState DoOperation(UInt32 offset, OperatorEffect operatorEffect, AbstractValue rhs)
        {
            var newState = new MachineState(this);

            switch (operatorEffect)
            {
            case OperatorEffect.Assignment:
            {
                newState.dataSegment[offset] =
                    DoOperation(newState.dataSegment[offset], operatorEffect, rhs).Value;
                break;
            }
            }

            return(newState);
        }
Exemplo n.º 6
0
        public MachineState DoOperation(RegisterName lhs, Int32 index, OperatorEffect operatorEffect, AbstractValue rhs)
        {
            var newState = new MachineState(this);

            switch (operatorEffect)
            {
            case OperatorEffect.Assignment:
            case OperatorEffect.Cmp:
            {
                var result = DoOperation(Registers[lhs].PointsTo[index], operatorEffect, rhs);
                newState.Registers[lhs].PointsTo[index] = result.Value;
                newState.ZeroFlag = result.ZeroFlag;
                break;
            }
            }

            return(newState);
        }
Exemplo n.º 7
0
        public MachineState DoOperation(RegisterName lhs, OperatorEffect operatorEffect, AbstractValue rhs)
        {
            var newState = new MachineState(this);

            if (Registers[lhs].IsPointer &&
                operatorEffect != OperatorEffect.Assignment)
            {
                var newBuffer = Registers[lhs].PointsTo.DoOperation(operatorEffect, rhs);
                newState.Registers[lhs] = new AbstractValue(newBuffer);
            }
            else
            {
                var result = DoOperation(Registers[lhs], operatorEffect, rhs);
                newState.Registers[lhs] = result.Value;
                newState.ZeroFlag       = result.ZeroFlag;
            }

            return(newState);
        }
Exemplo n.º 8
0
        private static UInt32 CalculateValueFor(UInt32 lhs, OperatorEffect operatorEffect, UInt32 rhs)
        {
            switch (operatorEffect)
            {
            case OperatorEffect.Add:
            {
                return(lhs + rhs);
            }

            case OperatorEffect.Sub:
            {
                return(lhs - rhs);
            }

            case OperatorEffect.And:
            {
                return(lhs & rhs);
            }

            case OperatorEffect.Xor:
            {
                return(lhs ^ rhs);
            }

            case OperatorEffect.Shr:
            {
                return(lhs >> (Byte)rhs);
            }

            case OperatorEffect.Shl:
            {
                return(lhs << (Byte)rhs);
            }

            default:
            {
                throw new ArgumentException(
                          String.Format("Unsupported OperatorEffect: {0}", operatorEffect), "operatorEffect");
            }
            }
        }
Exemplo n.º 9
0
        public AbstractBuffer DoOperation(OperatorEffect operatorEffect, AbstractValue rhs)
        {
            if (null == rhs)
            {
                throw new ArgumentNullException("rhs");
            }

            var lhs = this;

            // TODO: should have a guard for if rhs isn't a pointer
            switch (operatorEffect)
            {
                case OperatorEffect.Assignment:
                {
                    var result = new AbstractBuffer(lhs);

                    return result;
                }

                case OperatorEffect.Add:
                {
                    var result = new AbstractBuffer(lhs);
                    result.BaseIndex += rhs.Value;

                    return result;
                }

                case OperatorEffect.Sub:
                {
                    var result = new AbstractBuffer(lhs);

                    if (result.BaseIndex <
                        rhs.Value)
                    {
                        throw new ArgumentOutOfRangeException(
                            String.Format(
                                "Attempting to set a negative baseindex, baseindex: {0:x4}, _subValue {1:x4}",
                                result.BaseIndex,
                                rhs.Value
                                )
                            );
                    }

                    result.BaseIndex -= rhs.Value;
                    return result;
                }

                case OperatorEffect.And:
                {
                    var result = new AbstractBuffer(lhs);

                    result.BaseIndex &= rhs.Value;
                    return result;
                }

                default:
                    throw new ArgumentException(
                        String.Format("Unsupported OperatorEffect: {0}", operatorEffect), "operatorEffect");
            }
        }
Exemplo n.º 10
0
        private static UInt32 CalculateValueFor(UInt32 lhs, OperatorEffect operatorEffect, UInt32 rhs)
        {
            switch (operatorEffect)
            {
                case OperatorEffect.Add:
                {
                    return lhs + rhs;
                }

                case OperatorEffect.Sub:
                {
                    return lhs - rhs;
                }

                case OperatorEffect.And:
                {
                    return lhs & rhs;
                }

                case OperatorEffect.Xor:
                {
                    return lhs ^ rhs;
                }

                case OperatorEffect.Shr:
                {
                    return lhs >> (Byte)rhs;
                }

                case OperatorEffect.Shl:
                {
                    return lhs << (Byte)rhs;
                }

                default:
                {
                    throw new ArgumentException(
                        String.Format("Unsupported OperatorEffect: {0}", operatorEffect), "operatorEffect");
                }
            }
        }
Exemplo n.º 11
0
        private static OperationResult DoOperation(AbstractValue lhs, OperatorEffect operatorEffect, AbstractValue rhs)
        {
            if (rhs.IsPointer &&
                operatorEffect != OperatorEffect.Assignment)
            {
                throw new ArgumentException("rhs pointer only supported for OperatorEffect.Assignment.");
            }

            var result = new OperationResult();
            AbstractValue totalValue;

            if (operatorEffect == OperatorEffect.Assignment)
            {
                var newValue = new AbstractValue(rhs);
                if (rhs.IsInitialized &&
                    rhs.IsOutOfBounds)
                {
                    newValue.IsOutOfBounds = true;
                }

                result.Value = newValue;

                return result;
            }

            if (operatorEffect == OperatorEffect.Cmp)
            {
                result.ZeroFlag = (lhs.Value - rhs.Value) == 0;

                totalValue = lhs;
                result.Value = totalValue;
                return result;
            }

            if (lhs.IsPointer)
            {
                var newBuffer = lhs.PointsTo.DoOperation(operatorEffect, rhs);
                result.Value = new AbstractValue(newBuffer);
                return result;
            }

            var total = CalculateValueFor(lhs.Value, operatorEffect, rhs.Value);
            totalValue = new AbstractValue(total);

            if (lhs.IsTainted ||
                rhs.IsTainted)
            {
                totalValue = totalValue.AddTaint();
            }

            result.Value = totalValue;
            return result;
        }
Exemplo n.º 12
0
 public MachineState DoOperation(RegisterName lhs, OperatorEffect operatorEffect, RegisterName rhs)
 {
     var newState = new MachineState(this);
     var result = DoOperation(Registers[lhs], operatorEffect, Registers[rhs]);
     newState.Registers[lhs] = result.Value;
     newState.ZeroFlag = result.ZeroFlag;
     return newState;
 }
Exemplo n.º 13
0
        public MachineState DoOperation(RegisterName lhs, OperatorEffect operatorEffect, AbstractValue rhs)
        {
            var newState = new MachineState(this);

            if (Registers[lhs].IsPointer &&
                operatorEffect != OperatorEffect.Assignment)
            {
                var newBuffer = Registers[lhs].PointsTo.DoOperation(operatorEffect, rhs);
                newState.Registers[lhs] = new AbstractValue(newBuffer);
            }
            else
            {
                var result = DoOperation(Registers[lhs], operatorEffect, rhs);
                newState.Registers[lhs] = result.Value;
                newState.ZeroFlag = result.ZeroFlag;
            }

            return newState;
        }
Exemplo n.º 14
0
        public MachineState DoOperation(RegisterName lhs, Int32 index, OperatorEffect operatorEffect, AbstractValue rhs)
        {
            var newState = new MachineState(this);

            switch (operatorEffect)
            {
                case OperatorEffect.Assignment:
                case OperatorEffect.Cmp:
                {
                    var result = DoOperation(Registers[lhs].PointsTo[index], operatorEffect, rhs);
                    newState.Registers[lhs].PointsTo[index] = result.Value;
                    newState.ZeroFlag = result.ZeroFlag;
                    break;
                }
            }

            return newState;
        }
Exemplo n.º 15
0
        public MachineState DoOperation(UInt32 offset, OperatorEffect operatorEffect, AbstractValue rhs)
        {
            var newState = new MachineState(this);

            switch (operatorEffect)
            {
                case OperatorEffect.Assignment:
                {
                    newState.dataSegment[offset] =
                        DoOperation(newState.dataSegment[offset], operatorEffect, rhs).Value;
                    break;
                }
            }

            return newState;
        }
Exemplo n.º 16
0
        public MachineState DoOperation(OperatorEffect operatorEffect, AbstractValue offset)
        {
            if (offset.IsPointer)
            {
                throw new ArgumentException("_offset pointer not supported.");
            }

            var newState = new MachineState(this);
            switch (operatorEffect)
            {
                case OperatorEffect.Jnz:
                {
                    if (!newState.zeroFlag)
                    {
                        newState.instructionPointer += offset.Value;
                    }

                    break;
                }

                default:
                {
                    throw new ArgumentException(
                        String.Format("Unsupported OperatorEffect: {0}", operatorEffect), "operatorEffect");
                }
            }

            return newState;
        }