Merge asm.java into anal.java ##refactor

* Exposes an asm.assembler bug so we disable that for now

Co-authored-by: Richard Patel <me@terorie.dev>
Co-authored-by: pancake <pancake@nopcode.org>
This commit is contained in:
pancake 2022-07-03 11:15:20 +02:00 committed by GitHub
parent 9f12bfd0a8
commit d93c452768
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 87 additions and 159 deletions

View File

@ -1,5 +1,4 @@
STATIC="
asm.java
asm.arm_cs
asm.dalvik
asm.mips_cs

View File

@ -78,7 +78,6 @@ io.zip
asm.tms320
asm.arm_winedbg
asm.sparc
asm.java
asm.arm_cs
asm.dalvik
asm.x86_cs

View File

@ -74,7 +74,6 @@ asm.arm_winedbg
asm.dalvik
asm.null
asm.hppa_gnu
asm.java
asm.mips_cs
asm.mips_gnu
asm.or1k

View File

@ -1,5 +1,4 @@
STATIC="
asm.java
asm.arm_cs
asm.dalvik
asm.mips_cs

View File

@ -9,7 +9,6 @@
# MIPS_CS="asm.mips_cs anal.mips_cs"
STATIC="
asm.java
asm.arm_cs
asm.dalvik
asm.mips_cs

View File

@ -66,7 +66,6 @@ asm.arm_winedbg
asm.dalvik
asm.null
asm.hppa_gnu
asm.java
asm.mips_cs
asm.mips_gnu
asm.or1k

View File

@ -54,7 +54,6 @@ asm.arm_winedbg
asm.dalvik
asm.null
asm.hppa_gnu
asm.java
asm.mips_gnu
asm.or1k
asm.ppc_as

View File

@ -29,7 +29,6 @@ esil.dummy
asm.arm_cs
asm.arm_winedbg
asm.dalvik
asm.java
asm.mips_cs
asm.ppc_cs
asm.tms320

View File

@ -46,7 +46,6 @@ asm.arm_cs
asm.arm_gnu
asm.dalvik
asm.hppa_gnu
asm.java
asm.mips_cs
asm.mips_gnu
asm.ppc_cs

View File

@ -32,7 +32,6 @@ esil.dummy
asm.arc
asm.arm_cs
asm.dalvik
asm.java
asm.mips_cs
asm.riscv
asm.arm_as

View File

@ -42,7 +42,6 @@ asm.arm_gnu
asm.arm_winedbg
asm.dalvik
asm.null
asm.java
asm.mips_cs
asm.mips_gnu
asm.ppc_as

View File

@ -9,7 +9,6 @@
# MIPS_CS="asm.mips_cs anal.mips_cs"
STATIC="
asm.java
asm.arm_cs
asm.dalvik
asm.mips_cs

View File

@ -209,6 +209,28 @@ static int java_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len
java_switch_op (anal, op, addr, data, len);
// IN_SWITCH_OP = 1;
}
if (mask & R_ANAL_OP_MASK_DISASM) {
RBinJavaObj *obj = NULL;
RBin *bin = anal->binb.bin;
RBinPlugin *plugin = bin && bin->cur && bin->cur->o ?
bin->cur->o->plugin : NULL;
if (plugin && plugin->name) {
if (!strcmp (plugin->name, "java")) { // XXX slow
obj = bin->cur->o->bin_obj; //o;
//eprintf("Handling: %s disasm.\n", b->cur.file);
}
}
const int buf_asm_len = 256;
op->mnemonic = calloc (buf_asm_len, 1);
if (op->mnemonic) {
op->size = r_java_disasm (obj, addr, data, len, op->mnemonic, buf_asm_len);
if (op->mnemonic[0] == 0) {
R_FREE (op->mnemonic);
}
}
}
/* TODO:
// not sure how to handle the states for IN_SWITCH_OP, SWITCH_OP_CASES,
// and NUM_CASES_SEEN, because these are dependent on whether or not we
@ -238,7 +260,7 @@ static int java_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len
if (len < 4) {
// incomplete analysis here
return 0;
return op->size; // 0
}
if (op->type == R_ANAL_OP_TYPE_POP) {
op->stackop = R_ANAL_STACK_INC;
@ -267,6 +289,10 @@ static int java_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len
return op->size;
}
static int java_opasm(RAnal *a, ut64 addr, const char *str, ut8 *outbuf, int outsize) {
return r_java_assemble (addr, outbuf, str);
}
static void java_update_anal_types(RAnal *anal, RBinJavaObj *bin_obj) {
Sdb *D = anal->sdb_types;
if (D && bin_obj) {
@ -324,6 +350,7 @@ RAnalPlugin r_anal_plugin_java = {
.arch = "java",
.bits = 32,
.op = &java_op,
.opasm = &java_opasm,
.cmd_ext = java_cmd_ext,
0
};

View File

@ -298,14 +298,14 @@ R_API int r_asm_del(RAsm *a, const char *name) {
}
R_API bool r_asm_is_valid(RAsm *a, const char *name) {
RAsmPlugin *h;
RListIter *iter;
if (!name || !*name) {
return false;
}
r_list_foreach (a->plugins, iter, h) {
if (!strcmp (h->name, name)) {
return true;
// r_return_val_if_fail (a && name, false);
if (a && R_STR_ISNOTEMPTY (name)) {
RAsmPlugin *h;
RListIter *iter;
r_list_foreach (a->plugins, iter, h) {
if (!strcmp (h->name, name)) {
return true;
}
}
}
return false;

View File

@ -15,7 +15,6 @@ r_asm_sources = [
join_paths('p','asm_dalvik.c'),
#join_paths('p','asm_gas.c'),
#join_paths('p','asm_hppa_gnu.c'),
join_paths('p','asm_java.c'),
join_paths('p','asm_lanai_gnu.c'),
join_paths('p','asm_m68k_gnu.c'),
join_paths('p','asm_mips_cs.c'),

View File

@ -1,52 +0,0 @@
/* radare - LGPL - Copyright 2009-2019 - nibble, pancake */
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
#include <r_asm.h>
#include <r_core.h>
#include "../../shlr/java/code.h"
#include "../../shlr/java/class.h"
static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
RBinJavaObj *obj = NULL;
RBin *bin = a->binb.bin;
RBinPlugin *plugin = bin && bin->cur && bin->cur->o ?
bin->cur->o->plugin : NULL;
if (plugin && plugin->name) {
if (!strcmp (plugin->name, "java")) { // XXX slow
obj = bin->cur->o->bin_obj; //o;
//eprintf("Handling: %s disasm.\n", b->cur.file);
}
}
char buf_asm[256];
op->size = r_java_disasm (obj, a->pc, buf, len, buf_asm, sizeof (buf_asm));
r_strbuf_set (&op->buf_asm, buf_asm);
return op->size;
}
static int assemble(RAsm *a, RAsmOp *op, const char *input) {
// TODO: get class info from bin if possible
// XXX wrong usage of strbuf_get here
return op->size = r_java_assemble (a->pc, (ut8*)r_strbuf_get (&op->buf), input);
}
RAsmPlugin r_asm_plugin_java = {
.name = "java",
.desc = "Java bytecode",
.arch = "java",
.license = "Apache",
.bits = 32,
.endian = R_SYS_ENDIAN_BIG,
.disassemble = &disassemble,
.assemble = &assemble
};
#ifndef R2_PLUGIN_INCORE
R_API RLibStruct radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_java,
.version = R2_VERSION
};
#endif

View File

@ -1,17 +0,0 @@
OBJ_JAVA=asm_java.o
SHARED2_JAVA=$(addprefix ../,${SHARED_JAVA})
STATIC_OBJ+=${OBJ_JAVA}
SHARED_OBJ+=${SHARED_JAVA}
TARGET_JAVA=asm_java.${EXT_SO}
ifeq ($(WITHPIC),1)
ALL_TARGETS+=${TARGET_JAVA}
${TARGET_JAVA}: ${OBJ_JAVA} ${SHARED2_JAVA}
${CC} $(call libname,asm_java) ${LDFLAGS} ${CFLAGS} \
-o asm_java.${EXT_SO} ${OBJ_JAVA} ${SHARED2_JAVA} \
$(SHLR)/java/libr_java.$(EXT_AR) \
$(SHLR)/sdb/src/libsdb.$(EXT_AR)
endif

View File

@ -195,7 +195,6 @@ extern RAsmPlugin r_asm_plugin_null;
extern RAsmPlugin r_asm_plugin_dalvik;
extern RAsmPlugin r_asm_plugin_h8300;
extern RAsmPlugin r_asm_plugin_hppa_gnu;
extern RAsmPlugin r_asm_plugin_java;
extern RAsmPlugin r_asm_plugin_lanai_gnu;
extern RAsmPlugin r_asm_plugin_mips_cs;
extern RAsmPlugin r_asm_plugin_mips_gnu;

View File

@ -130,7 +130,6 @@ asm_plugins += [
'arm_cs',
'arm_winedbg',
'dalvik',
'java',
'mips_cs',
'or1k',
'ppc_as',

View File

@ -1,15 +1,8 @@
/* Apache 2.0 - Copyright 2007-2022 - pancake and dso
class.c rewrite: Adam Pridgen <dso@rice.edu || adam.pridgen@thecoverofnight.com>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <r_types.h>
#include <r_util.h>
#include <r_bin.h>
#include <math.h>
#include <sdb.h>
#include "class.h"
#ifdef IFDBG

View File

@ -1,14 +1,6 @@
/* radare - LGPL - Copyright 2007-2016 - pancake */
/* radare - LGPL - Copyright 2007-2022 - pancake */
#include <r_types.h>
#include <r_util.h>
#include <r_list.h>
#include <r_anal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "ops.h"
#include "code.h"
#include "class.h"
@ -28,7 +20,7 @@ static int update_switch_op(ut64 addr, const ut8 * bytes);
static int update_bytes_consumed(int sz);
static int handle_switch_op(ut64 addr, const ut8 * bytes, char *output, int outlen);
static ut8 IN_SWITCH_OP = 0;
static R_TH_LOCAL ut8 IN_SWITCH_OP = 0;
typedef struct current_table_switch_t {
ut64 addr;
int def_jmp;
@ -37,15 +29,15 @@ typedef struct current_table_switch_t {
int cur_val;
} CurrentTableSwitch;
static CurrentTableSwitch SWITCH_OP;
static ut64 BYTES_CONSUMED = 0LL;
static R_TH_LOCAL CurrentTableSwitch SWITCH_OP;
static R_TH_LOCAL ut64 BYTES_CONSUMED = 0LL;
//static RBinJavaObj *BIN_OBJ = NULL;
static void init_switch_op (void) {
static void init_switch_op(void) {
memset (&SWITCH_OP, 0, sizeof (SWITCH_OP));
}
static int enter_switch_op (ut64 addr, const ut8* bytes, int len) {
static int enter_switch_op(ut64 addr, const ut8* bytes, int len) {
#if 0
int sz = ((BYTES_CONSUMED+1) % 4)
? (1 + 4 - (BYTES_CONSUMED+1) % 4)
@ -57,7 +49,7 @@ static int enter_switch_op (ut64 addr, const ut8* bytes, int len) {
int sz = 4;
IFDBG {
int sz2 = (4 - (addr+1) % 4) + (addr+1) % 4;
int sz2 = (4 - (addr + 1) % 4) + (addr+1) % 4;
eprintf ("Addr approach: 0x%04x and BYTES_CONSUMED approach: 0x%04"PFMT64x", BYTES_CONSUMED%%4 = 0x%04x\n",
sz2, BYTES_CONSUMED, sz);
}
@ -71,7 +63,7 @@ static int enter_switch_op (ut64 addr, const ut8* bytes, int len) {
return sz;
}
static bool isRelative (ut32 type) {
static bool isRelative(ut32 type) {
if (type & R_ANAL_JAVA_CODEOP_CJMP) {
return true;
}
@ -81,32 +73,32 @@ static bool isRelative (ut32 type) {
return false;
}
static int update_bytes_consumed (int sz) {
static int update_bytes_consumed(int sz) {
BYTES_CONSUMED += sz;
return sz;
}
static int update_switch_op (ut64 addr, const ut8 * bytes) {
static int update_switch_op(ut64 addr, const ut8 * bytes) {
int sz = 4;
if (addr == SWITCH_OP.addr) {
SWITCH_OP.cur_val = 0;
} else {
SWITCH_OP.cur_val = (addr - SWITCH_OP.addr - 16) / 4;
}
int ccase = SWITCH_OP.cur_val + SWITCH_OP.min_val;
SWITCH_OP.cur_val++;
if (ccase+1 > SWITCH_OP.max_val) {
if (ccase + 1 > SWITCH_OP.max_val) {
IN_SWITCH_OP = 0;
}
IFDBG {
eprintf ("Addr approach: 0x%04"PFMT64x
" and BYTES_CONSUMED approach: 0x%04"PFMT64x
"\n", addr, BYTES_CONSUMED);
}
R_LOG_DEBUG ("Addr approach: 0x%04"PFMT64x" and BYTES_CONSUMED approach: 0x%04"PFMT64x, addr, BYTES_CONSUMED);
return update_bytes_consumed (sz);
}
static int handle_switch_op (ut64 addr, const ut8 * bytes, char *output, int outlen ) {
static int handle_switch_op(ut64 addr, const ut8 * bytes, char *output, int outlen) {
int sz = 4;
ut32 jmp = (int)(UINT (bytes, 0)) + SWITCH_OP.addr;
update_switch_op (addr, bytes);
int ccase = SWITCH_OP.cur_val + SWITCH_OP.min_val;
snprintf (output, outlen, "case %d: goto 0x%04x", ccase, jmp);
update_switch_op (addr, bytes);
return update_bytes_consumed (sz);
}

View File

@ -12,7 +12,7 @@ typedef struct java_op {
} JavaOp;
#define JAVA_OPS_COUNT 297
extern struct java_op JAVA_OPS[JAVA_OPS_COUNT];
extern const struct java_op JAVA_OPS[JAVA_OPS_COUNT] ;
R_API int java_print_opcode(RBinJavaObj *obj, ut64 addr, int idx, const ut8 *bytes, int len, char *output, int outlen);
R_API int r_java_disasm(RBinJavaObj *obj, ut64 addr, const ut8 *bytes, int len, char *output, int outlen);
R_API int r_java_assemble(ut64 addr, ut8 *bytes, const char *string);

View File

@ -1,10 +1,10 @@
/* radare - LGPL - Copyright 2007-2018 - pancake */
/* radare - LGPL - Copyright 2007-2022 - pancake */
#include <stdio.h>
#include "code.h"
#include "ops.h"
R_API struct java_op JAVA_OPS[JAVA_OPS_COUNT] = {
R_IPI const struct java_op JAVA_OPS[JAVA_OPS_COUNT] = {
{ "nop" , 0x00, 1, R_ANAL_JAVA_NOP },
{ "aconst_null" , 0x01, 1, R_ANAL_JAVA_LDST_LOAD_FROM_CONST_REF_TO_STACK },
{ "iconst_m1" , 0x02, 1, R_ANAL_JAVA_LDST_LOAD_FROM_CONST_TO_STACK },

View File

@ -5,31 +5,31 @@
#define R2_JAVA_OPS_H
enum {
R_ANAL_JAVA_ILL_OP =-1, /* illegal instruction // trap */
R_ANAL_JAVA_ILL_OP = -1, /* illegal instruction // trap */
R_ANAL_JAVA_NULL_OP = 0,
R_ANAL_JAVA_NOP = 1, /* does nothing */
R_ANAL_JAVA_STORE_OP = 1 << 20, // Load or Store memory operation
R_ANAL_JAVA_LOAD_OP = 1 << 21, // Load or Store memory operation
R_ANAL_JAVA_REG_OP = 1 << 22, // register operation
R_ANAL_JAVA_OBJ_OP = 1 << 23, // operates on an object
R_ANAL_JAVA_STACK_OP = 1 << 25, // stack based operation
R_ANAL_JAVA_BIN_OP = 1 << 26, // binary operation
R_ANAL_JAVA_CODE_OP = 1 << 27, // operates on code
R_ANAL_JAVA_DATA_OP = 1 << 28, // operates on data
R_ANAL_JAVA_UNK_OP = 1 << 29, /* unknown opcode type */
R_ANAL_JAVA_REP_OP = 1 << 30, /* repeats next instruction N times */
R_ANAL_JAVA_STORE_OP = 1 << 20, // Load or Store memory operation
R_ANAL_JAVA_LOAD_OP = 1 << 21, // Load or Store memory operation
R_ANAL_JAVA_REG_OP = 1 << 22, // register operation
R_ANAL_JAVA_OBJ_OP = 1 << 23, // operates on an object
R_ANAL_JAVA_STACK_OP = 1 << 25, // stack based operation
R_ANAL_JAVA_BIN_OP = 1 << 26, // binary operation
R_ANAL_JAVA_CODE_OP = 1 << 27, // operates on code
R_ANAL_JAVA_DATA_OP = 1 << 28, // operates on data
R_ANAL_JAVA_UNK_OP = 1 << 29, /* unknown opcode type */
R_ANAL_JAVA_REP_OP = 1 << 30, /* repeats next instruction N times */
R_ANAL_JAVA_COND_OP = 1 << 31,
};
enum {
R_ANAL_JAVA_TYPE_REF_NULL = 0,
R_ANAL_JAVA_TYPE_REF_UNK = 1 << 1,
R_ANAL_JAVA_TYPE_REF = 1 << 2,
R_ANAL_JAVA_TYPE_REF_NULL = 0,
R_ANAL_JAVA_TYPE_REF_UNK = 1 << 1,
R_ANAL_JAVA_TYPE_REF = 1 << 2,
R_ANAL_JAVA_TYPE_SIGNED = 1 << 3,
R_ANAL_JAVA_TYPE_PRIM = 1 << 4,
R_ANAL_JAVA_TYPE_CONST = 1 << 5,
R_ANAL_JAVA_TYPE_PRIM = 1 << 4,
R_ANAL_JAVA_TYPE_CONST = 1 << 5,
R_ANAL_JAVA_TYPE_STATIC = 1 << 6,
R_ANAL_JAVA_TYPE_VOLATILE = 1 << 7,
R_ANAL_JAVA_TYPE_VOLATILE = 1 << 7,
R_ANAL_JAVA_TYPE_PUBLIC = 1 << 8,
R_ANAL_JAVA_TYPE_BOOL = 1 << 10,
@ -62,18 +62,18 @@ enum {
enum {
// call return types
R_ANAL_JAVA_RET_TYPE_REF_NULL = 1 << 10,
R_ANAL_JAVA_RET_TYPE_REF = 1 << 11 ,
R_ANAL_JAVA_RET_TYPE_PRIM = 1 << 12 ,
R_ANAL_JAVA_RET_TYPE_CONST = 1 << 13,
R_ANAL_JAVA_RET_TYPE_STATIC = 1 << 14,
R_ANAL_JAVA_RET_TYPE_REF_NULL = 1 << 10,
R_ANAL_JAVA_RET_TYPE_REF = 1 << 11 ,
R_ANAL_JAVA_RET_TYPE_PRIM = 1 << 12 ,
R_ANAL_JAVA_RET_TYPE_CONST = 1 << 13,
R_ANAL_JAVA_RET_TYPE_STATIC = 1 << 14,
};
// jmp conditionals
enum {
// TODO these should be mapped to some sort of
// flags register
R_ANAL_JAVA_COND_EQ = 1 << 11,
R_ANAL_JAVA_COND_EQ = 1 << 11,
R_ANAL_JAVA_COND_NE = 1 << 12,
R_ANAL_JAVA_COND_GE = 1 << 13,
R_ANAL_JAVA_COND_GT = 1 << 14,
@ -128,7 +128,7 @@ enum {
R_ANAL_JAVA_LDST_FROM_VAR = 1 << 5,
// If indirect load, where are we getting the indirection,
R_ANAL_JAVA_LDST_INDIRECT_REF = 1 << 6,
R_ANAL_JAVA_LDST_INDIRECT_REF = 1 << 6,
R_ANAL_JAVA_LDST_INDIRECT_MEM = 1 << 6,
R_ANAL_JAVA_LDST_INDIRECT_REG = 1 << 7,
@ -137,7 +137,7 @@ enum {
R_ANAL_JAVA_LDST_INDIRECT_VAR = 1 << 10,
// Location to put the item,
R_ANAL_JAVA_LDST_TO_REF = 1 << 11,
R_ANAL_JAVA_LDST_TO_REF = 1 << 11,
R_ANAL_JAVA_LDST_TO_MEM = 1 << 11,
R_ANAL_JAVA_LDST_TO_REG = 1 << 12,
@ -145,7 +145,7 @@ enum {
R_ANAL_JAVA_LDST_TO_VAR = 1 << 14,
// Stack, Memory, Register, Bss, Data ,
R_ANAL_JAVA_LDST_OP_PUSH = 1 << 15 ,
R_ANAL_JAVA_LDST_OP_PUSH = 1 << 15 ,
R_ANAL_JAVA_LDST_OP_POP = 1 << 16,
R_ANAL_JAVA_LDST_OP_MOV = 1 << 17 ,
R_ANAL_JAVA_LDST_OP_EFF_ADDR = 1 << 18,
@ -160,8 +160,6 @@ enum {
R_ANAL_JAVA_LDST_TO_STACK |\
R_ANAL_JAVA_TYPE_REF,
R_ANAL_JAVA_LDST_LOAD_FROM_CONST_TO_STACK = R_ANAL_JAVA_LDST_OP_PUSH |\
R_ANAL_JAVA_LOAD_OP |\
R_ANAL_JAVA_LDST_FROM_CONST |\

View File

@ -18,7 +18,9 @@ FILE=-
CMDS=<<EOF
e asm.arch=x86
pa call eax
e asm.assembler=java
e asm.arch=java
# XXX asm.assembler only works if the assembler is in asm plugin, wont fallback on anal ones
# e asm.assembler=java
pa bipush 33
EOF
EXPECT=<<EOF