Rizin
unix-like reverse engineering framework and cli tools
asm_java.c File Reference
#include <rz_types.h>
#include <rz_util.h>
#include <rz_lib.h>
#include <rz_asm.h>
#include <rz_core.h>
#include "../arch/java/jvm.h"
#include "../arch/java/assembler.h"

Go to the source code of this file.

Classes

struct  java_analysis_context_t
 

Typedefs

typedef struct java_analysis_context_t JavaAsmContext
 

Functions

static void update_context (JavaAsmContext *ctx)
 
static int java_disassemble (RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
 
static bool java_init (void **user)
 
static bool java_fini (void *user)
 
static int java_assemble (RzAsm *a, RzAsmOp *ao, const char *str)
 

Variables

RzAsmPlugin rz_asm_plugin_java
 
RZ_API RzLibStruct rizin_plugin
 

Typedef Documentation

◆ JavaAsmContext

Function Documentation

◆ java_assemble()

static int java_assemble ( RzAsm a,
RzAsmOp ao,
const char *  str 
)
static

Definition at line 125 of file asm_java.c.

125  {
126  ut8 buffer[128];
127  st32 written = 0;
128  st32 slen = strlen(str);
129 
130  if (!java_assembler(str, slen, buffer, sizeof(buffer), a->pc, &written)) {
131  return -1;
132  }
133 
134  rz_strbuf_setbin(&ao->buf, (const ut8 *)&buffer, written);
135  return written;
136 }
bool java_assembler(const char *input, st32 input_size, ut8 *output, st32 output_size, ut64 pc, st32 *written)
Definition: assembler.c:472
uint8_t ut8
Definition: lh5801.h:11
RZ_API bool rz_strbuf_setbin(RzStrBuf *sb, const ut8 *s, size_t len)
Definition: strbuf.c:85
#define st32
Definition: rz_types_base.h:12
#define a(i)
Definition: sha256.c:41
Definition: buffer.h:15
RzStrBuf buf
Definition: rz_asm.h:71

References a, rz_asm_op_t::buf, java_assembler(), rz_strbuf_setbin(), st32, and cmd_descs_generate::str.

◆ java_disassemble()

static int java_disassemble ( RzAsm a,
RzAsmOp op,
const ut8 buf,
int  len 
)
static

Definition at line 31 of file asm_java.c.

31  {
32  JavaAsmContext *ctx = (JavaAsmContext *)a->plugin_data;
33  rz_strbuf_set(&op->buf_asm, "invalid");
34 
35  if (a->pc < ctx->last) {
36  ctx->switchop = BYTECODE_00_NOP;
37  }
38  ctx->last = a->pc;
39  switch (ctx->switchop) {
41  if (len < 4) {
42  RZ_LOG_ERROR("[!] java_analysis: no enough data for lookupswitch case.\n");
43  return -1;
44  }
45  op->size = 4;
46  ut64 jump = ctx->pc + rz_read_be32(buf);
47  rz_strbuf_setf(&op->buf_asm, "case %d: goto 0x%" PFMT64x, ctx->count + ctx->ts.low, jump);
49  return op->size;
50  }
52  if (len < 8) {
53  RZ_LOG_ERROR("[!] java_analysis: no enough data for lookupswitch case.\n");
54  return -1;
55  }
56  op->size = 8;
57  st32 number = (st32)rz_read_be32(buf);
58  ut64 jump = ctx->pc + rz_read_at_be32(buf, 4);
59  rz_strbuf_setf(&op->buf_asm, "case %d: goto 0x%" PFMT64x, number, jump);
61  return op->size;
62  }
63  default:
64  break;
65  }
66 
67  JavaVM vm = { 0 };
68  Bytecode bc = { 0 };
69 
70  rz_strbuf_set(&op->buf_asm, "invalid");
71 
72  ut64 section = a->pc;
73  if (a->binb.bin) {
74  const RzBinSection *sec = a->binb.get_vsect_at(a->binb.bin, a->pc);
75  if (sec) {
76  section = sec->paddr;
77  }
78  }
79 
80  if (!jvm_init(&vm, buf, len, a->pc, section)) {
81  RZ_LOG_ERROR("[!] java_disassemble: bad or invalid data.\n");
82  return -1;
83  }
84  op->size = 1;
85  if (jvm_fetch(&vm, &bc)) {
86  op->size = bc.size;
87  bytecode_snprint(&op->buf_asm, &bc);
88  if (bc.opcode == BYTECODE_AA_TABLESWITCH) {
89  ctx->count = 0;
90  ctx->switchop = BYTECODE_AA_TABLESWITCH;
91  ctx->ts = *((TableSwitch *)bc.extra);
92  ctx->pc = a->pc;
93  } else if (bc.opcode == BYTECODE_AB_LOOKUPSWITCH) {
94  ctx->count = 0;
95  ctx->switchop = BYTECODE_AB_LOOKUPSWITCH;
96  ctx->ls = *((LookupSwitch *)bc.extra);
97  ctx->pc = a->pc;
98  }
99  bytecode_clean(&bc);
100  } else {
101  RZ_LOG_ERROR("[!] java_disassemble: jvm fetch failed.\n");
102  return -1;
103  }
104  return op->size;
105 }
size_t len
Definition: 6502dis.c:15
static void update_context(JavaAsmContext *ctx)
Definition: asm_java.c:22
int jump(int a, int b)
Definition: bcj_test.c:35
#define BYTECODE_00_NOP
Definition: bytecode.h:8
#define BYTECODE_AA_TABLESWITCH
Definition: bytecode.h:178
#define BYTECODE_AB_LOOKUPSWITCH
Definition: bytecode.h:179
voidpf void * buf
Definition: ioapi.h:138
bool jvm_fetch(JavaVM *jvm, Bytecode *bytecode)
Definition: jvm.c:1553
bool jvm_init(JavaVM *jvm, const ut8 *buffer, const ut32 size, ut64 pc, ut64 section)
Definition: jvm.c:1541
void bytecode_clean(Bytecode *bytecode)
Definition: jvm.c:1536
void bytecode_snprint(RzStrBuf *sb, Bytecode *bytecode)
Definition: jvm.c:1458
static ut32 rz_read_at_be32(const void *src, size_t offset)
Definition: rz_endian.h:93
static ut32 rz_read_be32(const void *src)
Definition: rz_endian.h:87
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153
RZ_API const char * rz_strbuf_setf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
#define PFMT64x
Definition: rz_types.h:393
void * extra
Definition: bytecode.h:244
ut16 opcode
Definition: bytecode.h:237
ut16 size
Definition: bytecode.h:238
Definition: jvm.h:10
Definition: dis.c:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References a, BYTECODE_00_NOP, BYTECODE_AA_TABLESWITCH, BYTECODE_AB_LOOKUPSWITCH, bytecode_clean(), bytecode_snprint(), bytecode_t::extra, jump(), jvm_fetch(), jvm_init(), len, bytecode_t::opcode, rz_bin_section_t::paddr, PFMT64x, RZ_LOG_ERROR, rz_read_at_be32(), rz_read_be32(), rz_strbuf_set(), rz_strbuf_setf(), bytecode_t::size, st32, update_context(), and ut64().

◆ java_fini()

static bool java_fini ( void *  user)
static

Definition at line 116 of file asm_java.c.

116  {
117  if (!user) {
118  return false;
119  }
120  JavaAsmContext *ctx = (JavaAsmContext *)user;
121  free(ctx);
122  return true;
123 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free().

◆ java_init()

static bool java_init ( void **  user)
static

Definition at line 107 of file asm_java.c.

107  {
109  if (!ctx) {
110  return false;
111  }
112  *user = ctx;
113  return true;
114 }
#define RZ_NEW0(x)
Definition: rz_types.h:284

References RZ_NEW0.

◆ update_context()

static void update_context ( JavaAsmContext ctx)
static

Definition at line 22 of file asm_java.c.

22  {
23  ctx->count++;
24  if (ctx->switchop == BYTECODE_AA_TABLESWITCH && ctx->count > ctx->ts.length) {
25  ctx->switchop = BYTECODE_00_NOP;
26  } else if (ctx->switchop == BYTECODE_AB_LOOKUPSWITCH && ctx->count > ctx->ls.npairs) {
27  ctx->switchop = BYTECODE_00_NOP;
28  }
29 }

References BYTECODE_00_NOP, BYTECODE_AA_TABLESWITCH, and BYTECODE_AB_LOOKUPSWITCH.

Referenced by java_disassemble().

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_ASM,
}
RzAsmPlugin rz_asm_plugin_java
Definition: asm_java.c:138
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_asm.h:133

Definition at line 153 of file asm_java.c.

◆ rz_asm_plugin_java

RzAsmPlugin rz_asm_plugin_java
Initial value:
= {
.name = "java",
.desc = "Java bytecode disassembler",
.arch = "java",
.license = "LGPL-3",
.author = "deroad",
.bits = 32,
.endian = RZ_SYS_ENDIAN_BIG,
.init = java_init,
.fini = java_fini,
.disassemble = &java_disassemble,
.assemble = &java_assemble,
}
static bool java_init(void **user)
Definition: asm_java.c:107
static int java_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_java.c:31
static bool java_fini(void *user)
Definition: asm_java.c:116
static int java_assemble(RzAsm *a, RzAsmOp *ao, const char *str)
Definition: asm_java.c:125
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527

Definition at line 138 of file asm_java.c.