Skip to content

Commit

Permalink
Merge branch 'dev-v1.0' of github.com:JonathanSalwan/Triton into dev-…
Browse files Browse the repository at this point in the history
…v1.0
  • Loading branch information
JonathanSalwan committed Jun 4, 2022
2 parents 7fdc0ed + 74df66d commit ec72d86
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 29 deletions.
8 changes: 4 additions & 4 deletions src/libtriton/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,15 +1319,15 @@ namespace triton {
}


std::ostream& API::liftToPython(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr) {
std::ostream& API::liftToPython(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool icomment) {
this->checkLifting();
return this->lifting->liftToPython(stream, expr);
return this->lifting->liftToPython(stream, expr, icomment);
}


std::ostream& API::liftToSMT(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool assert_) {
std::ostream& API::liftToSMT(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool assert_, bool icomment) {
this->checkLifting();
return this->lifting->liftToSMT(stream, expr, assert_);
return this->lifting->liftToSMT(stream, expr, assert_, icomment);
}


Expand Down
54 changes: 41 additions & 13 deletions src/libtriton/bindings/python/objects/pyTritonContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ Lifts an AST node and all its references to LLVM IR. `fname` is the name of the
- <b>string liftToLLVM(\ref py_SymbolicExpression_page expr, string fname="__triton", bool optimize=False)</b><br>
Lifts a symbolic expression and all its references to LLVM IR. `fname` is the name of the LLVM function, by default it's `__triton`. If `optimize` is true, perform optimizations (-O3 -Oz).
- <b>string liftToPython(\ref py_SymbolicExpression_page expr)</b><br>
Lifts a symbolic expression and all its references to Python format.
- <b>string liftToPython(\ref py_SymbolicExpression_page expr, bool icomment=False)</b><br>
Lifts a symbolic expression and all its references to Python format. If `icomment` is true, then print instructions assembly in expression comments.
- <b>string liftToSMT(\ref py_SymbolicExpression_page expr, bool assert_=False)</b><br>
Lifts a symbolic expression and all its references to SMT format. If `assert_` is true, then (assert <expr>).
- <b>string liftToSMT(\ref py_SymbolicExpression_page expr, bool assert_=False, bool icomment=False)</b><br>
Lifts a symbolic expression and all its references to SMT format. If `assert_` is true, then (assert <expr>). If `icomment` is true, then print instructions assembly in expression comments.
- <b>\ref py_SymbolicExpression_page newSymbolicExpression(\ref py_AstNode_page node, string comment)</b><br>
Returns a new symbolic expression. Note that if there are simplification passes recorded, simplifications will be applied.
Expand Down Expand Up @@ -2256,13 +2256,33 @@ namespace triton {
}


static PyObject* TritonContext_liftToPython(PyObject* self, PyObject* expr) {
if (!PySymbolicExpression_Check(expr))
return PyErr_Format(PyExc_TypeError, "TritonContext::liftToPython(): Expects a SymbolicExpression as first argument.");
static PyObject* TritonContext_liftToPython(PyObject* self, PyObject* args, PyObject* kwargs) {
PyObject* expr = nullptr;
PyObject* icomment = nullptr;

static char* keywords[] = {
(char*)"expr",
(char*)"icomment",
nullptr
};

/* Extract keywords */
if (PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", keywords, &expr, &icomment) == false) {
return PyErr_Format(PyExc_TypeError, "TritonContext::liftToPython(): Invalid number of arguments");
}

if (expr == nullptr || !PySymbolicExpression_Check(expr))
return PyErr_Format(PyExc_TypeError, "TritonContext::liftToPython(): Expects a SymbolicExpression as expr argument.");

if (icomment != nullptr && !PyBool_Check(icomment))
return PyErr_Format(PyExc_TypeError, "TritonContext::liftToPython(): Expects a boolean as icomment argument.");

if (icomment == nullptr)
icomment = PyLong_FromUint32(false);

try {
std::ostringstream stream;
PyTritonContext_AsTritonContext(self)->liftToPython(stream, PySymbolicExpression_AsSymbolicExpression(expr));
PyTritonContext_AsTritonContext(self)->liftToPython(stream, PySymbolicExpression_AsSymbolicExpression(expr), PyLong_AsBool(icomment));
return xPyString_FromString(stream.str().c_str());
}
catch (const triton::exceptions::PyCallbacks&) {
Expand All @@ -2278,17 +2298,19 @@ namespace triton {


static PyObject* TritonContext_liftToSMT(PyObject* self, PyObject* args, PyObject* kwargs) {
PyObject* expr = nullptr;
PyObject* assert = nullptr;
PyObject* expr = nullptr;
PyObject* assert = nullptr;
PyObject* icomment = nullptr;

static char* keywords[] = {
(char*)"expr",
(char*)"assert_",
(char*)"icomment",
nullptr
};

/* Extract keywords */
if (PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", keywords, &expr, &assert) == false) {
if (PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", keywords, &expr, &assert, &icomment) == false) {
return PyErr_Format(PyExc_TypeError, "TritonContext::liftToSMT(): Invalid number of arguments");
}

Expand All @@ -2301,9 +2323,15 @@ namespace triton {
if (assert == nullptr)
assert = PyLong_FromUint32(false);

if (icomment != nullptr && !PyBool_Check(icomment))
return PyErr_Format(PyExc_TypeError, "TritonContext::liftToSMT(): Expects a boolean as icomment argument.");

if (icomment == nullptr)
icomment = PyLong_FromUint32(false);

try {
std::ostringstream stream;
PyTritonContext_AsTritonContext(self)->liftToSMT(stream, PySymbolicExpression_AsSymbolicExpression(expr), PyLong_AsBool(assert));
PyTritonContext_AsTritonContext(self)->liftToSMT(stream, PySymbolicExpression_AsSymbolicExpression(expr), PyLong_AsBool(assert), PyLong_AsBool(icomment));
return xPyString_FromString(stream.str().c_str());
}
catch (const triton::exceptions::PyCallbacks&) {
Expand Down Expand Up @@ -3457,7 +3485,7 @@ namespace triton {
{"isThumb", (PyCFunction)TritonContext_isThumb, METH_NOARGS, ""},
{"liftToDot", (PyCFunction)TritonContext_liftToDot, METH_O, ""},
{"liftToLLVM", (PyCFunction)(void*)(PyCFunctionWithKeywords)TritonContext_liftToLLVM, METH_VARARGS | METH_KEYWORDS, ""},
{"liftToPython", (PyCFunction)TritonContext_liftToPython, METH_O, ""},
{"liftToPython", (PyCFunction)(void*)(PyCFunctionWithKeywords)TritonContext_liftToPython, METH_VARARGS | METH_KEYWORDS, ""},
{"liftToSMT", (PyCFunction)(void*)(PyCFunctionWithKeywords)TritonContext_liftToSMT, METH_VARARGS | METH_KEYWORDS, ""},
{"newSymbolicExpression", (PyCFunction)TritonContext_newSymbolicExpression, METH_VARARGS, ""},
{"newSymbolicVariable", (PyCFunction)TritonContext_newSymbolicVariable, METH_VARARGS, ""},
Expand Down
15 changes: 13 additions & 2 deletions src/libtriton/engines/lifters/liftingToPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace triton {
}


std::ostream& LiftingToPython::liftToPython(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr) {
std::ostream& LiftingToPython::liftToPython(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool icomment) {
/* Save the AST representation mode */
triton::ast::representations::mode_e mode = this->astCtxt->getRepresentationMode();
this->astCtxt->setRepresentationMode(triton::ast::representations::PYTHON_REPRESENTATION);
Expand Down Expand Up @@ -101,7 +101,18 @@ namespace triton {

/* Print symbolic expressions */
for (const auto& id : symExprs) {
stream << ssa[id]->getFormattedExpression() << std::endl;
auto& e = ssa[id];
stream << e->getFormattedExpression();
if (icomment && !e->getDisassembly().empty()) {
if (e->getComment().empty()) {
stream << " # ";
}
else {
stream << " - ";
}
stream << e->getDisassembly();
}
stream << std::endl;
}

/* Restore the AST representation mode */
Expand Down
15 changes: 13 additions & 2 deletions src/libtriton/engines/lifters/liftingToSMT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace triton {
}


std::ostream& LiftingToSMT::liftToSMT(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool assert_) {
std::ostream& LiftingToSMT::liftToSMT(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool assert_, bool icomment) {
/* Save the AST representation mode */
triton::ast::representations::mode_e mode = this->astCtxt->getRepresentationMode();
this->astCtxt->setRepresentationMode(triton::ast::representations::SMT_REPRESENTATION);
Expand Down Expand Up @@ -157,7 +157,18 @@ namespace triton {

/* Print symbolic expressions */
for (const auto& id : symExprs) {
stream << ssa[id]->getFormattedExpression() << std::endl;
auto& e = ssa[id];
stream << e->getFormattedExpression();
if (icomment && !e->getDisassembly().empty()) {
if (e->getComment().empty()) {
stream << " ; ";
}
else {
stream << " - ";
}
stream << e->getDisassembly();
}
stream << std::endl;
}

if (assert_) {
Expand Down
8 changes: 4 additions & 4 deletions src/libtriton/includes/triton/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,11 +679,11 @@ namespace triton {
//! [**lifting api**] - Lifts a symbolic expression and all its references to LLVM format. `fname` represents the name of the LLVM function.
TRITON_EXPORT std::ostream& liftToLLVM(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, const char* fname="__triton", bool optimize=false);

//! [**lifting api**] - Lifts a symbolic expression and all its references to Python format.
TRITON_EXPORT std::ostream& liftToPython(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr);
//! [**lifting api**] - Lifts a symbolic expression and all its references to Python format. If `icomment` is true, then print instructions assembly in expression comments.
TRITON_EXPORT std::ostream& liftToPython(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool icomment=false);

//! [**lifting api**] - Lifts a symbolic expression and all its references to SMT format. If `assert_` is true, then (assert <expr>).
TRITON_EXPORT std::ostream& liftToSMT(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool assert_);
//! [**lifting api**] - Lifts a symbolic expression and all its references to SMT format. If `assert_` is true, then (assert <expr>). If `icomment` is true, then print instructions assembly in expression comments.
TRITON_EXPORT std::ostream& liftToSMT(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool assert_=false, bool icomment=false);

//! [**lifting api**] - Lifts an AST and all its references to Dot format.
TRITON_EXPORT std::ostream& liftToDot(std::ostream& stream, const triton::ast::SharedAbstractNode& node);
Expand Down
4 changes: 2 additions & 2 deletions src/libtriton/includes/triton/liftingToPython.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ namespace triton {
//! Constructor.
TRITON_EXPORT LiftingToPython(const triton::ast::SharedAstContext& astCtxt, triton::engines::symbolic::SymbolicEngine* symbolic);

//! Lifts a symbolic expression and all its references to Python format.
TRITON_EXPORT std::ostream& liftToPython(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr);
//! Lifts a symbolic expression and all its references to Python format. If `icomment` is true, then print instructions assembly in expression comments.
TRITON_EXPORT std::ostream& liftToPython(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool icomment=false);
};

/*! @} End of lifters namespace */
Expand Down
4 changes: 2 additions & 2 deletions src/libtriton/includes/triton/liftingToSMT.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ namespace triton {
//! Constructor.
TRITON_EXPORT LiftingToSMT(const triton::ast::SharedAstContext& astCtxt, triton::engines::symbolic::SymbolicEngine* symbolic);

//! Lifts a symbolic expression and all its references to SMT format. If `assert_` is true, then (assert <expr>).
TRITON_EXPORT std::ostream& liftToSMT(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool assert_);
//! Lifts a symbolic expression and all its references to SMT format. If `assert_` is true, then (assert <expr>). If `icomment` is true, then print instructions assembly in expression comments.
TRITON_EXPORT std::ostream& liftToSMT(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool assert_=false, bool icomment=false);
};

/*! @} End of lifters namespace */
Expand Down

0 comments on commit ec72d86

Please sign in to comment.