Пример #1
0
        private string ToString_Constants(Model model)
        {
            ISet <string> program = new SortedSet <string>();

            foreach (KeyValuePair <string, BitVecExpr> pair in this._constants)
            {
                BitVecExpr constant = pair.Value;
                program.Add(pair.Key + " = " + ToolsZ3.ToStringBin(ToolsZ3.GetTvArray(constant, 64, this._solver, this._ctx)));
            }
            foreach (Rn reg in new List <Rn>()
            {
                Rn.RAX
            })                                           //, Rn.RBX, Rn.RCX, Rn.RDX})
            {
                for (int lineNumber = 0; lineNumber <= this._nLines; ++lineNumber)
                {
                    BitVecExpr regValue = GetReg(reg, lineNumber, this._ctx);
                    program.Add(regValue.FuncDecl.Name + " = " + ToolsZ3.ToStringBin(ToolsZ3.GetTvArray(regValue, 64, this._solver, this._ctx)));
                }
            }
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Constants:");
            foreach (string s in program)
            {
                sb.AppendLine(s);
            }
            return(sb.ToString());
        }
Пример #2
0
        public Tv[] GetTvArrayMem(BitVecExpr address, int nBytes, bool addBranchInfo = true)
        {
            if (!addBranchInfo)
            {
                throw new Exception();  //TODO
            }
            this.UndefGrounding = true; // needed!

            bool popNeeded = false;

            if (addBranchInfo && (this.BranchInfoStore.Count > 0))
            {
                this.Solver.Push();
                this.Solver_U.Push();
                this.AssertBranchInfoToSolver();
                popNeeded = true;
            }

            using (BitVecExpr valueExpr = this.Create_Mem(address, nBytes))
            {
                Tv[] result = ToolsZ3.GetTvArray(valueExpr, nBytes << 3, this.Solver, this.Solver_U, this._ctx);

                if (popNeeded)
                {
                    this.Solver.Pop();
                    this.Solver_U.Pop();
                }
                return(result);
            }
        }
Пример #3
0
        private string Solver2Asm(Solver solver, Context ctx)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("\nAsm:");
            for (int lineNumber = 1; lineNumber <= this._nLines; ++lineNumber)
            {
                string codeLine = "";
                foreach (BoolExpr instruction_Switch in this._switches[lineNumber])
                {
                    Tv tv = ToolsZ3.GetTv(instruction_Switch, solver, ctx);

                    if (false)
                    {
                        if (solver.Check() == Status.SATISFIABLE)
                        {
                            Console.WriteLine(instruction_Switch + " = " + tv);
                            Console.WriteLine(ToString(solver.Model));
                        }
                    }
                    if (tv == Tv.ONE)
                    {
                        codeLine = instruction_Switch.FuncDecl.Name.ToString();
                        break;
                    }
                    else if (tv == Tv.UNKNOWN)
                    {
                        codeLine = instruction_Switch.FuncDecl.Name + " | " + codeLine;
                    }
                }
                sb.AppendLine(codeLine);
            }
            if (false)
            {
                foreach (KeyValuePair <string, BitVecExpr> pair in this._constants)
                {
                    BitVecExpr constant = pair.Value;
                    sb.AppendLine(pair.Key + " = " + ToolsZ3.ToStringBin(ToolsZ3.GetTvArray(constant, 64, this._solver, this._ctx)));
                }

                foreach (Rn reg in new List <Rn>()
                {
                    Rn.RAX
                })                                           //, Rn.RBX, Rn.RCX, Rn.RDX})
                {
                    for (int lineNumber = 0; lineNumber <= this._nLines; ++lineNumber)
                    {
                        BitVecExpr regValue = GetReg(reg, lineNumber, ctx);
                        sb.AppendLine(regValue.FuncDecl.Name + " = " + ToolsZ3.ToStringBin(ToolsZ3.GetTvArray(regValue, 64, this._solver, this._ctx)));
                    }
                }
            }
            return(sb.ToString());
        }
Пример #4
0
        public Tv[] GetTvArray(Rn regName)
        {
            lock (this._ctxLock)
            {
                if (this.Frozen && this._cached_Reg_Values.TryGetValue(regName, out var value))
                {
                    return(value);
                }
                try
                {
                    this.UndefGrounding = true; // needed!

                    bool popNeeded = false;
                    if ((this.BranchInfoStore != null) && (this.BranchInfoStore.Count > 0))
                    {
                        this.Solver.Push();
                        this.Solver_U.Push();
                        this.AssertBranchInfoToSolver();
                        popNeeded = true;
                    }

                    using (BitVecExpr regExpr = this.Create(regName))
                    {
                        Tv[] result = ToolsZ3.GetTvArray(regExpr, RegisterTools.NBits(regName), this.Solver, this.Solver_U, this._ctx);

                        if (popNeeded)
                        {
                            this.Solver.Pop();
                            this.Solver_U.Pop();
                        }

                        if (this.Frozen)
                        {
                            if (ADD_COMPUTED_VALUES && (RegisterTools.NBits(regName) == 64))
                            {
                                ulong?value2 = ToolsZ3.ToUlong(result);
                                if (value2 != null)
                                {
                                    this.Solver.Assert(this.Ctx.MkEq(regExpr, this.Ctx.MkBV(value2.Value, 64)));
                                    this.Solver_Dirty = true;
                                }
                            }
                            this._cached_Reg_Values[regName] = result;
                        }
                        return(result);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("WARNING: AsmSimulator: " + e.ToString());
                    return(new Tv[RegisterTools.NBits(regName)]);
                }
            }
        }
Пример #5
0
 public void Set_Mem(BitVecExpr address, string value)
 {
     this.Set_Mem(address, ToolsZ3.GetTvArray(value));
 }
Пример #6
0
 public void Set(Rn reg, string value)
 {
     this.Set(reg, ToolsZ3.GetTvArray(value));
 }