libTriton version 1.0 build 1590
Loading...
Searching...
No Matches
callbacks.cpp
Go to the documentation of this file.
1
2/*
3** Copyright (C) - Triton
4**
5** This program is under the terms of the Apache License 2.0.
6*/
7
8#include <triton/context.hpp>
10#include <triton/exceptions.hpp>
11
12
13
14namespace triton {
15 namespace callbacks {
16
18 this->defined = false;
19 this->mget = false;
20 this->mload = false;
21 this->mput = false;
22 this->mstore = false;
23 }
24
25
27 switch (kind) {
29 this->getConcreteMemoryValueCallbacks.push_back(cb);
30 break;
31
32 default:
33 return;
34 }
35 this->defined = true;
36 }
37
38
40 switch (kind) {
42 this->getConcreteRegisterValueCallbacks.push_back(cb);
43 break;
44
45 default:
46 return;
47 }
48 this->defined = true;
49 }
50
51
53 switch (kind) {
55 this->setConcreteMemoryValueCallbacks.push_back(cb);
56 break;
57
58 default:
59 return;
60 }
61 this->defined = true;
62 }
63
64
66 switch (kind) {
68 this->setConcreteRegisterValueCallbacks.push_back(cb);
69 break;
70
71 default:
72 return;
73 }
74 this->defined = true;
75 }
76
77
79 switch (kind) {
81 this->symbolicSimplificationCallbacks.push_back(cb);
82 break;
83
84 default:
85 return;
86 }
87 this->defined = true;
88 }
89
90
97 this->defined = false;
98 }
99
100
101 template <typename T>
102 void Callbacks::removeSingleCallback(std::list<T>& container, T cb) {
103 for (auto it = container.begin(); it != container.end(); ++it) {
104 if (cb == *it) {
105 container.erase(it);
106 return;
107 }
108 }
109 throw triton::exceptions::Exception("Unable to find callback for removal");
110 }
111
112
114 switch (kind) {
117 break;
118
119 default:
120 throw triton::exceptions::Exception("Incorrect callback kind for removal");
121 }
122
123 if (this->countCallbacks() == 0) {
124 this->defined = false;
125 }
126 }
127
128
130 switch (kind) {
133 break;
134
135 default:
136 throw triton::exceptions::Exception("Incorrect callback kind for removal");
137 }
138
139 if (this->countCallbacks() == 0) {
140 this->defined = false;
141 }
142 }
143
144
146 switch (kind) {
149 break;
150
151 default:
152 throw triton::exceptions::Exception("Incorrect callback kind for removal");
153 }
154
155 if (this->countCallbacks() == 0) {
156 this->defined = false;
157 }
158 }
159
160
162 switch (kind) {
165 break;
166
167 default:
168 throw triton::exceptions::Exception("Incorrect callback kind for removal");
169 }
170
171 if (this->countCallbacks() == 0) {
172 this->defined = false;
173 }
174 }
175
176
178 switch (kind) {
181 break;
182
183 default:
184 throw triton::exceptions::Exception("Incorrect callback kind for removal");
185 }
186
187 if (this->countCallbacks() == 0) {
188 this->defined = false;
189 }
190 }
191
192
194 switch (kind) {
196 for (auto& function: this->symbolicSimplificationCallbacks) {
197 // Reinject node in next callback
198 node = function(this->ctx, node);
199 if (node == nullptr)
200 throw triton::exceptions::Callbacks("Callbacks::processCallbacks(SYMBOLIC_SIMPLIFICATION): You cannot return a nullptr node.");
201 }
202 return node;
203 }
204
205 default:
206 throw triton::exceptions::Callbacks("Callbacks::processCallbacks(): Invalid kind of callback for this C++ polymorphism.");
207 }
208 }
209
210
212 switch (kind) {
214 /* Check if we are already in the callback to avoid infinite recursion */
215 if (this->mload) {
216 break;
217 }
218
219 for (auto& function: this->getConcreteMemoryValueCallbacks) {
220 this->mload = true;
221 function(this->ctx, mem);
222 if (mem.getLeaAst() != nullptr) {
223 this->ctx.getSymbolicEngine()->initLeaAst(const_cast<triton::arch::MemoryAccess&>(mem));
224 }
225 this->mload = false;
226 }
227
228 break;
229 }
230
231 default:
232 throw triton::exceptions::Callbacks("Callbacks::processCallbacks(): Invalid kind of callback for this C++ polymorphism.");
233 };
234 }
235
236
238 switch (kind) {
240 /* Check if we are already in the callback to avoid infinite recursion */
241 if (this->mget) {
242 break;
243 }
244
245 for (auto& function: this->getConcreteRegisterValueCallbacks) {
246 this->mget = true;
247 function(this->ctx, reg);
248 this->mget = false;
249 }
250
251 break;
252 }
253
254 default:
255 throw triton::exceptions::Callbacks("Callbacks::processCallbacks(): Invalid kind of callback for this C++ polymorphism.");
256 };
257 }
258
259
261 switch (kind) {
263 /* Check if we are already in the callback to avoid infinite recursion */
264 if (this->mstore) {
265 break;
266 }
267
268 for (auto& function: this->setConcreteMemoryValueCallbacks) {
269 this->mstore = true;
270 function(this->ctx, mem, value);
271 this->mstore = false;
272 }
273
274 break;
275 }
276
277 default:
278 throw triton::exceptions::Callbacks("Callbacks::processCallbacks(): Invalid kind of callback for this C++ polymorphism.");
279 };
280 }
281
282
284 switch (kind) {
286 /* Check if we are already in the callback to avoid infinite recursion */
287 if (this->mput) {
288 break;
289 }
290
291 for (auto& function: this->setConcreteRegisterValueCallbacks) {
292 this->mput = true;
293 function(this->ctx, reg, value);
294 this->mput = false;
295 }
296
297 break;
298 }
299
300 default:
301 throw triton::exceptions::Callbacks("Callbacks::processCallbacks(): Invalid kind of callback for this C++ polymorphism.");
302 };
303 }
304
305
307 triton::usize count = 0;
308
309 count += this->getConcreteMemoryValueCallbacks.size();
310 count += this->getConcreteRegisterValueCallbacks.size();
311 count += this->setConcreteMemoryValueCallbacks.size();
312 count += this->setConcreteRegisterValueCallbacks.size();
313 count += this->symbolicSimplificationCallbacks.size();
314
315 return count;
316 }
317
318
320 switch (kind) {
326 default: {
327 return false;
328 }
329 }
330 }
331
332
333 bool Callbacks::isDefined(void) const {
334 return this->defined;
335 }
336
337 }; /* callbacks namespace */
338}; /* triton namespace */
This is the main Triton Context class.
Definition context.hpp:45
TRITON_EXPORT triton::engines::symbolic::SymbolicEngine * getSymbolicEngine(void)
[symbolic api] - Returns the instance of the symbolic engine.
Definition context.cpp:699
This class is used to represent a memory access.
TRITON_EXPORT triton::ast::SharedAbstractNode getLeaAst(void) const
Returns the AST of the memory access (LEA).
This class is used when an instruction has a register operand.
Definition register.hpp:44
TRITON_EXPORT void removeCallback(triton::callbacks::callback_e kind, ComparableFunctor< void(triton::Context &, const triton::arch::MemoryAccess &)> cb)
Deletes a GET_CONCRETE_MEMORY_VALUE callback.
std::list< triton::callbacks::symbolicSimplificationCallback > symbolicSimplificationCallbacks
[c++] Callbacks for all symbolic simplifications.
TRITON_EXPORT void clearCallbacks(void)
Clears recorded callbacks.
Definition callbacks.cpp:91
TRITON_EXPORT void addCallback(triton::callbacks::callback_e kind, ComparableFunctor< void(triton::Context &, const triton::arch::MemoryAccess &)> cb)
Adds a GET_CONCRETE_MEMORY_VALUE callback.
Definition callbacks.cpp:26
std::list< triton::callbacks::setConcreteMemoryValueCallback > setConcreteMemoryValueCallbacks
[c++] Callbacks for all concrete memory needs (STORE).
triton::usize countCallbacks(void) const
Returns the number of callbacks recorded.
std::list< triton::callbacks::getConcreteMemoryValueCallback > getConcreteMemoryValueCallbacks
[c++] Callbacks for all concrete memory needs (LOAD).
std::list< triton::callbacks::setConcreteRegisterValueCallback > setConcreteRegisterValueCallbacks
[c++] Callbacks for all concrete register needs (PUT).
TRITON_EXPORT Callbacks(triton::Context &ctx)
Constructor.
Definition callbacks.cpp:17
TRITON_EXPORT bool isDefined(void) const
Returns true if at least one callback is defined.
void removeSingleCallback(std::list< T > &container, T cb)
Trys to find and remove the callback, raises an exception if not able.
std::list< triton::callbacks::getConcreteRegisterValueCallback > getConcreteRegisterValueCallbacks
[c++] Callbacks for all concrete register needs (GET).
TRITON_EXPORT triton::ast::SharedAbstractNode processCallbacks(triton::callbacks::callback_e kind, triton::ast::SharedAbstractNode node)
Processes callbacks according to the kind and the C++ polymorphism.
TRITON_EXPORT void initLeaAst(triton::arch::MemoryAccess &mem, bool force=true)
Initializes the effective address of a memory access.
The exception class used by callbacks.
The root class of all exceptions.
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
Definition ast.hpp:59
std::size_t usize
unsigned MAX_INT 32 or 64 bits according to the CPU.
math::wide_integer::uint512_t uint512
unsigned 512-bits
The Triton namespace.