1 //! \file
2 /*
3 **  Copyright (C) - Triton
4 **
5 **  This program is under the terms of the Apache License 2.0.
6 */
7 
8 /* pintool */
9 #include "bindings.hpp"
10 #include "api.hpp"
11 #include "context.hpp"
12 
13 #include <cstring>
14 #include <stdexcept>
15 
16 /* libTriton */
17 #include <triton/api.hpp>
18 #include <triton/cpuSize.hpp>
19 #include <triton/coreUtils.hpp>
20 #include <triton/x86Specifications.hpp>
21 
22 
23 
24 
25 namespace tracer {
26   namespace pintool {
27     namespace context {
28 
29       CONTEXT* lastContext    = nullptr;
30       bool     mustBeExecuted = false;
31 
32 
getCurrentRegisterValue(const triton::arch::Register & reg)33       triton::uint512 getCurrentRegisterValue(const triton::arch::Register& reg) {
34         triton::uint8 buffer[triton::size::dqqword] = {0};
35         triton::uint512 value = 0;
36 
37         if (tracer::pintool::context::lastContext == nullptr)
38           return 0;
39 
40         #if defined(__x86_64__) || defined(_M_X64)
41           switch (reg.getParent()) {
42             case triton::arch::ID_REG_X86_RAX:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RAX,    reinterpret_cast<triton::uint8*>(buffer)); break;
43             case triton::arch::ID_REG_X86_RBX:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RBX,    reinterpret_cast<triton::uint8*>(buffer)); break;
44             case triton::arch::ID_REG_X86_RCX:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RCX,    reinterpret_cast<triton::uint8*>(buffer)); break;
45             case triton::arch::ID_REG_X86_RDX:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RDX,    reinterpret_cast<triton::uint8*>(buffer)); break;
46             case triton::arch::ID_REG_X86_RDI:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RDI,    reinterpret_cast<triton::uint8*>(buffer)); break;
47             case triton::arch::ID_REG_X86_RSI:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RSI,    reinterpret_cast<triton::uint8*>(buffer)); break;
48             case triton::arch::ID_REG_X86_RBP:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RBP,    reinterpret_cast<triton::uint8*>(buffer)); break;
49             case triton::arch::ID_REG_X86_RSP:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RSP,    reinterpret_cast<triton::uint8*>(buffer)); break;
50             case triton::arch::ID_REG_X86_RIP:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RIP,    reinterpret_cast<triton::uint8*>(buffer)); break;
51             case triton::arch::ID_REG_X86_EFLAGS:  PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RFLAGS, reinterpret_cast<triton::uint8*>(buffer)); break;
52             case triton::arch::ID_REG_X86_R8:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R8,     reinterpret_cast<triton::uint8*>(buffer)); break;
53             case triton::arch::ID_REG_X86_R9:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R9,     reinterpret_cast<triton::uint8*>(buffer)); break;
54             case triton::arch::ID_REG_X86_R10:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R10,    reinterpret_cast<triton::uint8*>(buffer)); break;
55             case triton::arch::ID_REG_X86_R11:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R11,    reinterpret_cast<triton::uint8*>(buffer)); break;
56             case triton::arch::ID_REG_X86_R12:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R12,    reinterpret_cast<triton::uint8*>(buffer)); break;
57             case triton::arch::ID_REG_X86_R13:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R13,    reinterpret_cast<triton::uint8*>(buffer)); break;
58             case triton::arch::ID_REG_X86_R14:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R14,    reinterpret_cast<triton::uint8*>(buffer)); break;
59             case triton::arch::ID_REG_X86_R15:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R15,    reinterpret_cast<triton::uint8*>(buffer)); break;
60             case triton::arch::ID_REG_X86_MM0:     return 0; /* Pin doesn't support MMX */
61             case triton::arch::ID_REG_X86_MM1:     return 0; /* Pin doesn't support MMX */
62             case triton::arch::ID_REG_X86_MM2:     return 0; /* Pin doesn't support MMX */
63             case triton::arch::ID_REG_X86_MM3:     return 0; /* Pin doesn't support MMX */
64             case triton::arch::ID_REG_X86_MM4:     return 0; /* Pin doesn't support MMX */
65             case triton::arch::ID_REG_X86_MM5:     return 0; /* Pin doesn't support MMX */
66             case triton::arch::ID_REG_X86_MM6:     return 0; /* Pin doesn't support MMX */
67             case triton::arch::ID_REG_X86_MM7:     return 0; /* Pin doesn't support MMX */
68             case triton::arch::ID_REG_X86_XMM0:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM0,   reinterpret_cast<triton::uint8*>(buffer)); break;
69             case triton::arch::ID_REG_X86_XMM1:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM1,   reinterpret_cast<triton::uint8*>(buffer)); break;
70             case triton::arch::ID_REG_X86_XMM2:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM2,   reinterpret_cast<triton::uint8*>(buffer)); break;
71             case triton::arch::ID_REG_X86_XMM3:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM3,   reinterpret_cast<triton::uint8*>(buffer)); break;
72             case triton::arch::ID_REG_X86_XMM4:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM4,   reinterpret_cast<triton::uint8*>(buffer)); break;
73             case triton::arch::ID_REG_X86_XMM5:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM5,   reinterpret_cast<triton::uint8*>(buffer)); break;
74             case triton::arch::ID_REG_X86_XMM6:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM6,   reinterpret_cast<triton::uint8*>(buffer)); break;
75             case triton::arch::ID_REG_X86_XMM7:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM7,   reinterpret_cast<triton::uint8*>(buffer)); break;
76             case triton::arch::ID_REG_X86_XMM8:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM8,   reinterpret_cast<triton::uint8*>(buffer)); break;
77             case triton::arch::ID_REG_X86_XMM9:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM9,   reinterpret_cast<triton::uint8*>(buffer)); break;
78             case triton::arch::ID_REG_X86_XMM10:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM10,  reinterpret_cast<triton::uint8*>(buffer)); break;
79             case triton::arch::ID_REG_X86_XMM11:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM11,  reinterpret_cast<triton::uint8*>(buffer)); break;
80             case triton::arch::ID_REG_X86_XMM12:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM12,  reinterpret_cast<triton::uint8*>(buffer)); break;
81             case triton::arch::ID_REG_X86_XMM13:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM13,  reinterpret_cast<triton::uint8*>(buffer)); break;
82             case triton::arch::ID_REG_X86_XMM14:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM14,  reinterpret_cast<triton::uint8*>(buffer)); break;
83             case triton::arch::ID_REG_X86_XMM15:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM15,  reinterpret_cast<triton::uint8*>(buffer)); break;
84             case triton::arch::ID_REG_X86_YMM0:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM0,   reinterpret_cast<triton::uint8*>(buffer)); break;
85             case triton::arch::ID_REG_X86_YMM1:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM1,   reinterpret_cast<triton::uint8*>(buffer)); break;
86             case triton::arch::ID_REG_X86_YMM2:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM2,   reinterpret_cast<triton::uint8*>(buffer)); break;
87             case triton::arch::ID_REG_X86_YMM3:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM3,   reinterpret_cast<triton::uint8*>(buffer)); break;
88             case triton::arch::ID_REG_X86_YMM4:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM4,   reinterpret_cast<triton::uint8*>(buffer)); break;
89             case triton::arch::ID_REG_X86_YMM5:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM5,   reinterpret_cast<triton::uint8*>(buffer)); break;
90             case triton::arch::ID_REG_X86_YMM6:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM6,   reinterpret_cast<triton::uint8*>(buffer)); break;
91             case triton::arch::ID_REG_X86_YMM7:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM7,   reinterpret_cast<triton::uint8*>(buffer)); break;
92             case triton::arch::ID_REG_X86_YMM8:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM8,   reinterpret_cast<triton::uint8*>(buffer)); break;
93             case triton::arch::ID_REG_X86_YMM9:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM9,   reinterpret_cast<triton::uint8*>(buffer)); break;
94             case triton::arch::ID_REG_X86_YMM10:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM10,  reinterpret_cast<triton::uint8*>(buffer)); break;
95             case triton::arch::ID_REG_X86_YMM11:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM11,  reinterpret_cast<triton::uint8*>(buffer)); break;
96             case triton::arch::ID_REG_X86_YMM12:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM12,  reinterpret_cast<triton::uint8*>(buffer)); break;
97             case triton::arch::ID_REG_X86_YMM13:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM13,  reinterpret_cast<triton::uint8*>(buffer)); break;
98             case triton::arch::ID_REG_X86_YMM14:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM14,  reinterpret_cast<triton::uint8*>(buffer)); break;
99             case triton::arch::ID_REG_X86_YMM15:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM15,  reinterpret_cast<triton::uint8*>(buffer)); break;
100             case triton::arch::ID_REG_X86_ZMM0:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM0,   reinterpret_cast<triton::uint8*>(buffer)); break;
101             case triton::arch::ID_REG_X86_ZMM1:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM1,   reinterpret_cast<triton::uint8*>(buffer)); break;
102             case triton::arch::ID_REG_X86_ZMM2:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM2,   reinterpret_cast<triton::uint8*>(buffer)); break;
103             case triton::arch::ID_REG_X86_ZMM3:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM3,   reinterpret_cast<triton::uint8*>(buffer)); break;
104             case triton::arch::ID_REG_X86_ZMM4:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM4,   reinterpret_cast<triton::uint8*>(buffer)); break;
105             case triton::arch::ID_REG_X86_ZMM5:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM5,   reinterpret_cast<triton::uint8*>(buffer)); break;
106             case triton::arch::ID_REG_X86_ZMM6:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM6,   reinterpret_cast<triton::uint8*>(buffer)); break;
107             case triton::arch::ID_REG_X86_ZMM7:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM7,   reinterpret_cast<triton::uint8*>(buffer)); break;
108             case triton::arch::ID_REG_X86_ZMM8:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM8,   reinterpret_cast<triton::uint8*>(buffer)); break;
109             case triton::arch::ID_REG_X86_ZMM9:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM9,   reinterpret_cast<triton::uint8*>(buffer)); break;
110             case triton::arch::ID_REG_X86_ZMM10:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM10,  reinterpret_cast<triton::uint8*>(buffer)); break;
111             case triton::arch::ID_REG_X86_ZMM11:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM11,  reinterpret_cast<triton::uint8*>(buffer)); break;
112             case triton::arch::ID_REG_X86_ZMM12:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM12,  reinterpret_cast<triton::uint8*>(buffer)); break;
113             case triton::arch::ID_REG_X86_ZMM13:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM13,  reinterpret_cast<triton::uint8*>(buffer)); break;
114             case triton::arch::ID_REG_X86_ZMM14:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM14,  reinterpret_cast<triton::uint8*>(buffer)); break;
115             case triton::arch::ID_REG_X86_ZMM15:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM15,  reinterpret_cast<triton::uint8*>(buffer)); break;
116             case triton::arch::ID_REG_X86_ZMM16:   return 0; /* Pin doesn't support AVX-512 */
117             case triton::arch::ID_REG_X86_ZMM17:   return 0; /* Pin doesn't support AVX-512 */
118             case triton::arch::ID_REG_X86_ZMM18:   return 0; /* Pin doesn't support AVX-512 */
119             case triton::arch::ID_REG_X86_ZMM19:   return 0; /* Pin doesn't support AVX-512 */
120             case triton::arch::ID_REG_X86_ZMM20:   return 0; /* Pin doesn't support AVX-512 */
121             case triton::arch::ID_REG_X86_ZMM21:   return 0; /* Pin doesn't support AVX-512 */
122             case triton::arch::ID_REG_X86_ZMM22:   return 0; /* Pin doesn't support AVX-512 */
123             case triton::arch::ID_REG_X86_ZMM23:   return 0; /* Pin doesn't support AVX-512 */
124             case triton::arch::ID_REG_X86_ZMM24:   return 0; /* Pin doesn't support AVX-512 */
125             case triton::arch::ID_REG_X86_ZMM25:   return 0; /* Pin doesn't support AVX-512 */
126             case triton::arch::ID_REG_X86_ZMM26:   return 0; /* Pin doesn't support AVX-512 */
127             case triton::arch::ID_REG_X86_ZMM27:   return 0; /* Pin doesn't support AVX-512 */
128             case triton::arch::ID_REG_X86_ZMM28:   return 0; /* Pin doesn't support AVX-512 */
129             case triton::arch::ID_REG_X86_ZMM29:   return 0; /* Pin doesn't support AVX-512 */
130             case triton::arch::ID_REG_X86_ZMM30:   return 0; /* Pin doesn't support AVX-512 */
131             case triton::arch::ID_REG_X86_ZMM31:   return 0; /* Pin doesn't support AVX-512 */
132             case triton::arch::ID_REG_X86_MXCSR:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_MXCSR, reinterpret_cast<triton::uint8*>(buffer)); break;
133             case triton::arch::ID_REG_X86_CR0:     return 0; /* Don't care about this register in ring3 */
134             case triton::arch::ID_REG_X86_CR1:     return 0; /* Don't care about this register in ring3 */
135             case triton::arch::ID_REG_X86_CR2:     return 0; /* Don't care about this register in ring3 */
136             case triton::arch::ID_REG_X86_CR3:     return 0; /* Don't care about this register in ring3 */
137             case triton::arch::ID_REG_X86_CR4:     return 0; /* Don't care about this register in ring3 */
138             case triton::arch::ID_REG_X86_CR5:     return 0; /* Don't care about this register in ring3 */
139             case triton::arch::ID_REG_X86_CR6:     return 0; /* Don't care about this register in ring3 */
140             case triton::arch::ID_REG_X86_CR7:     return 0; /* Don't care about this register in ring3 */
141             case triton::arch::ID_REG_X86_CR8:     return 0; /* Don't care about this register in ring3 */
142             case triton::arch::ID_REG_X86_CR9:     return 0; /* Don't care about this register in ring3 */
143             case triton::arch::ID_REG_X86_CR10:    return 0; /* Don't care about this register in ring3 */
144             case triton::arch::ID_REG_X86_CR11:    return 0; /* Don't care about this register in ring3 */
145             case triton::arch::ID_REG_X86_CR12:    return 0; /* Don't care about this register in ring3 */
146             case triton::arch::ID_REG_X86_CR13:    return 0; /* Don't care about this register in ring3 */
147             case triton::arch::ID_REG_X86_CR14:    return 0; /* Don't care about this register in ring3 */
148             case triton::arch::ID_REG_X86_CR15:    return 0; /* Don't care about this register in ring3 */
149             case triton::arch::ID_REG_X86_CS:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_CS,       reinterpret_cast<triton::uint8*>(buffer)); break;
150             case triton::arch::ID_REG_X86_DS:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_DS,       reinterpret_cast<triton::uint8*>(buffer)); break;
151             case triton::arch::ID_REG_X86_ES:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_ES,       reinterpret_cast<triton::uint8*>(buffer)); break;
152             case triton::arch::ID_REG_X86_FS:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_FS_BASE,  reinterpret_cast<triton::uint8*>(buffer)); break;
153             case triton::arch::ID_REG_X86_GS:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_GS_BASE,  reinterpret_cast<triton::uint8*>(buffer)); break;
154             case triton::arch::ID_REG_X86_SS:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_SS,       reinterpret_cast<triton::uint8*>(buffer)); break;
155             default:
156               if (reg.getId() >= triton::arch::ID_REG_X86_AC && reg.getId() <= triton::arch::ID_REG_X86_ZF)
157                 PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RFLAGS, reinterpret_cast<triton::uint8*>(buffer));
158               else if (reg.getId() >= triton::arch::ID_REG_X86_IE && reg.getId() <= triton::arch::ID_REG_X86_FZ)
159                 PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_MXCSR, reinterpret_cast<triton::uint8*>(buffer));
160               else
161                 throw std::runtime_error("tracer::pintool::context::getCurrentRegisterValue(): Invalid register.");
162               break;
163           }
164 
165         /* Sync with the libTriton */
166         const triton::arch::Register* syncReg = nullptr;
167         if (reg.getId() >= triton::arch::ID_REG_X86_AC && reg.getId() <= triton::arch::ID_REG_X86_ZF)
168           syncReg = &tracer::pintool::api.getRegister(triton::arch::ID_REG_X86_EFLAGS);
169         else if (reg.getId() >= triton::arch::ID_REG_X86_IE && reg.getId() <= triton::arch::ID_REG_X86_FZ)
170           syncReg = &tracer::pintool::api.getRegister(triton::arch::ID_REG_X86_MXCSR);
171         else
172           syncReg = &tracer::pintool::api.getParentRegister(reg.getId());
173         #endif
174 
175         #if defined(__i386) || defined(_M_IX86)
176           switch (reg.getParent()) {
177             case triton::arch::ID_REG_X86_EAX:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EAX,    reinterpret_cast<triton::uint8*>(buffer)); break;
178             case triton::arch::ID_REG_X86_EBX:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EBX,    reinterpret_cast<triton::uint8*>(buffer)); break;
179             case triton::arch::ID_REG_X86_ECX:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_ECX,    reinterpret_cast<triton::uint8*>(buffer)); break;
180             case triton::arch::ID_REG_X86_EDX:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EDX,    reinterpret_cast<triton::uint8*>(buffer)); break;
181             case triton::arch::ID_REG_X86_EDI:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EDI,    reinterpret_cast<triton::uint8*>(buffer)); break;
182             case triton::arch::ID_REG_X86_ESI:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_ESI,    reinterpret_cast<triton::uint8*>(buffer)); break;
183             case triton::arch::ID_REG_X86_EBP:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EBP,    reinterpret_cast<triton::uint8*>(buffer)); break;
184             case triton::arch::ID_REG_X86_ESP:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_ESP,    reinterpret_cast<triton::uint8*>(buffer)); break;
185             case triton::arch::ID_REG_X86_EIP:     PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EIP,    reinterpret_cast<triton::uint8*>(buffer)); break;
186             case triton::arch::ID_REG_X86_EFLAGS:  PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EFLAGS, reinterpret_cast<triton::uint8*>(buffer)); break;
187             case triton::arch::ID_REG_X86_MM0:     return 0; /* Pin doesn't support MMX */
188             case triton::arch::ID_REG_X86_MM1:     return 0; /* Pin doesn't support MMX */
189             case triton::arch::ID_REG_X86_MM2:     return 0; /* Pin doesn't support MMX */
190             case triton::arch::ID_REG_X86_MM3:     return 0; /* Pin doesn't support MMX */
191             case triton::arch::ID_REG_X86_MM4:     return 0; /* Pin doesn't support MMX */
192             case triton::arch::ID_REG_X86_MM5:     return 0; /* Pin doesn't support MMX */
193             case triton::arch::ID_REG_X86_MM6:     return 0; /* Pin doesn't support MMX */
194             case triton::arch::ID_REG_X86_MM7:     return 0; /* Pin doesn't support MMX */
195             case triton::arch::ID_REG_X86_XMM0:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM0,   reinterpret_cast<triton::uint8*>(buffer)); break;
196             case triton::arch::ID_REG_X86_XMM1:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM1,   reinterpret_cast<triton::uint8*>(buffer)); break;
197             case triton::arch::ID_REG_X86_XMM2:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM2,   reinterpret_cast<triton::uint8*>(buffer)); break;
198             case triton::arch::ID_REG_X86_XMM3:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM3,   reinterpret_cast<triton::uint8*>(buffer)); break;
199             case triton::arch::ID_REG_X86_XMM4:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM4,   reinterpret_cast<triton::uint8*>(buffer)); break;
200             case triton::arch::ID_REG_X86_XMM5:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM5,   reinterpret_cast<triton::uint8*>(buffer)); break;
201             case triton::arch::ID_REG_X86_XMM6:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM6,   reinterpret_cast<triton::uint8*>(buffer)); break;
202             case triton::arch::ID_REG_X86_XMM7:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM7,   reinterpret_cast<triton::uint8*>(buffer)); break;
203             case triton::arch::ID_REG_X86_YMM0:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM0,   reinterpret_cast<triton::uint8*>(buffer)); break;
204             case triton::arch::ID_REG_X86_YMM1:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM1,   reinterpret_cast<triton::uint8*>(buffer)); break;
205             case triton::arch::ID_REG_X86_YMM2:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM2,   reinterpret_cast<triton::uint8*>(buffer)); break;
206             case triton::arch::ID_REG_X86_YMM3:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM3,   reinterpret_cast<triton::uint8*>(buffer)); break;
207             case triton::arch::ID_REG_X86_YMM4:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM4,   reinterpret_cast<triton::uint8*>(buffer)); break;
208             case triton::arch::ID_REG_X86_YMM5:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM5,   reinterpret_cast<triton::uint8*>(buffer)); break;
209             case triton::arch::ID_REG_X86_YMM6:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM6,   reinterpret_cast<triton::uint8*>(buffer)); break;
210             case triton::arch::ID_REG_X86_YMM7:    PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM7,   reinterpret_cast<triton::uint8*>(buffer)); break;
211             case triton::arch::ID_REG_X86_MXCSR:   PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_MXCSR,  reinterpret_cast<triton::uint8*>(buffer)); break;
212             case triton::arch::ID_REG_X86_CR0:     return 0; /* Don't care about this register in ring3 */
213             case triton::arch::ID_REG_X86_CR1:     return 0; /* Don't care about this register in ring3 */
214             case triton::arch::ID_REG_X86_CR2:     return 0; /* Don't care about this register in ring3 */
215             case triton::arch::ID_REG_X86_CR3:     return 0; /* Don't care about this register in ring3 */
216             case triton::arch::ID_REG_X86_CR4:     return 0; /* Don't care about this register in ring3 */
217             case triton::arch::ID_REG_X86_CR5:     return 0; /* Don't care about this register in ring3 */
218             case triton::arch::ID_REG_X86_CR6:     return 0; /* Don't care about this register in ring3 */
219             case triton::arch::ID_REG_X86_CR7:     return 0; /* Don't care about this register in ring3 */
220             case triton::arch::ID_REG_X86_CR8:     return 0; /* Don't care about this register in ring3 */
221             case triton::arch::ID_REG_X86_CR9:     return 0; /* Don't care about this register in ring3 */
222             case triton::arch::ID_REG_X86_CR10:    return 0; /* Don't care about this register in ring3 */
223             case triton::arch::ID_REG_X86_CR11:    return 0; /* Don't care about this register in ring3 */
224             case triton::arch::ID_REG_X86_CR12:    return 0; /* Don't care about this register in ring3 */
225             case triton::arch::ID_REG_X86_CR13:    return 0; /* Don't care about this register in ring3 */
226             case triton::arch::ID_REG_X86_CR14:    return 0; /* Don't care about this register in ring3 */
227             case triton::arch::ID_REG_X86_CR15:    return 0; /* Don't care about this register in ring3 */
228             case triton::arch::ID_REG_X86_CS:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_CS,       reinterpret_cast<triton::uint8*>(buffer)); break;
229             case triton::arch::ID_REG_X86_DS:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_DS,       reinterpret_cast<triton::uint8*>(buffer)); break;
230             case triton::arch::ID_REG_X86_ES:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_ES,       reinterpret_cast<triton::uint8*>(buffer)); break;
231             case triton::arch::ID_REG_X86_FS:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_FS_BASE,  reinterpret_cast<triton::uint8*>(buffer)); break;
232             case triton::arch::ID_REG_X86_GS:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_GS_BASE,  reinterpret_cast<triton::uint8*>(buffer)); break;
233             case triton::arch::ID_REG_X86_SS:      PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_SS,       reinterpret_cast<triton::uint8*>(buffer)); break;
234             default:
235               if (reg.getId() >= triton::arch::ID_REG_X86_AC && reg.getId() <= triton::arch::ID_REG_X86_ZF)
236                 PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EFLAGS, reinterpret_cast<triton::uint8*>(buffer));
237               else if (reg.getId() >= triton::arch::ID_REG_X86_IE && reg.getId() <= triton::arch::ID_REG_X86_FZ)
238                 PIN_GetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_MXCSR, reinterpret_cast<triton::uint8*>(buffer));
239               else
240                 throw std::runtime_error("tracer::pintool::context::getCurrentRegisterValue(): Invalid register.");
241               break;
242           }
243 
244         /* Sync with the libTriton */
245         const triton::arch::Register* syncReg = nullptr;
246         if (reg.getId() >= triton::arch::ID_REG_X86_AC && reg.getId() <= triton::arch::ID_REG_X86_ZF)
247           syncReg = &tracer::pintool::api.getRegister(triton::arch::ID_REG_X86_EFLAGS);
248         else if (reg.getId() >= triton::arch::ID_REG_X86_IE && reg.getId() <= triton::arch::ID_REG_X86_FZ)
249           syncReg = &tracer::pintool::api.getRegister(triton::arch::ID_REG_X86_MXCSR);
250         else
251           syncReg = &tracer::pintool::api.getParentRegister(reg.getId());
252         #endif
253 
254         value = triton::utils::fromBufferToUint<triton::uint512>(buffer);
255         tracer::pintool::api.getCpuInstance()->setConcreteRegisterValue(*syncReg, value);
256 
257         /* Returns the good casted value */
258         return tracer::pintool::api.getConcreteRegisterValue(reg, false);
259       }
260 
261 
getCurrentMemoryValue(const triton::arch::MemoryAccess & mem)262       triton::uint512 getCurrentMemoryValue(const triton::arch::MemoryAccess& mem) {
263         return tracer::pintool::context::getCurrentMemoryValue(mem.getAddress(), mem.getSize());
264       }
265 
266 
getCurrentMemoryValue(triton::__uint addr)267       triton::uint512 getCurrentMemoryValue(triton::__uint addr) {
268         triton::uint512 value = 0;
269 
270         if (PIN_CheckReadAccess(reinterpret_cast<triton::uint8*>(addr)) == false)
271           throw std::runtime_error("tracer::pintool::context::getCurrentMemoryValue(): Page not readable.");
272 
273         value = *(reinterpret_cast<triton::uint8*>(addr));
274 
275         return value;
276       }
277 
278 
getCurrentMemoryValue(triton::__uint addr,triton::uint32 size)279       triton::uint512 getCurrentMemoryValue(triton::__uint addr, triton::uint32 size) {
280         triton::uint512 value = 0;
281 
282         if (PIN_CheckReadAccess(reinterpret_cast<triton::uint8*>(addr)) == false || PIN_CheckReadAccess(reinterpret_cast<triton::uint8*>(addr+size-1)) == false)
283           throw std::runtime_error("tracer::pintool::context::getCurrentMemoryValue(): Page not readable.");
284 
285         switch(size) {
286           case triton::size::byte:    value = *(reinterpret_cast<triton::uint8*>(addr));  break;
287           case triton::size::word:    value = *(reinterpret_cast<triton::uint16*>(addr)); break;
288           case triton::size::dword:   value = *(reinterpret_cast<triton::uint32*>(addr)); break;
289           case triton::size::qword:   value = *(reinterpret_cast<triton::uint64*>(addr)); break;
290           case triton::size::dqword:  value = triton::utils::fromBufferToUint<triton::uint128>(reinterpret_cast<triton::uint8*>(addr)); break;
291           case triton::size::qqword:  value = triton::utils::fromBufferToUint<triton::uint256>(reinterpret_cast<triton::uint8*>(addr)); break;
292           case triton::size::dqqword: value = triton::utils::fromBufferToUint<triton::uint512>(reinterpret_cast<triton::uint8*>(addr)); break;
293         }
294 
295         return value;
296       }
297 
298 
setCurrentRegisterValue(const triton::arch::Register & reg,triton::uint512 value)299       void setCurrentRegisterValue(const triton::arch::Register& reg, triton::uint512 value) {
300         triton::uint8 buffer[triton::size::dqqword] = {0};
301 
302         if (reg.getId() != reg.getParent() || tracer::pintool::api.isFlag(reg))
303           throw std::runtime_error("tracer::pintool::context::setCurrentRegisterValue(): You cannot set a Pin register value on a sub-register or a flag.");
304 
305         if (tracer::pintool::context::lastContext == nullptr)
306           return;
307 
308         triton::utils::fromUintToBuffer(value, buffer);
309 
310         #if defined(__x86_64__) || defined(_M_X64)
311           switch (reg.getId()) {
312             case triton::arch::ID_REG_X86_RAX:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RAX,    reinterpret_cast<triton::uint8*>(buffer)); break;
313             case triton::arch::ID_REG_X86_RBX:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RBX,    reinterpret_cast<triton::uint8*>(buffer)); break;
314             case triton::arch::ID_REG_X86_RCX:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RCX,    reinterpret_cast<triton::uint8*>(buffer)); break;
315             case triton::arch::ID_REG_X86_RDX:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RDX,    reinterpret_cast<triton::uint8*>(buffer)); break;
316             case triton::arch::ID_REG_X86_RDI:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RDI,    reinterpret_cast<triton::uint8*>(buffer)); break;
317             case triton::arch::ID_REG_X86_RSI:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RSI,    reinterpret_cast<triton::uint8*>(buffer)); break;
318             case triton::arch::ID_REG_X86_RBP:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RBP,    reinterpret_cast<triton::uint8*>(buffer)); break;
319             case triton::arch::ID_REG_X86_RSP:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RSP,    reinterpret_cast<triton::uint8*>(buffer)); break;
320             case triton::arch::ID_REG_X86_RIP:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RIP,    reinterpret_cast<triton::uint8*>(buffer)); break;
321             case triton::arch::ID_REG_X86_EFLAGS:  PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_RFLAGS, reinterpret_cast<triton::uint8*>(buffer)); break;
322             case triton::arch::ID_REG_X86_R8:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R8,     reinterpret_cast<triton::uint8*>(buffer)); break;
323             case triton::arch::ID_REG_X86_R9:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R9,     reinterpret_cast<triton::uint8*>(buffer)); break;
324             case triton::arch::ID_REG_X86_R10:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R10,    reinterpret_cast<triton::uint8*>(buffer)); break;
325             case triton::arch::ID_REG_X86_R11:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R11,    reinterpret_cast<triton::uint8*>(buffer)); break;
326             case triton::arch::ID_REG_X86_R12:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R12,    reinterpret_cast<triton::uint8*>(buffer)); break;
327             case triton::arch::ID_REG_X86_R13:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R13,    reinterpret_cast<triton::uint8*>(buffer)); break;
328             case triton::arch::ID_REG_X86_R14:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R14,    reinterpret_cast<triton::uint8*>(buffer)); break;
329             case triton::arch::ID_REG_X86_R15:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_R15,    reinterpret_cast<triton::uint8*>(buffer)); break;
330             case triton::arch::ID_REG_X86_XMM0:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM0,   reinterpret_cast<triton::uint8*>(buffer)); break;
331             case triton::arch::ID_REG_X86_XMM1:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM1,   reinterpret_cast<triton::uint8*>(buffer)); break;
332             case triton::arch::ID_REG_X86_XMM2:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM2,   reinterpret_cast<triton::uint8*>(buffer)); break;
333             case triton::arch::ID_REG_X86_XMM3:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM3,   reinterpret_cast<triton::uint8*>(buffer)); break;
334             case triton::arch::ID_REG_X86_XMM4:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM4,   reinterpret_cast<triton::uint8*>(buffer)); break;
335             case triton::arch::ID_REG_X86_XMM5:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM5,   reinterpret_cast<triton::uint8*>(buffer)); break;
336             case triton::arch::ID_REG_X86_XMM6:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM6,   reinterpret_cast<triton::uint8*>(buffer)); break;
337             case triton::arch::ID_REG_X86_XMM7:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM7,   reinterpret_cast<triton::uint8*>(buffer)); break;
338             case triton::arch::ID_REG_X86_XMM8:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM8,   reinterpret_cast<triton::uint8*>(buffer)); break;
339             case triton::arch::ID_REG_X86_XMM9:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM9,   reinterpret_cast<triton::uint8*>(buffer)); break;
340             case triton::arch::ID_REG_X86_XMM10:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM10,  reinterpret_cast<triton::uint8*>(buffer)); break;
341             case triton::arch::ID_REG_X86_XMM11:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM11,  reinterpret_cast<triton::uint8*>(buffer)); break;
342             case triton::arch::ID_REG_X86_XMM12:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM12,  reinterpret_cast<triton::uint8*>(buffer)); break;
343             case triton::arch::ID_REG_X86_XMM13:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM13,  reinterpret_cast<triton::uint8*>(buffer)); break;
344             case triton::arch::ID_REG_X86_XMM14:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM14,  reinterpret_cast<triton::uint8*>(buffer)); break;
345             case triton::arch::ID_REG_X86_XMM15:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM15,  reinterpret_cast<triton::uint8*>(buffer)); break;
346             case triton::arch::ID_REG_X86_YMM0:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM0,   reinterpret_cast<triton::uint8*>(buffer)); break;
347             case triton::arch::ID_REG_X86_YMM1:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM1,   reinterpret_cast<triton::uint8*>(buffer)); break;
348             case triton::arch::ID_REG_X86_YMM2:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM2,   reinterpret_cast<triton::uint8*>(buffer)); break;
349             case triton::arch::ID_REG_X86_YMM3:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM3,   reinterpret_cast<triton::uint8*>(buffer)); break;
350             case triton::arch::ID_REG_X86_YMM4:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM4,   reinterpret_cast<triton::uint8*>(buffer)); break;
351             case triton::arch::ID_REG_X86_YMM5:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM5,   reinterpret_cast<triton::uint8*>(buffer)); break;
352             case triton::arch::ID_REG_X86_YMM6:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM6,   reinterpret_cast<triton::uint8*>(buffer)); break;
353             case triton::arch::ID_REG_X86_YMM7:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM7,   reinterpret_cast<triton::uint8*>(buffer)); break;
354             case triton::arch::ID_REG_X86_YMM8:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM8,   reinterpret_cast<triton::uint8*>(buffer)); break;
355             case triton::arch::ID_REG_X86_YMM9:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM9,   reinterpret_cast<triton::uint8*>(buffer)); break;
356             case triton::arch::ID_REG_X86_YMM10:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM10,  reinterpret_cast<triton::uint8*>(buffer)); break;
357             case triton::arch::ID_REG_X86_YMM11:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM11,  reinterpret_cast<triton::uint8*>(buffer)); break;
358             case triton::arch::ID_REG_X86_YMM12:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM12,  reinterpret_cast<triton::uint8*>(buffer)); break;
359             case triton::arch::ID_REG_X86_YMM13:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM13,  reinterpret_cast<triton::uint8*>(buffer)); break;
360             case triton::arch::ID_REG_X86_YMM14:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM14,  reinterpret_cast<triton::uint8*>(buffer)); break;
361             case triton::arch::ID_REG_X86_YMM15:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM15,  reinterpret_cast<triton::uint8*>(buffer)); break;
362             case triton::arch::ID_REG_X86_MXCSR:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_MXCSR,  reinterpret_cast<triton::uint8*>(buffer)); break;
363             case triton::arch::ID_REG_X86_CS:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_CS, reinterpret_cast<triton::uint8*>(buffer)); break;
364             case triton::arch::ID_REG_X86_DS:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_DS, reinterpret_cast<triton::uint8*>(buffer)); break;
365             case triton::arch::ID_REG_X86_ES:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_ES, reinterpret_cast<triton::uint8*>(buffer)); break;
366             case triton::arch::ID_REG_X86_FS:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_FS, reinterpret_cast<triton::uint8*>(buffer)); break;
367             case triton::arch::ID_REG_X86_GS:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_GS, reinterpret_cast<triton::uint8*>(buffer)); break;
368             case triton::arch::ID_REG_X86_SS:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_SS, reinterpret_cast<triton::uint8*>(buffer)); break;
369             default:
370               throw std::runtime_error("tracer::pintool::context::setCurrentRegisterValue(): Invalid register.");
371           }
372         #endif
373 
374         #if defined(__i386) || defined(_M_IX86)
375           switch (reg.getId()) {
376             case triton::arch::ID_REG_X86_EAX:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EAX,    reinterpret_cast<triton::uint8*>(buffer)); break;
377             case triton::arch::ID_REG_X86_EBX:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EBX,    reinterpret_cast<triton::uint8*>(buffer)); break;
378             case triton::arch::ID_REG_X86_ECX:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_ECX,    reinterpret_cast<triton::uint8*>(buffer)); break;
379             case triton::arch::ID_REG_X86_EDX:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EDX,    reinterpret_cast<triton::uint8*>(buffer)); break;
380             case triton::arch::ID_REG_X86_EDI:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EDI,    reinterpret_cast<triton::uint8*>(buffer)); break;
381             case triton::arch::ID_REG_X86_ESI:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_ESI,    reinterpret_cast<triton::uint8*>(buffer)); break;
382             case triton::arch::ID_REG_X86_EBP:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EBP,    reinterpret_cast<triton::uint8*>(buffer)); break;
383             case triton::arch::ID_REG_X86_ESP:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_ESP,    reinterpret_cast<triton::uint8*>(buffer)); break;
384             case triton::arch::ID_REG_X86_EIP:     PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EIP,    reinterpret_cast<triton::uint8*>(buffer)); break;
385             case triton::arch::ID_REG_X86_EFLAGS:  PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_EFLAGS, reinterpret_cast<triton::uint8*>(buffer)); break;
386             case triton::arch::ID_REG_X86_XMM0:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM0,   reinterpret_cast<triton::uint8*>(buffer)); break;
387             case triton::arch::ID_REG_X86_XMM1:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM1,   reinterpret_cast<triton::uint8*>(buffer)); break;
388             case triton::arch::ID_REG_X86_XMM2:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM2,   reinterpret_cast<triton::uint8*>(buffer)); break;
389             case triton::arch::ID_REG_X86_XMM3:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM3,   reinterpret_cast<triton::uint8*>(buffer)); break;
390             case triton::arch::ID_REG_X86_XMM4:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM4,   reinterpret_cast<triton::uint8*>(buffer)); break;
391             case triton::arch::ID_REG_X86_XMM5:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM5,   reinterpret_cast<triton::uint8*>(buffer)); break;
392             case triton::arch::ID_REG_X86_XMM6:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM6,   reinterpret_cast<triton::uint8*>(buffer)); break;
393             case triton::arch::ID_REG_X86_XMM7:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_XMM7,   reinterpret_cast<triton::uint8*>(buffer)); break;
394             case triton::arch::ID_REG_X86_YMM0:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM0,   reinterpret_cast<triton::uint8*>(buffer)); break;
395             case triton::arch::ID_REG_X86_YMM1:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM1,   reinterpret_cast<triton::uint8*>(buffer)); break;
396             case triton::arch::ID_REG_X86_YMM2:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM2,   reinterpret_cast<triton::uint8*>(buffer)); break;
397             case triton::arch::ID_REG_X86_YMM3:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM3,   reinterpret_cast<triton::uint8*>(buffer)); break;
398             case triton::arch::ID_REG_X86_YMM4:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM4,   reinterpret_cast<triton::uint8*>(buffer)); break;
399             case triton::arch::ID_REG_X86_YMM5:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM5,   reinterpret_cast<triton::uint8*>(buffer)); break;
400             case triton::arch::ID_REG_X86_YMM6:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM6,   reinterpret_cast<triton::uint8*>(buffer)); break;
401             case triton::arch::ID_REG_X86_YMM7:    PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_YMM7,   reinterpret_cast<triton::uint8*>(buffer)); break;
402             case triton::arch::ID_REG_X86_MXCSR:   PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_MXCSR,  reinterpret_cast<triton::uint8*>(buffer)); break;
403             case triton::arch::ID_REG_X86_CS:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_CS, reinterpret_cast<triton::uint8*>(buffer)); break;
404             case triton::arch::ID_REG_X86_DS:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_DS, reinterpret_cast<triton::uint8*>(buffer)); break;
405             case triton::arch::ID_REG_X86_ES:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_ES, reinterpret_cast<triton::uint8*>(buffer)); break;
406             case triton::arch::ID_REG_X86_FS:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_FS, reinterpret_cast<triton::uint8*>(buffer)); break;
407             case triton::arch::ID_REG_X86_GS:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_GS, reinterpret_cast<triton::uint8*>(buffer)); break;
408             case triton::arch::ID_REG_X86_SS:      PIN_SetContextRegval(tracer::pintool::context::lastContext, LEVEL_BASE::REG_SEG_SS, reinterpret_cast<triton::uint8*>(buffer)); break;
409             default:
410               throw std::runtime_error("tracer::pintool::context::setCurrentRegisterValue(): Invalid register.");
411           }
412         #endif
413 
414         /* Sync with the libTriton */
415         const triton::arch::Register syncReg(reg);
416         tracer::pintool::api.setConcreteRegisterValue(syncReg, value);
417 
418         /* Define that the context must be executed as soon as possible */
419         tracer::pintool::context::mustBeExecuted = true;
420       }
421 
422 
setCurrentMemoryValue(const triton::arch::MemoryAccess & mem,triton::uint512 value)423       void setCurrentMemoryValue(const triton::arch::MemoryAccess& mem, triton::uint512 value) {
424         triton::__uint addr = mem.getAddress();
425         triton::uint32 size = mem.getSize();
426 
427         /* Sync with the libTriton */
428         tracer::pintool::api.setConcreteMemoryValue(mem, value);
429 
430         /* Inject memory value */
431         for (triton::uint32 i = 0; i < size; i++) {
432           if (PIN_CheckWriteAccess(reinterpret_cast<triton::uint8*>((addr+i))) == false)
433             throw std::runtime_error("tracer::pintool::context::setCurrentMemoryValue(): Page not writable.");
434           *((triton::uint8 *)(addr+i)) = (value & 0xff).convert_to<triton::uint8>();
435           value >>= 8;
436         }
437       }
438 
439 
setCurrentMemoryValue(triton::__uint addr,triton::uint8 value)440       void setCurrentMemoryValue(triton::__uint addr, triton::uint8 value) {
441         if (PIN_CheckWriteAccess(reinterpret_cast<triton::uint8*>(addr)) == false)
442           throw std::runtime_error("tracer::pintool::context::setCurrentMemoryValue(): Page not writable.");
443 
444         /* Sync with the libTriton */
445         tracer::pintool::api.setConcreteMemoryValue(addr, value);
446 
447         /* Inject memory value */
448         *((triton::uint8*)(addr)) = (value & 0xff);
449       }
450 
451 
executeContext(void)452       void executeContext(void) {
453         if (tracer::pintool::context::mustBeExecuted == true) {
454           PIN_UnlockClient();
455           PIN_ExecuteAt(tracer::pintool::context::lastContext);
456         }
457       }
458 
459 
needConcreteMemoryValue(triton::API & api,const triton::arch::MemoryAccess & mem)460       void needConcreteMemoryValue(triton::API& api, const triton::arch::MemoryAccess& mem) {
461         triton::uint512 cv = tracer::pintool::context::getCurrentMemoryValue(mem);
462         tracer::pintool::api.getCpuInstance()->setConcreteMemoryValue(mem, cv);
463       }
464 
465 
synchronizeContext(void)466       void synchronizeContext(void) {
467         if (tracer::pintool::api.isSymbolicEngineEnabled() == false)
468           return;
469 
470         for (const triton::arch::Register* reg : tracer::pintool::api.getParentRegisters()) {
471           triton::arch::register_e regId = reg->getId();
472 
473           if (regId > triton::arch::ID_REG_X86_EFLAGS && !(regId >= triton::arch::ID_REG_X86_CS && regId <= triton::arch::ID_REG_X86_SS))
474             continue;
475 
476           triton::uint512 cv = tracer::pintool::context::getCurrentRegisterValue(triton::arch::Register(*reg));
477           triton::uint512 sv = tracer::pintool::api.getSymbolicRegisterValue(triton::arch::Register(*reg));
478 
479           if (sv != cv) {
480             tracer::pintool::api.setConcreteRegisterValue(*reg, cv);
481           }
482         }
483       }
484 
485     };
486   };
487 };
488