Added definitions/declarations of methods for the ARM64 register state
[dyninst.git] / dataflowAPI / rose / semantics / SymEvalSemantics.h
1 //
2 // Created by ssunny on 7/1/16.
3 //
4
5 #ifndef DYNINST_SYMEVALSEMANTICS_H
6 #define DYNINST_SYMEVALSEMANTICS_H
7
8 #include "external/rose/armv8InstructionEnum.h"
9 #include "BaseSemantics2.h"
10 #include "../../h/SymEval.h"
11
12 namespace rose {
13     namespace BinaryAnalysis {
14         namespace InstructionSemantics2 {
15             namespace SymEvalSemantics {
16
17                 /***************************************************************************************************/
18                 /*                                              SValue                                             */
19                 /***************************************************************************************************/
20                 typedef Sawyer::SharedPointer<class SValue> SValuePtr;
21
22                 class SValue : public BaseSemantics::SValue {
23                 protected:
24                     Dyninst::AST::Ptr expr;
25
26                     SValue(Dyninst::Absloc r, Dyninst::ParseAPI::Address addr): BaseSemantics::SValue(0) {
27                         //FIXME
28                     }
29
30                     SValue(size_t nbits, uint64_t num): BaseSemantics::SValue(nbits) {
31                         expr = Dyninst::DataflowAPI::ConstantAST::create(Dyninst::DataflowAPI::Constant(num, nbits));
32                     }
33
34                     //FIXME
35                     SValue(Dyninst::AST::Ptr expr): BaseSemantics::SValue(0) {
36                         this->expr = expr;
37                     }
38
39                 public:
40                     virtual BaseSemantics::SValuePtr undefined_(size_t nbits) const {
41                         return SValuePtr(new SValue(Dyninst::DataflowAPI::BottomAST::create(false)));
42                     }
43
44                     virtual BaseSemantics::SValuePtr unspecified_(size_t nbits) const {
45                         //FIXME
46                     }
47
48                     //TODO
49                     virtual BaseSemantics::SValuePtr bottom_(size_t nbits) const {
50                         return SValuePtr(new SValue(Dyninst::DataflowAPI::BottomAST::create(true)));
51                     }
52
53                     virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t num) const {
54                         return SValuePtr(new SValue(nbits, num));
55                     }
56
57                     virtual BaseSemantics::SValuePtr boolean_(bool value) const {
58                         return SValuePtr(new SValue(Dyninst::DataflowAPI::ConstantAST::create(Dyninst::DataflowAPI::Constant(value?1:0, 1))));
59                     }
60
61                     virtual BaseSemantics::SValuePtr copy(size_t new_width = 0) const {
62                         SValuePtr retval(new SValue(*this));
63                         if (new_width!=0 && new_width!=retval->get_width())
64                             retval->set_width(new_width);
65                         return retval;
66                     }
67
68                     virtual Sawyer::Optional<BaseSemantics::SValuePtr>
69                             createOptionalMerge(const BaseSemantics::SValuePtr &other, const BaseSemantics::MergerPtr&, SMTSolver*) const;
70
71                 public:
72                     static SValuePtr promote(const BaseSemantics::SValuePtr &v) {
73                         SValuePtr retval = v.dynamicCast<SValue>();
74                         ASSERT_not_null(retval);
75                         return retval;
76                     }
77
78                 public:
79                     //TODO
80                     virtual bool isBottom() const {
81                         return expr->getID() == Dyninst::DataflowAPI::V_BottomAST;
82                     }
83
84                     virtual bool is_number() const {
85                         return expr->getID() == Dyninst::DataflowAPI::V_ConstantAST;
86                     }
87
88                     virtual uint64_t get_number() const {
89                         assert(expr->getID() == Dyninst::DataflowAPI::V_ConstantAST);
90                         Dyninst::DataflowAPI::Constant constant = expr->val();
91                         ASSERT_not_null(constant);
92                         return constant.val;
93                     }
94
95                     virtual void print(std::ostream &, BaseSemantics::Formatter &) const {
96                         //TODO
97                     }
98
99                 };
100
101                 typedef boost::shared_ptr<class RegisterStateARM64> RegisterStateARM64Ptr;
102
103                 class RegisterStateARM64 : public BaseSemantics::RegisterState {
104                 protected:
105                     explicit RegisterStateARM64(const BaseSemantics::SValuePtr &protoval, const RegisterDictionary *regdict): RegisterState(protoval, regdict) {
106                         clear();
107                     }
108
109                     RegisterStateARM64(Dyninst::DataflowAPI::Result_t &r,
110                                        Dyninst::Address a,
111                                        Dyninst::Architecture ac,
112                                        Dyninst::InstructionAPI::Instruction::Ptr insn_,
113                                        const BaseSemantics::SValuePtr &protoval,
114                                        const RegisterDictionary *regdict) : RegisterState(protoval, regdict), res(r),
115                                                                             addr(a), arch(ac), insn(insn_) {
116                         for (Dyninst::DataflowAPI::Result_t::iterator iter = r.begin();
117                              iter != r.end(); ++iter) {
118                             Dyninst::Assignment::Ptr a = iter->first;
119                             // For a different instruction...
120                             if (a->addr() != addr)
121                                 continue;
122                             Dyninst::AbsRegion &o = a->out();
123
124                             if (o.containsOfType(Dyninst::Absloc::Register)) {
125                                 // We're assuming this is a single register...
126                                 //std::cerr << "Marking register " << a << std::endl;
127                                 aaMap[o.absloc()] = a;
128                             }
129                             else {
130                                 // Use sufficiently-unique (Heap,0) Absloc
131                                 // to represent a definition to a memory absloc
132                                 aaMap[Absloc(0)] = a;
133                             }
134                         }
135                     }
136
137                 public:
138                     virtual BaseSemantics::RegisterStatePtr create(const SValuePtr &protoval, const RegisterDictionary *regdict) const {
139                         return RegisterStateARM64Ptr(new RegisterStateARM64(protoval, regdict));
140                     }
141
142                     //FIXME
143                     virtual BaseSemantics::RegisterStatePtr clone() const {
144                         ASSERT_not_implemented("Yet to be implemented");
145                     }
146
147                     static RegisterStateARM64Ptr promote(const BaseSemantics::RegisterStatePtr &from) {
148                         RegisterStateARM64Ptr  retval = boost::dynamic_pointer_cast<RegisterStateARM64>(from);
149                         ASSERT_not_null(retval);
150                         return retval;
151                     }
152
153                 public:
154                     virtual void clear();
155                     virtual void zero();
156                     virtual BaseSemantics::SValuePtr readRegister(const RegisterDescriptor &reg, const BaseSemantics::SValuePtr &dflt, BaseSemantics::RiscOperators *ops);
157                     virtual void writeRegister(const RegisterDescriptor &reg, const BaseSemantics::SValuePtr &value, BaseSemantics::RiscOperators *ops);
158                     virtual void print(std::ostream &, BaseSemantics::Formatter &) const;
159                     virtual bool merge(const RegisterDescriptor &other, BaseSemantics::RiscOperators *ops);
160
161                 private:
162                     Dyninst::AST::Ptr wrap(Dyninst::Absloc r) {
163                         return Dyninst::DataflowAPI::VariableAST::create(Dyninst::DataflowAPI::Variable(Dyninst::AbsRegion(r), addr));
164                     }
165
166                     Dyninst::Absloc convert(ARMv8GeneralPurposeRegister r);
167
168                     Dyninst::DataflowAPI::Result_t &res;
169                     Dyninst::Architecture arch;
170                     Dyninst::Address addr;
171                     Dyninst::InstructionAPI::Instruction::Ptr insn;
172
173                     std::map<Dyninst::Absloc, Dyninst::Assignment::Ptr> aaMap;
174                 };
175
176
177                 class MemoryState : public BaseSemantics::MemoryState {
178
179                 };
180
181
182                 /***************************************************************************************************/
183                 /*                                          RiscOperators                                          */
184                 /***************************************************************************************************/
185                 typedef boost::shared_ptr<class RiscOperators> RiscOperatorsPtr;
186
187                 /** RISC operators for use by the Symbolic Semantics Domain of Dyninst.
188                  *
189                  */
190                 class RiscOperators : public BaseSemantics::RiscOperators {
191                 protected:
192                     explicit RiscOperators(const BaseSemantics::SValuePtr &protoval, SMTSolver *solver = NULL)
193                             : BaseSemantics::RiscOperators(protoval, solver) {
194                         //SValue::promote(protoval);
195                     }
196
197                     explicit RiscOperators(const BaseSemantics::StatePtr &state, SMTSolver *solver = NULL)
198                             : BaseSemantics::RiscOperators(state, solver) {
199                         //SValue::promote(state->protoval());
200                     }
201
202                 public:
203                     static RiscOperatorsPtr instance(const RegisterDictionary *regdict, SMTSolver *solver = NULL) {
204                         //init SValue
205                         //init RegisterState
206                         //init MemoryState
207                         //init State
208                         //return new RiscOperatorsPtr(new RiscOperators(state, solver));
209                     }
210
211                     static RiscOperatorsPtr instance(const BaseSemantics::SValuePtr &protoval,
212                                                      SMTSolver *solver = NULL) {
213                         return RiscOperatorsPtr(new RiscOperators(protoval, solver));
214                     }
215
216                     static RiscOperatorsPtr instance(const BaseSemantics::StatePtr &state, SMTSolver *solver = NULL) {
217                         return RiscOperatorsPtr(new RiscOperators(state, solver));
218                     }
219
220                 public:
221                     virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval,
222                                                                    SMTSolver *solver = NULL) {
223                         return instance(protoval, solver);
224                     }
225
226                     virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::StatePtr &state,
227                                                                    SMTSolver *solver = NULL) {
228                         return instance(state, solver);
229                     }
230
231                 public:
232                     /** Run-time promotion of a base RiscOperators pointer to symbolic operators. This is a checked conversion--it
233                     *  will fail if @p x does not point to a SymbolicSemantics::RiscOperators object. */
234                     static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr &x) {
235                         RiscOperatorsPtr retval = boost::dynamic_pointer_cast<RiscOperatorsPtr>(x);
236                         ASSERT_not_null(retval);
237
238                         return retval;
239                     }
240
241                 public:
242                     virtual BaseSemantics::SValuePtr boolean_(bool b) {
243                         SValuePtr retVal = SValue::promote(BaseSemantics::RiscOperators::boolean_(b));
244                         return retval;
245                     }
246
247                     virtual BaseSemantics::SValuePtr number_(size_t nbits, uint64_t value) {
248                         SValuePtr retval = SValue::promote(BaseSemantics::RiscOperators::number_(nbits, value));
249                         return retval;
250                     }
251
252                 public:
253                     virtual BaseSemantics::SValuePtr and_(const BaseSemantics::SValuePtr &a_,
254                                                           const BaseSemantics::SValuePtr &b_);
255                     virtual BaseSemantics::SValuePtr or_(const BaseSemantics::SValuePtr &a_,
256                                                           const BaseSemantics::SValuePtr &b_);
257                     virtual BaseSemantics::SValuePtr xor_(const BaseSemantics::SValuePtr &a_,
258                                                           const BaseSemantics::SValuePtr &b_);
259                     virtual BaseSemantics::SValuePtr invert(const BaseSemantics::SValuePtr &a_);
260                     virtual BaseSemantics::SValuePtr extract(const BaseSemantics::SValuePtr &a_,
261                                                              size_t begin, size_t end);
262                     virtual BaseSemantics::SValuePtr ite(const BaseSemantics::SValuePtr &sel_,
263                                                          const BaseSemantics::SValuePtr &a_,
264                                                          const BaseSemantics::SValuePtr &b_);
265
266                     virtual BaseSemantics::SValuePtr readRegister(const RegisterDescriptor &reg,
267                                                                   const BaseSemantics::SValuePtr &dflt);
268                     virtual void writeRegister(const RegisterDescriptor &reg, const BaseSemantics::SValuePtr &a_);
269                 };
270             }
271         }
272     }
273 }
274
275 #endif //DYNINST_SYMEVALSEMANTICS_H