Skip to content

Commit

Permalink
#1121: Python binding for BasicBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanSalwan committed Apr 19, 2022
1 parent 3f206a9 commit 37e2756
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/examples/cpp/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int main(int ac, const char **av) {

std::cout << "First addr: 0x" << std::hex << block.getFirstAddress() << std::endl;
std::cout << "Last addr: " << std::hex << block.getLastAddress() << std::endl;
std::cout << "Number of instructions: " << std::hex << block.size() << std::endl;
std::cout << "Number of instructions: " << std::hex << block.getSize() << std::endl;

std::cout << "----------" << std::endl;

Expand Down
1 change: 1 addition & 0 deletions src/libtriton/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ if(PYTHON_BINDINGS)
bindings/python/objects/pyAstContext.cpp
bindings/python/objects/pyAstNode.cpp
bindings/python/objects/pyBitsVector.cpp
bindings/python/objects/pyBasicBlock.cpp
bindings/python/objects/pyImmediate.cpp
bindings/python/objects/pyInstruction.cpp
bindings/python/objects/pyMemoryAccess.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/libtriton/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ namespace triton {
}


std::vector<triton::arch::Instruction> API::disassembly(triton::uint64 addr) const {
triton::arch::BasicBlock API::disassembly(triton::uint64 addr) const {
this->checkArchitecture();
return this->arch.disassembly(addr);
}
Expand Down
4 changes: 2 additions & 2 deletions src/libtriton/arch/architecture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ namespace triton {
}


std::vector<triton::arch::Instruction> Architecture::disassembly(triton::uint64 addr) const {
triton::arch::BasicBlock Architecture::disassembly(triton::uint64 addr) const {
std::vector<triton::arch::Instruction> ret;

do {
Expand All @@ -292,7 +292,7 @@ namespace triton {
addr += inst.getSize();
} while (!ret.back().isControlFlow());

return ret;
return triton::arch::BasicBlock(ret);
}


Expand Down
4 changes: 2 additions & 2 deletions src/libtriton/arch/basicBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace triton {
}


triton::usize BasicBlock::size(void) const {
triton::usize BasicBlock::getSize(void) const {
return this->instructions.size();
}

Expand All @@ -79,7 +79,7 @@ namespace triton {


std::ostream& operator<<(std::ostream& stream, BasicBlock& block) {
triton::usize size = block.size();
triton::usize size = block.getSize();
for (const auto& inst : block.getInstructions()) {
stream << inst;
if (--size) {
Expand Down
2 changes: 1 addition & 1 deletion src/libtriton/arch/irBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace triton {


bool IrBuilder::buildSemantics(triton::arch::BasicBlock& block) {
triton::usize count = block.size();
triton::usize count = block.getSize();

for (auto& inst : block.getInstructions()) {
if (this->buildSemantics(inst) == false) {
Expand Down
44 changes: 43 additions & 1 deletion src/libtriton/bindings/python/modules/tritonCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#include <triton/pythonObjects.hpp>
#include <triton/pythonUtils.hpp>
#include <triton/pythonXFunctions.hpp>
#include <triton/exceptions.hpp>
#include <triton/basicBlock.hpp>
#include <triton/bitsVector.hpp>
#include <triton/exceptions.hpp>
#include <triton/immediate.hpp>
#include <triton/memoryAccess.hpp>
#include <triton/register.hpp>
Expand All @@ -32,6 +33,7 @@ to wrap more generic concepts.
- \ref py_AstContext_page
- \ref py_AstNode_page
- \ref py_BasicBlock_page
- \ref py_BitsVector_page
- \ref py_Immediate_page
- \ref py_Instruction_page
Expand Down Expand Up @@ -72,6 +74,45 @@ namespace triton {
namespace bindings {
namespace python {

static PyObject* triton_BasicBlock(PyObject* self, PyObject* args) {
PyObject* obj = nullptr;

/* Extract arguments */
if (PyArg_ParseTuple(args, "|O", &obj) == false) {
return PyErr_Format(PyExc_TypeError, "BasicBlock(): Invalid constructor.");
}

try {
/* Check if it's a default constructor */
if (obj == nullptr)
return PyBasicBlock();

/* Check if argument is a list of instruction */
else if (obj != nullptr && PyList_Check(obj)) {
triton::arch::BasicBlock block;

for (Py_ssize_t i = 0; i < PyList_Size(obj); i++) {
PyObject* item = PyList_GetItem(obj, i);

if (PyInstruction_Check(item) == false)
return PyErr_Format(PyExc_TypeError, "BasicBlock(): All items must be an Instruction objet.");

block.add(*PyInstruction_AsInstruction(item));
}

return PyBasicBlock(block);
}

/* Otherwise, invalid constructor */
else {
return PyErr_Format(PyExc_TypeError, "BasicBlock(): Invalid constructor.");
}
}
catch (const triton::exceptions::Exception& e) {
return PyErr_Format(PyExc_TypeError, "%s", e.what());
}
}

static PyObject* triton_Immediate(PyObject* self, PyObject* args) {
PyObject* value = nullptr;
PyObject* size = nullptr;
Expand Down Expand Up @@ -189,6 +230,7 @@ namespace triton {


PyMethodDef tritonCallbacks[] = {
{"BasicBlock", (PyCFunction)triton_BasicBlock, METH_VARARGS, ""},
{"Immediate", (PyCFunction)triton_Immediate, METH_VARARGS, ""},
{"Instruction", (PyCFunction)triton_Instruction, METH_VARARGS, ""},
{"MemoryAccess", (PyCFunction)triton_MemoryAccess, METH_VARARGS, ""},
Expand Down
Loading

0 comments on commit 37e2756

Please sign in to comment.