Skip to content

Commit

Permalink
#1121: Process a block from a given base address
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanSalwan committed May 26, 2022
1 parent e73520c commit e0ff06f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/libtriton/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,9 @@ namespace triton {
}


bool API::processing(triton::arch::BasicBlock& block) {
bool API::processing(triton::arch::BasicBlock& block, triton::uint64 addr) {
this->checkArchitecture();
this->arch.disassembly(block);
this->arch.disassembly(block, addr);
return this->irBuilder->buildSemantics(block);
}

Expand Down
26 changes: 19 additions & 7 deletions src/libtriton/bindings/python/objects/pyTritonContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ Returns the new symbolic volatile expression and links this expression to the in
- <b>void disassembly(\ref py_Instruction_page inst)</b><br>
Disassembles the instruction and sets up operands. You must define an architecture before.
- <b>void disassembly(\ref py_BasicBlock_page block)</b><br>
Disassembles a basic block. You must define an architecture before.
- <b>void disassembly(\ref py_BasicBlock_page block, integer addr=0)</b><br>
Disassembles a basic block with a potential given base address. You must define an architecture before.
- <b>[\ref py_Instruction_page inst, ...] disassembly(integer addr, integer count)</b><br>
Disassembles a concrete memory area from `addr` and returns a list of at most `count` disassembled instructions.
Expand Down Expand Up @@ -307,8 +307,8 @@ Pops the last constraints added to the path predicate.
- <b>bool processing(\ref py_Instruction_page inst)</b><br>
Processes an instruction and updates engines according to the instruction semantics. Returns true if the instruction is supported. You must define an architecture before.
- <b>bool processing(\ref py_BasicBlock_page block)</b><br>
Processes a basic block and updates engines according to the instructions semantics.
- <b>bool processing(\ref py_BasicBlock_page block, integer addr=0)</b><br>
Processes a basic block with a potential given base address and updates engines according to the instructions semantics.
- <b>void pushPathConstraint(\ref py_AstNode_page node, string comment="")</b><br>
Pushs constraints to the current path predicate.
Expand Down Expand Up @@ -2394,15 +2394,27 @@ namespace triton {
}


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

/* Extract arguments */
if (PyArg_ParseTuple(args, "|OO", &obj, &addr) == false) {
return PyErr_Format(PyExc_TypeError, "TritonContext::processing(): Invalid number of arguments");
}

try {
if (PyInstruction_Check(obj)) {
if (PyTritonContext_AsTritonContext(self)->processing(*PyInstruction_AsInstruction(obj)))
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
else if (PyBasicBlock_Check(obj)) {
if (PyTritonContext_AsTritonContext(self)->processing(*PyBasicBlock_AsBasicBlock(obj)))
triton::uint64 base = 0;
if (addr != nullptr && (PyLong_Check(addr) || PyInt_Check(addr))) {
base = PyLong_AsUint64(addr);
}
if (PyTritonContext_AsTritonContext(self)->processing(*PyBasicBlock_AsBasicBlock(obj), base))
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
Expand Down Expand Up @@ -3446,7 +3458,7 @@ namespace triton {
{"newSymbolicExpression", (PyCFunction)TritonContext_newSymbolicExpression, METH_VARARGS, ""},
{"newSymbolicVariable", (PyCFunction)TritonContext_newSymbolicVariable, METH_VARARGS, ""},
{"popPathConstraint", (PyCFunction)TritonContext_popPathConstraint, METH_NOARGS, ""},
{"processing", (PyCFunction)TritonContext_processing, METH_O, ""},
{"processing", (PyCFunction)TritonContext_processing, METH_VARARGS, ""},
{"pushPathConstraint", (PyCFunction)(void*)(PyCFunctionWithKeywords)TritonContext_pushPathConstraint, METH_VARARGS | METH_KEYWORDS, ""},
{"removeCallback", (PyCFunction)TritonContext_removeCallback, METH_VARARGS, ""},
{"reset", (PyCFunction)TritonContext_reset, METH_NOARGS, ""},
Expand Down
2 changes: 1 addition & 1 deletion src/libtriton/includes/triton/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ namespace triton {
TRITON_EXPORT bool processing(triton::arch::Instruction& inst);

//! [**proccesing api**] - Processes a block of instructions and updates engines according to instructions semantics. Returns false if an instruction is not supported.
TRITON_EXPORT bool processing(triton::arch::BasicBlock& block);
TRITON_EXPORT bool processing(triton::arch::BasicBlock& block, triton::uint64 addr=0);

//! [**proccesing api**] - Initializes everything.
TRITON_EXPORT void initEngines(void);
Expand Down
23 changes: 23 additions & 0 deletions src/testers/unittests/test_disass.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,26 @@ def test_inst6(self):
raw = b"".join(code)
self.ctx.setConcreteMemoryAreaValue(0x1000, raw)
self.assertRaises(Exception, self.ctx.disassembly, 0x1000)

def test_inst7(self):
block = BasicBlock([
Instruction(b"\x48\xb9\x88\x77\x66\x55\x44\x33\x22\x11"), # mov rcx, 0x1122334455667788
Instruction(b"\x48\xff\xc1"), # inc rcx
Instruction(b"\x48\x89\xc8"), # mov rax, rcx
Instruction(b"\xc9"), # leave
Instruction(b"\xc3"), # ret
])
self.ctx.disassembly(block)
self.assertEqual(block.getInstructions()[0].getAddress(), 0x0)

self.ctx.disassembly(block, 0x1000)
self.assertEqual(block.getInstructions()[0].getAddress(), 0x1000)

self.ctx.disassembly(block)
self.assertEqual(block.getInstructions()[0].getAddress(), 0x0)

self.ctx.processing(block)
self.assertEqual(block.getInstructions()[0].getAddress(), 0x0)

self.ctx.processing(block, 0x112233)
self.assertEqual(block.getInstructions()[0].getAddress(), 0x112233)

0 comments on commit e0ff06f

Please sign in to comment.