Improved pdc, added pdco and pdcj, print orphan nodes ##print (#18518)

This commit is contained in:
pancake 2021-03-31 23:46:59 +02:00 committed by GitHub
parent fd1484525a
commit 25597533bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 242 additions and 109 deletions

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2015-2017 - pancake */
/* radare - LGPL - Copyright 2015-2021 - pancake */
#include <r_core.h>
#define TYPE_NONE 0
@ -23,7 +23,7 @@ typedef struct _find_ctx {
int type;
} RFindCTX;
static void find_and_change (char* in, int len) {
static void find_and_change(char* in, int len) {
// just to avoid underflows.. len can't be < then len(padding).
if (!in || len < 1) {
return;
@ -90,10 +90,6 @@ static void find_and_change (char* in, int len) {
ctx.comment = in - 1;
ctx.comment[1] = '/';
ctx.comment[2] = '/';
while (!IS_WHITESPACE (*(ctx.comment - ctx.commentcolor))) {
ctx.commentcolor++;
}
ctx.commentcolor--;
} else if (!ctx.comment && ctx.type == TYPE_NONE) {
if (IS_STRING (in, ctx)) {
ctx.type = TYPE_STR;
@ -166,7 +162,36 @@ static void find_and_change (char* in, int len) {
}
}
static const char *help_msg_pdc[] = {
"Usage: pdc[oj]", "", "experimental, unreliable and hacky pseudo-decompiler",
"pdc", "", "pseudeo decompile function in current offset",
"pdco", "", "show associated offset next to pseudecompiled output",
"pdcj", "", "in json format for r2cutter codemeta annotations",
NULL
};
#define I_TAB 2
#define K_MARK(x) sdb_fmt("mark.%"PFMT64x,x)
#define K_ELSE(x) sdb_fmt("else.%"PFMT64x,x)
#define K_INDENT(x) sdb_fmt("loc.%"PFMT64x,x)
#define SET_INDENT(x) { (x) = (x)>0?(x):0; memset (indentstr, ' ', sizeof(indentstr)); indentstr [((x) * I_TAB)] = 0; }
R_API int r_core_pseudo_code(RCore *core, const char *input) {
if (*input == '?') {
r_core_cmd_help (core, help_msg_pdc);
return false;
}
#define NEWLINE(a,i) {\
size_t eos = R_MIN ((i)*2, sizeof (indentstr)-2);\
if (eos < 1) eos = 0;\
memset (indentstr, ' ', sizeof(indentstr)); indentstr [(eos * 2)] = 0;\
if (pj) {\
if (show_addr) r_strbuf_appendf (codestr, "\n0x%08"PFMT64x" | %s", a, indentstr);\
else r_strbuf_appendf (codestr, "\n%s", indentstr);\
} else {\
r_cons_printf ("\n");\
if (show_addr) r_cons_printf (" 0x%08"PFMT64x" | %s", a, indentstr);\
else r_cons_printf ("%s", indentstr); }\
}
const char *cmdPdc = r_config_get (core->config, "cmd.pdc");
if (cmdPdc && *cmdPdc && !strstr (cmdPdc, "pdc")) {
if (strstr (cmdPdc, "!*") || strstr (cmdPdc, "#!")) {
@ -180,6 +205,8 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
}
return r_core_cmdf (core, "%s%s", cmdPdc, input);
}
const bool show_json = (*input == 'j');
const bool show_addr = (*input == 'o');
Sdb *db;
ut64 queuegoto = 0LL;
@ -201,12 +228,12 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
}
r_config_set_i (core->config, "scr.color", 0);
r_config_set_i (core->config, "asm.stackptr", 0);
r_config_set_i (core->config, "asm.pseudo", 1);
r_config_set_i (core->config, "asm.decode", 0);
r_config_set_b (core->config, "asm.pseudo", true);
r_config_set_b (core->config, "asm.decode", false);
r_config_set_i (core->config, "asm.sub.names", 1);
r_config_set_i (core->config, "asm.lines", 0);
r_config_set_i (core->config, "asm.bytes", 0);
r_config_set_i (core->config, "asm.offset", 0);
r_config_set_i (core->config, "asm.offset", 1);
r_config_set_i (core->config, "asm.flags", 0);
r_config_set_i (core->config, "asm.emu", 1);
r_config_set_i (core->config, "emu.str", 1);
@ -219,7 +246,12 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
r_config_set_i (core->config, "asm.cmt.col", 30);
r_config_set_i (core->config, "io.cache", 1);
r_core_cmd0 (core, "aeim");
PJ *pj = NULL;
if (show_json) {
pj = r_core_pj_new (core);
}
RStrBuf *codestr = r_strbuf_new ("");
db = sdb_new0 ();
// walk all basic blocks
// define depth level for each block
@ -228,51 +260,88 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
// asm.decode=true
RAnalBlock *bb = r_list_first (fcn->bbs);
char indentstr[1024] = {0};
int n_bb = r_list_length (fcn->bbs);
r_cons_printf ("function %s () {", fcn->name);
int indent = 1;
int indent = 0;
int nindent = 1;
int n_bb = r_list_length (fcn->bbs);
if (show_json) {
pj = r_core_pj_new (core);
pj_o (pj);
pj_ka (pj, "annotations");
} else {
NEWLINE (fcn->addr, indent);
r_cons_printf ("// This function contains %d basic blocks and its %d long.",
n_bb, (int)r_anal_function_realsize (fcn));
NEWLINE (fcn->addr, indent);
r_cons_printf ("function %s () {", fcn->name);
}
indent++;
RList *visited = r_list_newf (NULL);
r_cons_printf ("\n // %d basic blocks\n", n_bb);
ut64 addr = fcn->addr;
do {
#define I_TAB 4
#define K_MARK(x) sdb_fmt("mark.%"PFMT64x,x)
#define K_ELSE(x) sdb_fmt("else.%"PFMT64x,x)
#define K_INDENT(x) sdb_fmt("loc.%"PFMT64x,x)
#define SET_INDENT(x) { (x) = (x)>0?(x):1; memset (indentstr, ' ', (x)*I_TAB); indentstr [((x)*I_TAB)-2] = 0; }
if (!bb) {
break;
}
r_list_append (visited, bb);
r_cons_push ();
bool html = r_config_get_i (core->config, "scr.html");
r_config_set_i (core->config, "scr.html", 0);
bool html = r_config_get_b (core->config, "scr.html");
r_config_set_b (core->config, "scr.html", false);
char *code = r_core_cmd_str (core, sdb_fmt ("pD %"PFMT64d" @ 0x%08"PFMT64x"\n", bb->size, bb->addr));
r_cons_pop ();
r_config_set_i (core->config, "scr.html", html);
r_config_set_b (core->config, "scr.html", html);
if (indent * I_TAB + 2 >= sizeof (indentstr)) {
indent = (sizeof (indentstr) / I_TAB) - 4;
indent = (sizeof (indentstr) / I_TAB) - 2;
}
SET_INDENT (indent);
code = r_str_prefix_all (code, indentstr);
if (!code) {
eprintf ("No code here\n");
break;
}
int len = strlen (code);
size_t len = strlen (code);
code[len - 1] = 0; // chop last newline
find_and_change (code, len);
if (!sdb_const_get (db, K_MARK (bb->addr), 0)) {
bool mustprint = !queuegoto || queuegoto != bb->addr;
if (mustprint) {
if (queuegoto) {
r_cons_printf ("\n%s goto loc_0x%llx",
indentstr, queuegoto);
if (!pj) {
NEWLINE (bb->addr, indent);
r_cons_printf ("goto loc_0x%"PFMT64x, queuegoto);
}
queuegoto = 0LL;
}
r_cons_printf ("\n%s loc_0x%llx:\n", indentstr, bb->addr);
indentstr[(indent * I_TAB) - 2] = 0;
r_cons_printf ("\n%s", code);
if (!pj) {
NEWLINE (bb->addr, indent);
r_cons_printf ("loc_0x%"PFMT64x":", bb->addr);
}
// foreach lines
RList *lines = r_str_split_list (code, "\n", 0);
RListIter *iter;
const char *line;
addr = bb->addr;
r_list_foreach (lines, iter, line) {
if (*line == '0') {
ut64 at = r_num_get (NULL, line);
if (at && at != UT64_MAX) {
addr = at;
}
char *s = strchr (line, ' ');
if (s) {
line = r_str_trim_head_ro (s + 1);
}
}
if (pj) {
pj_o (pj);
pj_kn (pj, "start", r_strbuf_length (codestr));
r_strbuf_append (codestr, line);
pj_kn (pj, "end", r_strbuf_length (codestr));
pj_kn (pj, "offset", addr);
pj_ks (pj, "type", "offset");
pj_end (pj);
} else {
NEWLINE (addr, indent);
r_cons_printf ("%s", line);
}
}
free (code);
sdb_num_set (db, K_MARK (bb->addr), 1, 0);
}
@ -285,11 +354,14 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
if (addr == UT64_MAX) {
int i;
nindent = 1;
for (i = indent; i != nindent; i--) {
SET_INDENT (i);
r_cons_printf ("\n%s}", indentstr);
if (!pj) {
for (i = indent; i != nindent && i > 0; i--) {
NEWLINE (bb->addr, i);
r_cons_printf ("}");
}
NEWLINE (bb->addr, indent);
r_cons_printf ("return;");
}
r_cons_printf ("\n%sreturn;\n", indentstr);
RAnalBlock *nbb = r_anal_bb_from_offset (core->anal, bb->fail);
if (r_list_contains (visited, nbb)) {
nbb = r_anal_bb_from_offset (core->anal, bb->jump);
@ -304,14 +376,18 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
indent--;
continue;
}
if (sdb_num_get (db, K_ELSE (bb->addr), 0)) {
if (!strcmp (blocktype, "else")) {
r_cons_printf ("\n%s } %s {", indentstr, blocktype);
if (!pj) {
if (sdb_num_get (db, K_ELSE (bb->addr), 0)) {
NEWLINE (addr, indent);
if (!strcmp (blocktype, "else")) {
r_cons_printf ("} %s {", blocktype);
} else {
r_cons_printf ("} %s (?);", blocktype);
}
} else {
r_cons_printf ("\n%s } %s (?);", indentstr, blocktype);
NEWLINE (addr, indent);
r_cons_printf ("}");
}
} else {
r_cons_printf ("\n%s}", indentstr);
}
if (addr != bb->addr) {
queuegoto = addr;
@ -327,11 +403,14 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
if (indent > nindent && !strcmp (blocktype, "else")) {
int i;
for (i = indent; i != nindent; i--) {
SET_INDENT (i);
r_cons_printf ("\n%s }", indentstr);
if (pj) {
} else {
NEWLINE (addr, i);
r_cons_printf ("}");
}
}
}
indent = nindent;
indent = nindent - 1;
} else {
sdb_set (db, K_INDENT (bb->addr), "passed", 0);
if (bb->jump != UT64_MAX) {
@ -343,13 +422,18 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
RAnalFunction *curfcn = r_anal_get_fcn_in (core->anal, jump, R_ANAL_FCN_TYPE_NULL);
if (curfcn != fcn) {
// chop that branch
r_cons_printf ("\n // chop\n");
if (!pj) {
NEWLINE (jump, indent);
r_cons_printf ("// chop");
}
// break;
}
if (sdb_get (db, K_INDENT (jump), 0)) {
// already tracekd
if (!sdb_get (db, K_INDENT (fail), 0)) {
bb = r_anal_bb_from_offset (core->anal, fail);
} else {
eprintf ("FAIL%c", 10);
}
} else {
bb = r_anal_bb_from_offset (core->anal, jump);
@ -368,11 +452,15 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
sdb_array_push_num (db, "indent", fail, 0);
sdb_num_set (db, K_INDENT (fail), indent, 0);
sdb_num_set (db, K_ELSE (fail), 1, 0);
SET_INDENT (indent);
r_cons_printf ("\n%s {", indentstr);
NEWLINE (bb->addr, indent);
if (pj) {
r_strbuf_appendf (codestr, "{");
} else {
r_cons_printf ("{");
}
}
} else {
r_cons_printf ("\n%s do", indentstr);
NEWLINE (bb->addr, indent);
sdb_array_push_num (db, "indent", jump, 0);
sdb_num_set (db, K_INDENT (jump), indent, 0);
sdb_num_set (db, K_ELSE (jump), 1, 0);
@ -381,35 +469,86 @@ R_API int r_core_pseudo_code(RCore *core, const char *input) {
} else {
blocktype = "else";
}
r_cons_printf ("\n%s {", indentstr);
if (pj) {
r_strbuf_appendf (codestr, "do {");
} else {
r_cons_printf ("do {");
}
indent++;
}
}
} else {
ut64 addr = sdb_array_pop_num (db, "indent", NULL);
if (addr == UT64_MAX) {
r_cons_printf ("\n(break)\n");
NEWLINE (bb->addr, indent);
r_cons_printf ("(break)");
break;
}
bb = r_anal_bb_from_offset (core->anal, addr);
nindent = sdb_num_get (db, K_INDENT (addr), NULL);
if (indent > nindent) {
int i;
for (i = indent; i != nindent; i--) {
SET_INDENT (i);
r_cons_printf ("\n%s}", indentstr);
if (!pj) {
if (indent > nindent) {
int i;
for (i = indent; i != nindent; i--) {
NEWLINE (bb->addr, i);
if (pj) {
r_strbuf_appendf (codestr, "}");
} else {
r_cons_printf ("}");
}
}
}
if (nindent != indent) {
NEWLINE (bb->addr, indent);
if (pj) {
r_strbuf_appendf (codestr, "} else {");
} else {
r_cons_printf ("} else {");
}
}
}
if (nindent != indent) {
r_cons_printf ("\n%s} else {\n", indentstr);
}
indent = nindent;
}
}
//n_bb --;
} while (n_bb > 0);
RListIter *iter;
size_t orphan = 0;
r_list_foreach (fcn->bbs, iter, bb) {
if (!r_list_contains (visited, bb)) {
orphan ++;
char *s = r_core_cmd_strf (core, "pdb@0x%08"PFMT64x"@e:asm.offset=0", bb->addr);
s = r_str_prefix_all (s, indentstr);
if (pj) {
pj_o (pj);
pj_kn (pj, "start", r_strbuf_length (codestr));
r_strbuf_append (codestr, s);
pj_kn (pj, "end", r_strbuf_length (codestr));
pj_kn (pj, "offset", addr);
pj_ks (pj, "type", "offset");
pj_end (pj);
} else {
NEWLINE (bb->addr, indent);
r_cons_printf ("orphan_0x%08"PFMT64x":\n%s", bb->addr, s);
}
free (s);
}
}
r_list_free (visited);
r_cons_printf ("\n}\n");
indent = 0;
if (pj) {
pj_end (pj);
char *kode = r_strbuf_drain (codestr);
pj_ks (pj, "code", kode);
pj_end (pj);
char *j = pj_drain (pj);
r_cons_printf ("%s\n", j);
free (kode);
free (j);
} else {
NEWLINE (addr, indent);
r_cons_printf ("}\n");
}
r_config_hold_restore (hc);
r_config_hold_free (hc);
sdb_free (db);

View File

@ -6,8 +6,8 @@ pdc @ main~str.250382
pdc @ main~strcmp
EOF
EXPECT=<<EOF
dword [s2] = "250382" //[0x804858f:4]=0x33303532 ; str.250382 ; const char *s2
int strcmp("", "250382")
dword [s2] = "250382" //[0x804858f:4]=0x33303532 ; str.250382 ; const char *s2
int strcmp("", "250382")
EOF
RUN
@ -53,60 +53,54 @@ EXPECT=<<EOF
| 0x0040206e ff15b8304000 call dword [MessageBoxA] ; 0x4030b8 ; int MessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
| 0x00402074 6a00 push 0
\ 0x00402076 ff1584304000 call dword [ExitProcess] ; 0x403084 ; VOID ExitProcess(UINT uExitCode)
// This function contains 3 basic blocks and its 109 long.
function entry0 () {
// 3 basic blocks
loc_0x40200f:
si = 3
fcn.00402000 ()
do
{
si = 3
fcn.00402000 ()
do {
loc_0x402018:
push 0
push "LABEL" //0x40100e ; (pstr 0x0040100e) "LABEL"
push "Hello, World!" //section..data
//0x401000 ; (pstr 0x00401000) "Hello, World!"
push 0
dword [MessageBoxA] () //0x4030b8 ; reloc.user32.dll_MessageBoxA
//int MessageBoxA(NULL, 0x6c6c6548, 0x4542414c, NULL)
push "LABEL" //0x40100e ; (pstr 0x0040100e) "LABEL"
dword [puts] () //0x4030dc ; reloc.crtdll.dll_puts
//int puts("LABEL")
esp += 4
si--
var = si - 0
if (var > 0) goto 0x402018 //unlikely
} while (?);
return;
loc_0x402042:
eax = dword [puts] //[0x4030dc:4]=0x30e8 reloc.crtdll.dll_puts ; reloc.crtdll.dll_puts
push "Hello, World!" //section..data
//0x401000 ; (pstr 0x00401000) "Hello, World!"
eax () //(pstr 0x0040100e) "LABEL" ; reloc.crtdll.dll_puts
//int puts("Hello, World!")
dword [GetProcessHeap] () //0x403088 ; reloc.kernel32.dll_GetProcessHeap
//HANDLE GetProcessHeap(void)
push eax
push ">%p\\n" //0x401014 ; (pstr 0x00401014) ">%p\n"
dword [printf] () //0x4030e0 ; (pstr 0x0040100e) "LABEL" ; reloc.crtdll.dll_printf
//int printf(">%p\n")
push 0
push "Hello, World!" //section..data
//0x401000 ; (pstr 0x00401000) "Hello, World!"
push "Hello, World!" //section..data
//0x401000 ; (pstr 0x00401000) "Hello, World!"
push 0
dword [MessageBoxA] () //0x4030b8 ; reloc.user32.dll_MessageBoxA
//int MessageBoxA(NULL, 0x6c6c6548, 0x6c6c6548, NULL)
push 0
dword [ExitProcess] () //0x403084 ; reloc.kernel32.dll_ExitProcess
//VOID ExitProcess(NULL)
push 0
push "LABEL" //0x40100e ; (pstr 0x0040100e) "LABEL"
push "Hello, World!" //section..data
//0x401000 ; (pstr 0x00401000) "Hello, World!"
push 0
dword [MessageBoxA] () //0x4030b8 ; reloc.user32.dll_MessageBoxA
//int MessageBoxA(NULL, 0x6c6c6548, 0x4542414c, NULL)
push "LABEL" //0x40100e ; (pstr 0x0040100e) "LABEL"
dword [puts] () //0x4030dc ; reloc.crtdll.dll_puts
//int puts("LABEL")
esp += 4
si--
var = si - 0
if (var > 0) goto 0x402018 //unlikely
} while (?);
return;
loc_0x402042:
eax = dword [puts] //[0x4030dc:4]=0x30e8 reloc.crtdll.dll_puts ; reloc.crtdll.dll_puts
push "Hello, World!" //section..data
//0x401000 ; (pstr 0x00401000) "Hello, World!"
eax () //(pstr 0x0040100e) "LABEL" ; reloc.crtdll.dll_puts
//int puts("Hello, World!")
dword [GetProcessHeap] () //0x403088 ; reloc.kernel32.dll_GetProcessHeap
//HANDLE GetProcessHeap(void)
push eax
push ">%p\\n" //0x401014 ; (pstr 0x00401014) ">%p\n"
dword [printf] () //0x4030e0 ; (pstr 0x0040100e) "LABEL" ; reloc.crtdll.dll_printf
//int printf(">%p\n")
push 0
push "Hello, World!" //section..data
//0x401000 ; (pstr 0x00401000) "Hello, World!"
push "Hello, World!" //section..data
//0x401000 ; (pstr 0x00401000) "Hello, World!"
push 0
dword [MessageBoxA] () //0x4030b8 ; reloc.user32.dll_MessageBoxA
//int MessageBoxA(NULL, 0x6c6c6548, 0x6c6c6548, NULL)
push 0
dword [ExitProcess] () //0x403084 ; reloc.kernel32.dll_ExitProcess
//VOID ExitProcess(NULL)
(break)
}
EOF
RUN