Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for more RAnalOp fields to Python arch plugin #41

Merged
merged 3 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions python/examples/test-py-arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,31 @@ def decode(buf, pc):
},
1: {
"op": {
"mnemonic" : "mov",
"type" : R.R_ANAL_OP_TYPE_MOV,
"mnemonic" : "mov r{}, #0x{:02x}".format(buf[1], buf[2]),
"type" : R.R_ANAL_OP_TYPE_MOV,
"cycles" : 2,
"val" : buf[2],
},
"size": 3
},
2: {
"op": {
"mnemonic" : "fadd",
"mnemonic" : "fadd r{}, ${}".format(buf[1], buf[2]),
"type" : R.R_ANAL_OP_TYPE_ADD,
"family" : R.R_ANAL_OP_FAMILY_FPU,
"cycles" : 2,
"ptr" : buf[2],
},
"size": 3
},
3: {
"op": {
"mnemonic" : "jne",
"mnemonic" : "jne 0x{:04x}".format((buf[1] << 8) | buf[2]),
"type" : R.R_ANAL_OP_TYPE_CJMP,
"cycles" : 2,
"jump" : (buf[1] << 8) | buf[2],
"fail" : pc+3,
"cond" : R.R_ANAL_COND_NE,
"eob" : True
},
"size": 3
Expand Down
29 changes: 22 additions & 7 deletions python/python/arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,28 @@ static bool py_arch_decode(RArchSession *as, RAnalOp *op, RAnalOpMask mask) {
if (len && dict) {
if (PyDict_Check (dict)) {
op->size = PyNumber_AsSsize_t (len, NULL);
op->mnemonic = strdup (getS (dict, "mnemonic"));
op->family = (getI (dict, "family"));
op->cycles = (getI (dict, "cycles"));
op->type = (getI (dict, "type"));
op->jump = (getI (dict, "jump"));
op->fail = (getI (dict, "fail"));
op->eob = (getB (dict, "eob"));
if (contains (dict, "mnemonic"))
op->mnemonic = strdup (getS (dict, "mnemonic"));
if (contains (dict, "familiy"))
op->family = (getI (dict, "family"));
if (contains (dict, "cycles"))
op->cycles = (getI (dict, "cycles"));
if (contains (dict, "type"))
op->type = (getI (dict, "type"));
if (contains (dict, "cond"))
op->cond = (getI (dict, "cond"));
if (contains (dict, "jump"))
op->jump = (getI (dict, "jump"));
if (contains (dict, "fail"))
op->fail = (getI (dict, "fail"));
if (contains (dict, "eob"))
op->eob = (getB (dict, "eob"));
if (contains (dict, "ptr"))
op->ptr = (getI (dict, "ptr"));
if (contains (dict, "ptrsize"))
op->ptrsize = (getI (dict, "ptrsize"));
if (contains (dict, "val"))
op->val = (getI (dict, "val"));
res = true;
}
}
Expand Down
8 changes: 7 additions & 1 deletion python/python/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#include "common.h"

bool contains(PyObject *o, const char *name) {
if (!o || !name) return false;
PyObject *str = PyUnicode_FromString (name);
return (PyDict_Contains (o, str) == 1);
}

PyObject *getO(PyObject *o, const char *name) {
if (!o) return NULL;
PyObject *res = PyDict_GetItemString (o, name);
Expand Down Expand Up @@ -30,6 +36,6 @@ void *getF(PyObject *o, const char *name) {

bool getB(PyObject *o, const char *name) {
if (!o || o == Py_None) return NULL;
if (PyObject_IsTrue(o)) return true;
if (PyObject_IsTrue (o)) return true;
return false;
}
2 changes: 2 additions & 0 deletions python/python/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ typedef struct {
int number;
} Radare;

bool contains(PyObject *o, const char *name);

PyObject *getO(PyObject *o, const char *name);

char *getS(PyObject *o, const char *name);
Expand Down
Loading