1 //! \file
2 /*
3 **  Copyright (C) - Triton
4 **
5 **  This program is under the terms of the Apache License 2.0.
6 */
7 
8 #ifndef TRITON_AARCH64SEMANTICS_H
9 #define TRITON_AARCH64SEMANTICS_H
10 
11 #include <triton/architecture.hpp>
12 #include <triton/dllexport.hpp>
13 #include <triton/instruction.hpp>
14 #include <triton/semanticsInterface.hpp>
15 #include <triton/symbolicEngine.hpp>
16 #include <triton/taintEngine.hpp>
17 
18 
19 
20 //! The Triton namespace
21 namespace triton {
22 /*!
23  *  \addtogroup triton
24  *  @{
25  */
26 
27   //! The Architecture namespace
28   namespace arch {
29   /*!
30    *  \ingroup triton
31    *  \addtogroup arch
32    *  @{
33    */
34 
35     //! The ARM namespace
36     namespace arm {
37     /*!
38      *  \ingroup arch
39      *  \addtogroup arm
40      *  @{
41      */
42 
43       //! The aarch64 namespace
44       namespace aarch64 {
45       /*!
46        *  \ingroup arm
47        *  \addtogroup aarch64
48        *  @{
49        */
50 
51         /*! \class AArch64Semantics
52             \brief The AArch64 ISA semantics. */
53         class AArch64Semantics : public SemanticsInterface {
54           private:
55             //! Architecture API
56             triton::arch::Architecture* architecture;
57 
58             //! Symbolic Engine API
59             triton::engines::symbolic::SymbolicEngine* symbolicEngine;
60 
61             //! Taint Engine API
62             triton::engines::taint::TaintEngine* taintEngine;
63 
64             //! The AST Context API
65             triton::ast::SharedAstContext astCtxt;
66 
67           public:
68             //! Constructor.
69             TRITON_EXPORT AArch64Semantics(triton::arch::Architecture* architecture,
70                                            triton::engines::symbolic::SymbolicEngine* symbolicEngine,
71                                            triton::engines::taint::TaintEngine* taintEngine,
72                                            const triton::ast::SharedAstContext& astCtxt);
73 
74             //! Builds the semantics of the instruction. Returns true if the instruction is supported.
75             TRITON_EXPORT bool buildSemantics(triton::arch::Instruction& inst);
76 
77           private:
78             //! Control flow semantics. Used to represent PC.
79             void controlFlow_s(triton::arch::Instruction& inst);
80 
81             //! Creates a conditional node.
82             triton::ast::SharedAbstractNode getCodeConditionAst(triton::arch::Instruction& inst,
83                                                                 triton::ast::SharedAbstractNode& thenNode,
84                                                                 triton::ast::SharedAbstractNode& elseNode);
85 
86             //! Gets the taint state (based on flags) of a conditional instruction
87             bool getCodeConditionTainteSate(const triton::arch::Instruction& inst);
88 
89             /* Generic flags computation ------------------------------------- */
90 
91             //! Clears a flag.
92             void clearFlag_s(triton::arch::Instruction& inst, const triton::arch::Register& flag, std::string comment="");
93 
94             //! Sets a flag.
95             void setFlag_s(triton::arch::Instruction& inst, const triton::arch::Register& flag, std::string comment="");
96 
97             //! The NF semantics.
98             void nf_s(triton::arch::Instruction& inst,
99                       const triton::engines::symbolic::SharedSymbolicExpression& parent,
100                       triton::arch::OperandWrapper& dst);
101 
102             //! The NF semantics for the CCMP operation.
103             void nfCcmp_s(triton::arch::Instruction& inst,
104                           const triton::engines::symbolic::SharedSymbolicExpression& parent,
105                           triton::arch::OperandWrapper& dst,
106                           triton::ast::SharedAbstractNode& nzcv);
107 
108             //! The ZF semantics.
109             void zf_s(triton::arch::Instruction& inst,
110                       const triton::engines::symbolic::SharedSymbolicExpression& parent,
111                       triton::arch::OperandWrapper& dst);
112 
113             //! The ZF semantics for the CCMP operation.
114             void zfCcmp_s(triton::arch::Instruction& inst,
115                           const triton::engines::symbolic::SharedSymbolicExpression& parent,
116                           triton::arch::OperandWrapper& dst,
117                           triton::ast::SharedAbstractNode& nzcv);
118 
119             /* Specific flags computation ------------------------------------ */
120 
121             //! The CF semantics for the ADDS operation.
122             void cfAdd_s(triton::arch::Instruction& inst,
123                          const triton::engines::symbolic::SharedSymbolicExpression& parent,
124                          triton::arch::OperandWrapper& dst,
125                          triton::ast::SharedAbstractNode& op1,
126                          triton::ast::SharedAbstractNode& op2);
127 
128             //! The CF semantics for the SUBS operation.
129             void cfSub_s(triton::arch::Instruction& inst,
130                          const triton::engines::symbolic::SharedSymbolicExpression& parent,
131                          triton::arch::OperandWrapper& dst,
132                          triton::ast::SharedAbstractNode& op1,
133                          triton::ast::SharedAbstractNode& op2);
134 
135             //! The CF semantics for the CCMP operation.
136             void cfCcmp_s(triton::arch::Instruction& inst,
137                           const triton::engines::symbolic::SharedSymbolicExpression& parent,
138                           triton::arch::OperandWrapper& dst,
139                           triton::ast::SharedAbstractNode& op1,
140                           triton::ast::SharedAbstractNode& op2,
141                           triton::ast::SharedAbstractNode& nzcv);
142 
143             //! The VF semantics for the ADDS operation.
144             void vfAdd_s(triton::arch::Instruction& inst,
145                          const triton::engines::symbolic::SharedSymbolicExpression& parent,
146                          triton::arch::OperandWrapper& dst,
147                          triton::ast::SharedAbstractNode& op1,
148                          triton::ast::SharedAbstractNode& op2);
149 
150             //! The VF semantics for the SUBS operation.
151             void vfSub_s(triton::arch::Instruction& inst,
152                          const triton::engines::symbolic::SharedSymbolicExpression& parent,
153                          triton::arch::OperandWrapper& dst,
154                          triton::ast::SharedAbstractNode& op1,
155                          triton::ast::SharedAbstractNode& op2);
156 
157             //! The VF semantics for the CCMP operation.
158             void vfCcmp_s(triton::arch::Instruction& inst,
159                           const triton::engines::symbolic::SharedSymbolicExpression& parent,
160                           triton::arch::OperandWrapper& dst,
161                           triton::ast::SharedAbstractNode& op1,
162                           triton::ast::SharedAbstractNode& op2,
163                           triton::ast::SharedAbstractNode& nzcv);
164 
165             /* Instruction semantics ----------------------------------------- */
166 
167             //! The ADC semantics.
168             void adc_s(triton::arch::Instruction& inst);
169 
170             //! The ADD semantics.
171             void add_s(triton::arch::Instruction& inst);
172 
173             //! The ADR semantics.
174             void adr_s(triton::arch::Instruction& inst);
175 
176             //! The ADRP semantics.
177             void adrp_s(triton::arch::Instruction& inst);
178 
179             //! The AND semantics.
180             void and_s(triton::arch::Instruction& inst);
181 
182             //! The ASR semantics.
183             void asr_s(triton::arch::Instruction& inst);
184 
185             //! The B and B.cond semantics.
186             void b_s(triton::arch::Instruction& inst);
187 
188             //! The BFI semantics.
189             void bfi_s(triton::arch::Instruction& inst);
190 
191             //! The BIC semantics.
192             void bic_s(triton::arch::Instruction& inst);
193 
194             //! The BL semantics.
195             void bl_s(triton::arch::Instruction& inst);
196 
197             //! The BLR semantics.
198             void blr_s(triton::arch::Instruction& inst);
199 
200             //! The BR semantics.
201             void br_s(triton::arch::Instruction& inst);
202 
203             //! The CBNZ semantics
204             void cbnz_s(triton::arch::Instruction& inst);
205 
206             //! The CBZ semantics
207             void cbz_s(triton::arch::Instruction& inst);
208 
209             //! The CCMP semantics
210             void ccmp_s(triton::arch::Instruction& inst);
211 
212             //! The CINC semantics
213             void cinc_s(triton::arch::Instruction& inst);
214 
215             //! The CLZ semantics
216             void clz_s(triton::arch::Instruction& inst);
217 
218             //! The CMN semantics
219             void cmn_s(triton::arch::Instruction& inst);
220 
221             //! The CMP semantics
222             void cmp_s(triton::arch::Instruction& inst);
223 
224             //! The CSEL semantics
225             void csel_s(triton::arch::Instruction& inst);
226 
227             //! The CSET semantics
228             void cset_s(triton::arch::Instruction& inst);
229 
230             //! The CSINC semantics
231             void csinc_s(triton::arch::Instruction& inst);
232 
233             //! The CSNEG semantics
234             void csneg_s(triton::arch::Instruction& inst);
235 
236             //! The EON semantics.
237             void eon_s(triton::arch::Instruction& inst);
238 
239             //! The EOR semantics.
240             void eor_s(triton::arch::Instruction& inst);
241 
242             //! The EXTR semantics.
243             void extr_s(triton::arch::Instruction& inst);
244 
245             //! The LDAR semantics.
246             void ldar_s(triton::arch::Instruction& inst);
247 
248             //! The LDARB semantics.
249             void ldarb_s(triton::arch::Instruction& inst);
250 
251             //! The LDARH semantics.
252             void ldarh_s(triton::arch::Instruction& inst);
253 
254             //! The LDAXR semantics.
255             void ldaxr_s(triton::arch::Instruction& inst);
256 
257             //! The LDAXRB semantics.
258             void ldaxrb_s(triton::arch::Instruction& inst);
259 
260             //! The LDAXRH semantics.
261             void ldaxrh_s(triton::arch::Instruction& inst);
262 
263             //! The LDP semantics.
264             void ldp_s(triton::arch::Instruction& inst);
265 
266             //! The LDR semantics.
267             void ldr_s(triton::arch::Instruction& inst);
268 
269             //! The LDRB semantics.
270             void ldrb_s(triton::arch::Instruction& inst);
271 
272             //! The LDRH semantics.
273             void ldrh_s(triton::arch::Instruction& inst);
274 
275             //! The LDRSB semantics.
276             void ldrsb_s(triton::arch::Instruction& inst);
277 
278             //! The LDRSH semantics.
279             void ldrsh_s(triton::arch::Instruction& inst);
280 
281             //! The LDRSW semantics.
282             void ldrsw_s(triton::arch::Instruction& inst);
283 
284             //! The LDUR semantics.
285             void ldur_s(triton::arch::Instruction& inst);
286 
287             //! The LDURB semantics.
288             void ldurb_s(triton::arch::Instruction& inst);
289 
290             //! The LDURH semantics.
291             void ldurh_s(triton::arch::Instruction& inst);
292 
293             //! The LDURSB semantics.
294             void ldursb_s(triton::arch::Instruction& inst);
295 
296             //! The LDURSH semantics.
297             void ldursh_s(triton::arch::Instruction& inst);
298 
299             //! The LDURSW semantics.
300             void ldursw_s(triton::arch::Instruction& inst);
301 
302             //! The LDXR semantics.
303             void ldxr_s(triton::arch::Instruction& inst);
304 
305             //! The LDXRB semantics.
306             void ldxrb_s(triton::arch::Instruction& inst);
307 
308             //! The LDXRH semantics.
309             void ldxrh_s(triton::arch::Instruction& inst);
310 
311             //! The LSL semantics.
312             void lsl_s(triton::arch::Instruction& inst);
313 
314             //! The LSR semantics.
315             void lsr_s(triton::arch::Instruction& inst);
316 
317             //! The MADD semantics.
318             void madd_s(triton::arch::Instruction& inst);
319 
320             //! The MNEG semantics.
321             void mneg_s(triton::arch::Instruction& inst);
322 
323             //! The MOV semantics.
324             void mov_s(triton::arch::Instruction& inst);
325 
326             //! The MOVK semantics.
327             void movk_s(triton::arch::Instruction& inst);
328 
329             //! The MOVN semantics.
330             void movn_s(triton::arch::Instruction& inst);
331 
332             //! The MOVZ semantics.
333             void movz_s(triton::arch::Instruction& inst);
334 
335             //! The MSUB semantics.
336             void msub_s(triton::arch::Instruction& inst);
337 
338             //! The MUL semantics.
339             void mul_s(triton::arch::Instruction& inst);
340 
341             //! The MVN semantics.
342             void mvn_s(triton::arch::Instruction& inst);
343 
344             //! The NEG semantics.
345             void neg_s(triton::arch::Instruction& inst);
346 
347             //! The NOP semantics.
348             void nop_s(triton::arch::Instruction& inst);
349 
350             //! The ORN semantics.
351             void orn_s(triton::arch::Instruction& inst);
352 
353             //! The ORR semantics.
354             void orr_s(triton::arch::Instruction& inst);
355 
356             //! The RBIT semantics.
357             void rbit_s(triton::arch::Instruction& inst);
358 
359             //! The RET semantics.
360             void ret_s(triton::arch::Instruction& inst);
361 
362             //! The REV semantics.
363             void rev_s(triton::arch::Instruction& inst);
364 
365             //! The REV16 semantics.
366             void rev16_s(triton::arch::Instruction& inst);
367 
368             //! The REV32 semantics.
369             void rev32_s(triton::arch::Instruction& inst);
370 
371             //! The ROR semantics.
372             void ror_s(triton::arch::Instruction& inst);
373 
374             //! The SBFX semantics.
375             void sbfx_s(triton::arch::Instruction& inst);
376 
377             //! The SDIV semantics.
378             void sdiv_s(triton::arch::Instruction& inst);
379 
380             //! The SMADDL semantics.
381             void smaddl_s(triton::arch::Instruction& inst);
382 
383             //! The SMSUBL semantics.
384             void smsubl_s(triton::arch::Instruction& inst);
385 
386             //! The SMULH semantics.
387             void smulh_s(triton::arch::Instruction& inst);
388 
389             //! The SMULL semantics.
390             void smull_s(triton::arch::Instruction& inst);
391 
392             //! The STLR semantics.
393             void stlr_s(triton::arch::Instruction& inst);
394 
395             //! The STLRB semantics.
396             void stlrb_s(triton::arch::Instruction& inst);
397 
398             //! The STLRH semantics.
399             void stlrh_s(triton::arch::Instruction& inst);
400 
401             //! The STP semantics.
402             void stp_s(triton::arch::Instruction& inst);
403 
404             //! The STR semantics.
405             void str_s(triton::arch::Instruction& inst);
406 
407             //! The STRB semantics.
408             void strb_s(triton::arch::Instruction& inst);
409 
410             //! The STRH semantics.
411             void strh_s(triton::arch::Instruction& inst);
412 
413             //! The STUR semantics.
414             void stur_s(triton::arch::Instruction& inst);
415 
416             //! The STURB semantics.
417             void sturb_s(triton::arch::Instruction& inst);
418 
419             //! The STURH semantics.
420             void sturh_s(triton::arch::Instruction& inst);
421 
422             //! The SUB semantics.
423             void sub_s(triton::arch::Instruction& inst);
424 
425             //! The SVC semantics.
426             void svc_s(triton::arch::Instruction& inst);
427 
428             //! The SXTB semantics.
429             void sxtb_s(triton::arch::Instruction& inst);
430 
431             //! The SXTH semantics.
432             void sxth_s(triton::arch::Instruction& inst);
433 
434             //! The SXTW semantics.
435             void sxtw_s(triton::arch::Instruction& inst);
436 
437             //! The TBNZ semantics.
438             void tbnz_s(triton::arch::Instruction& inst);
439 
440             //! The TBZ semantics.
441             void tbz_s(triton::arch::Instruction& inst);
442 
443             //! The TST semantics.
444             void tst_s(triton::arch::Instruction& inst);
445 
446             //! The UBFIZ semantics.
447             void ubfiz_s(triton::arch::Instruction& inst);
448 
449             //! The UBFX semantics.
450             void ubfx_s(triton::arch::Instruction& inst);
451 
452             //! The UDIV semantics.
453             void udiv_s(triton::arch::Instruction& inst);
454 
455             //! The UMADDL semantics.
456             void umaddl_s(triton::arch::Instruction& inst);
457 
458             //! The UMNEGL semantics.
459             void umnegl_s(triton::arch::Instruction& inst);
460 
461             //! The UMSUBL semantics.
462             void umsubl_s(triton::arch::Instruction& inst);
463 
464             //! The UMULH semantics.
465             void umulh_s(triton::arch::Instruction& inst);
466 
467             //! The UMULL semantics.
468             void umull_s(triton::arch::Instruction& inst);
469 
470             //! The UXTB semantics.
471             void uxtb_s(triton::arch::Instruction& inst);
472 
473             //! The UXTH semantics.
474             void uxth_s(triton::arch::Instruction& inst);
475         };
476 
477       /*! @} End of aarch64 namespace */
478       };
479     /*! @} End of arm namespace */
480     };
481   /*! @} End of arch namespace */
482   };
483 /*! @} End of triton namespace */
484 };
485 
486 #endif /* TRITON_AARCH64SEMANTICS_H */
487